I don't often get to use the else clause that is part of the Python for loop, so when I had a problem that it was perfect for I made note.
Fractran, it seems, is a new (to me), curiosity that someone turned into a Rosetta Code task recently. I couldn't quite understand what was being asked for for a while, but when it finally clicked it was so straight-forward that I thought it couldn't be that simple.
Fractran goes something like this:
You are given a finite list of fractions. (You know, numerator over denominator where both are integers).
And an integer, n that is the first term in a series
Successive terms in the series are found by:
Finding the first fraction from the list that when multiplied by n gives an integer result
If found then replace n by n times the found fraction to give the next output term
In no fraction is found then the series terminates.
The Rosetta code task also restricts the format of the list of fractions.
Many C-like programming languages have for-loops. The Python for loop has an optional else clause where the body of the else clause is only executed when the for loop finishes by "naturally" coming to the end of what it was iterating over - as opposed to if the for loop is exited by an exception/break.
The fractran algorithm has that "terminate if fraction not found" part that happens only after iterating through all the fractions and not finding one that meets a condition.
fractran is written as a generator and of course uses the fractions module.
If you look up Fraction you will find that it can take a string as input. The string parser is rudimentary and will not accept spaces around the '/' character so I remove them in my input string parsing
First 15 members of fractran(2):
2, 15, 825, 725, 1925, 2275, 425, 390, 330, 290, 770, 910, 170, 156, 132
There are two break statements in the for loop. The one in the inner if statement breaks out of the for block without running the code in the else clause. The second break is in the else clause which is only run when the list of fractions in flist is exausted. This second break exits the next outer loop i.e. the while loop which then terminates the generator.