+ 1

I have written a code. I want to make it look clean, please help.

def wsd(n): k = [int(x) for x in str(n)] p = [] for i in range(0,len(k)): p.append(k[i]*(i+1)) return sum(p) k = [wsd(x) for x in input().split()] print(*k)

21st Apr 2022, 10:39 AM
Anshuman Routray
Anshuman Routray - avatar
10 Respostas
+ 3
Paul I have written the same code!! The code takes an integer It multiplies every index of the given number to a real number(1,2,3....) Respectively and then output the sum of digits of the result. Example:- Input 15 Output 11 here 11 is the answer as wsd of 15 =1*1 + 5*2 = 11 Similarly, 70 = 7*1 + 0*2 = 7 Hopefully, you understood the problem. thank You
21st Apr 2022, 12:07 PM
Anshuman Routray
Anshuman Routray - avatar
+ 2
Another version, don't know if it is better, but i thought i could use yield: def wsd(n): for y in n: p = 0 for i in range(len(y)): p += (int(y[i])*(i + 1)) yield p k = wsd([x for x in input().split()]) print(*k)
21st Apr 2022, 1:18 PM
Paul
Paul - avatar
+ 1
This code looks in my opinion clean. You want to make it no clean and shorter?
21st Apr 2022, 10:44 AM
JaScript
JaScript - avatar
+ 1
JaScript I have edited the code now, earlier it had some repeated statements now I have deleted all of them. Thanks for your Opinion.
21st Apr 2022, 10:54 AM
Anshuman Routray
Anshuman Routray - avatar
0
It is easier if i know what your code is supposed to do, but i tried to make it simpler def wsd(n): k = [int(x) for x in str(n)] p = 0 for i in range(len(k)): p += (k[i]*(i + 1)) return p k = [wsd(x) for x in input().split()] print(*k)
21st Apr 2022, 11:19 AM
Paul
Paul - avatar
0
# so in your opinion us this two(one)-liner a cleaner solution: # my opinion: nah a = list(map(int,iter(input('~> ')))) print(sum([i*-~a.index(i)for i in a]))
21st Apr 2022, 5:07 PM
Ervis Meta
Ervis Meta - avatar
0
It looks correct to me .by the way nice work
22nd Apr 2022, 5:33 PM
James Benita
0
Código para resolver
23rd Apr 2022, 12:24 AM
Víctor hugo Urtado
0
James Benita Thank You
23rd Apr 2022, 2:48 AM
Anshuman Routray
Anshuman Routray - avatar
0
Anshuman Routray would this work? crazy mix of python functions, no list. def wsd(n): return sum(i*v for i,v in enumerate(map(int, [j for j in n]), 1)) k = [wsd(x) for x in input().split()] print(*k)
23rd Apr 2022, 2:51 AM
Bob_Li
Bob_Li - avatar