-
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
emasatsugu python bootcamp #10
base: master
Are you sure you want to change the base?
Changes from all commits
50becf7
685e688
ff2f613
bf3f73c
ddddd9a
5e34fd2
c9c2f6d
7ae2efa
b5dd47b
5e97fcf
de8a990
2266084
af3c172
7e68aaf
3329366
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 |
---|---|---|
|
@@ -9,3 +9,5 @@ | |
# | ||
# TODO: write your code below | ||
|
||
|
||
print "hello world"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env python | ||
print "this is a python script!" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,73 +2,113 @@ | |
# 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.split()) | ||
|
||
# PROB 2 | ||
# Return the sum of all the numbers in lst. If lst is empty, return 0. | ||
def sum_list(lst): | ||
return 0 | ||
sum = 0 | ||
for num in lst: | ||
sum += num | ||
return sum | ||
|
||
# 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 [] | ||
newlst = [] | ||
for x in reversed(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. return reversed(lst) |
||
newlst.append(x) | ||
return newlst; | ||
|
||
|
||
# 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 [] | ||
newlst = 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. return sorted(lst, reverse=True) |
||
return reverse_list(newlst) | ||
|
||
# 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): | ||
return s | ||
news = s; | ||
vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] | ||
for letter in s: | ||
if letter in vowels: | ||
news = news.replace(letter, "") | ||
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. no need to have the 'for letter in s' loop--just loop over vowels and remove them from s! |
||
return news | ||
|
||
|
||
# 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 | ||
longest = "" | ||
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. can you think of a way of using sorted()? python functions can take named parameters. if you look at the documentation for sorted, there's a named parameter called key which allows you to sort a list of things based on the value you get by applying the key function on all the items in the list. so if you want to compare strings based on length, you can pass in |
||
if len(lst) == 0: | ||
return None; | ||
for word in lst: | ||
if len(word) > len(longest): | ||
longest = word | ||
return longest; | ||
|
||
# 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 {} | ||
cnt = {} | ||
for word in lst: | ||
if word in cnt: | ||
cnt[word] += 1 | ||
else: | ||
cnt[word] = 1 | ||
return cnt; | ||
|
||
# PROB 10 | ||
# Return the tuple (word, count) for the word that appears the most frequently | ||
# in the list, and the number of times the word appears. If the list is empty, return None. | ||
# e.g. most_frequent_word(["a", "a", "aaa", "b", "b", "b"]) == ("b", 3) | ||
def most_frequent_word(lst): | ||
return None | ||
if len(lst) == 0: | ||
return None | ||
freqs = word_frequency(lst) | ||
sortedfreqs = sorted(freqs.items(), key=lambda (word,count): count, reverse=True) | ||
return sortedfreqs[0] | ||
|
||
|
||
# PROB 11 | ||
# Compares the two lists and finds all the positions that are mismatched in the list. | ||
# Assume that len(lst1) == len(lst2). Return a list containing the indices of all the | ||
# mismatched positions in the list. | ||
# e.g. find_mismatch(["a", "b", "c", "d", "e"], ["f", "b", "c", "g", "e"]) == [0, 3] | ||
def find_mismatch(lst1, lst2): | ||
return [] | ||
mismatch = [] | ||
for i in range(len(lst1)): | ||
if lst1[i] != lst2[i]: | ||
mismatch.append(i) | ||
return mismatch | ||
|
||
# PROB 12 | ||
# Returns the list of words that are in word_list but not in vocab_list. | ||
def spell_checker(vocab_list, word_list): | ||
return [] | ||
#vocab = set(vocab_list) | ||
misspelled = set() | ||
for word in word_list: | ||
if word not in vocab_list: | ||
misspelled.add(word) | ||
return misspelled | ||
|
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 | ||
words = set(); | ||
word_file = open(dictionary_name, "rb") | ||
for word in word_file: | ||
word = word.strip() | ||
words.add(word) | ||
word_file.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(list(dictionary)) | ||
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. no need to transform dictionary into a list! you can just call |
||
|
||
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)