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 8061237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:16:16+00:00 2026-06-05T10:16:16+00:00

I’m a beginner at Python and I’m getting a strange out of bounds error.

  • 0

I’m a beginner at Python and I’m getting a strange out of bounds error.

The idea is that I need a cache initialized using:

arr = [0]*1000000

then accessed in the same function by calling

def func (i) :
    k=1
    a = i
    arr = [0]*1000000
    while (i>1):    
        if arr[i] != 0:
            k = k + arr[i] - 1
            break
        if i%2 == 0:
            i = i/2
        else:
            i = 3*i + 1
        k += 1
    arr[a] = k
    return k

if the value i is over 1500 it gives me an out of bounds error. The cache, however, is supposed to be initialized to a million ints. Am I missing something? Thanks

  • 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-06-05T10:16:18+00:00Added an answer on June 5, 2026 at 10:16 am

    Updated now that the code is posted:

    I don’t seem to get any problems up to 1500. I do get an IndexError for func(1819), for which the i evolution begins

    1819
    5458
    2729
    

    and winds up at

    851290
    425645
    1276936
    [...]
    IndexError: list index out of range
    

    but that’s not a bug, that’s simply a fact that it goes higher than you made room for. You could use a dictionary instead of a list to avoid this problem.

    —

    To be clear, here’s the sort of thing I had in mind:

    def func_with_dict(i) :
        k=1
        a = i
        arr = {}
        while (i>1):    
            print i
            if i in arr:
                k = k + arr[i] - 1
                break
            if i%2 == 0:
                i = i/2
            else:
                i = 3*i + 1
            k += 1
            arr[a] = k
        return k
    

    which produces

    1819
    5458
    2729
    8188
    [...]
    851290
    425645
    1276936
    638468
    319234
    [...]
    20
    10
    5
    16
    8
    4
    2
    

    and a final answer of 162. I don’t think I’d use arr this way myself, though, but what I would do depends upon what you’re trying to do.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.