Click here to Skip to main content
15,071,928 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, the question is the same as the Climbing stairs problem in leetcodeLoading...[^]

My code for the base case works fine but I keep getting
<pre>KeyError                                  Traceback (most recent call last)
<ipython-input-298-6bc45dd79565> in <module>
----> 1 fsum(3,[1,2])

<ipython-input-297-62cae64351b1> in fsum(tsum, num, memo)
     11         memo[n]=remr
     12 
---> 13     return memo[tsum]
     14 

KeyError: 3
in the last line for the memoized solution.

I solve by deleting numbers from tsum such that the base case would be a 0 or a -ve number. If anyone could provide another memoized solution, then it'd be cool too.

Input is
fsum(3,[1,2])


What I have tried:

def fsum(tsum,num,memo={}):
    if tsum in memo:
        return memo[tsum]
    if tsum==0:
        return 1
    if tsum<0:
        return 0
    for n in num:
        rem= tsum-n
        remr= fsum(rem,num,memo)
        memo[n]=remr
        
    return memo[tsum]
Posted
Updated 21-Aug-21 5:16am
v2
Comments
Richard MacCutchan 21-Aug-21 10:34am
   
Your code does not contain any of the data that it needs so it is impossible to guess why it is failing. Please show the missing variables.
Absy 2021 21-Aug-21 11:16am
   
Hi, I made the necessary changes. Hope it helps :)
Richard MacCutchan 21-Aug-21 11:36am
   
Adding a few print statements shows that the value of tsum in the call to fsum needs to be the same as the number of items in num. But the list needs to be sequential numbers, i.e. [1, 2], [1, 2, 3], but not [1, 2, 4].
fsum(2,[1,2])     # this works
fsum(3,[1,2, 3])  # and so does this

Having said that I have no real idea what this code is supposed to be doing.
Absy 2021 21-Aug-21 16:28pm
   
The main idea is to compute the number of ways the numbers in num can sum upto tsum. I use recursion in my approach.
Richard MacCutchan 22-Aug-21 3:28am
   
I think you need to add some print statements to capture the intermediate values, or use the debugger. Until you can see what happens at each stage it is difficult to see why it fails.

1 solution

If you get an error message you don't understand, google it.
A very quick google for "Keyerror" would have got you here: KeyError - Python Wiki[^]
Which explains what causes the problem.

Now all you need to do is work out why - and since that requires your dtaa and yoru code running, we can't do that for you.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then start here: pdb — The Python Debugger — Python 3.9.6 documentation[^]

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   
Comments
Absy 2021 21-Aug-21 10:57am
   
Hi, I'm trying the leetcode problem: 'Climbing stairs'. Basically we have to find the number of ways we can sum upto tsum using the numbers in num. I am looking into the debugging part but could you provide a memoized solution using the approach which I provided? Thanks.
OriginalGriff 21-Aug-21 11:56am
   
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Absy 2021 21-Aug-21 16:33pm
   
Hi, so I'm using recursion to solve this dynamic program. I subtract numbers(n) from tsum in the for loop and then pass on that value in the recursive fashion. Base case would be if tsum is either 0 (which means that I've received an actual way to sum upto tsum) or a -ve number (meaning that the numbers dont sum upto tsum). My solution works fine without memoization. It's in the memoization part where I get an error.
OriginalGriff 21-Aug-21 16:46pm
   
So debug it ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900