Python Foundations
X2: Additional Python Exercises¶
Instructor: Wesley Beckner
Contact: wesleybeckner@gmail.com
A set of additional exercises in Python!
โ๏ธ Q1: Write a function that orders a list¶
my_list = [1, 1, 1, 1, 2, 5, 10, 30, 2, 1, 4, 5, 1, 1, 1]
my_list = [1, 1, 1, 1, 2, 5, 10, 30, 2, 1, 4, 5, 1, 1, 1]
new_list = []
for item in my_list:
if len(new_list) == 0:
new_list.append(item)
else:
for idx, ranked in enumerate(new_list):
if item > ranked:
new_list.insert(idx, item)
break
elif idx == len(new_list)-1:
new_list.append(item)
break
new_list
[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'
punc = [".", ",", "-", ";", ":"]
words = {}
for word in corpus.split(" "):
word = word.lower()
for p in punc:
if len(word.split(p)) == 2:
word = word.split(p)[0]
break
if word not in words:
words[word] = 1
else:
words[word] += 1
๐ Q3: Use the function you created in Q1 to rank order the appearance of words in the corpus¶
def order_dict(word_dict):
my_list = word_dict.values()
my_words = list(word_dict.keys())
new_list = []
new_words = []
for idx1, item in enumerate(my_list):
if len(new_list) == 0:
new_list.append(item)
new_words.append(my_words[idx1])
else:
for idx2, ranked in enumerate(new_list):
if item > ranked:
new_list.insert(idx2, item)
new_words.insert(idx2, my_words[idx1])
break
elif idx2 == len(new_list) - 1:
new_list.append(item)
new_words.append(my_words[idx1])
break
return dict(zip(new_words, new_list))
sort = order_dict(words)
sort
{'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...:!."
def check_pal(string, verbose=0):
punc = ["!", ".", ",", ":", ";"]
idx = 0
edx = -1
estep = 0
while True:
if (string[idx] not in punc) and (string[edx] not in punc):
if string[idx].lower() != string[edx].lower():
return False
elif string[idx] in punc:
if verbose:
print(string[idx])
idx += 1
continue
elif string[edx] in punc:
if verbose:
print(string[edx])
edx -= 1
continue
if verbose:
print(string[idx].lower(), string[edx].lower())
idx += 1
edx -= 1
if idx == len(string)-1 or edx == -len(string):
break
return True
check_pal(str4)
True
๐งฎ 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
def check_sum(arr, k, verbose=0):
winsum = arr[0]
sdx = 0
edx = 1
while edx < len(arr)-1:
while winsum < k:
winsum += arr[edx]
if verbose:
print(winsum)
edx += 1
if winsum == k:
return True
while winsum > k:
winsum -= arr[sdx]
if verbose:
print(winsum)
sdx += 1
if winsum == k:
return True
return False
check_sum(arr3, k3)
True