John Davidson

python - some two values and add to next value pyramid calculation

0 comments
Message:


i need some solution in php or python calculation in pyramid form need loop until two digit value remains


i now this form not for code creation but am trying many things but not find solution of my problem


here is input


12345

need output like this add value to next value remove (0)


12345
3579
837
21

Explain : now am explain output formula
first line in input you can ad first two value from input

1 + 2 and 2 + 3 and 3 + 4 and 4 + 5 = 3579


2nd line is 3579


3 + 5 = [8]  = 5 + 7 = (12) and add 1 + 2 = [3] and 7 + 9 = 16 and 1 + 6 = [7] = 837

3rd line 837


8 + 3 = (11) = 1 + 1 = [2] and 3 + 7 = (10) 1 + 0 = [1] = 21


My Answer:
Here is a Python solution to your problem:

python
def pyramid_calculation(input_str):
while len(input_str) > 2:
new_str = ""
for i in range(len(input_str) - 1):
new_str += str(int(input_str[i]) + int(input_str[i+1]))
input_str = new_str
print(input_str)

input_str = "12345"
print(input_str)
pyramid_calculation(input_str)


You can run this code in a Python environment and it will produce the desired output in pyramid form as you described.

Rate this post

5 of 5 based on 4120 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us