Answer to Question #337779 in Python for jamil

Question #337779

Write a Python program that takes a string as an input where multiple numbers are separated by hashes(#). Your first task is to create a list of numbers and print it.

Your second task is to create a dictionary that will have the index of the

list as key, and for the even indices, multiplication from start to that index as value, while for the odd indices, summation from start to that

index as value. Finally, print the dictionary.


Sample Input 1:

1#2#3#4


Sample Output 1:

[1, 2, 3, 4]


(0:1, 1:3, 2:6, 3:10}

Sample Input 2:

5#6#7

Sample Output 2:

[5, 6, 7]

{0: 5, 1:11, 2: 210}


1
Expert's answer
2022-05-05T16:26:12-0400
s = input('Please, input your string that contains numbers which separated by hashes => ').strip()
lst = [int(el) for el in s.split('#')]
dct = {}
tmp = []


for i in range(len(lst)):
    if len(tmp) == 0:
        dct[i] = lst[i]
        tmp.append(lst[i])
    else:
        if i % 2 != 0:
            tmp.append(lst[i])
            dct[i] = sum(tmp)
        elif i % 2 == 0:
            product = 1
            tmp.append(lst[i])
            for el in tmp:
                product *= el
            dct[i] = product


print(lst)
print()
print(dct)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS