Question

Write code in Python: explain with comments Starting with two one-digit positive integers a and b,...

Write code in Python: explain with comments Starting with two one-digit positive integers a and b, consider the sequence in which the next number is the digit in the ones place of the sum of the previous two numbers. For example, if a = 1 and b = 1, the sequence is 1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, … Write a function mystery(a, b) that returns the length of the sequence when the last two numbers repeat the values of a and b for the first time. For example, if a = 1 and b = 1, the function should return 62.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python code screenshot

Python sample output screenshot

Python code in text

def mystery(a,b):

#initialize resulting list with a,b
resultinglist=[a,b]
#store initial value of a
inita=a
#store initial value of b
initb=b
#keep computing while until termination condition is reached with break
while True:

#the new element is ones place of summation of a and b, ones place of x is obtained by x%10
newelement=(a+b)%10
resultinglist.append(newelement)
#next iteration is to be taken for the b and the new element
a = b
b = newelement
#when last 2 elements are inital a and initial b respectively, termination condition is reached
if resultinglist[len(resultinglist)-2]==inita and resultinglist[len(resultinglist)-1]==initb:

break

print("Sequence is:-")
print(resultinglist)
#len(resultinglist) gives the length of this list
return len(resultinglist)

print(mystery(1,1))
print(mystery(2,2))
  

Add a comment
Know the answer?
Add Answer to:
Write code in Python: explain with comments Starting with two one-digit positive integers a and b,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write code in Python: explain with comments Starting with two one-digit positive integers a and b,...

    Write code in Python: explain with comments Starting with two one-digit positive integers a and b, consider the sequence in which the next number is the digit in the ones place of the sum of the previous two numbers. For example, if a = 1 and b = 1, the sequence is 1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, … Write a function mystery(a, b) that returns the length of the sequence...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • *PYTHON EXPERTS ONLY PLEASE* Please provide the answer coded in Python using comments to explain code...

    *PYTHON EXPERTS ONLY PLEASE* Please provide the answer coded in Python using comments to explain code function. Good answers will be rated with thumbs up! Please answer both parts. Thank you for your time. Question 3 - Suppose you have a file named numbers.csv which contains a bunch of integers, five per line of text, separated by commas. Write code below that will open the _le, read the numbers from it, and print the sum of all the even numbers...

  • Please answer this python questions.Thank you! Question Two      Write a program that calculates the amount...

    Please answer this python questions.Thank you! Question Two      Write a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what the salary was for each day, and then show the total pay at the end...

  • #Write a function called next_fib. next_fib should take #have two parameters: a list of integers and...

    #Write a function called next_fib. next_fib should take #have two parameters: a list of integers and a single integer. #For this description, we'll call the single integer n. # #next_fib should modify the list to add the next n pseudo- #Fibonacci numbers to the end of the sequence. A pseudo- #Fibonacci number is the sum of the previous two numbers in #the sequence, but in our case, the previous two numbers may #not be the original numbers from the Fibonacci...

  • Language is Python 3, please include comments Question 1 Write function collatz, that takes a positive...

    Language is Python 3, please include comments Question 1 Write function collatz, that takes a positive integer x as input and prints the Collatz sequence starting at x. A Collatz sequence is obtained by repeatedly applying this rule to the previous number x in the sequence: 3/2 if x is even 3.2 +1 if x is odd. Your function should stop when the sequence gets to number 1. Note: It is an open question whether the Collatz sequence of every...

  • Write a function in python this program must also include line comments and validate def repeat(string,...

    Write a function in python this program must also include line comments and validate def repeat(string, n, delim) that returns the string string repeated n times, separated by the string delim. For example, repeat("ho", 3,",") returns "ho, ho, ho".

  • Python code that: Accepts a list of integers and an integer, and returns the index of...

    Python code that: Accepts a list of integers and an integer, and returns the index of the SECOND  occurrence of the integer in the list. It returns None if the number does not occur two or more times in the list. Example 1: second_index([2,34,3,45,34,45,3,3], 3) returns 6 Example 2: second_index([2,34,3,45,34,45,3,3], 45) returns 5 Example 3: second_index([2,34,3,45,134,45,3,3], 134) returns None Example 4: second_index([2,34,3,45,134,45,3,3], 100) returns None

  • How to write python code that is a dice rolling function that generates random numbers. The...

    How to write python code that is a dice rolling function that generates random numbers. The dice rolling function takes two arguments: the first argument is the number of sides on the dice and the second argument is the number of dice. The function returns the sum of the random dice rolls. For example, if I call roll dice(6,2) it might return 7, which is the sum of randomly chosen numbers 4 and 3. It somewhere along the lines of:...

  • ***USING PYTHON***Write a program that reads a five-digit positive integer and breaks it into a sequence...

    ***USING PYTHON***Write a program that reads a five-digit positive integer and breaks it into a sequence of individual digits. for example , thin put 16384 is displayed as: 1 6 3 8 4

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT