class Fraction: def getXSmallest(self, index, denominator): x = [] for i in range(1, denominator+1): for j in range(1, i): k = j/float(i) x.append(k) print k x.sort() if index > len(x): retrun 1 return round(x[index-1],3) ''' Implement a class Fraction, which contains a method getXSmallest. The method is passed two integers: an index and a maximum denominator. The program should find all fractions of the form a / b where b is less than or equal to the maximum denominator and a / b is less than 1 and then return the index-th smallest of these fractions. The method should return the fraction as a double value, rounded to 3 decimal points (use the supplied formatDouble function). Note: - If index value is greater than the number of fractions that are less than 1, the method should return 1. - Fractions should not be simplified. 1/2 and 2/4 are two unique fractions, despite their equal double values - The class and method must be declared as public - The index must be between 1 and 70, inclusive. The max denominator must be between 2 and 12, inclusive. (TopCoder will check these constraints, you need not.) The method signature is : public double getXSmallest(int, int); Example: For example, if index = 3 and max denominator = 4, all the fractions of the form a / b are 1/4, 1/3, 1/2, 2/4, 2/3, 3/4, and the method should return the third smallest, as a double: 0.5. '''
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question