Adjacent number multiplication in a string.
Hello, sololearners. Let's assume I have a list with 6 digit numbers as a string. The length of list is N (unknown). So we have: ls = ['123456', '789123', '456789', and so on... ] How can I loop through each of the string, and multiply each number? e.g. 123456 = 1x2x3x4x5x6 = 720 e.g. 789123 = 7x8x9x1x2x3 = 3024 I tried nested for loop, such that: n = 0 for x in ls: for l in x: n += int(l)*int(l) and tried to compare products of values followed by if statements, but it didn't work. Any suggestions how can I loop through each of the string, and multiply each number?
Lamron , first step is to get the string of numbers to individual digits: > use a for loop to iterate over the list. take the string of digits and convert it to a list. this will separate the digits, but they are still strings. > in the same step we can use map() function and make each of the *string digits* an integer value. ... temp_lst = list(map(int, list(<string_of_digits>))) second step is to multiply the digits: > to get the product, we can use an additional loop and build the result of all digits. or > we can use the prod() method from the math module, that takes a list of int numbers and returns the result.
Lamron , your assumption is correct. if we have a list with 5 strings of digits, and we iterate over them, we will get each of the strings from the list. > in each loop iteration we get 1 string e.g. => '123456', that we can convert to a list => ['1', '2', '3', '4', '5', '6'] > applying map(int, ...) converts the list as => [1, 2, 3, 4, 5, 6]. > now as we have a list of integers, we can calculatd the product. *this is the basic version using 2 nested for loops* nums_lst = ['123456', '789123', '456789', '123'] for num in nums_lst: total = 1 for dig in num: total *= int(dig) print(total)
Mirielle , *readability counts*
I've got a cheat https://code.sololearn.com/c4sGFlEqAZ1n/?ref=app
You can also use the ord() function to get the digit character's ASCII code. With a bit of value manipulation we can have a for...loop do something that we want. Here's how it goes ... lst = [ "123456", "789123", "3278433" ] def digit_prod( s ): if not s.isdigit(): return 0 # for non-numeric strings rv = 1 for c in s: rv *= ord( c ) - 48 return rv result = list( map( digit_prod, lst ) ) print( result ) # 720 3024 12096 (Edited)
Ipang I'll try that code too and see how it goes!
Lothar , what do you mean by this: " > use a for loop to iterate over the list. take the string of digits and convert it to a list. list will separate the digits, but they are still strings. " Does it mean, if I have in total 5 items in **ls** , I will have to make a separate list for each of these items to split digits? Or is it implemented somehow different?
And others might say lambda is scary. As long as it works, it's all good
It's however good to know what the second code does. Let me introduce you to reduce() function Reduce is supposed to be part of the general python functions, I'm not sure why python think it shouldn't be on the same heirachy with map and filter as it is in other languages. so I have to import it from the functools module It accept two argument, a function and an iterable The function also accepts two arguments . Suppose there's a list [1, 2, 3, 4] reduce(lambda a, b: a + b, list) will give the result for 1 + 2 + 3 + 4 = 10 In this case, we need to use * so reduce(lambda a, b: a * b, list) will give the results for 1*2*3*4 = 24
Readability counts and optimization counts too..
The problem with using for loop is that you are assigning 0 to n then incrementing it rather than multiplying Any number multiply with 0 is always zero. My loop solution is to make n initially the first item in the list, then using string slicing to get other part of the items It seems to be more optimized compare to using functions and lambdas https://code.sololearn.com/cQMN1i2hzt1a/?ref=app
I know some people will say that eval is harmful, so I've created another one without eval https://code.sololearn.com/cpAH5JHm3shz/?ref=app
These all seem to be interesting
Oparah anord welcome to sololesrn. Don't hesitate to read this post, and make your way through courses that suit you the best: https://www.sololearn.com/Discuss/3021159/?ref=app