Python Code: Problem – listlib.lengths() - Define a function listlib.lengths which accepts a list of lists as an argumen

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

Python Code: Problem – listlib.lengths() - Define a function listlib.lengths which accepts a list of lists as an argumen

Post by answerhappygod »

Python Code:
Problem – listlib.lengths() - Define a function listlib.lengths
which accepts a list of lists as an argument, and returns a new
list of integers, containing the lengths of all inner lists.
Clearly, the result should have the same length as the (outer) list
input. Again, you should not modify any of the lists in any way.
For example, the function call lengths([[1,2], ['a', [100, 10],
'b']]) should return a list equal to [2, 3].
Hint: This is no more difficult than the convert_inputs function
from the previous assignment; don’t let the data type of the
(outer) list’s elements lead you to overthinking. ;-) More
specifically, you already implemented the “transform” [
s0,s1,...,sN−1 ] → [ float(s0),float(s1),...,float(sN−1) ]. The
“transform” in this problem, i.e., [ `0,`1,...,`N−1 ] →[
len(`0),len(`1),...,len(`N−1) ] isn’t really that different.
Problem – listlib.longest() - Define a function lstlib.longest
which accepts a non-empty list of lists‡ as an argument, and
returns the longest (sub-)list. You can assume that the input
list is non-empty (i.e., contains at least one (sub-)list). Just to
be clear, you should return the (sub-)list itself, not it’s length,
or a copy of the (sub-)list, or anything else. If there are ties,
then you should return the earliest list. Finally, once again you
should not modify the input list in any way. For example, the
function call longest([[1,2], ['a', [100, 10], 'b']]) should simply
return the second list from the input argument (i.e., ['a', [100,
10], 'b']). Or, for a little less contrived input, the call
longest([[-1,0], [1,2,3], [2,4], [], [3,2,1]]) should return the
second list from the input argument (i.e., [1,2,3]); this also
illustrates the tiebreaker requirement (both [1,2,3] and [3,2,1]
have maximal length, so the earliest was returned).
Hint [1]: The similarity is that, once again, you have to work
out a conditional update rule. You need to return one of the
(sub-)lists, so you’ll still be keeping track of a “longest list
(so far)”. However, the condition on whether to update depends on
the length (of the current list vs the longest so far), not of the
lists themselves.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply