19
\$\begingroup\$

Mixed Number to an Improper Fraction

In this challenge you will be converting a mixed number to an improper fraction.

Because improper fractions use fewer numbers, your code will need to be as short as possible.


Examples

4 1/2
9/2

12 2/4
50/4

0 0/2
0/2

11 23/44
507/44

Specification

You may assume the denominator of the input will never be 0. The input will always be in the format x y/z where x,y,z are arbitrary nonnegative integers. You do not need to simplify the output.


This is so shortest code in bytes wins.

\$\endgroup\$
  • 5
    \$\begingroup\$ You should add the tag "parsing". I'm sure most answers will spend more bytes on parsing the input and formatting the output than on doing the math. \$\endgroup\$ – nimi Dec 17 '15 at 18:31
  • 3
    \$\begingroup\$ Can the output be a rational number type or does it have to be a string? \$\endgroup\$ – Martin Ender Dec 17 '15 at 18:33
  • 2
    \$\begingroup\$ @AlexA.: ... but a large part of the challenge. According to it's description the tag should be used in such cases. \$\endgroup\$ – nimi Dec 17 '15 at 18:38
  • 7
    \$\begingroup\$ Can x, y and z be negative? \$\endgroup\$ – Dennis Dec 17 '15 at 19:21
  • 2
    \$\begingroup\$ Based on the challenge I'm assuming it is, but is the input format "x y/z" mandatory, or can the space be a new-line, and/or the x,y,z be separated inputs? Most answers are assuming the input format is indeed mandatory to be x y/z, but some aren't, hence this question to have a definitive answer. \$\endgroup\$ – Kevin Cruijssen Jun 15 '18 at 13:04

31 Answers 31

0
\$\begingroup\$

Ouroboros, 16 bytes

rr\r.@*@+n47on1(

Try it here!

Explanation

The reason why Ouroboros is great for this challenge: its integer-reading primitive, r, skips over any non-digit characters until it comes to a number. So no parsing is required; we just read three numbers and process them.

rr                Read the first two numbers
                   Stack: 11 23
  \r              Swap and read the third one
                   Stack: 44 23 11
    .             Duplicate
                   Stack: 44 44 23 11
     @            Rotate (moving third item to top of stack)
                   Stack: 23 44 44 11
      *           Multiply
                   Stack: 1012 44 11
       @+         Rotate and add
                   Stack: 1023 44
         n        Output as number
                   Stack: 44  Output: 1023
          47o     Push 47 and output as character
                   Stack: 44  Output: 1023/
             n    Output as number
                   Output: 1023/44
              1(  Terminate
\$\endgroup\$

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.