+ 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)
10 ответов
+ 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
+ 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)
+ 1
This code looks in my opinion clean. You want to make it no clean and shorter?
+ 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.
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)
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]))
0
It looks correct to me .by the way nice work
0
Código para resolver
0
James Benita Thank You
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)