Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

# Test sequence e-->\'\'.join([v for v in sequence(\'abc\', \'d\', \'ef\', \'ghi

ID: 3606685 • Letter: #

Question

# Test sequence
e-->''.join([v for v in sequence('abc', 'd', 'ef', 'ghi')])-->abcdefghi
e-->''.join([v for v in sequence(hide('abc'), hide('d'), hide('ef'), hide('ghi'))])-->abcdefghi
e-->''.join([v for v in sequence(hide('ab'), hide('cdefgh'), hide('i'), hide('jklm'))])-->abcdefghijklm

# Test group_when
==-->[v for v in group_when('combustibles', lambda x : x in 'aeiou')]-->[['c', 'o'], ['m', 'b', 'u'], ['s', 't', 'i'], ['b', 'l', 'e'], ['s']]
==-->[v for v in group_when(hide('combustibles'), lambda x : x in 'aeiou')]-->[['c', 'o'], ['m', 'b', 'u'], ['s', 't', 'i'], ['b', 'l', 'e'], ['s']]
==-->[v for v in group_when(hide('combustibles'), lambda x : x in 'us')]-->[['c', 'o', 'm', 'b', 'u'], ['s'], ['t', 'i', 'b', 'l', 'e', 's']]

Remember, if an argument is iterable, it means that you can call only iter on it, and then call next on the value iter returns (recall for loops do this automatically) There is no guarantee you can call len on the iterable or index/slice it. You may not copy all the values of an iterable into a list (or any other data structure) so that you can perform these operations (that is not in the spirit of the assignment, and some iterables could produce an infinite number of values). You may create local data structures storing as many values as the arguments or the result that the function returns, but not all the values in the iterable(s): in fact, some may be infinite. Remember the "exchange print vs. yield" heuristic for writing generators from the notes 1. (20 pts) Write generators below (each one is worth 4 points) that satisfy the following specifications. You may not import any of the generators in itertools or any other modules to write your generators. You may use any of the standard functions like zip and enumerate a. The sequence generator takes any number of iterables as parameters: it produces every value from the first iterable, followed by every value from the second iterable, etc., Hint: I used only for loops. For example for i in sequence ('abc', 'd', 'ef', 'ghi'): print(i, end-'") prints abcdefghi: all characters from 'abc', followed by all in 'd', followed by all in 'ef' etc The group when generator takes one iterable and one predicate as parameters: it produces lists that each end in a value from the iterable where the predicate is True. If the iterable ends on a value for which the predicate retums False, yield a final list containing all the values from the one after the previous end to the last value produced by the iterable. Hint: I used a for loop. For example b. for i in group when ( 'combustibles', lambda x : x in 'aeiou'): print(i, end-') prints the 5 lists ['c', 'o'] ['m', 'b', 'u'] ['s', 't', 'i']['b', '1', 'e']['s']

Explanation / Answer

def sequence(*arg):
for iterableItems in arg:
for item in iterableItems:
yield item
def hide(iterable):
for v in iterable:
yield v
# Test sequence
print(''.join([v for v in sequence('abc', 'd', 'ef', 'ghi')]))
print(''.join([v for v in sequence(hide('abc'), hide('d'), hide('ef'), hide('ghi'))]))
print(''.join([v for v in sequence(hide('ab'), hide('cdefgh'), hide('i'), hide('jklm'))]))

def group_when(iterable, predicate):
yield_list = []
for item in iterable:
yield_list.append(item)
if predicate(item):
yield yield_list
yield_list = []
if len(yield_list):
yield yield_list

print([v for v in group_when('combustibles', lambda x : x in 'aeiou')])
print([v for v in group_when(hide('combustibles'), lambda x : x in 'aeiou')])
print([v for v in group_when(hide('combustibles'), lambda x : x in 'us')])

Sample run

# copy pastable link: https://paste.ee/p/8qphx