0% found this document useful (0 votes)
61 views8 pages

CSE103 Problem Solving Assignment-1

Uploaded by

jinay.s5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views8 pages

CSE103 Problem Solving Assignment-1

Uploaded by

jinay.s5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CSE103 The World of Computer Programming

Assignment 1: Problem-Solving using Python


Instructions
This assignment contains problem-solving questions curated from experience and multiple knowledge
sources. They are suitable for the first time learners in Engineering/Management/Arts and Sciences.
You may use Python to solve the given problems. We encourage you to write core logic to solve
questions instead of using library functions or the third-party packages. The questions are divided into
three sections: Hard Questions, Intermediate Questions, and Easy Questions. It is compulsory to do
questions from all sections. You need to complete and submit your solutions Python file (executable
.py file) on or before September 25, 2024 on LMS as per the instruction given in a class. The learning
evaluation of this assignment will be based on an online contest. You are also encouraged to complete
ENR106 Programming Assignment to perform well in the contest.

Happy problem solving! All the best!

Hard Questions
1. You are given a rectangular grid of size M×N. You have an unlimited supply of
dominoes, each of size 2×1. Your task is to determine the maximum number of
dominoes that can fit on the grid, following these rules:
a. Each domino must cover exactly two grid squares.
b. No two dominoes can overlap.
c. Each domino must lie entirely within the grid.

Example:

Input: 2 4

Output: 4

Input: 3 3

Output: 4

Hint: Consider the total number of squares on the grid and how many can be
covered by a domino.

2. An elephant decided to visit his friend. It turned out that the elephant's house is
located at point 0 and his friend's house is located at point x(x > 0) of the coordinate
line. In one step the elephant can move 1, 2, 3, 4 or 5 positions forward. Determine,

1
what is the minimum number of steps he need to make in order to get to his friend's
house.

Test Cases:

Input: 5

Output: 1

Input:12

Output:3

3. You are provided with a large text file containing data entries, each on a new line.
Each entry consists of a name followed by a list of integers (e.g., John 12 15 19 22).

Write a Python program to:

a. Read the file and process each line.


b. Calculate the sum of integers for each name.
c. Identify the name with the highest sum.
d. Output the name and the corresponding sum to a new file.

Input: A text file data.txt with contents:

Alice 12 15 19

Bob 10 20 30

Charlie 5 15 25 35

Output: A new file output.txt with content:

Charlie 80

Hint: Consider using Python’s file handling methods such as open(), readlines(),
and write(). You might need to handle exceptions for file operations and ensure
efficient processing for large files.

4. Volodya, being a nonconformist, is unhappy with the usual order of natural numbers.
He decides to rearrange the first n natural numbers by placing all the odd numbers
first in ascending order, followed by all the even numbers in ascending order. Given
this new sequence, you are asked to determine which number is at the k-th position.

Example:

2
a. For n = 7 and k = 3, the sequence would be: 1, 3, 5, 7, 2, 4, 6. The 3rd number
is 5.
b. For n = 8 and k = 5, the sequence would be: 1, 3, 5, 7, 2, 4, 6, 8. The 5th
number is 2.

5. You are given two sorted arrays, A and B, each containing n elements. You need to
merge these arrays into a single sorted array, but with a twist. After merging, the
new array should contain all the elements of A in their original order followed by all
the elements of B in their original order. Your task is to find the k-th element in this
new merged array.

Hint: Understand that you don't need to actually merge the arrays; you can calculate
the position of the k-th element directly.

Example:

If A = [1, 3, 5], B = [2, 4, 6], and k = 4, the merged array would be [1, 3, 5, 2, 4, 6], and
the 4th element is 2.

6. Write a program to rotate a list to the left by k positions. The rotation should be
performed in place without using extra space.

Example Input/Output:

Input: List = [1, 2, 3, 4, 5], k = 2

Output: [3, 4, 5, 1, 2]

3
Intermediate Questions
7. Palindrome Check
Write a Python function that checks if a given string is a palindrome. A palindrome is
a word, phrase, number, or other sequence of characters that reads the same
forward and backward (ignoring spaces, punctuation, and capitalization).

Example:
Input: "abba"
Output: True
Input: "121"
Output: True

Hint: Normalize the string by removing spaces and punctuation, and converting it to
lowercase. Then, check if the normalized string is the same as its reverse.

8. Find the Missing Number


