Skip to content

Open In Colab

Python Foundations
X2: Additional Python Exercises

Instructor: Wesley Beckner

Contact: wesleybeckner@gmail.com

Solved: Notebook



A set of additional exercises in Python!



โœ๏ธ Q1: Write a function that orders a list

# list to order
my_list = [1, 1, 1, 1, 2, 5, 10, 30, 2, 1, 4, 5, 1, 1, 1]
# Cell for Q1
[30, 10, 5, 5, 4, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1]

๐Ÿ‘ฝ Q2: Write a function that reads sentences and displays a word count

corpus = 'DREAM SEQUENCE: EXT. ARRAKIS - END OF DAY 1\
The planet Arrakis, as seen from space. \
Track across its endless windswept terrain. \
We glide into a low-hanging dark cloud thatโ€™s generated by a\
massive mining vehicle, a HARVESTER, kicking up glowing\
flecks of SPICE. We PUSH through the SPICE, creating a\
dreamlike swirl of orange flakes. \
Through the swirl WE REVEAL a SECOND HARVESTER airborne,\
being hauled by a powerful CARRYALL. \
ON THE GROUND - HARKONNEN SOLDIERS flanking the harvester,\
leading the industrial nightmare through the darkness. One of\
them holds a massive flag bearing the HARKONNEN EMBLEM. \
Now these soldiers are observed through the P.O.V. of a\
thermal scope. Reveal that this scope is attached to a\
strange MISSLE LAUNCHER, one of multiple cloth-shrouded\
weapons being wielded by a small band of blue-eye FREMEN\
FIGHTERS taking cover behind a sprawling black rock. A young\
female fighter, CHANI, is among them; along with a'
# Cell for Q2

๐Ÿ”€ Q3: Use the function you created in Q1 to rank order the appearance of words in the corpus

# Cell for Q3
{'a': 9,
 'the': 8,
 'of': 6,
 'through': 4,
 'we': 3,
 'by': 3,
 'harvester': 3,
 'arrakis': 2,
 '': 2,
 'spice': 2,
 'swirl': 2,
 'reveal': 2,
 'harkonnen': 2,
 'soldiers': 2,
 'one': 2,
 'scope': 2,
 'is': 2,
 'dream': 1,
 'sequence': 1,
 'ext': 1,
 'end': 1,
 'day': 1,
 '1the': 1,
 'planet': 1,
 'as': 1,
 'seen': 1,
 'from': 1,
 'space': 1,
 'track': 1,
 'across': 1,
 'its': 1,
 'endless': 1,
 'windswept': 1,
 'terrain': 1,
 'glide': 1,
 'into': 1,
 'low': 1,
 'dark': 1,
 'cloud': 1,
 'thatโ€™s': 1,
 'generated': 1,
 'amassive': 1,
 'mining': 1,
 'vehicle': 1,
 'kicking': 1,
 'up': 1,
 'glowingflecks': 1,
 'push': 1,
 'creating': 1,
 'adreamlike': 1,
 'orange': 1,
 'flakes': 1,
 'second': 1,
 'airborne': 1,
 'hauled': 1,
 'powerful': 1,
 'carryall': 1,
 'on': 1,
 'ground': 1,
 'flanking': 1,
 'industrial': 1,
 'nightmare': 1,
 'darkness': 1,
 'ofthem': 1,
 'holds': 1,
 'massive': 1,
 'flag': 1,
 'bearing': 1,
 'emblem': 1,
 'now': 1,
 'these': 1,
 'are': 1,
 'observed': 1,
 'p.o.v.': 1,
 'athermal': 1,
 'that': 1,
 'this': 1,
 'attached': 1,
 'to': 1,
 'astrange': 1,
 'missle': 1,
 'launcher': 1,
 'multiple': 1,
 'cloth': 1,
 'being': 1,
 'wielded': 1,
 'small': 1,
 'band': 1,
 'blue': 1,
 'fremenfighters': 1,
 'taking': 1,
 'cover': 1,
 'behind': 1,
 'sprawling': 1,
 'black': 1,
 'rock': 1,
 'youngfemale': 1,
 'fighter': 1,
 'chani': 1,
 'among': 1,
 'them': 1,
 'along': 1,
 'with': 1}

๐ŸŽ๏ธ Q4 Write a function that checks if a string input is a palindrome

the function should operate in place and should ignore capitalization and punctuation

# the following should all return true
str1 = "racecar"
str2 = "RacEcar"
str3 = ".Ra.cecar"
str4 = ".Ra.cecar...:!."

๐Ÿงฎ Q5: Write a function that returns true if any contiguous numbers in a list sum to the value of K

arr1, k1 = [1, 3, 1, 4], 8 # returns True
arr2, k2 = [1, 3, 1, 4], 7 # returns False
arr3, k3 = [1, 3, 1, 4, 5, 6, 1, 1, 1], 7 # returns True