#๐Ÿ”’ How do i fix my code

82 messages ยท Page 1 of 1 (latest)

gentle ridge
#

The problem i m facing is that when the input is (9812, 2), when the remove() function is called on the list, it deletes the first instance of "2" on index 2 while i want to remove the one on index 4( provided the ss pls check) . I can't also think of any alternate solution to this kata/problem kindly help

polar fieldBOT
#

@gentle ridge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

gentle ridge
waxen vault
#

you'll have better luck here if you post your code as text, not a screenshot

gentle ridge
#

def digitwise_addition(n, k):
x = str(n)
sep = list(x)
z = 0
n = 0

for a in range(0,k):
    for i in sep:
        z = int(i)
        z += 1
        z = str(z)
        sep.insert(n, z)
        sep.remove(i)
        n += 1
        
        if a > 0:
            n = 0
y = ''.join(sep)
return len(y)
waxen vault
#

ah I think you asked this question earlier

gentle ridge
#

yea but got no replies :(

waxen vault
#

where can I find a clear and precise definition of "digitwise addition"?

#

that doesn't require me to squint at a screenshot? ๐Ÿ™‚

waxen vault
#

that's a 500 error ๐Ÿ˜ฆ

gentle ridge
#

yes this is the one !

unreal gorge
#

omg you're doin 5 kyu!?

waxen vault
#

ok, now what's your question?

unreal gorge
#

well good luck with that

gentle ridge
gentle ridge
# waxen vault ok, now what's your question?

The problem i m facing is that when the input is (9812, 2), when the remove() function is called on the list, it deletes the first instance of "2" on index 2 while i want to remove the one on index 4( provided the ss pls check) . I can't also think of any alternate solution to this kata/problem kindly help

unreal gorge
#

my brain kept frying up on 5 kyus... Stuck at 6 kyu. Best of luck! ๐Ÿ™‚

waxen vault
#
>>> digitwise_addition(9999, 1)
y='10101010'
8
>>> digitwise_addition(9999, 2)
y='12111011'
8
>>> 
``` that second one looks wrong
gentle ridge
waxen vault
#

that's OK

#

what's the expected answer for (9812,2)? Is it 6?

waxen vault
#

I must say, your variable names are about as bad as they can possibly be. I'm studying your code trying to figure out wtf they mean. I'm pretty sure that sep should be renamed to digits, but that's as far as I've gotten.

gentle ridge
waxen vault
#

I'm sorry, but your code is so confusing that I can't suggest any way to fix it. I can show you how I'd do it, if you're interested.

gentle ridge
#

btw you can visualise my code

waxen vault
#

!e

def mine(n, k):
    while k > 0:
        digits = [int(d) for d in str(n)]
        incremented = [d + 1 for d in digits]
        n = int("".join([str(n) for n in incremented]))
        print(f"{digits=} {incremented=} {n=}")
        k -= 1
    return len(str(n))

print(mine(9812, 2))
gentle ridge
#

in pythontutor

polar fieldBOT
waxen vault
gentle ridge
#

you can copy paste my code here

waxen vault
#

๐Ÿคท

gentle ridge
#

and visualise it

waxen vault
#

I can also just run it myself. That web site won't help.

gentle ridge
waxen vault
#

I know how python works; I don't need a step-by-step explanation.

gentle ridge
#

no man you took it wrong , i thought you didn't understand my code because of how badly i named them so i thought you could understand it better on the site

waxen vault
#

Unless that site is run by a genius who can read your mind, and explain how each step of the code implements your solution to this problem, I doubt it'll be useful to me, in understanding how your code implements your solution to the problem. Let's forget it for now.

jaunty sundial
gentle ridge
jaunty sundial
#

instead of for i in sep:

#

use for i in range(len(sep))

#

then use sep[i] where you have i to get the value in that index

#

replace insert/remove with sep[i] = z

#

you will have to use .join then turn that into a list again after each inside loop.

#

this last step takes care of the 9s that turned into 10s

#

List indexing lets you get and change a member of a list

#

Remember list indexing starts a 0 but you don't have to worry about that here as range also starts at 0

gentle ridge
#

can u pls re explain the last step?

jaunty sundial
#

indent y= ... one level to join the strings into one string. Then sep=list(y) on the next line to make the digits a list again befor runing the outer loop again.

jaunty sundial
#

OK how did that work?

gentle ridge
jaunty sundial
#

Error or wrong result?

gentle ridge
jaunty sundial
#

z=int(i) needs to be z=int(sep[i])
which gets the value of list at that index

gentle ridge
#

i also get it where it was wrong

#

thank u

#

but i m stuck at one part now and i rly dont know what that means

jaunty sundial
#

Still here

gentle ridge
jaunty sundial
#

Timed out after 12 seconds. It said it could be server problem. Did you run again and get the same error?

gentle ridge
gentle ridge
jaunty sundial
#

I just Googled and it looks like you need to do it with math to get faster results but it is above my math level.
https://stackoverflow.com/questions/78830347/digitwise-addition-exceed-limit-if-exceeded-code-time-out#:~:text=Digitwise addition is a special,over to the next digit.

jaunty sundial
gentle ridge
#

so i spent half the day just to not complete this kata ๐Ÿ˜ญ

jaunty sundial
#

But I think you learned a few things. Take care.

gentle ridge
polar fieldBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.