Write a generator function named count_seq that doesn't take any parameters and generates a sequence that starts like this: 2, 12, 1112, 3112, 132112, 1113122112, 311311222112, 13211321322112, ...

To get a number in the sequence, enumerate how many there are of each digit (in a row) in the previous number. For example, the first number is "one 2", which gives us the second number "12". That number is "one 1" followed by "one 2", which gives us the third number "1112". That number is "three 1" followed by "one 2", or 3112. Etc.

Your generator function won't just go up to some limit - it will keep going indefinitely. It may need to treat the first one or two values as special cases, which is fine.

Your file must be named: count_seq.py

Respuesta :

Answer:

#code (count_seq.py)

def count_seq():

   n='2'

   while True:

       yield int(n)

       next_value=''

       while len(n)>0:

           first=n[0]

           count=0

     

           while len(n)>0 and n[0]==first:

               count+=1

               n=n[1:]

           next_value+='{}{}'.format(count,first)

       n=next_value

if __name__ == '__main__':

   gen=count_seq()

   for i in range(10):

       print(next(gen))

Explanation:

  • Start with number 2. Use string instead of integers for easy manipulation .
  • Loop indefinitely .
  • Yield the integer value of current n .
  • Loop until n is an empty string .
  • Loop as long as n is non empty and first digit of n is same as first .
  • Append count and first digit to next_value .
fichoh

The program produces a number sequence, the function named count_seq is written in python 3 thus :

def count_seq():

n= str(2)

#use string value starting from 2

while True:

#while loop continues indefinitely

yield int(n)

#yiield integer value of variable n

next_value=''

while len(n)>0:

first=n[0]

#subset the first index value of n and assign to first

count=0

while len(n)>0 and n[0]==first:

#use the boolean to set a condition for the while loop

count+=1

#increase count by 1

n=n[1:]

next_value+='{}{}'.format(count,first)

n=next_value

if __name__ == '__main__':

g =count_seq()

#assign the function to g

for i in range(10):

print(next(gen))

#print 10 iterations.

A sample run of the program is attached.

Learn more :https://brainly.com/question/19754999

Ver imagen fichoh