Some simple Python exercises😎

1. Sum of List Elements

Problem Description

Sum of List Elements

Write a Python function that calculates the sum of all elements in a given list of integers.

Parameters:

  • numbers (List of integers): The input list containing integers.

Returns:

  • An integer representing the sum of all elements in the input list.

Example:

  1. Input: numbers = [1, 2, 3, 4, 5]
    Output: 15
  2. Input: numbers = [10, -5, 7, 8, -2]
    Output: 18
def sum_list(numbers):
    sum=0
    for n in numbers:
        sum=sum+n
        
    return sum

2. Largest Element in a List

Find the Largest Element in a List

Write a Python function that finds and returns the largest element in a given list of integers.

Parameters:

  • numbers (List of integers): The input list containing integers.

Returns:

  • An integer representing the largest element in the input list.

Example:

  1. Input: numbers = [3, 8, 2, 10, 5]
    Output: 10
  2. Input: numbers = [-5, -10, -2, -1, -7]
    Output: -1
def find_largest(numbers):
    max=numbers[0]
    for n in numbers:
      if(n>max):
         max=n
          
    return max

3. Remove Duplicate in a List

Remove Duplicates from a List

You are given a list of integers. Write a Python program that removes any duplicate elements from the list and returns a new list with only unique elements. The order of elements in the list should be maintained.

Parameters:

  • lst (List of integers): The list of integers from which duplicates should be removed.

Returns:

  • A list of integers where all duplicates have been removed, preserving the original order.

Example:

  1. Input: lst = [1, 2, 2, 3, 4, 4, 5]
    Output: [1, 2, 3, 4, 5]
  2. Input: lst = [4, 5, 5, 4, 6, 7]
    Output: [4, 5, 6, 7]
def remove_duplicates(lst):
    set_from_list=set(lst)
    my_list=list(set_from_list)
    
    return my_list

4. Check if all elements in a List are Unique

Check if All Elements in a List are Unique

You are given a list of integers. Write a Python program that checks if all elements in the list are unique. If all elements are unique, return True; otherwise, return False.

Parameters:

  • lst (List of integers): The list of integers to check for uniqueness.

Returns:

  • A boolean value True if all elements in the list are unique, False otherwise.

Example:

  1. Input: lst = [1, 2, 3, 4, 5]
    Output: True
  2. Input: lst = [1, 2, 3, 3, 4, 5]
    Output: False
def check_unique(lst):
    total_elements=len(lst)
    new_set=set(lst)
    total_el_in_set=len(new_set)
    return total_el_in_set == total_elements

5. Reverse a List

Reverse a List (Non-Slicing Approach)

You are given a list of integers. Write a Python program that reverses the list without using slicing (lst[::-1]). The program should return the reversed list.

Parameters:

  • lst (List of integers): The list of integers to be reversed.

Returns:

  • A list of integers where the order of elements is reversed from the input list.

Example:

  1. Input: lst = [1, 2, 3, 4, 5]
    Output: [5, 4, 3, 2, 1]
def reverse_list(lst):
    return  [lst[i] for i in range(len(lst) - 1, -1, -1)]  

6. Number of Odd and Even Elements in a list

Count Even and Odd Numbers in a List

You are given a list of integers. Write a Python program that counts and returns the number of even and odd numbers in the list.

Parameters:

  • lst (List of integers): The list of integers where you will count the even and odd numbers.

Returns:

  • A tuple (even_count, odd_count) where even_count is the number of even numbers and odd_count is the number of odd numbers.

Example:

  1. Input: lst = [1, 2, 3, 4, 5]
    Output: (2, 3)
    • There are 2 even numbers: 2, 4
    • There are 3 odd numbers: 1, 3, 5
def count_even_odd(lst):
    odNum=0
    evenNum=0
    
    for num in lst:
        if num%2==0:
            evenNum+=1
        else:
             odNum+=1

    result=(evenNum,odNum)
    return result

7. Check if List is Subset of Another List

Check if a List is a Subset of Another List

You are given two lists of integers. Write a Python program that checks whether the first list is a subset of the second list, without using the in keyword. A list is considered a subset if all elements of the first list are present in the second list.

Parameters:

  • lst1 (List of integers): The first list, which is being checked as a subset.
  • lst2 (List of integers): The second list, which is the list to compare against.

Returns:

  • A boolean value True if lst1 is a subset of lst2, otherwise False.

Example:

  1. Input: lst1 = [1, 2, 3], lst2 = [1, 2, 3, 4, 5]
    Output: True
    • All elements in lst1 are present in lst2.
  2. Input: lst1 = [1, 6], lst2 = [1, 2, 3, 4, 5]
    Output: False
    • The element 6 is not present in lst2.
def is_subset(lst1, lst2):
    return set(lst1).issubset(lst2)

8. Maximum Difference between 2 consecutive elements in a List

Find Maximum Difference Between Two Consecutive Elements

You are given a list of integers. Write a Python program to find the maximum difference between two consecutive elements in the list. The difference is defined as the absolute value of the difference between two consecutive elements.

Parameters:

  • lst (List of integers): A list of integers.

Returns:

  • An integer representing the maximum difference between two consecutive elements.

Example:

  1. Input: lst = [1, 7, 3, 10, 5]
    Output: 7
    • The maximum difference is between 3 and 10 (i.e., |3 - 10| = 7).
  2. Input: lst = [10, 11, 15, 3]
    Output: 12
    • The maximum difference is between 15 and 3 (i.e., |15 - 3| = 12).
def max_consecutive_difference(lst):
    maxValue = 0
    for index, item in enumerate(lst):
        if abs(item-lst[index-1])>maxValue:
            maxValue= abs(item-lst[index-1])
            
    return maxValue

9. Merge two sorted List

Merge Two Sorted Lists

You are given two sorted lists of integers. Write a Python function to merge these two sorted lists into one sorted list. The resulting list should also be in non-decreasing order.

Parameters:

  • list1 (List of integers): The first sorted list.
  • list2 (List of integers): The second sorted list.

Returns:

  • A single list of integers, containing all elements from list1 and list2, sorted in non-decreasing order.

Example:

  1. Input: list1 = [1, 3, 5], list2 = [2, 4, 6]
    Output: [1, 2, 3, 4, 5, 6]
  2. Input: list1 = [1, 4, 7], list2 = [2, 3, 5, 8]
    Output: [1, 2, 3, 4, 5, 7, 8]
def merge_two_sorted_lists(list1, list2):
    merged_list= list2+list1
    merged_list.sort()
    return merged_list

10. Rotate a List

Rotate a List (Without Slicing)

You are given a list of integers and an integer k. Write a Python function to rotate the list to the right by k positions without using slicing. A rotation shifts elements from the end of the list to the front.

Parameters:

  • lst (List of integers): The list to be rotated.
  • k (Integer): The number of positions to rotate the list.

Returns:

  • A list of integers rotated by k positions.

Example:

  1. Input: lst = [1, 2, 3, 4, 5], k = 2
    Output: [4, 5, 1, 2, 3]
  2. Input: lst = [10, 20, 30, 40, 50], k = 3
    Output: [30, 40, 50, 10, 20]
def rotate_list(lst, k):
    if not lst:
        return []
        
    n = len(lst)
    k = k % n
    
    return lst[n - k:] + lst[:n - k]

Leave a Reply

Your email address will not be published. Required fields are marked *