0

Please I need help with debugging this code or writing it entirely

def my_sort (numbers): If numbers == 0: return "invalid" elif len(numbers []) ==1: return numbers else: a = set(list(filter(lambda x:x%2!=0, numbers ))) b =set (list(filter(lambda x:x%2==0,numbers))) c=a|b return c print (my_sort([1,2,3,4,6]))

9th Jun 2017, 12:02 AM
Obumneme Anichebe
Obumneme Anichebe - avatar
6 Antworten
+ 1
Thanks Bogdan.... However I tweaked and arrived at this def my_sort (numbers): if len(numbers) == 0: return "invalid" elif len(numbers) ==1: return numbers else: a = list(filter(lambda x:x%2!=0, numbers )) a.sort() b =list(filter(lambda x:x%2==0,numbers)) b.sort() c=0 max_index = len(b)- 1 while c <= max_index: a.append(b[c]) c = c +1 return a print (my_sort([1,20,3,40,5,6,7]))
9th Jun 2017, 9:06 AM
Obumneme Anichebe
Obumneme Anichebe - avatar
0
Edited
9th Jun 2017, 12:45 AM
Obumneme Anichebe
Obumneme Anichebe - avatar
0
The objective is to sort the list such that the odd numbers come first and even numbers follow
9th Jun 2017, 12:47 AM
Obumneme Anichebe
Obumneme Anichebe - avatar
0
@Obumneme - your code definitely does _not_ sort the list - you are returning a set, which is unsorted by definition :) Since you are already getting your lists correctly, I see no reason for converting them to sets. Try this: a = list(filter(lambda x:x%2!=0,numbers)) b = list(filter(lambda x:x%2==0,numbers)) a.extend(b) return a
9th Jun 2017, 7:31 AM
Bogdan Sass
Bogdan Sass - avatar
0
That's great - you basically implemented "extend" in your own code. There are always multiple ways of solving the same problem - so as long as your code works, it's a valid solution! :D
9th Jun 2017, 9:26 AM
Bogdan Sass
Bogdan Sass - avatar
- 1
def my_sort (numbers): if len(numbers) == 0: return "invalid" elif len(numbers) ==1: return numbers else: a = set(list(filter(lambda x:x%2!=0, numbers ))) b =set (list(filter(lambda x:x%2==0,numbers))) c=a|b return c print (my_sort([1,2,3,4,5,6,7]))
9th Jun 2017, 12:45 AM
Obumneme Anichebe
Obumneme Anichebe - avatar