From 2358111e0d4df929dab2bc726e4f7df98aa5a8e0 Mon Sep 17 00:00:00 2001 From: szellboglarka Date: Sun, 18 Mar 2018 19:22:24 +0100 Subject: [PATCH 1/4] list1 --- basic/list1.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/basic/list1.py b/basic/list1.py index 4e6171c..26767d7 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 + items = 0 + for i in words: + if (len(i) > 1) and (i[0] == i[-1]): + items = items + 1 + return items # B. front_x @@ -33,8 +36,14 @@ 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 + beginsWithX= list() + notBeginsWithX = list() + for i in words: + if i[0] == 'x': + beginsWithX.append(i) + else: + notBeginsWithX.append(i) + return sorted(beginsWithX) + sorted(notBeginsWithX) # C. sort_last @@ -44,8 +53,7 @@ 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 + return sorted(tuples,key=lambda x: x[1]) # Simple provided test() function used in main() to print From 7ef3e4c41e3f9d83e714751b3d3f3d74b5cb9b57 Mon Sep 17 00:00:00 2001 From: szellboglarka Date: Sun, 18 Mar 2018 20:03:23 +0100 Subject: [PATCH 2/4] list2 --- basic/list2.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/basic/list2.py b/basic/list2.py index f8e65da..9796aa9 100755 --- a/basic/list2.py +++ b/basic/list2.py @@ -13,8 +13,11 @@ # 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 + numsWithoutRepeating = list() + for i in nums: + if i not in numsWithoutRepeating: + numsWithoutRepeating.append(i) + return numsWithoutRepeating # E. Given two lists sorted in increasing order, create and return a merged @@ -22,8 +25,23 @@ 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 + mergedList= list() + i =0 + j=0 + while (i < len(list1)) and (j < len (list2) ): + if list1[i] <= list2[j]: + mergedList.append(list1[i]) + i= i+1 + elif list1[i] > list2[j]: + mergedList.append(list2[j]) + j=j+1 + while (i < len(list1)): + mergedList.append(list1[i]) + i= i+1 + while (j < len(list2)): + mergedList.append(list2[j]) + j = j + 1 + return mergedList # Note: the solution above is kind of cute, but unforunately list.pop(0) From 48eeba0b6820b00ded03a36dbfed27899cc77c61 Mon Sep 17 00:00:00 2001 From: szellboglarka Date: Sun, 18 Mar 2018 21:49:53 +0100 Subject: [PATCH 3/4] string1 --- basic/string1.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/basic/string1.py b/basic/string1.py index 34d2716..fe2ac89 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 + if count > 9: + return "Number of donuts: many" + else: + return "Number of donuts: " + str(count) # B. both_ends @@ -34,11 +36,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: + s = '' + return s + else: + return s[0] + s[1] + s[-2] + s[-1] + + # C. fix_start -# C. fix_start # Given a string s, return a string # where all occurences of its first char have # been changed to '*', except do not change @@ -48,8 +54,9 @@ 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 + listOfChars = list(s.replace(s[0], '*')) + listOfChars[0] = s[0] + return ''.join(listOfChars) # D. MixUp @@ -60,14 +67,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 + aSwapped = b[:2] + a[2:] + bSwapped = a[:2] + b[2:] + return aSwapped + ' ' + bSwapped # Provided simple test() function used in main() to print # what each function returns vs. what it's supposed to return. def test(got, expected): - if got == expected: + if got== expected: prefix = ' OK ' else: prefix = ' X ' From 5d353890c6c6511640c0b31906a0043362bab71a Mon Sep 17 00:00:00 2001 From: szellboglarka Date: Tue, 20 Mar 2018 00:03:19 +0100 Subject: [PATCH 4/4] string2 --- basic/string2.py | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/basic/string2.py b/basic/string2.py index 631091a..bcfe5b5 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: + if s[-3:] == "ing": + s = s + "ly" + else: + s = s + "ing" + return s # E. not_bad @@ -29,9 +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 - + index = s.find("not") + index2 = s.find("bad") + if index > 0 and index2 > 0 and index < index2: + s = s[:index] + "good" + s[index2 + 3:] + return s # F. front_back # Consider dividing a string into two halves. @@ -41,8 +47,27 @@ 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 + la = len(a) + lb = len(b) + afront = "" + bfront = "" + aback = "" + bback = "" + if divmod(la, 2) == 0: + afront = a[:la/2] + aback = a[(la/2) + 2:] + else: + afront = a[:(int)((la+1) / 2)] + aback = a[(int)((la-1) / 2):] + + if divmod(lb, 2) == 0: + bfront = b[:lb/2] + bback = b[(lb/2) + 2:] + else: + bfront = b[:(int)((lb+1) / 2)] + bback = b[(int)((lb-1) / 2):] + + return afront + bfront + aback + bback # Simple provided test() function used in main() to print