Lab Assignment 3 Solutions

advertisement
Lab Assignment 3 Solutions
Question
Write a Python program that given a list of 0's and 1's will invert every 0 into 1 and every
1 into 0 and place them into a new list y.
Example:
Input:
x = [1,0,0,1]
Output:
y = [0,1,1,0]
Solution
x = eval(input())
i=0
y = [0] * len(x)
while ( i<len(x)):
if(x[i] == 0):
y[i] = 1
else:
y[i] = 0
i = i+1
print("y=", y)
Question
Write a Python program that given two lists X and Y, will print the sum of the products of
each two numbers in corresponding positions from the two lists. Assume both lists have
the same length.
Example:
Input:
x = [2,3,4]
y = [5,2,2]
Output:
Sum of products = 24
Solution
x = eval(input())
y = eval(input())
i=0
sum = 0
while ( i<len(x)):
product = x[i] * y[i]
sum = sum + product
i = i+1
print("Sum of products=", sum)
Question
Write a Python program that given 2 lists X and Y, will count the occurrences of 1 in list
X with a corresponding 2 in the same position in list Y.
Hint : you can use the compounded term "and"
Example:
Input:
x = [1,2,1,1]
y = [2,1,2,5]
Output:
The number of occurences of 1 in x to 2 in y is 2.
Solution
x = eval(input())
y = eval(input())
i=0
count = 0
while ( i<len(x)):
if (x[i] == 1 and y[i] ==2):
count += 1
i += 1
print("The number of occurrences of 1 in x to 2 in y is:",count)
Question
Write a Python program, that given a list performs the following:
elements in even positions are multiplied by 2 elements in odd positions are divided by
2.
The results are placed in the same list in the corresponding position.
Hint: when you are testing your program, enter the list using square brackets so for
example [2,6,5,8]
Example:
Input:
x = [2,6,5,8]
Output:
x = [4,3,10,4]
Solution
x = eval(input())
n = len(x)
i=0
while i < n:
if i % 2 == 0:
x[i] = x[i] * 2
else:
x[i] = x[i] / 2
i+=1
print(x)
Question
Write a Python program that given 3 lists X, Y and Z where X and Y are lists of numbers
and Z is a list of two symbols (+ for addition and - for subtraction) will perform the
operation in Z to the corresponding elements in X and Y. Assume the 3 lists are of the
same length. The result should be stored in a new list L.
Hint1: You can compare symbols as strings using " ". For example, if (z[i] == "+").
Example:
Input:
x = [1, 2, 6, 3]
y = [3, 4, 5, 2]
z = ["+", "+", "-", "+"]
Output:
l = [4, 6, 1, 5]
Solution
x = eval(input())
y = eval(input())
z = eval(input())
l = [0] * len(x)
i=0
while(i<len(x)):
if (z[i] == "+"):
l[i] = x[i] + y[i]
else:
l[i] = x[i] - y[i]
i = i+1
print("l = ", l)
Question
Write a Python program that given 2 lists X, Y where X is a lists of numbers and Y is a
list of two symbols (+ for addition and - for subtraction) will perform the operation in Y to
the corresponding elements in X with itself. Assume the 2 lists are of the same length.
The result should be stored in a new
list Z.
Hint1: You can compare symbols as strings using " ". For example, if (y[i] == "+").
Example:
Input:
x = [1, 2, 6, 3]
y = ["+", "+", "-", "+"]
Output:
z = [2, 4, 0, 6]
Solution
x = eval(input())
y = eval(input())
l = [0] * len(x)
i=0
while(i<len(x)):
if (y[i] == "+"):
l[i] = x[i] * 2
else:
l[i] = 0
i = i+1
print("l = ", l)
Question
Write a Python program that given a list will perform the following:
add elements in even positions together
add elements in odd positions together
Example:
Input:
x = [2,3,4,5,7]
Output:
Sum even: 13
Sum odd: 8
Solution
x = eval(input())
i=0
sumEven = 0
sumOdd = 0
while ( i<len(x)):
if (i % 2 == 0):
sumEven = sumEven + x[i]
else:
sumOdd = sumOdd + x[i]
i += 1
print("Sum Even =",sumEven)
print("Sum Odd =",sumOdd)
Question
Write a Python program that given two lists X and Y, of 0's and 1's will perform the
"And" operation between each two elements in corresponding positions. Assume that
both X and Y has the same length.
The result should be saved in a new list z.
Please note that :
0 and 0 = 0
0 and 1 = 0
1 and 0 = 0
1 and 1 = 1
Example:
Input:
x = [0,1,1,0]
y = [1,1,1,0]
Output:
z = [0,1,1,0]
Solution
x = eval(input())
y = eval(input())
z = [0] * len(x)
i=0
while(i<len(x)):
z[i] = x[i] and y[i]
i = i+1
print("z = ", z)
Another Solution
x = eval(input())
y = eval(input())
z = [0] * len(x)
i=0
while(i<len(x)):
if (x[i] == 0 and y[i] == 0):
z[i] = 0
elif (x[i] == 0 and y[i] == 1):
z[i] = 0
elif (x[i] == 1 and y[i] == 0):
z[i] = 0
else:
z[i] = 1
i = i+1
print("z = ", z)
Question
Write a Python program that given two lists X and Y, of 0's and 1's will perform the "Or"
operation between each two elements in corresponding positions. Assume that both X
and Y has the same length.
The result should be saved in the first list X.
Please note that :
0 or 0 = 0
0 or 1 = 0
1 or 0 = 0
1 or 1 = 1
Example:
Input:
x = [0,1,1,0]
y = [1,1,1,0]
Output:
x = [1,1,1,0]
Solution
x = eval(input())
y = eval(input())
i=0
while(i<len(x)):
x[i] = x[i] or y[i]
i = i+1
print("x = ", x)
Another Solution
x = eval(input())
y = eval(input())
i=0
while(i<len(x)):
if (x[i] == 0 and y[i] == 0):
x[i] = 0
elif (x[i] == 0 and y[i] == 1):
x[i] = 1
elif (x[i] == 1 and y[i] == 0):
x[i] = 1
else:
x[i] = 1
i = i+1
print("x = ", x)
Question
Write a Python program, that given 2 lists, count the number of times the sum of each 2
corresponding elements is greater than 5.
Example:
Input:
a = [2,3,7,5]
b = [3,4,4,6]
Output:
There are 3 elements with sum greater than 5.
Solution
a = eval(input())
b = eval(input())
occ = 0
i=0
n = len(a)
while i < n:
if a[i]+b[i] > 5:
occ+=1
i+=1
print("There are ", occ, "elements with sum greater than 5")
Question
Write a Python program, that given a list of numbers, replaces the odd numbers with the
word "odd" in the same list.
Example:
Input:
x = [1,2,3,5]
Output:
x = ["odd",2,"odd","odd"]
Solution
x = eval(input())
n = len(x)
i=0
while i < n:
if x[i] % 2 != 0:
x[i] = "odd"
i+=1
print(x)
Question
Write a Python program, that given a list, finds the maximum even number and the
maximum odd number.
Note: You can use the and operator.
Example:
Input:
x = [5,4,6,2,3]
Output:
Maximum Even number: 6
Maximum Odd number: 5
Solution
x = eval(input())
n = len(x)
i=0
maxEven = -1
maxOdd = -1
while i < n:
if x[i] % 2 == 0 and x[i] > maxEven:
maxEven = x[i]
if x[i] % 2 != 0 and x[i] > maxOdd:
maxOdd = x[i]
i+=1
print("Maximum Even number:", maxEven)
print("Maximum Odd number:", maxOdd)
Question
Write a Python program, that given a list, finds the maximum element that is divisible by
5.
Note: You can use the and operator.
Example:
Input:
x = [20,4,6,2,10]
Output:
Maximum number divisible by 5 is 20
Solution
x = eval(input())
n = len(x)
i=0
maxNum = -1
while i < n:
if x[i] % 5 == 0 and x[i] >= maxNum:
maxNum = x[i]
i+=1
print("Maximum element divisible by 5 is", maxNum)
Question
Write a Python program, that given a list of numbers, converts each 1 into 2 and each 2
into 1 in the same list.
Note:
Assume the numbers inserted in the list are only 1's and 2's.
Example:
Input:
x = [1,1,2,2,1]
Output:
x = [2,2,1,1,2]
Solution
x= eval(input())
n = len(x)
i=0
while i < n:
if x[i] == 1:
x[i] = 2
else:
x[i] = 1
i+=1
print(x)
Download