From 8fba8153a3d8acb19d836dab220fee70884f3eac Mon Sep 17 00:00:00 2001 From: Popovics Date: Tue, 20 Mar 2018 02:36:59 +0100 Subject: [PATCH] Elso hazi --- basic/list1.py | 28 ++++++++++++++++++++++++---- basic/list2.py | 10 ++++++++-- basic/string1.py | 13 +++++++++---- basic/string2.py | 21 ++++++++++++++++++--- 4 files changed, 59 insertions(+), 13 deletions(-) diff --git a/basic/list1.py b/basic/list1.py index 4e6171c..142e11d 100755 --- a/basic/list1.py +++ b/basic/list1.py @@ -22,8 +22,12 @@ # Note: python does not have a ++ operator, but += works. def match_ends(words): # +++your code here+++ - return - + szamlalo=0 + for elem in words: + if len(elem)>=2 and elem[0]==elem[-1]: + szamlalo+=1 + + return szamlalo # B. front_x # Given a list of strings, return a list with the strings @@ -34,7 +38,15 @@ def match_ends(words): # before combining them. def front_x(words): # +++your code here+++ - return + x_lista = [] + nem_x_lista = [] + for elem in words: + if elem[0] == 'x': + x_lista.append(elem) + else: + nem_x_lista.append(elem) + + return sorted(x_lista) + sorted(nem_x_lista) # C. sort_last @@ -45,7 +57,15 @@ def front_x(words): # Hint: use a custom key= function to extract the last element form each tuple. def sort_last(tuples): # +++your code here+++ - return + sorted_tuples=[0 for i in range(len(tuples))] + szamok=[] + for elem in tuples: + szamok.append(elem[-1]) + sorted_szamok=sorted(szamok) + for elem in tuples: + sorted_tuples[sorted_szamok.index(elem[-1])]=elem + + return sorted_tuples # Simple provided test() function used in main() to print diff --git a/basic/list2.py b/basic/list2.py index f8e65da..5e688bf 100755 --- a/basic/list2.py +++ b/basic/list2.py @@ -14,7 +14,13 @@ # modify the passed in list. def remove_adjacent(nums): # +++your code here+++ - return + l = [] + for i in range(len(nums)-1): + if nums[i] != nums[i+1]: + l.append(nums[i]) + if i == len(nums)-2: + l.append(nums[i+1]) + return l # E. Given two lists sorted in increasing order, create and return a merged @@ -23,7 +29,7 @@ def remove_adjacent(nums): # 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/string1.py b/basic/string1.py index 34d2716..91891b8 100755 --- a/basic/string1.py +++ b/basic/string1.py @@ -25,7 +25,10 @@ # 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 @@ -35,7 +38,7 @@ def donuts(count): # is less than 2, return instead the empty string. def both_ends(s): # +++your code here+++ - return + return s[:2]+s[-2:] # C. fix_start @@ -49,7 +52,9 @@ def both_ends(s): # where all instances of stra have been replaced by strb. def fix_start(s): # +++your code here+++ - return + sztring=s[1:] + a=sztring.replace(s[0],'*') + return s[0]+a # D. MixUp @@ -61,7 +66,7 @@ def fix_start(s): # Assume a and b are length 2 or more. def mix_up(a, b): # +++your code here+++ - return + return b[:2]+a[2:]+' '+a[:2]+b[2:] # Provided simple test() function used in main() to print diff --git a/basic/string2.py b/basic/string2.py index 631091a..4c2b071 100755 --- a/basic/string2.py +++ b/basic/string2.py @@ -17,7 +17,12 @@ # Return the resulting string. def verbing(s): # +++your code here+++ - return + if s[-3:]=='ing: + return s[:-3]+'ly' + elif len(s)<3: + return s + else: + return s+'ing' # E. not_bad @@ -30,7 +35,10 @@ def verbing(s): # This dinner is good! def not_bad(s): # +++your code here+++ - return + not_index=s.find('not') + bad_index=s.find('bad') + + return s[:not_index]+'good'+s[bad_index+3:] # F. front_back @@ -42,7 +50,14 @@ def not_bad(s): # a-front + b-front + a-back + b-back def front_back(a, b): # +++your code here+++ - return + def splits(x): + if len(x)%2==0: + half = len(x)/2 + return [x[:int(half)],x[int(half):]] + else: + half = len(x)/2 + return [x[:int(half)+1],x[int(half)+1:]] + return splits(a)[0]+splits(b)[0]+splits(a)[1]+splits(b)[1] # Simple provided test() function used in main() to print