List Exercises (Part 1)

Table of contents

  1. Exercise 1: Reverse The Input
  2. Example 2: Sort the Input
  3. Example 3: Random Unique Numbers
  4. Example 4: Is Equivalent
  5. Exercise 5 (Code Reading)

Exercise 1: Reverse The Input

Write a program that reads a list of 20 integers from the user, then prints them in reverse order.

N = 20
nums = [0] * N
for i in range(N):
    num = int(input("Enter an integer: "))
    nums[i] = num

print("The numbers in reverse order are:")
print(nums[::-1])

Example 2: Sort the Input

Write a program that keeps reading integers from the user until the user enters -1. Then, the program prints the integers sorted in ascending order (excluding the -1).

nums = []
while True:
    num = int(input("Enter an integer (-1 to stop): "))
    if num == -1:
        break
    nums.append(num)

print("The numbers in sorted order are:")
print(sorted(nums))

Example 3: Random Unique Numbers

Write a program that generates a list of 100 unique random integers between 0 and 1000, then prints the list sorted in ascending order.

To solve this problem, we will begin with an empty list, and keep generating random integers and adding them to the list until the list has 100 unique integers. When generating a random integer, we will check if it is already in the list; if it is, we will not add it again.

import random

nums = []
while len(nums) < 100:
    num = random.randint(0, 1000)
    if num not in nums:
        nums.append(num)

print("The unique random numbers are:")
print(sorted(nums))

Example 4: Is Equivalent

Define a function is_equivalent(a, b) that takes two lists of integers as arguments and returns True if both lists contain the same elements (regardless of order and frequency), and False otherwise.

Examples.

# should print equivalent
a = [1, 2, 3]
b = [1, 2, 1, 2, 2, 2, 1, 3, 3]
print(is_equivalent(a, b))

# should print not equivalent because of 3 and 4
a = [1, 2, 4]
b = [1, 2, 1, 2, 2, 2, 1, 3, 3]
print(is_equivalent(a, b))

Solution.

def is_equivalent(a, b):   
    # check that every element in a is in b 
    for element in a:
        if element not in b:
            return False

    # check that every element in b is in a
    for element in b:
        if element not in a:
            return False

    return True

Exercise 5 (Code Reading)

Read and understand the following function, then suggest suitable name for it.

def mystery(a, b):
    N1 = len(a)
    N2 = len(b)

    if N1 > N2:
        return False
    
    for i in range(N2):
        if a == b[i: i+N1]:
            return True
    
    return False
Solution The function checks if list a is a sublist of list b. A suitable name would be is_sublist(a, b) or contains_sublist(a, b).

© Ibrahim Albluwi. · Last updated: