From 6b685a18a8da4ecb9bec55c21c18000702658881 Mon Sep 17 00:00:00 2001 From: vankodg Date: Tue, 13 Mar 2018 20:54:50 +0000 Subject: [PATCH 1/3] first week homework --- basic/list1.py | 23 +++++++++++++++++------ basic/list2.py | 20 ++++++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/basic/list1.py b/basic/list1.py index 4e6171c..9e90894 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 + ends = 0 + for word in words: + if len(word)>1 and word[0] == word[-1:]: + ends += 1 + return ends # 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 + words.sort() + l = [] + for word in words: + if word[0] == 'x': + l.append(word) + for word in words: + if word[0] != 'x': + l.append(word) + return l # C. sort_last @@ -44,8 +54,9 @@ def front_x(words): # [(2, 2), (1, 3), (3, 4, 5), (1, 7)] # Hint: use a custom key= function to extract the last element form each tuple. def sort_last(tuples): - # +++your code here+++ - return + tuples = sorted(tuples, key=lambda tuple: tuple[-1]) + #source: https://docs.python.org/3/howto/sorting.html + return tuples # Simple provided test() function used in main() to print diff --git a/basic/list2.py b/basic/list2.py index f8e65da..4df1fee 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 + i = 0 + while True: + if i+1 >= len(nums): + break + elif nums[i] == nums[i+1]: + nums.pop(i+1) + else: + i+=1 + return nums # E. Given two lists sorted in increasing order, create and return a merged @@ -22,8 +29,13 @@ 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 + ans = [] + for a in list1: + ans.append(a) + for a in list2: + ans.append(a) + ans.sort() + return ans # Note: the solution above is kind of cute, but unforunately list.pop(0) From 3a0fc565697bf93a8da82f8f79a7fae4696e755b Mon Sep 17 00:00:00 2001 From: vankodg Date: Tue, 20 Mar 2018 15:31:53 +0000 Subject: [PATCH 2/3] string1 solved --- basic/string1.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/basic/string1.py b/basic/string1.py index 34d2716..4c227df 100755 --- a/basic/string1.py +++ b/basic/string1.py @@ -24,8 +24,9 @@ # So donuts(5) returns 'Number of donuts: 5' # and donuts(23) returns 'Number of donuts: many' def donuts(count): - # +++your code here+++ - return + if count >=10: + count = 'many' + return ('Number of donuts: ' + str(count)) # B. both_ends @@ -34,8 +35,9 @@ 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 '' + return s[0] + s[1] + s[-2] + s[-1] # C. fix_start @@ -48,8 +50,7 @@ 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 + return s[0] + s[1:].replace(s[0], '*') # D. MixUp @@ -60,8 +61,7 @@ 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 + return b[0:2] + a[2:] + ' ' + a[0:2] + b[2:] # Provided simple test() function used in main() to print From 1ed4c5829ba5bbaae6d1a9a924e1bc10334cb19e Mon Sep 17 00:00:00 2001 From: vankodg Date: Tue, 20 Mar 2018 16:37:06 +0000 Subject: [PATCH 3/3] solved string2 --- basic/string2.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/basic/string2.py b/basic/string2.py index 631091a..3c82f20 100755 --- a/basic/string2.py +++ b/basic/string2.py @@ -16,8 +16,12 @@ # 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: + return s + elif s[-3:] == 'ing': + return s + 'ly' + else: + return s + 'ing' # E. not_bad @@ -29,8 +33,11 @@ def verbing(s): # So 'This dinner is not that bad!' yields: # This dinner is good! def not_bad(s): - # +++your code here+++ - return + n = s.find('not') + b = s.find('bad') + if n < b: + s = s[:n] + 'good' + s[b+3:] + return s # F. front_back @@ -39,10 +46,19 @@ def not_bad(s): # If the length is odd, we'll say that the extra char goes in the front half. # e.g. 'abcde', the front half is 'abc', the back half 'de'. # Given 2 strings, a and b, return a string of the form -# a-front + b-front + a-back + b-back +# a-front + b-front + a-back + b-back +def halve(n): + if len(n) % 2 == 0: + return len(n)/2 + else: + return len(n)/2+1 + def front_back(a, b): - # +++your code here+++ - return + a_front = a[:halve(a)] + a_back = a[halve(a):] + b_front = b[:halve(b)] + b_back = b[halve(b):] + return a_front + b_front + a_back + b_back # Simple provided test() function used in main() to print