-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
He's pull request #2
base: master
Are you sure you want to change the base?
Changes from all commits
9c75caf
e417b8d
1ac8118
1c5e9fa
73e8309
ba53e0b
9ee8f28
99ed351
31ba0e6
64a6efb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env python | ||
print "this is a python script!" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,55 +2,70 @@ | |
# Return the number of words in the string s. Words are separated by spaces. | ||
# e.g. num_words("abc def") == 2 | ||
def num_words(s): | ||
return 0 | ||
return len(s) | ||
|
||
# PROB 2 | ||
# Return the sum of all the numbers in lst. If lst is empty, return 0. | ||
def sum_list(lst): | ||
return 0 | ||
if len(lst) > 0: | ||
return sum(lst) | ||
else: | ||
return 0 | ||
|
||
# PROB 3 | ||
# Return True if x is in lst, otherwise return False. | ||
def appears_in_list(x, lst): | ||
return False | ||
return x in lst | ||
|
||
# PROB 4 | ||
# Return the number of unique strings in lst. | ||
# e.g. num_unique(["a", "b", "a", "c", "a"]) == 3 | ||
def num_unique(lst): | ||
return 0 | ||
return len(set(lst)) | ||
|
||
# PROB 5 | ||
# Return a new list, where the contents of the new list are lst in reverse order. | ||
# e.g. reverse_list([3, 2, 1]) == [1, 2, 3] | ||
def reverse_list(lst): | ||
return [] | ||
return reversed(lst) | ||
|
||
# PROB 6 | ||
# Return a new list containing the elements of lst in sorted decreasing order. | ||
# e.g. sort_reverse([5, 7, 6, 8]) == [8, 7, 6, 5] | ||
def sort_reverse(lst): | ||
return [] | ||
return reversed(sorted(lst)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another way: sorted(lst, reverse=True) |
||
|
||
# PROB 7 | ||
# Return a new string containing the same contents of s, but with all the | ||
# vowels (upper and lower case) removed. Vowels do not include 'y' | ||
# e.g. remove_vowels("abcdeABCDE") == "bcdBCD" | ||
def remove_vowels(s): | ||
for letter in s: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for c in 'aeiouAEIOU':
s = s.replace(c, '') |
||
if letter in ['a','e','i','o','u']: | ||
s = s.replace(letter,'') | ||
return s | ||
|
||
# PROB 8 | ||
# Return the longest word in the lst. If the lst is empty, return None. | ||
# e.g. longest_word(["a", "aaaaaa", "aaa", "aaaa"]) == "aaaaaa" | ||
def longest_word(lst): | ||
return None | ||
if len(lst) > 0: | ||
return max(lst, key=len) | ||
else: | ||
return None | ||
|
||
# PROB 9 | ||
# Return a dictionary, mapping each word to the number of times the word | ||
# appears in lst. | ||
# e.g. word_frequency(["a", "a", "aaa", "b", "b", "b"]) == {"a": 2, "aaa": 1, "b": 3} | ||
def word_frequency(lst): | ||
return {} | ||
new_dict = {} | ||
for word in lst: | ||
if word in new_dict: | ||
new_dict[word] += 1 | ||
else: | ||
new_dict[word] = 1 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return new_dict |
||
|
||
# PROB 10 | ||
# Return the tuple (word, count) for the word that appears the most frequently | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,22 +16,28 @@ def load(dictionary_name): | |
Each line in the file contains exactly one word. | ||
""" | ||
# TODO: remove the pass line and write your own code | ||
pass | ||
f = open(dictionary_name, "r") | ||
words = set() | ||
for line in f: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. google 'set comprehensions'--that's another cool way of doing this! |
||
line_stripped = line.strip() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could combine these two lines and get rid of line_stripped |
||
words.add(line_stripped) | ||
f.close() | ||
return words | ||
|
||
def check(dictionary, word): | ||
""" | ||
Returns True if `word` is in the English `dictionary`. | ||
""" | ||
pass | ||
return word in dictionary | ||
|
||
def size(dictionary): | ||
""" | ||
Returns the number of words in the English `dictionary`. | ||
""" | ||
pass | ||
return len(dictionary) | ||
|
||
def unload(dictionary): | ||
""" | ||
Removes everything from the English `dictionary`. | ||
""" | ||
pass | ||
dictionary.clear() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return sum(lst)
i believe sum([]) returns 0 by default