Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7494549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:37:45+00:00 2026-05-29T17:37:45+00:00

I am new to python, and was going through some pre written code to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T17:37:57+00:00Added an answer on May 29, 2026 at 5:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a python django newbie, and was going through some open source code,
I'm new to python and have been going through some tutorials on log parsing
I was going through some Python automation scripts and I saw the new (for
I am new to Python, and I'm working on writing some database code using
I'm quite new to python, so I'm doing my usual of going through Project
I'm new to Python, and stumped by this piece of code from the Boto
Hey All- I'm completely new to Django/python and am going through a tutorial for
I'm new to programming and currently going through the exercises in Zed Shaw's Python
New to python and django. Using the forms module and going through the errors
I am new to python and have been working through the examples in Swaroop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.