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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:51:08+00:00 2026-06-12T01:51:08+00:00

I’m trying to write some code that calculates powers of 2 but stores them

  • 0

I’m trying to write some code that calculates powers of 2 but stores them in a dictionary that has powers of 10 as the key, so for example, 2^9 would be stored as

{0:2, 1:1, 2:5}

where you have 5*10^2 + 1*10^1 + 2*10^0.

So currently, I have something like

def foo():
    result={0:2}
    for i in range(2,10):
        for p in result.keys():
            result[p]*=2
        for p in result.keys():
            if result[p] / 10 != 0:
                result.setdefault(p+1,result[p]/10)
                result[p] = result[p] % 10

The issue is

result.setdefault(p+1,result[p]/10)

It overwrites whatever was in result[p+1]. I know it’s possible to just check the dictionary and then “initialize” the new key if needed, but is there a more elegant way to do it on the fly? I could initialize result to be long enough, but since I don’t necessarily know how long is “long enough”, it seems to me to make more sense to extend it on the fly.

Basically I’d like something along the lines of

for p in result.keys():
    if result[p] / 10 != 0:
        if result[p+1] exists:
            result[p+1]+=result[p]/10
        else:
            create result[p+1] and set its value to result[p]/10

Any help is much appreciated!

  • 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-12T01:51:09+00:00Added an answer on June 12, 2026 at 1:51 am

    So, what you’re doing each time through the main loop is: double each digit, then carry extra tens to the next digit. The problem is that, for the highest digit, the next digit doesn’t exist.

    So here’s a solution—although it’s not going to work for any power other than 2, for reasons I’ll get to below.

    def foo():
        result={0:2}
        for i in range(2,10):
            for p in result.keys():
                result[p]*=2
            for p in result.keys()[:]:
                if result[p] / 10 != 0:
                    result[p+1] = result.get(p+1, 0) + result[p] / 10
                    result[p] = result[p] % 10
    

    There are two key changes from your original code.

    First, we had to add [:] to the end of the second result.keys(), to iterate over a copy of the dictionary’s set of keys before the loop, rather than its current keys. The reason is that if the last digit is >5 we’re going to be adding a new key to the dictionary, and you’re not allowed to do that while iterating over it. (Why? A few reasons, but the simple one is that dictionaries are in arbitrary order, and every time you add a key, the whole order can change. This will be important later, too.)

    Second, there’s your original question: How can you avoid having to check if p+1 in result to decide whether to store r_p or add r_p to the existing value?

    When you’re dealing with counts, you use collection.Counter, which is like a dict except that it only stores integers, and any missing key has a value of 0. Otherwise, you usually use collection.defaultdict, which is like a dict except that you can specify an default value that all missing keys have. With either a Counter or defaultdict, all you’d need is result[p+1] += result[p]/10.

    But I’ve used a different alternative: the get method on the dict, which lets you specify a default value. I’ll explain why later, but keep in mind that in general, when you find yourself reaching for get, you may want a defaultdict or Counter instead.

    So, now it’ll run, and work, for powers of 2. But it won’t work for other powers, for two reasons:

    First, we’re doing the carries in random order. For powers of 2, the most you could carry is a 1, and there’s no way a 1 could affect whether the next digit up carries. (8 won’t carry and neither will 8+1, and there’s now way to get a 9.) But for any other power, that isn’t true.

    In simple testing, you might not notice this, because it just so happens that (at least in CPython 2.7 and 3.2) when you start with an empty dict and add a small number of small keys (actually, keys with small hash values, but small ints hash to themselves) in sorted order, and don’t delete anything, the dict will often iterate (and print) in order. But in general, the order is not predictable, and you can’t rely on it.

    The solution to that problem is to use collections.OrderedDict, which iterates over the keys in the order in which they were added. And that’s why I didn’t want to use defaultdict or Counter: because if you have to switch to OrderedDict, your only option is get.

    Second, once your exponent gets over 10, you may need to carry 100. That means you have to carry the 10, which will cause the next digit to carry even if it’s 0. That means that we can’t just copy the keys in advance. For example, let’s say we’re doing powers of 75. Start off with {0:5, 1:7}. Multiply each digit by 75 to {0:375, 1:525}. Now carry the 37: {0:5, 1:562}. Now carry the 56: {0:5, 1:2, 2:56}. Because we were only iterating over a copy of the original keys—that is, [0, 1]—we don’t get a chance to carry the 5.

    I’ll leave it up to you to fix that problem.

    However, one thing you might want to consider is creating a new dictionary, instead of modifying the old one in place. Then you can get around all of these problems. (As a general rule, using pure functions instead of mutating state gets around a lot of problems, but of course at the cost of extra copying.)

    def double(d):
        result = Counter()
        for p in d:
            result[p] += d[p] % 10
            result[p+1] += d[p] / 10
        return result
    
    digits={0:2}
    for i in range(2,10):
        for p in digits:
            digits[p]*=2
        digits = double(digits)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.