Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me code these while meeting all the assertions? Thank you in advance. Each new problem is bolded. Exercise 1 defexercise_1(my_list): Assume`my_list`isalistofallnumericvalues,`my_list`hasatleast2items

Can someone help me code these while meeting all the assertions? Thank you in advance. Each new problem is bolded.

Exercise 1

defexercise_1(my_list):

"""

Assume`my_list`isalistofallnumericvalues,`my_list`hasatleast2items

Returnalist

Goal:Returnalistcontainingthe2largestvaluesina

"""

###BEGINSOLUTION

###ENDSOLUTION

assertexercise_1([1,2,3,4,5,6])==[5,6]orexercise_1([1,2,3,4,5,6])==[6,5]

assertexercise_1([1,1,1,1,1])==[1,1]

assertexercise_1([-100,-1000,-10000])==[-100,-1000]orexercise_1([-100,-1000,-10000])==[-1000,-100]

Exercise 2

defexercise_2(my_list):

"""

Assume`my_list`isalistofstrings.

Returnanint

Goal:Returnthenumberofstringsin`my_list`thebeginwiththecharacter'T'

"""

###BEGINSOLUTION

###ENDSOLUTION

assertexercise_2(['a','b','c'])==0

assertexercise_2(['Totally','Dude'])==1

assertexercise_2(['Toronto','Torino','Toledo'])==3

Exercise 3

defexercise_3(my_list):

"""

Assume`my_list`isalistoflists,thesublistsarealllistsofints

Returnalist

Goal:"flatten"`my_list`fromalistoflistsintoaSORTED

listcontainingalloftheelementsofthesublists

"""

###BEGINSOLUTION

###ENDSOLUTION

assertexercise_3([[1,2,3],[4,5,6]])==[1,2,3,4,5,6]

assertexercise_3([[1,1,1,1,1]])==[1,1,1,1,1]

assertexercise_3([[1],[2],[4],[6,7]])==[1,2,4,6,7]

Exercise 4

defexercise_4(my_list):

"""

Assume`my_list`listofstrings

Returnalist

Goal:Returnalistoftheuniquevaluesin`my_list`;inotherwordsreturn

alistwiththevaluesof`my_list`withoutduplicates

"""

###BEGINSOLUTION

###ENDSOLUTION

return_val=exercise_4(['1','1','1','1'])

assertlen(return_val)==1andall(iteminreturn_valforitemin['1'])

return_val=exercise_4(['Trump','Obama','Bush','Clinton','Bush'])

assertlen(return_val)==4andall(iteminreturn_valforitemin['Clinton','Trump','Obama','Bush'])

Exercise 5

defexercise_5(my_list):

"""

Assume`my_list`isalistwithanevennumberofelements

Returnalist

Goal:Addthestring'Hi!'asthemiddleelementofmy_list

Hint:Ifmy_listisgivenwith4elementsreturnvalueshouldhave5with'Hi!'between

thefirst2elementsofmy_listandthelast2elementsofmy_list

"""

###BEGINSOLUTION

###ENDSOLUTION

assertexercise_5([1,2,4,5])==[1,2,'Hi!',4,5]

assertexercise_5([[],[]])==[[],'Hi!',[]]

assertexercise_5(list('abcdef'))==['a','b','c','Hi!','d','e','f']

Exercise 6

defexercise_6(my_list):

"""

Assume`my_list`isaliststrings,bools,ornumerics

Retrunabool

Goal:Ifthefirstandlastelementofmy_listarethesamereturnTrueotherwisereturnFalse

"""

###BEGINSOLUTION

###ENDSOLUTION

assertexercise_6(['a','b','a'])==True

assertexercise_6(['a','b''c'])==False

assertexercise_6(['-1'])==True

Exercise 7

defexercise_7(my_list):

"""

Assume`my_list`isalistofnumerics

Retrunanlistof2ints

Goal:ReturnTheindexofthe2largestelementsinthelist

Donotusenumpy!Itwillbegradedasincorrect

"""

###BEGINSOLUTION

###ENDSOLUTION

assertexercise_7([1,2,3,4,5,6])==[4,5]orexercise_7([1,2,3,4,5,6])==[5,4]

assertexercise_7([-100,-1000,-10000])==[0,1]orexercise_7([-100,-1000,-10000])==[1,0]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

1 An exact definition for gross domestic product, or GDP

Answered: 1 week ago