COSI 2a Lecture Notes - Lecture 3: Form Letter, Abbreviation

53 views3 pages

Document Summary

Write a function abbrev2(w) which accepts a word w that has at least 4 letters. It replaces all but the first and last letters with three periods so, abbrev2(happy) --> hapy abbrev2(president) --> prnt abbrev2(goshdarn) --> gorn Solution: def abbrev2(w): newword = w[0:2] + + w[-2:] return newword. Getting the first 3 values from a list. Write a function first3(vals) which accepts a list which has length at least 3 and returns a new list containing the first three elements. So, first3([2,3,5,7,11,13,17,19,23]) --> [2,3,5] first3([apple,bag,can,drill,engine,fire]) --> [apple,bag,can] first3([25,zzz,3. 14,19,97,hike] ) --> [25,zzz,3. 14] Solution: def first3(vals): newlist = vals[0:3] return newlist. Changing the position of a value from a list. Write a function rotate1(vals) which accepts a list vals and returns a new list by putting the first element at the end. So rotate1([1,2,3,4,5]) --> [2,3,4,5,1] rotate1([yoda, says, hi, to, you]) --> [ says, hi, to, you, yoda] rotate1([3,6,9,9,9,9]) --> [6,9,9,9,9,3] Solution: def rotate1(vals): newlist=vals[1:] + vals[0:1] return newlist.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents