diff --git a/basic/list1.py b/basic/list1.py index 4e6171c..58ea5cb 100755 --- a/basic/list1.py +++ b/basic/list1.py @@ -1,28 +1,14 @@ -#!/usr/bin/python -tt -# Copyright 2010 Google Inc. -# Licensed under the Apache License, Version 2.0 -# http://www.apache.org/licenses/LICENSE-2.0 - -# Google's Python Class -# http://code.google.com/edu/languages/google-python-class/ - -# Basic list exercises -# Fill in the code for the functions below. main() is already set up -# to call the functions with a few different inputs, -# printing 'OK' when each function is correct. -# The starter code for each function includes a 'return' -# which is just a placeholder for your code. -# It's ok if you do not complete all the functions, and there -# are some additional functions to try in list2.py. - # A. match_ends # Given a list of strings, return the count of the number of # strings where the string length is 2 or more and the first # 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 + n = 0 + for word in words: + if len(word) >= 2 and word[0] == word[len(word) - 1]: + n += 1 + return n # B. front_x @@ -33,8 +19,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 + listx = [] + listnorm = [] + for word in words: + if word[0] == 'x': + listx.append(word) + else: + listnorm.append(word) + + return sorted(listx) + sorted(listnorm) # C. sort_last @@ -44,8 +37,17 @@ 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 + endings = [] + res = [] + for e in tuples: + endings.append(e[-1]) + endings.sort() + for ending in endings: + for tup in tuples: + if tup[-1] == ending: + res.append(tup) + return res + # Simple provided test() function used in main() to print diff --git a/basic/list2.py b/basic/list2.py index f8e65da..2fdf3b4 100755 --- a/basic/list2.py +++ b/basic/list2.py @@ -1,4 +1,4 @@ -#!/usr/bin/python -tt +# !/usr/bin/python -tt # Copyright 2010 Google Inc. # Licensed under the Apache License, Version 2.0 # http://www.apache.org/licenses/LICENSE-2.0 @@ -13,8 +13,13 @@ # 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 i < len(nums) - 1: + if nums[i + 1] == nums[i]: + del nums[i + 1] + else: + i += 1 + return nums # E. Given two lists sorted in increasing order, create and return a merged @@ -22,9 +27,27 @@ 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 - + res = [] + if len(list1) > len(list2): + length = len(list1) + v = 0 + else: + length = len(list2) + v = 1 + i = 0 + while i < length - 1: + if list1[i] < list2[i]: + res.append(list1[i]) + res.append(list2[i]) + else: + res.append(list2[i]) + res.append(list1[i]) + i += 1 + if v == 0: + res += list1[i:] + else: + res += list2[i:] + return res # Note: the solution above is kind of cute, but unforunately list.pop(0) # is not constant time with the standard python list implementation, so diff --git a/basic/string1.py b/basic/string1.py index 34d2716..18390cc 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 < 10: + return "Number of donuts: " + str(count) + else: + return "Number of donuts: many" # B. both_ends @@ -34,13 +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: + return s[0:2] + s[-2:] + else: + return '' # C. fix_start # Given a string s, return a string -# where all occurences of its first char have +# where all occurrences of its first char have # been changed to '*', except do not change # the first char itself. # e.g. 'babble' yields 'ba**le' @@ -48,8 +52,10 @@ 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 + first = s[0] + rest = s[1:] + res = rest.replace(first, '*') + return first + res # D. MixUp @@ -60,8 +66,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 @@ -108,4 +113,4 @@ def main(): # Standard boilerplate to call the main() function. if __name__ == '__main__': - main() + main() \ No newline at end of file diff --git a/basic/string2.py b/basic/string2.py index 631091a..9d62177 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 + if len(s) > 2: + if s[-3:] != 'ing': + return s + 'ing' + else: + return s + 'ly' + else: + return s # E. not_bad @@ -29,9 +34,11 @@ def verbing(s): # So 'This dinner is not that bad!' yields: # This dinner is good! def not_bad(s): - # +++your code here+++ - return - + not_ind = s.find('not') + bad_ind = s.find('bad') + if not_ind != -1 and bad_ind != -1 and not_ind < bad_ind: + s = s[:not_ind] + 'good' + s[bad_ind + 3:] + return s # F. front_back # Consider dividing a string into two halves. @@ -41,8 +48,13 @@ 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 + amid = len(a) / 2 + bmid = len(b) / 2 + if len(a) % 2 == 1: + amid += 1 + if len(b) % 2 == 1: + bmid += 1 + return a[:int(amid)] + b[:int(bmid)] + a[int(amid):] + b[int(bmid):] # Simple provided test() function used in main() to print @@ -76,4 +88,4 @@ def main(): if __name__ == '__main__': - main() + main() \ No newline at end of file