0

I have a long code e.g 545362783 and would like to add the consists numbers together and multiply them, like (1*5)+(2*4)+(3*5)+(4*3)+(5*6) etc.

Is there a simple solution to get them as a integer so I could use these in math. Many thanks!

shahaf
  • 4,750
  • 2
  • 29
  • 32
Me Myself
  • 3
  • 1
  • There is a simple solution but its not robust. To make it robust you will probably need a parser. You can use Python's `ast` module but it will parse more than just math: `>>> ast.dump(ast.parse('(1 + 2) + (3 + 4)'))` will give you a parsed expression: `'Module(body=[Expr(value=BinOp(left=BinOp(left=Num(n=1), op=Add(), right=Num(n=2)), op=Add(), right=BinOp(left=Num(n=3), op=Add(), right=Num(n=4))))])'` – Reut Sharabani Oct 08 '19 at 10:11
  • Hi and welcome onboard of StackOverflow. As your question is generally clear, a more structured question, especially with an example that can easily be copied would be helpful. – MichaelA Oct 08 '19 at 10:13

5 Answers5

2

One way of doing it:

s = '545362783'

a = sum(x * int(c) for x, c in zip(range(1, len(s) + 1), s))
print(a)
# 222

What is happening is the following:

  • s is just a string so looping through it will give each char
  • range(1, len(s) + 1) will give integers from 1 until the length of s
  • zip() is grouping its arguments so that 1 and '5' (etc.) are yielded at the same time by the for loop
  • then, c which contains the characters, is converted to int
  • finally all is multiplied together and summed up with sum()

The same could be done a little more efficiently enumerate() (making good use of the start parameter, similarly to what is done in @shahaf's answer):

a = sum(x * int(c) for x, c in enumerate(s, 1))

This is essentially the same as above but a bit more elegant (and probably also faster). The behavior of enumerate() is just to yield an index accompanying the object being looped through.

norok2
  • 25,683
  • 4
  • 73
  • 99
  • That is the solution I'd prefer, I didn't know I did not need a list comprehension inside the sum, though. – MichaelA Oct 08 '19 at 10:21
  • `sum()` is fine with a generator and this avoids creating the intermediate unnecessary `list`. – norok2 Oct 08 '19 at 10:29
  • 1
    Hey. Works like a charm. Would it still be possible to loop through each number but the last one. Let's say I have 10 charecters, but i'd like to only multiply with 9 of them. Thanks for the help! – Me Myself Oct 08 '19 at 10:35
  • 1
    just slice the list: sum(a * int(b) for a, b in enumerate(s[:-1], 1)) should work. – MichaelA Oct 08 '19 at 11:48
1

try unpacking

s = '545362783'
# --> to map each character of the string to a list:
print([*s])
['5', '4', '5', '3', '6', '2', '7', '8', '3']

# --> to map each character of the string directly to a list of integers:
# (thanks Mykola Zotko for the comment!)
print(list(map(int, s)))
[5, 4, 5, 3, 6, 2, 7, 8, 3]

more on the unpacking operator * see e.g. here.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
1

you can write a fairly simple method to do it, smth like so

num_str = "545362783"
total = 0;
for idx, num in enumerate(num_str, 1):
  total += idx * int(num)
shahaf
  • 4,750
  • 2
  • 29
  • 32
0

Here's the Solution. I hope it may help u:

a = list(str(545362783))
count = 1
c = []
for i in a:
    b = count * int(i)
    c.append(b)
    count = count + 1

sum_of_a = sum(c)
print(sum_of_a)

Output:

222
Smack Alpha
  • 1,828
  • 1
  • 17
  • 37
0
def count_substring(string):
    string = '545362783'
    multi = []
    for i in range(0, len(string)):
       multiplied = int(i+1) * int(string[i])
       multi.append(multiplied) 

print (multi)
print (sum(multi))

You may try this.

Output:

[5, 8, 15, 12, 30, 12, 49, 64, 27]

222

indhu
  • 91
  • 7