From 771558691344e400055e5fb94c9a1b0e45376729 Mon Sep 17 00:00:00 2001 From: "andreka.zoltan@freemail.hu" Date: Fri, 16 Mar 2018 23:56:38 +0100 Subject: [PATCH] HF 1 --- basic/list1.py | 23 ++++++++++++++--------- basic/list2.py | 9 +++++++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/basic/list1.py b/basic/list1.py index 4e6171c..28e6690 100755 --- a/basic/list1.py +++ b/basic/list1.py @@ -21,9 +21,7 @@ # 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 - + if len(words)>2 and words[0]==words[(len(words)-1)]: return (len(words)) # B. front_x # Given a list of strings, return a list with the strings @@ -32,9 +30,14 @@ def match_ends(words): # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'] # 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 + def front_x(words): + tabla=[] + tabla2=[] + for i in words: + if i[0]=="x":tabla.append(i) + else: tabla2.append(i) + words=sorted(tabla)+sorted(tabla2) + return words # C. sort_last @@ -43,9 +46,11 @@ 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 sort_last(tuples): - # +++your code here+++ - return +>>>def MyFn(s): + return s[-1] + +>>>def sort_last(tuples): + return sorted(tuples, key=MyFn) # Simple provided test() function used in main() to print diff --git a/basic/list2.py b/basic/list2.py index f8e65da..e325733 100755 --- a/basic/list2.py +++ b/basic/list2.py @@ -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 + new=[] + i=0 + while i < len(nums)-1: + if nums[i]!=nums[i+1]:new.append(nums[i]) + i+=1 + if nums[(len(nums)-1)]!=nums[(len(nums)-2)]:new.append(nums[(len(nums)-1)]) + return new # E. Given two lists sorted in increasing order, create and return a merged