diff --git a/basic/list1.py b/basic/list1.py index 4e6171c..089347e 100755 --- a/basic/list1.py +++ b/basic/list1.py @@ -21,8 +21,11 @@ # and last chars of the string are the same. # Note: python does not have a ++ operator, but += works. def match_ends(words): - # +++your code here+++ - return + c = 0; + for x in words: + if len(x) >= 2 and x[0] == x[len(x) - 1]: + c += 1 + return c; # B. front_x @@ -33,8 +36,15 @@ def match_ends(words): # Hint: this can be done by making 2 lists and sorting each of them # before combining them. def front_x(words): - # +++your code here+++ - return + xWords = [] + wordss = [] + for x in words: + if x[0] == 'x': + xWords.append(x) + else: + wordss.append(x) + + return sorted(xWords) + sorted(wordss) # C. sort_last @@ -43,9 +53,12 @@ def front_x(words): # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields # [(2, 2), (1, 3), (3, 4, 5), (1, 7)] # Hint: use a custom key= function to extract the last element form each tuple. +def callMe(e): + return e[len(e) - 1] + def sort_last(tuples): - # +++your code here+++ - return + tuples.sort(key=callMe) + return tuples # Simple provided test() function used in main() to print diff --git a/basic/list2.py b/basic/list2.py index f8e65da..a42ff53 100755 --- a/basic/list2.py +++ b/basic/list2.py @@ -13,8 +13,15 @@ # so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or # modify the passed in list. def remove_adjacent(nums): - # +++your code here+++ - return + temp = [] + if len(nums) != 0: + temp.append(nums[0]) + + for i in range(1, len(nums)): + if nums[i - 1] != nums[i]: + temp.append(nums[i]) + + return temp # E. Given two lists sorted in increasing order, create and return a merged @@ -22,8 +29,7 @@ def remove_adjacent(nums): # Ideally, the solution should work in "linear" time, making a single # pass of both lists. def linear_merge(list1, list2): - # +++your code here+++ - return + return sorted(list1 + list2) # Note: the solution above is kind of cute, but unforunately list.pop(0) diff --git a/basic/mimic.py b/basic/mimic.py index 40e6834..dd0cd86 100755 --- a/basic/mimic.py +++ b/basic/mimic.py @@ -46,14 +46,31 @@ def mimic_dict(filename): - """Returns mimic dict mapping each word to list of words which follow it.""" - # +++your code here+++ - return + f = open(filename) + words = f.read().split() + f.close() + dict = {} + + first = '' + for i in range(0, len(words)): + if(first not in dict.keys()): + dict[first] = [] + + dict[first].append(words[i]) + + first = words[i] + + return dict def print_mimic(mimic_dict, word): - """Given mimic dict and start word, prints 200 random words.""" - # +++your code here+++ + + + for i in range(0, 200): + print(word + ' ') + if(word not in mimic_dict.keys()): + word = '' + word = random.choice(mimic_dict[word]) return diff --git a/basic/string1.py b/basic/string1.py index 34d2716..f04a429 100755 --- a/basic/string1.py +++ b/basic/string1.py @@ -24,8 +24,10 @@ # So donuts(5) returns 'Number of donuts: 5' # and donuts(23) returns 'Number of donuts: many' def donuts(count): - # +++your code here+++ - return + res = count + if count > 9: + res = 'many' + return 'Number of donuts: {0}'.format(res) # B. both_ends @@ -34,8 +36,10 @@ def donuts(count): # so 'spring' yields 'spng'. However, if the string length # is less than 2, return instead the empty string. def both_ends(s): - # +++your code here+++ - return + s_len = len(s) + if s_len < 2: + return '' + return s[0] + s[1] + s[s_len - 2] + s[s_len - 1] # C. fix_start @@ -48,8 +52,11 @@ def both_ends(s): # Hint: s.replace(stra, strb) returns a version of string s # where all instances of stra have been replaced by strb. def fix_start(s): - # +++your code here+++ - return + save = s[0] + s2 = s.replace(s[0], '*') + s2 = list(s2) + s2[0] = save + return ''.join(s2) # D. MixUp @@ -60,8 +67,14 @@ def fix_start(s): # 'dog', 'dinner' -> 'dig donner' # Assume a and b are length 2 or more. def mix_up(a, b): - # +++your code here+++ - return + a = list(a) + b = list(b) + temp = a[0:2] + a[0] = b[0] + a[1] = b[1] + b[0] = temp[0] + b[1] = temp[1] + return ''.join(a) + ' ' + ''.join(b) # Provided simple test() function used in main() to print diff --git a/basic/string2.py b/basic/string2.py index 631091a..f148427 100755 --- a/basic/string2.py +++ b/basic/string2.py @@ -16,8 +16,13 @@ # If the string length is less than 3, leave it unchanged. # Return the resulting string. def verbing(s): - # +++your code here+++ - return + slen = len(s) + if slen >= 3: + if(s[slen - 3 : slen] == 'ing'): + s += 'ly' + else: + s += 'ing' + return s # E. not_bad @@ -29,8 +34,14 @@ def verbing(s): # So 'This dinner is not that bad!' yields: # This dinner is good! def not_bad(s): - # +++your code here+++ - return + snot = s.find('not') + sbad = s.find('bad') + if ('bad' in s) and ('not' in s): + if snot < sbad: + firstPart = s[0 : snot] + lastPart = s[sbad + 3 : len(s)] + s = firstPart + 'good' + lastPart + return s # F. front_back @@ -41,8 +52,15 @@ def not_bad(s): # Given 2 strings, a and b, return a string of the form # a-front + b-front + a-back + b-back def front_back(a, b): - # +++your code here+++ - return + a_i = int(len(a) / 2) + b_i = int(len(b) / 2) + + if(len(a) % 2 != 0): + a_i += 1 + if(len(b) % 2 != 0): + b_i += 1 + + return a[:a_i] + b[:b_i] + a[a_i:] + b[b_i:] # Simple provided test() function used in main() to print diff --git a/basic/wordcount.py b/basic/wordcount.py index a6c7c2e..64f837c 100755 --- a/basic/wordcount.py +++ b/basic/wordcount.py @@ -36,7 +36,6 @@ print_words() and print_top(). """ - import sys @@ -46,6 +45,37 @@ # and builds and returns a word/count dict for it. # Then print_words() and print_top() can just call the utility function. +from collections import Counter + + +def print_words(filename): + f = open(filename) + words = f.read().split() + f.close() + c = Counter() + + for x in words: + c[x.lower()] += 1 + + for x in sorted(c.items()): + print(x[0] + ' ' + str(x[1])) + + return + +def print_top(filename): + f = open(filename) + words = f.read().split() + f.close() + c = Counter() + + for x in words: + c[x.lower()] += 1 + + for x in sorted(c.most_common(20)): + print(x[0] + ' ' + str(x[1])) + + + return ### # This basic command line argument parsing code is provided and