diff --git a/basic/list1.py b/basic/list1.py index 4e6171c..ae4a509 100755 --- a/basic/list1.py +++ b/basic/list1.py @@ -21,8 +21,13 @@ # 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 + counter = 0; + for word in words: + wordlen = len(word); + if wordlen > 1: + if(word[wordlen-1] == word[0]): + counter+=1; + return counter; # B. front_x @@ -33,8 +38,16 @@ 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 + xlist = list(); + nlist = list(); + for word in words: + if word[0]=='x': + xlist.append(word); + else: + nlist.append(word); + xlist = sorted(xlist); + nlist = sorted(nlist); + return xlist + nlist; # C. sort_last @@ -43,9 +56,13 @@ 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 tuple_last(tuplee): + for i,var in enumerate(tuplee): + if i == len(tuplee) - 1: + return var; + def sort_last(tuples): - # +++your code here+++ - return + return sorted(tuples, key = tuple_last); # Simple provided test() function used in main() to print diff --git a/basic/list2.py b/basic/list2.py index 390dd1f..2d8a772 100755 --- a/basic/list2.py +++ b/basic/list2.py @@ -13,8 +13,10 @@ # 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 + for num in nums: + if nums.count(num) > 1: + nums.remove(num); + return nums; # E. Given two lists sorted in increasing order, create and return a merged @@ -22,8 +24,25 @@ 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 + lista = list() + count1 = 0 + count2 = 0 + while len(list1)> count1 and len(list2) > count2: + if list1[count1] < list2[count2]: + lista.append(list1[count1]) + count1 += 1 + else: + lista.append(list2[count2]) + count2 += 1 + if len(list1)> count1: + while(len(list1)> count1): + lista.append(list1[count1]) + count1 += 1 + if len(list2) > count2: + while(len(list2)> count2): + lista.append(list2[count2]) + count2 += 1 + return lista # 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..b523820 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 + fileobj = open(filename,"r") + filestr = fileobj.read().split() + filestr.insert(0,"") + words = {} + for i in range(len(filestr)-1): + if filestr[i] not in words: + words[filestr[i]] = [filestr[i+1]] + else: + lista = words[filestr[i]] + lista.append(filestr[i+1]) + words[filestr[i]] = lista + return words def print_mimic(mimic_dict, word): - """Given mimic dict and start word, prints 200 random words.""" - # +++your code here+++ + last_word= word + for i in range(200): + if last_word in mimic_dict: + lista = mimic_dict[last_word] + print(last_word) + last_word = lista[random.randint(0,len(lista)-1)] + else: + lista = mimic_dict[word] + print(last_word) + last_word = lista[random.randint(0,len(lista)-1)] return diff --git a/basic/string1.py b/basic/string1.py index 34d2716..dd15e12 100755 --- a/basic/string1.py +++ b/basic/string1.py @@ -24,8 +24,13 @@ # So donuts(5) returns 'Number of donuts: 5' # and donuts(23) returns 'Number of donuts: many' def donuts(count): - # +++your code here+++ - return + strr = 'Number of donuts: ' + if count > 9: + strr += 'many' + return strr + else: + strr += str(count) + return strr # B. both_ends @@ -34,8 +39,15 @@ 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 + if len(s)<2: + return '' + else: + strr = '' + strr += s[0] + strr += s[1] + strr += s[len(s)-2] + strr += s[len(s)-1] + return strr # C. fix_start @@ -48,8 +60,13 @@ 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 + char = s[0] + s = s[1:len(s)] + s=s.replace(char,'*') + s2='' + s2 += char + s2 += s + return s2 # D. MixUp @@ -60,8 +77,15 @@ 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 + string = '' + string += b[0] + string += b[1] + string += a[2:len(a)] + string += ' ' + string += a[0] + string += a[1] + string += b[2:len(b)] + return string # Provided simple test() function used in main() to print diff --git a/basic/string2.py b/basic/string2.py index 631091a..13a581d 100755 --- a/basic/string2.py +++ b/basic/string2.py @@ -16,8 +16,15 @@ # If the string length is less than 3, leave it unchanged. # Return the resulting string. def verbing(s): - # +++your code here+++ - return + if len(s) > 3: + if s.rfind('ing') == len(s)-3: + s += 'ly' + return s + else: + s+= 'ing' + return s + else: + return s # E. not_bad @@ -29,8 +36,15 @@ def verbing(s): # So 'This dinner is not that bad!' yields: # This dinner is good! def not_bad(s): - # +++your code here+++ - return + appn = s.find('not') + appb = s.find('bad') + if appn < appb: + string='' + string += s[0:appn] + string += 'good' + string += s[appb+3:len(s)] + return string + return s # F. front_back @@ -41,9 +55,31 @@ 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 - + afront='' + aback='' + bfront='' + bback='' + if len(a) % 2 == 0: + afront=a[0:int(len(a)/2)] + aback=a[int(len(a)/2):len(a)] + else: + szam = int(len(a)/2) + afront=a[0:szam+1] + aback=a[szam+1:len(a)] + + if len(b) % 2 == 0: + bfront=b[0:int(len(b)/2)] + bback=b[int(len(b)/2):len(b)] + else: + szam = int(len(b)/2) + bfront=b[0:szam+1] + bback=b[szam+1:len(b)] + final='' + final+=afront + final+=bfront + final+=aback + final+=bback + return final # Simple provided test() function used in main() to print # what each function returns vs. what it's supposed to return. diff --git a/basic/wordcount.py b/basic/wordcount.py index a6c7c2e..264f64e 100755 --- a/basic/wordcount.py +++ b/basic/wordcount.py @@ -46,8 +46,39 @@ # and builds and returns a word/count dict for it. # Then print_words() and print_top() can just call the utility function. -### - +def print_words(filename): + fileobj = open(filename,"r") + words = {} + for line in fileobj: + for word in line.split(): + if word not in words: + words[word] = 1 + else: + words[word] += 1 + print(str(words) + "\n") + + + + +def print_top(filename): + fileobj = open(filename,"r") + words = {} + for line in fileobj: + for word in line.split(): + if word not in words: + words[word] = 1 + else: + words[word] += 1 + from operator import itemgetter + words = sorted(words.items(), key=itemgetter(1),reverse=True) + i=0 + for k in words: + if(i == 20): + break + print(k) + i += 1 + + # This basic command line argument parsing code is provided and # calls the print_words() and print_top() functions which you must define. def main():