0% found this document useful (0 votes)
98 views

Type C list

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Type C list

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Type C: Programming Practice/Knowledge based Questions

Question 1 Write a program to increment the elements of a list with a number.

Solution

lst = eval(input("Enter a list: "))


print("Existing list is:", lst)

n = int(input("Enter a number: "))

for i in range(len(lst)):
lst[i] += n

print("List after increment:", lst)

Output

Enter a list: [1, 2, 3, 4, 5]


Existing list is: [1, 2, 3, 4, 5]
Enter a number: 10
List after increment: [11, 12, 13, 14, 15]

Question 2 Write a program that reverses a list of integers (in place).

Solution

l = eval(input("Enter a list: "))


print("Original list:", l)
l.reverse()
print("Reversed list:", l)

Output

Enter a list: [1, 2, 3, 4, 5]


Original list: [1, 2, 3, 4, 5]
Reversed list: [5, 4, 3, 2, 1]

Question 3

Write a program that inputs two lists and creates a third, that contains all elements of the first followed by all
elements of the second.

Solution

l1 = eval(input("Enter first list: "))


l2 = eval(input("Enter second list: "))
l3 = l1 + l2
print("Joined List:", l3)

Output

Enter first list: [1, 2, 3, 4, 5]


Enter second list: [11, 12, 13, 14, 15]
Joined List: [1, 2, 3, 4, 5, 11, 12, 13, 14, 15]
Question 4 Ask the user to enter a list containing numbers between 1 and 12. Then replace all of the entries
in the list that are greater than 10 with 10.

Solution

l = eval(input("Enter list having numbers between 1 & 12: "))

for i in range(len(l)):
if l[i] > 10:
l[i] = 10

print("List after removing numbers greater than 10:")


print(l)

Output

Enter list having numbers between 1 & 12: [1, 3, 15, 8, 20]
List after removing numbers greater than 10:
[1, 3, 10, 8, 10]

Question 5 Ask the user to enter a list of strings. Create a new list that consists of those strings with their
first characters removed.

Solution

l1 = eval(input("Enter a list of strings: "))


l2 = []

for i in range(len(l1)):
l2.append(l1[i][1:])

print("List after removing first characters:")


print(l2)

Output

Enter a list of strings: ["red", "green", "blue", "pink", "cyan"]


List after removing first characters:
['ed', 'reen', 'lue', 'ink', 'yan']
Question 6 Write a program to check if a number is present in the list or not. If the number is present, print
the position of the number. Print an appropriate message if the number is not present in the list.
Solution
l = eval(input("Enter list: "))
n = int(input("Enter number to search: "))

if n in l:
print(n, "found at index", l.index(n))
else :
print(n, "not found in list")

Output

Enter list: [1, 3, 15, 8, 20]


Enter number to search: 15
15 found at index 2

=====================================

Enter list: [1, 3, 15, 8, 20]


Enter number to search: 25
25 not found in list
Question 7a

Create the following lists using a for loop:

A list consisting of the integers 0 through 49.

Solution

l = []

for i in range(50):
l.append(i)

print("List with integers from 0 to 49:")


print(l)

Output

List with integers from 0 to 49:


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49]

Question 7b

Create the following lists using a for loop:

A list containing the squares of the integers 1 through 50.

Solution

l = []

for i in range(1, 51):


l.append(i * i)

print("List with square of integers from 1 to 50:")


print(l)

Output

List with square of integers from 1 to 50:


[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361,
400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225,
1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401,
2500]

Question 7c Create the following lists using a for loop:

The list ['a','bb','ccc','dddd', . . . ] that ends with 26 copies of the letter z.

Solution

l = []

for i in range(1, 27):


l.append(chr(i + 96) * i)

print("Created List:")
print(l)
Output

Created List:
['a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii',
'jjjjjjjjjj', 'kkkkkkkkkkk', 'llllllllllll', 'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn',
'ooooooooooooooo', 'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr',
'sssssssssssssssssss', 'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu',
'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww', 'xxxxxxxxxxxxxxxxxxxxxxxx',
'yyyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzzz']

Question 8

Write a program that takes any two lists L and M of the same size and adds their elements together to form a
new list N whose elements are sums of the corresponding elements in L and M. For instance, if L = [3, 1, 4]
and M = [1, 5, 9], then N should equal [4,6,13].

Solution

print("Enter two lists of same size")


L = eval(input("Enter first list(L): "))
M = eval(input("Enter second list(M): "))
N = []

for i in range(len(L)):
N.append(L[i] + M[i])

print("List N:")
print(N)

Output

Enter two lists of same size


Enter first list(L): [3, 1, 4]
Enter second list(M): [1, 5, 9]
List N:
[4, 6, 13]

Question 9

Write a program rotates the elements of a list so that the element at the first index moves to the second
index, the element in the second index moves to the third index, etc., and the element in the last index moves
to the first index.

