7
\$\begingroup\$

Given a ruler length in inches and fractions of an inch, and a granularity in fractions of an inch, output the values of all marks on the ruler, starting from 0, in ascending order.

Input ruler length will be a mixed fraction (integer and proper fraction)

Input granularity will be a proper fraction in the form 1/(2ⁿ), where 1 ≤ n ≤ 6.

Output numbers will all be expressed similarly to the input. For values where the number of inches is a whole number, no fractional part will be expressed. For values where the number of inches is 0 < n < 1, only the fractional part will be expressed.

If the input granularity is not fine enough to represent the ruler length, then the final number will be the closest representable number below the ruler length.

Examples

Input 1 1/2 would output:

0
1/2
1

Input 2 3/4 1/8 would output:

0
1/8
1/4
3/8
1/2
5/8
3/4
7/8
1
1 1/8
1 1/4
1 3/8
1 1/2
1 5/8
1 3/4
1 7/8
2
2 1/8
2 1/4
2 3/8
2 1/2
2 5/8
2 3/4

Input 2 3/4 1/2 would output:

0
1/2
1
1 1/2
2
2 1/2
\$\endgroup\$
  • 1
    \$\begingroup\$ Can we take decimals instead of fractions? \$\endgroup\$ – Okx Feb 24 '17 at 20:52
  • \$\begingroup\$ @Okx No - decimals are not allowed. \$\endgroup\$ – Digital Trauma Feb 24 '17 at 20:54
  • 1
    \$\begingroup\$ Can I take input as 11/4 instead of 2 3/4 ? \$\endgroup\$ – cleblanc Feb 24 '17 at 21:43
  • 1
    \$\begingroup\$ Could we take input as 2+3/4? \$\endgroup\$ – Blue Feb 24 '17 at 21:44
  • 7
    \$\begingroup\$ I was expecting a Star Wars challenge :( \$\endgroup\$ – Daniel Feb 25 '17 at 4:12
3
\$\begingroup\$

Pyke, 38 bytes

KQz\/cebj*Bh Fj.DRIIid)ij.Hi'f jRf\/Rs

Try it online!

Not too bad for a language that has literally no fractional support. Explanation later if at all.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Mathematica, 55 53 bytes

Thanks to JungHwan Min for saving 2 bytes.

DeleteCases[{a=Floor@#,#-a},0,1,1]&/@Range[0,+##2,#]&

Takes three numbers as input, the step width, the integer part of the ruler length, the fractional part of the ruler length.

Output is a list of lists with sublists like {1} or {2, 3/4}.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Batch, 213 bytes

@echo off
set n=%4
set/an*=%1,n+=%2*%1/%3
for /l %%i in (0,1,%n%)do call:c %%i %1
exit/b
:c
set/ad=%2,i=%1/d,n=%1%%d
if %n%==0 echo %i%&exit/b
set/an/=p=n^&-n,d/=p
if %i% gtr 0 set n=%i% %n%
echo %n%/%d%

Takes 4 numbers as input:

  1. The number of marks per inch i.e. the reciprocal/denominator of the granularity
  2. The numerator of the ruler length
  3. The denominator of the ruler length
  4. (Optional) The whole number of inches of the ruler length

So for the second example the parameters would be 8 3 4 2. Note that for lengths that are an exact number of integers you still need to specify a dummy fraction e.g. 4 0 1 2, or an improper fraction 4 2 1 would also work.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Ohm, 17 bytes

II¢/II/a/#:$_*,;

Explanation:

II/¢II/a/#:$_*,;  ■Main wire
II/¢              ■Evaluate fraction and save to register
    II/           ■Evaluate another fraction
       a/         ■Divide first by second evaluating to the required steps
         #:    ;  ■foreach _ in range(0,steps){
           $_*,   ■  print (register * _)
                  ■}
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Sorry, I'm not familiar with Ohm. Is there an interpreter somewhere (preferably online) I can use to verify this answer? \$\endgroup\$ – Digital Trauma Mar 1 '17 at 18:29
  • 1
    \$\begingroup\$ Please include the byte count in your answer. \$\endgroup\$ – Erik the Outgolfer Mar 5 '17 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.