Let's solve Mixed Fraction. Chances are, you've already encountered fractions of this sort during your education (if not, Google it - you have no idea what you're missing out on), so the maths shouldn't be too frightening.
As usual, let's sketch out a plan before hitting the keyboard.
mixin.txt
.mixout.txt
.So how exactly does one convert an improper fraction into a mixed one? Since the problem statement asks us to keep the same denominator, there are just two numbers we have to find. The problem statement calls them a and b, but those aren't very descriptive. We could call them the whole number part and the fractional part, or 'big number' and 'little number' if we're so inclined.
Here's one way of thinking about it that might be helpful. You might already know that fractions and division are practically the same thing. That is, writing 'n divided by d' is the same as writing 'n/d'. Because of this, we might try treating our improper fraction as some kind of division question. Let's try that on the sample data.
Input | Output |
---|---|
22 6 |
3 4/6 |
49 7 |
7 |
The second case is obvious - 49 divided by 7 gives a result (or quotient if you prefer) of 7, which is the output. So in some sense the value of a must be connected to the result of the division.
Now let us consider the first case. 22 divided by 6 gives 3 once it is rounded down. Once again, the '3' corresponds to the first part of the output, but where does the '4' come from? A little experimentation gives us one possible explanation - the number 4 is the remainder, the amount left over after division is performed. It is not hard to show that the quotient and remainder correspond exactly to a and b.
This is great news, because it is now very clear to us what kind of calculations we should be performing. To get a, we need to take n/d rounded down. It gets better - thanks to the way the typical computer stores data, in most languages, when you tell the computer to divide two integers the result is automatically rounded down. This immediately suggests a way to calculate a.
# Python 3 is a language where division (with "/") doesn't round down by default.
# Instead, Python 3 gives another operator "//" which does round down.
a = n // d
a = n / d;
(Again, name your variables whatever you want. In a problem like this, a
and b
are probably not the best names to be giving your variables. Just remember to choose something meaningful.)
Now, how do we get b (the remainder)? Here's one way: we can subtract out ad. Think of it this way - if your mixed fraction looks like 'a b/d', this step serves to remove the whole number part so that we can get a better look at the smaller part.
b = n - a * d # The remainder?
b = n - a * d; // The remainder?
Don't do that. It may be correct, but it's not obvious what the code is doing, and if we need to come back and debug it, we'll be wasting time trying to remember what this line does.
Here's a better (read: lazier) way. You may not know this, but most languages provide you with an operator that calculates remainders for you. (An operator is a mathematical symbol that does something to one or more numbers. For example, +
, -
and *
are all operators.)
It's called the modulo operator and it is defined as such: x modulo y = 'the remainder left over when calculating x ÷ y'.
That is, it does exactly what we need.
Now, some languages like BASIC and Pascal write the operator as mod
, but chances are that your language writes it as %
. So, if we were after the remainder left over from n/d, we'd write:
b = n % d # The remainder!
b = n % d; // The remainder!
If you haven't used modulos before, you may feel that the modulo operator is too confusing a concept to be worth the trouble. Nonetheless, persist with them - you'll find that they save you some work in the long run.
Any time you need to test if a number is even (check if i % 2 == 0
), or get the last two digits of a number (x % 100
), or find the next multiple of 23 after a certain number (x - (x % 23) + 23
), using the modulo operator results in shorter (and - some would claim - more readable) code.
But enough with tangents; let's get back to the problem.
The problem statement makes it very clear that there are two different situations we need to worry about: either b is not 0, or it is. In each of those cases a different kind of output is expected.
Like the previous problem, the issue is resolved with if
statements.
if b != 0:
outputFile.write(str(a) + " " + str(b) + "/" + str(d))
else:
outputFile.write(str(a))
if (b != 0) {
fprintf(outputFile, "%d %d/%d",a,b,d); //print the whole fraction
} else {
fprintf(outputFile, "%d",a); //just print the whole number
}
Yes, it's a little more clunky than previous output sections, but there's not a lot we can do about that.
Anyway, once we've assembled all the pieces, we should have a correct solution to Mixed Fraction on our hands.