Problem 6 - listlib.pairs() (10 points] Define a function listlib.pairs() which accepts a list as an argument, and retur
Posted: Sun May 15, 2022 8:04 am
Problem 6 - listlib.pairs() (10 points] Define a function listlib.pairs() which accepts a list as an argument, and returns a new list containing all pairs of elements from the input list. More specifically, the returned list should (a) contain lists of length two, and (b) have length one less than the length of the input list. If the input has length less than two, the returned list should be empty. Again, your function should not modify the input list in any way. For example, the function call pairs ('a', 'b', 'e'l should return t'a', b'], ['b', 'e'll whereas the call pairs('a', 'b') should return b'l), and the calls pairattal) as well as pairs (1) should return a new empty list. To be clear, it does not matter what the data type of ele ments is, for example, the call pairs (11. a. th. 211) should just return 111, ta'l. Bat. B, 211 On your own. If this wasn't challenging enough, how about defining a generalized operation? Specifically, function windows which takes three arguments: a liste, an integer window size w, and an integer step It should return a list containing all sliding windows of the w, each starting elements after the previous window. To be clear, the elements of the returned list are lists themselves. Also make the stepan optional argument, with a default value of 2. Some examples should clarify what window does. First off the function call window should behave identically to main(x), for any list windows (1,2,30991 should retum 13.10. The function call window (2) should tumn 1.1 and the function call window should rolum you get the idea of course the input it does cantan anything, woda fw.conto integy to make it easier to see how the watput relates to the input
REN 006 7 def pairs(1st): if len(1st) < 2: return [] result = [] for i in range(len(1st) - 1): result.append([1st, Ist[i + 1]]) return result 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 def windows (lst, w, s=1): result = [] index = 0 while (index + w) <= len(1st): result.append(1st[index:index + w]) index += S return result if name_ '__main__': print(pairs(['a', 'b', 'c'])) print(pairs([1, 'a', ['b', 2]])) print(windows([1, 2, 3, 4, 5], 2, 1)) print(windows([1, 2, 3, 4, 5], 3, 1)) print(windows([1, 2, 3, 4, 5], 2, 3))
Pairs Tests test_empty_result test_empty_not_alias test_singleton_result test_singleton_result... test_simple test_range99 o test_import © test is fibrary
REN 006 7 def pairs(1st): if len(1st) < 2: return [] result = [] for i in range(len(1st) - 1): result.append([1st, Ist[i + 1]]) return result 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 def windows (lst, w, s=1): result = [] index = 0 while (index + w) <= len(1st): result.append(1st[index:index + w]) index += S return result if name_ '__main__': print(pairs(['a', 'b', 'c'])) print(pairs([1, 'a', ['b', 2]])) print(windows([1, 2, 3, 4, 5], 2, 1)) print(windows([1, 2, 3, 4, 5], 3, 1)) print(windows([1, 2, 3, 4, 5], 2, 3))
Pairs Tests test_empty_result test_empty_not_alias test_singleton_result test_singleton_result... test_simple test_range99 o test_import © test is fibrary