2024 25 COL100 Lab 7 Lists
2024 25 COL100 Lab 7 Lists
Lab 7: Lists
February 13, 2025
1 Lists
A list is a collection of elements that can be of the same or different data types, stored
in a defined order. Lists are dynamic, meaning their size can be adjusted, and they can
hold different types of elements. Lists are widely used to manage groups of related data.
You can initialize an empty list and add/append elements to it later in the code:
1 numbers = []
2 numbers . append (2)
3 numbers . append (4)
Note that lists are dynamic and can grow or shrink in size as needed.
It is important to manage list indices within the valid range to avoid accessing el-
ements outside the list’s bounds. Accessing indices that are out of range will raise an
IndexError.
Always ensure your index values remain within the valid range of the list to maintain
program stability and avoid runtime errors.
Accessing elements in a 2D list involves specifying both row and column indices. Rows
and columns are both 0-indexed:
1 element = matrix [1][2] # ( value : 6) Accessing element at the second
row ( at index 1) and third column ( at index 2)
Page 2
2 Submission Problem
2.1 Frequency Count
Given a list of integers, implement a program that counts the frequency of each element
and prints the results.
Requirements
• Read an integer n (1 ≤ n ≤ 1000) representing the size of the list.
• Implement a program that calculates and prints the frequency of each element in
sorted order.
Sample Input
1 8
2 10 20 30 20 40 20 10 30
Sample Output
1 10 2
2 20 3
3 30 2
4 40 1
Sample Input
1 10
2 40 23 15 23 40 12 13 15 23 40
Sample Output
1 12 1
2 13 1
3 15 2
4 23 3
5 40 3
Page 3
3 Practice Problem
3.1 Subarray with Given Sum
Given a list of integers and a target sum, implement a program to check whether there
exists a subarray in the given list with the specified sum.
A subarray is a sequence of consecutive elements from the list.
Requirements
• Read an integer n (1 ≤ n ≤ 100) representing the size of the list.
• Read n integers representing the list elements.
• Read an integer targetSum representing the target sum.
• Implement a program that prints YES if a subarray exists with the given sum,
otherwise print NO.
Sample Input
1 5
2 1 2 3 4 5
3 9
Sample Output
1 YES
Requirements
• Read an integer n (n ≤ 100) representing the size of the list.
• Read n integers representing the list elements.
• Read an integer k representing the number of positions to rotate the list to the
right.
• Implement a program that rotates the list in-place by k positions.
• Print the rotated list after the rotation.
Page 4
Sample Input
1 6
2 1 2 3 4 5 6
3 2
Sample Output
1 5 6 1 2 3 4
• Read an integer n (n ≤ 1000) denoting the size of the list from the user.
• Read n integers representing the list elements from the user (Each element is a
non-negative integer not greater than 100).
• Implement a program that takes the list and its size, and calculates the frequency
of each element in the list.
• Print the frequency of each element in a readable format (in sorted order of the
elements).
1 8
2 1 2 3 2 4 2 1 3
Output
1 Element 1: 2
2 Element 2: 3
3 Element 3: 2
4 Element 4: 1
Page 5
3.4 Mode of an Array
You are given an array of integers. The mode of an array is the element that appears
most frequently in the array. Write a Python program to find the mode of an array of
integers.
• On a single line, print the mode of the array; if there is no unique most frequent
element, then print -1.
Note: You must dynamically handle the creation of lists in your program.
Input
1 5
2 1 2 3 4 5
Output
1 −1
Note: There is no unique most frequent element.
Input
1 5
2 1 2 2 1 2
Output
1 2
Note: The element 1 appears with frequency 2, and the element 2 appears with frequency
3, hence 2 is the mode of the array.
Page 6
• Then read n integers representing the array
• Your program should reverse the elements of the array in-place, i.e. without using
an extra array, you can use a fixed number of extra bytes (for eg. one or two extra
integers, the number of such extra integers you use should not depend on the size
of the array given to you)
Input
1 3
2 1 2 3
Output
1 3 2 1
Input
1 6
2 3 2 1 4 5 6
Output
1 6 5 4 1 2 3
• Then read n1 integers representing the first array (all the elements will be unique
and sorted)
• Then read n2 integers representing the second array (all the elements will be unique
and sorted)
Page 7
• First print the intersection of the two arrays, in the sorted order as space separated
integers
• Then on the next line print the union of the two arrays, in the sorted order as space
separated integers
Input
1 4 3
2 1 2 3 4
3 1 2 3
Output
1 1 2 3
2 1 2 3 4
1 4 5
2 1 3 4 6
3 3 6 7 8 9
Output
1 3 6
2 1 3 4 6 7 8 9
Page 8