Problem 6 - listlib.pairs() (10 points] Define a function listlib.pairs() which accepts a list as an argument, and retur

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Problem 6 - listlib.pairs() (10 points] Define a function listlib.pairs() which accepts a list as an argument, and retur

Post by answerhappygod »

Problem 6 Listlib Pairs 10 Points Define A Function Listlib Pairs Which Accepts A List As An Argument And Retur 1
Problem 6 Listlib Pairs 10 Points Define A Function Listlib Pairs Which Accepts A List As An Argument And Retur 1 (47.65 KiB) Viewed 52 times
Problem 6 Listlib Pairs 10 Points Define A Function Listlib Pairs Which Accepts A List As An Argument And Retur 2
Problem 6 Listlib Pairs 10 Points Define A Function Listlib Pairs Which Accepts A List As An Argument And Retur 2 (47.65 KiB) Viewed 52 times
Problem 6 Listlib Pairs 10 Points Define A Function Listlib Pairs Which Accepts A List As An Argument And Retur 3
Problem 6 Listlib Pairs 10 Points Define A Function Listlib Pairs Which Accepts A List As An Argument And Retur 3 (60.71 KiB) Viewed 52 times
Problem 6 Listlib Pairs 10 Points Define A Function Listlib Pairs Which Accepts A List As An Argument And Retur 4
Problem 6 Listlib Pairs 10 Points Define A Function Listlib Pairs Which Accepts A List As An Argument And Retur 4 (95.97 KiB) Viewed 52 times
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
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply