-
-
Save chriszf/6785719 to your computer and use it in GitHub Desktop.
Exercise solutions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def custom_len(input_list): | |
"""custom_len(input_list) imitates len(input_list)""" | |
count = 0 | |
for item in input_list: | |
count += 1 | |
return count | |
def custom_remove(input_list, value): | |
"""custom_remove(input_list, value) imitates input_list.remove(value)""" | |
for i in range(custom_len(input_list)): | |
item = input_list[i] | |
if item == value: | |
del input_list[i] | |
input_list[i:i+1] = [] | |
break | |
def custom_pop(input_list): | |
"""custom_pop(input_list) imitates input_list.pop()""" | |
item = input_list[-1] | |
del input_list[-1] | |
return item | |
def custom_reverse(input_list): | |
"""custom_reverse(input_list) imitates input_list.reverse()""" | |
for i in range(custom_len(input_list)/2): | |
front = i | |
back = len(input_list)-1-i | |
tmp = input_list[front] | |
input_list[front] = input_list[back] | |
input_list[back] = tmp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ASCII_A = ord('a') | |
ASCII_Z = ord('z') | |
def main(): | |
letter_counts = [0] * 26 | |
_file = open("whatever.txt") | |
raw_text = _file.read() | |
_file.close() | |
text = raw_text.lower() | |
# text = open("whatever.txt").read().lower() | |
for letter in text: | |
ascii_code = ord(letter) | |
if ascii_code >= ASCII_A and ascii_code <= ASCII_Z: | |
letter_counts[ascii_code - ASCII_A] += 1 | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(): | |
f = open("whatevs") | |
text = f.read().replace(".", "").replace(",", "").replace("!", "") | |
#sorryimnotsorry | |
word_counts = {} | |
words = text.split() | |
for word in words: | |
word_counts[word] = word_counts.get(word, 0) + 1 | |
unique_words = word_counts.keys() | |
unique_words.sort() | |
for word in unique_words: | |
print word_counts[word] | |
tuples = word_counts.items() | |
temp_list = [] | |
for t in tuples: | |
new_tuple = (t[1], t[0]) | |
temp_list.append(new_tuple) | |
temp_list.sort() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment