given a number as a string write a algorithm to map to its oral description. I.e. "1" -> "11" //this can be thought of as there is one one. "11" -> "21" // there are two ones "21" -> "1211" // there is one two and one one "1211" -> "111221" ect. //there is one one, one two and two ones
Sigiloso
Rio, your code breaks on your own provided example. The following code solves this: f = string[0] for i in range(0, len(string)): if string[i] == f: count = 1 if i == len(string)-1: sys.stdout.write("%d%s" %(count, f)) break while (string[i+1] == string[i]): count += 1 i += 1 if (i+1) >= len(string): break sys.stdout.write("%d%s" %(count, f)) if (i+1) >= len(string): break f = string[i+1] print "" I might have some extra unnecessary code and could definitely clean this up a little, but it's a working version.