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 .
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
