whatf
whatf

Reputation: 6468

python code explanation reqd

I am new to python, and was going through some pre written code to get a better grip at it. This code tries to get the number of digits each non-termination cycle has when a number is at the denominator to 1. For eg. 1/3 = 0.(3) it has a cycle of 1. similary 7 has a cycle of 6 as 1/7 = 0.(142856)

def get_decimals(num, div, current=([], [])):
    """Return a tuple (integer_part, decimal_part, cycle_length) for num/div"""
    headtail = lambda lst: (lst[0], lst[1:])
    memory, values = current
    if values and num == 0:
        integer, decimals = headtail(values)
        return integer, decimals, 0
    elif num in memory:
        integer, decimals = headtail(values)
        print integer, decimals
        lencycle = len(memory) - memory.index(num)
        return integer, decimals, lencycle
    a, b = divmod(num, div)
    return get_decimals(10*b, div, (memory+[num], values+[a]))

print max((get_decimals(1, num)[2], num) for num in xrange(2, 10))[1]

could anyone please explain me in context of the code pasted above. I could not understand the following:

  1. the subscripts [2] and [1] in the last print statement.

  2. memory.index(num) this one inside the get_decimals function at the line 4th to last.

Upvotes: 0

Views: 235

Answers (2)

whatf
whatf

Reputation: 6468

Unless you are very new, the above explanation would have made sense. If not, i try explaining in a simpler manner:

for a list a = [1, 2, 3] you would access first element as: a[0] similarly a subscript of 2 after get_decimals(1, num)[2] means if the function is returning a tuple/dictionary access the third element, in your case the length of the cycle of the non-terminating series. For the input number 7, the output would be 6 as it has a non terminating cycle of 142856.

similarly for line: max((get_decimals(1, num)[2], num) for num in xrange(2, 10))[1] if you go without the subscript [1] you will see two values printing, but the coder was concerned only with the second value being returned. Apparently the code says:

call the function get_decimals for the values 2 to 10.

Find the max of the tuple being returned and print the second item of the tuple which has been filtered as the max.

Index has been explained pretty clearly and needs no further explanation. Just another simplification: [1, 2, 3] the index of 2 in list is 1. This will clarify the stuff.

Please refer to the official python documentation, before reading codes. IMHO.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 613592

get_decimals(1, num)[2]

get_decimals returns a tuple containing 3 items, named integer, decimals and lencycle. So the subscript [2] picks out lencycle.

max((get_decimals(1, num)[2], num) for num in xrange(2, 10))[1]

The subscript [1] picks out num from a tuple of the form (get_decimals(1, num)[2], num). Note that the max function is applied before the [1] subscript. In this case max is operating on a iterable of tuple pairs and uses lexicographic ordering to compare items.

memory.index(num) calls the method index on the object memory passing num as the parameter. Since memory is a list then this is simply finding the index of the first occurrence of the value num in that list.

Upvotes: 1

Related Questions