I am just testing out a few small functions for learning purposes:
def peaks(iterable): # returns a list of int for those values in the iterable that are bigger # than the value preceding and following them. itr = iter(iterable) peak = curr = next(itr) last = next(itr) try: while True: first = curr curr = last last = next(itr) if curr > first and curr > last: peak.append(curr) except: pass return peak def compress(v_iterable,b_iterable): #takes two iterables as parameters: it produces every value from the first iterable that # has its equivalent position in the second iterable representing what Python would consider # a True value. Terminate the iteration when either iterable terminates mega_it = dict(zip(v_iterable, b_iterable)) for nxt in sorted(mega_it): if mega_it[nxt]: yield nxt def skipper(iterable,n=0): #akes one iterable and one int (whose default value is 0) as parameters: it produces values from # the iterable, starting at the first one, skipping the number of values specified by the int #(skipping 0 means producing every value from the iterable) itr = iter(iterable) yield next(itr) while True: for i in range(n): skipped = next(itr) yield next(itr)
I feel like my codes are lengthy for the kind of work it does. Is there a way to make my functions cleaner or smaller?