The Imperial Ruler

7

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

Digital Trauma

Posted 2017-02-24T20:43:35.550

Reputation: 64 644

1Can we take decimals instead of fractions? – Okx – 2017-02-24T20:52:59.297

@Okx No - decimals are not allowed. – Digital Trauma – 2017-02-24T20:54:02.147

1Can I take input as 11/4 instead of 2 3/4 ? – cleblanc – 2017-02-24T21:43:14.417

1Could we take input as 2+3/4? – Blue – 2017-02-24T21:44:54.273

@cleblanc Input ruler length will be a mixed fraction (integer and proper fraction) - No, sorry. – Digital Trauma – 2017-02-24T21:58:48.127

@muddyfish I was intentionally vague about out how fractions are presented. So yes, this is fine. – Digital Trauma – 2017-02-24T22:00:29.327

7I was expecting a Star Wars challenge :( – Daniel – 2017-02-25T04:12:16.050

Answers

3

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.

Blue

Posted 2017-02-24T20:43:35.550

Reputation: 26 661

2

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}.

Martin Ender

Posted 2017-02-24T20:43:35.550

Reputation: 184 808

0

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 * _)
                  ■}

Roman Gräf

Posted 2017-02-24T20:43:35.550

Reputation: 2 915

1Sorry, I'm not familiar with Ohm. Is there an interpreter somewhere (preferably online) I can use to verify this answer? – Digital Trauma – 2017-03-01T18:29:24.807

1Please include the byte count in your answer. – Erik the Outgolfer – 2017-03-05T11:54:59.427

0

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.

Neil

Posted 2017-02-24T20:43:35.550

Reputation: 95 035