Skip to content
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

Pull request ipod/Roger hey it works! #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified examples/ifelse.py
100644 → 100755
Empty file.
2 changes: 2 additions & 0 deletions exercises-hello/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
#
# TODO: write your code below

print "hello world"

2 changes: 2 additions & 0 deletions exercises-hello/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python
print "this is a python script!"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay!!!

79 changes: 68 additions & 11 deletions exercises-more/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,130 @@
# 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
s = s.split()
return len(s)

# PROB 2
# Return the sum of all the numbers in lst. If lst is empty, return 0.
def sum_list(lst):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return sum(lst)

return 0
n = 0
for num in lst:
n += num
return n

# 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
s = set()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can convert a list into a set with set(lst)

for a in lst:
s.add(a)
return len(s)

# 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 []
lst.reverse()
return 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 []
#
#for i in len(lst) - 2:
# if list[i] > list[i + 1]
lst.sort()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is also a built-in sorted(lst, reverse=True)

return reverse_list(lst)

# 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):
s = s.replace("a", "")
s = s.replace("A", "")
s = s.replace("e", "")
s = s.replace("E", "")
s = s.replace("i", "")
s = s.replace("I", "")
s = s.replace("o", "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for c in 'aeiouAEIOU':
    s = s.replace(c, "")

s = s.replace("O", "")
s = s.replace("u", "")
s = s.replace("U", "")
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
n = 0
g = ""
for word in lst:
if (len(word) > n):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you think of a way of using sorted()? you'll want to use the named parameter key in sorted.

g = word
n = len(word)
if n == 0:
return None
else:
return g

# 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 {}
di = {}
for word in lst:
if word in di.keys():
di[word] += 1
else:
di[word] = 1
return di

# 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
di = word_frequency(lst)
hold = ""
n = 0
for key in di:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here--try to use sorted() instead

if di[key] > n:
hold = key
n = di[key]
if n == 0:
return None
else:
return (hold, n)

# 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 []
num = []
a = 0
for l in lst1:
if l not in lst2[a]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is O(n^2). can you do this in O(n)?

num.append(a)
a += 1
return num

# 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 []
lstr = []
for word in word_list:
if not word in vocab_list:
lstr.append(word)
return lstr

17 changes: 10 additions & 7 deletions exercises-spellchecker/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,25 @@ 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:
stripper = line.strip() #takes away new lines
words.add(stripper)
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()