Solution

l = eval(input("Enter the list: "))


print("Original List")
print(l)

l = l[-1:] + l[:-1]

print("Rotated List")
print(l)

Output

Enter the list: [8, 10, 13, 25, 7, 11]


Original List
[8, 10, 13, 25, 7, 11]
Rotated List
[11, 8, 10, 13, 25, 7]
Question 10

Write a program that reads the n to display nth term of Fibonacci series.

The Fibonacci sequence works as follows:

• element 0 has the value 0


• element 1 has the value 1
• every element after that has the value of the sum of the two preceding elements

The beginning of the sequence looks like:


0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

The program prompts for element and prints out the value of that element of the Fibonacci sequence.

Thus:

• input 7, produces 13
• input 9, produces 34

Hints:
A Don't try to just type out the entire list. It gets big very fast. Element 25 is 75205. Element 100 is
354224848179261915075. So keep upper limit of n to 20.

Solution

n = int(input("Enter n: "))

if (n > 20):
print("n should be less than or equal to 20")
else :
a = 0
b = 1
c = a + b
for i in range(3, n + 1):
a = b
b = c
c = a + b

print(n, "term of Fibonacci series =", c)

Output

Enter n: 7
7 term of Fibonacci series = 13

=====================================

Enter n: 9
9 term of Fibonacci series = 34

=====================================

Enter n: 25
n should be less than or equal to 20
Question 11a

Write programs as per following specifications:

'''Print the length of the longest


string in the list of strings str_list.
Precondition : the list will contain
at least one element.'''

Solution

l = eval(input("Enter list of strings: "))


largeIdx = 0
largeLen = 0

for i in range(len(l)):
length = len(l[i])
if length > largeLen:
largeLen = length
largeIdx = i

print("Longest String:", l[largeIdx])

Output

Enter list of strings: ["apple", "orange", "pear", "strawberry", "kiwi"]


Longest String: strawberry

Question 11b

Write programs as per following specifications:

'''L is a list of numbers. Print a new list where each element is the corresponding element of list L summed
with number num.'''

Solution

l1 = eval(input("Enter list of numbers: "))


num = int(input("Enter the number to sum with (num): "))

l2 = []

for i in l1:
l2.append(i + num)

print("New list:")
print(l2)

Output

Enter list of numbers: [10, 20, 30, 40, 50]


Enter the number to sum with (num): 15
New list:
[25, 35, 45, 55, 65]
Question 12

Write a program to read two lists num and denum which contain the numerators and denominators of same
fractions at the respective indexes. Then display the smallest fraction along with its index.

Solution

num = eval(input("Enter numerators list: "))


denum = eval(input("Enter denominators list: "))

small = 0.0
smallIdx = 0

for i in range(len(num)):
t = num[i] / denum[i]
if t < small:
small = t
smallIdx = i

print("Smallest Fraction =", num[smallIdx], "/", denum[smallIdx])


print("Index of Smallest Fraction =", smallIdx)

Output

Enter numerators list: [1, 3, 1, 7, 3]


Enter denominators list: [2, 4, 4, 13, 8]
Smallest Fraction = 1 / 2
Index of Smallest Fraction = 0

Question 13

Write a program to display the maximum and minimum values from the specified range of indexes of list.

Solution

l = eval(input("Enter the list: "))


start = int(input("Enter start index: "))
stop = int(input("Enter stop index: "))

slice = l[start : stop + 1]


mx = max(slice)
mi = min(slice)

print("Maximum =", mx)


print("Minimum =", mi)

Output

Enter the list: [89, 42, 12, 56, 35, 2, 8, 7, 13, 69]
Enter start index: 3
Enter stop index: 8
Maximum = 56
Minimum = 2
Question 14

Write a program to move all duplicate values in a list to the end of the list.

Solution

l = eval(input("Enter the list: "))


dedup = []
dup = []
for i in l:
if i in dedup:
dup.append(i)
else:
dedup.append(i)

l = dedup + dup

print("Modified List:")
print(l)

Output

Enter the list: [20, 15, 18, 15, 7, 18, 12, 13, 7]
Modified List:
[20, 15, 18, 7, 12, 13, 15, 18, 7]

Question 15

Write a program to compare two equal sized lists and print the first index where they differ.

Solution

print("Enter two equal sized lists")


l1 = eval(input("Enter first list: "))
l2 = eval(input("Enter second list: "))

for i in range(len(l1)):
if l1[i] != l2[i]:
print("Lists differ at index", i)
break;
else:
print("Lists are equal")

Output

Enter two equal sized lists


Enter first list: [80, 60, 50, 40, 30]
Enter second list: [80, 60, 55, 42, 30]
Lists differ at index 2

=====================================

Enter two equal sized lists


Enter first list: [80, 60, 50, 40, 30]
Enter second list: [80, 60, 50, 40, 30]
Lists are equal

You might also like