You are given a list of n-1 integers in the range 1 to n. There are no duplicates in the
list. One of the integers is missing. Write a Python function to find the missing
integer.

Example:
Input: [1, 2, 4, 5, 6]
Output: 3
Input: [7, 8, 10, 12, 13]
Output: 11

Hint: The sum of the first n natural numbers can be calculated using the formula n *
(n + 1) / 2. Find the difference between this sum and the sum of the elements in the
list to identify the missing number.

9. Count Vowels in a String


Write a Python function that takes a string as input and counts the number of vowels
(a, e, i, o, u) in the string.

Example:
Input: "Hello World"
Output: 3
Input: "Ahmedabad University"
Output: 8
Hint: Loop through the string and count occurrences of each vowel. You can use a set
of vowels for easy lookup.

10. Finding the Longest Word in a Sentence

4
Write a Python function that takes a sentence as input and returns the longest word
in that sentence.

Example:
Input: "I love programming in Python"
Output: "programming"
Input: "Computer Science & Engineering"
Output: "Engineering"

Hint: Split the sentence into words, then find the word with the maximum length.

11. Area of a Circle


Write a Python function that takes the radius of a circle as input and returns the area
of the circle.

Example:
Input: radius = 2
Output: 12.56

Hint: Use the formula area = π * r^2, and you can use 3.14 for π or import it from the
math module.

12. Write a Python function that determines if a person is eligible to vote based on their
age and citizenship status. If citizenship status is True, then he is from India else he is
not from India. If the age entered is 120 or above, display "Invalid age." Also, if the
age is 0 or less, display "Invalid."

Example:
Input: 20, True
Output: "Eligible to vote”
Input: 16, True
Output: "Not eligible to vote - Underage"
Input: 34, False
Output: "Not eligible to vote - Non-citizen"
Input: -5, True
Output: "Invalid Age"

Hint: Use nested IF , check citizenship status -> check age

13. Write a Python function that calculates the tax owed based on a person's income.
The tax brackets are as follows:

a. Income up to ₹10,000: 10%


b. Income between ₹10,001 and ₹50,000: 20%

5
c. Income above ₹50,000: 30% If the income entered is negative, display
"Invalid income."

Example:

Input: 9000

Output: 900

Input: 30000

Output: 5000

Input: 60000

Output: 13000

Hint: To solve this problem, divide the income into different tax brackets,
calculate the tax for each bracket, and then sum them up. Ensure that higher tax
rates are only applied to the portion of income that exceeds the lower brackets.

6
Easy Questions
14. Convert Celsius to Fahrenheit
Write a Python function that converts a temperature from Celsius to Fahrenheit. Use
the formula F = C * 9/5 + 32.

Example:
Input: 0
Output: 32
Input: 25
Output: 77

15. Count Occurrences of a Character in a String


Write a Python function that counts the occurrences of a specific character in a given
string.

Example:
Input: "hello", 'l'
Output: 2
Input: "banana", 'a'
Output: 3

16. Print the First n Natural Numbers


Write a Python function that prints the first n natural numbers.

Example:
Input: 5
Output: 1 2 3 4 5
Input: 3
Output: 1 2 3

17. Check if a String is a Substring


Write a Python function that checks if a given substring is present in a string.

Example:
Input: "hello world", "world"
Output: True
Input: "Python", "code"
Output: False

Hint: Use the in keyword to check if the substring is present in the string.

18. Calculate the Average of a List of Numbers


Write a Python program that calculates the average of a list of numbers.

7
Example:
Input: [10, 20, 30, 40]
Output: 25
Input: [5, 15, 25]
Output: 15

19. Calculate the Perimeter of a Rectangle


Write a Python program that calculates the perimeter of a rectangle given its length
and width. Use the formula Perimeter = 2 * (length + width).

Example:
Input: 5, 3
Output: 16
Input: 7, 2
Output: 18

20. Write a Python program that finds the length of the longest word in a list of strings.

Example:
Input: ["sun", "moon", "stars"]
Output: 5

Hint: Iterate through the list and keep track of the maximum length encountered.

21. Write a Python program that calculates the Body Mass Index (BMI) of a person. The program
should take the person's weight (in kilograms) and height (in meters) as input and compute
the BMI using the formula
𝐻𝑒𝑖𝑔ℎ𝑡
𝐵𝑀𝐼 =
(𝑊𝑒𝑖𝑔ℎ𝑡)2

-- --

You might also like