In mathematics, the look-and-say sequence is the sequence of integers beginning
ID: 3817859 • Letter: I
Question
In mathematics, the look-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211... To generate a member of the sequence from the previous member, read off the digits of the previous member counting the number of digits in groups of the same digit. For example: 1 is read off as "one 1"or 11 11 is read off as "two 1s" or 21 21 is read off as "one 2, then one 1"or 1211 1211 is read off as "one 1, one 2, then two 1s"or 111221 111221 is read off as "three 1s, two 2s, then one 1"or 312211 write the function look say that () takes an integer giving which term of the look-and-say sequence we want, starting from 0, and returns a string containing that term. Term #0 is '1', term #1 is '11', term #2 is '21', and so on. Your function must return the correct string for any non-negative integer argument. No matter how long the numbers become, the only digits that will appear are 1, 2 and 3. Note that quotation marks displayed in the return values are there to emphasize that the return values are the strings. You should not add quotation marks to your return values.Explanation / Answer
# Pastebinlink: https://pastebin.com/vNeC2zhj
def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
def look_say(num):
result = "1"
for i in range(num):
result = lookandsay(result)
return result
print(look_say(3))
print(look_say(8))
print(look_say(12))
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.