Docsity

Algorithmic Thinking Assignment Questions – Python Practice, Exercises of Algorithms and Programming

Toggle sidebar
bg1
Question 1: Polynomial Evaluation
Question*:
Write a program that evaluates a polynomial at a given value of x. The polynomial is represented
by a list of coefficients in ascending order of power.
For example, a list [2, 3, 1] represents the polynomial 2+ 3x + x2
The program should take the list of coefficients and a value of x as input and return the
evaluated result.
Approach:
• Use a loop to iterate through each coefficient.
• For each coefficient c at index i, calculate c * (x ** i) and accumulate the result.
Language*: Python
Input:
• A list of integers (coefficients)
• An integer x
Output:
• An integer (result of the polynomial evaluated at x)
Constraints:
• No specific constraints; assume valid input.
Question 2: Find Second Maximum and Second Minimum
Question*:
Write a program that takes a list of numbers and returns the second maximum and second
minimum values in the list. The result should be returned as a tuple (second_max,
second_min).
You can assume the list contains at least two distinct elements.
Approach:
• Remove duplicates using a set.
• Sort the set.
• Pick the second last element as second maximum and the second element as second
minimum.
Language*: Python
Input:
• A list of integers
Output:
pf3
pf4
pf5

Partial preview of the text

Download Algorithmic Thinking Assignment Questions – Python Practice and more Exercises Algorithms and Programming in PDF only on Docsity!

Question 1: Polynomial Evaluation Question:* Write a program that evaluates a polynomial at a given value of x. The polynomial is represented by a list of coefficients in ascending order of power. For example, a list [2, 3, 1] represents the polynomial 2+ 3x + x^2 The program should take the list of coefficients and a value of x as input and return the evaluated result. Approach :

  • Use a loop to iterate through each coefficient.
  • For each coefficient c at index i, calculate c * (x ** i) and accumulate the result. Language: Python Input:*
  • A list of integers (coefficients)
  • An integer x Output:
  • An integer (result of the polynomial evaluated at x) Constraints:
  • No specific constraints; assume valid input. Question 2: Find Second Maximum and Second Minimum Question:* Write a program that takes a list of numbers and returns the second maximum and second minimum values in the list. The result should be returned as a tuple (second_max, second_min). You can assume the list contains at least two distinct elements. Approach :
  • Remove duplicates using a set.
  • Sort the set.
  • Pick the second last element as second maximum and the second element as second minimum. Language: Python Input:*
  • A list of integers Output:
  • A tuple containing two integers: second maximum and second minimum. Constraints:
  • List contains at least two distinct integers. Question 3: Sum of Even Numbers Question:* Write a program that takes a list of integers and returns the sum of all even numbers in the list. Approach :
  • Use a loop to iterate through the list.
  • Check if each number is even using the modulus operator.
  • Accumulate the sum of even numbers. Language: Python Input:*
  • A list of integers Output:
  • An integer representing the sum of even numbers Constraints:
  • List may contain negative numbers and zero Question 4: Finding a Book Question:* Write a program that simulates finding a book on a shelf. The function takes a list of book titles and the title of the book you're looking for. It should return the index of the book if found, or - 1 if the book is not on the shelf. Approach :
  • Use a loop to perform a linear search through the list.
  • Compare each book with the target.
  • If found, return the index; otherwise, return - 1. Language: Python Input:*
  • A list of strings (book titles)
  • A string (target book title)

Question 7: Reverse List Question:* Write a program that takes a list and returns a new list with the elements in reverse order. Do not use Python’s built-in reverse() method or reversed() function. Approach :

  • Use a loop to iterate through the list from the last index to the first.
  • Append each element to a new list. Language: Python Input:*
  • A list of integers Output:
  • A list of integers in reverse order Question 8: Polynomial Derivative Question:* Write a program that takes a list of polynomial coefficients (in ascending order of power) and returns a new list representing the coefficients of its derivative. Approach :
  • Use a loop to multiply each coefficient by its power index.
  • Skip the constant term (as its derivative is zero).
  • Return the resulting list of new coefficients. Language: Python Input:*
  • A list of integers (coefficients in ascending power) Output:
  • A list of integers (derivative coefficients) Question 9: Lost Item Finder Question:* Write a program that helps find a lost item in different rooms of a house. The function takes:
  • A list of room names
  • A list of lists, where each inner list contains items in the corresponding room
  • A string representing the lost item Return a tuple: (room_name, item_index) indicating where the item was found, or (None, - 1) if not found. Approach :
  • Use nested loops to iterate through the rooms and their corresponding items.
  • Compare each item to the lost item.
  • If found, return the corresponding room name and index. Language: Python Input:*
  • A list of room names
  • A list of lists of items
  • A string representing the lost item Output:
  • A tuple: (room_name, item_index) or (None, - 1) Question 10: Merging Sorted Lists Question:* Write a program that takes two sorted lists (in ascending order) and merges them into a single sorted list (also in ascending order). Approach :
  • Use two pointers to compare elements from both lists.
  • Add the smaller element to the result list.
  • Append any remaining elements after one list is exhausted. Language: Python Input:*
  • Two lists of integers, both sorted in ascending order Output:
  • A single merged list sorted in ascending order Question 11: Polynomial Operations System