231k views
0 votes
Codio Challenge Activity Python

We are passing in a list of numbers. You need to create 2 new lists in your chart, then

put all odd numbers in one list

put all even numbers in the other list

output the odd list first, the even list second

Tip: you should use the modulo operator to decide whether the number is odd or even. We provided a function for you to call that does this.

Don’t forget to define the 2 new lists before you start adding elements to them.

------------------------------------------------------------

Requirements:

Program Failed for Input: 1,2,3,4,5,6,7,8,9

Expected Output: [1, 3, 5, 7, 9]
[2, 4, 6, 8]
------------------------------------------------------------

Given Code:

# Get our input from the command line
import sys
numbers = sys.argv[1].split(',')
for i in range(0,len(numbers)):
numbers[i]= int(numbers[i])

def isEven(n) :
return ((n % 2) == 0)

# Your code goes here

User Lone Ronin
by
6.0k points

1 Answer

5 votes

Answer:

You can use the following code in order to generate the odd and even lists separately:

def splitOddEven(number_list):

"""it separate a list of numbers into odd and even"""

odd_list = [];

even_list = [];

while(number_list):

current_number = number_list.pop();

if(current_number % 2 == 0):

even_list.append(current_number)

else:

odd_list.append(current_number)

return (even_list, odd_list)

test_list = [2,3,1,5,78,32,121,12]

even, odd = splitOddEven(test_list[:])

print("These are the even numbers of your list:" + str(even))

print("\\These are the odd numbers of your list: " + str(odd))

Step-by-step explanation:

User Bokonic
by
5.4k points