0% found this document useful (0 votes)
9 views3 pages

Practice Problems 1

Programming problems

Uploaded by

priyamsahoojnvkp
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)
9 views3 pages

Practice Problems 1

Programming problems

Uploaded by

priyamsahoojnvkp
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
You are on page 1/ 3

Practice Problems in Python

BSDS, 1st Year 2024–2025


Date: 13 August, 2024

PRACTICING INSTRUCTIONS
1. Write down the best possible implementation of the problems listed below.
2. Take care of the error cases too.
3. After you finish the coding, look at the model answers at the end and understand.

NOTE: The programs are to be written in Python and should be well commented. All programs
should take the required inputs from standard input buffer and print the desired outputs to the
standard output buffer, until otherwise stated.

Q1. (EASY) Given an string, write a program to verify whether the string has equal number of
vowels and consonants or not.

Q2. (EASY) An n-digit number is SPECIAL if the addition of its sum of the digits and the product
of its digits equals to the original number. E.g., 19 is a SPECIAL 2-digit number. Write a
program to verify whether a given number is SPECIAL or not. Extend this program to verify
whether there exists any SPECIAL number for a given value of number of digits n.

Q3. (EASY) The abundancy of a natural number n is defined as the rational number σ(n) n
, the
ratio between the sum of divisors of the number and the number itself. A number n is defined
as friendly if it shares abundancy with one or more other numbers. This means there might
exist a pair of numbers i and j such that i ̸= j but σ(i)
j
= σ(j)
j
. For example, 6 and 28 are
friendly with each other because σ(6)6
= σ(28)
28
= 2. Write a program to verify whether a pair
of integers given as user input are friendly or not.

Q4. (EASY) Write a program to check whether a number given as user input is a power of 32 or
not. Accordingly, print POWER OF 32 or NOT POWER OF 32.

Q5. (MEDIUM) Two elements A[i] and A[j] of a list A are said to form an inversion pair if
A[i] > A[j] but i < j. Write a program to count the number of inversion pairs in a list A
containing distinct integers.
Note that, for the array A = {8, 4, 2, 1}, the inversion pairs are (8, 4), (4, 2), (8, 2), (8, 1), (4,
1) and (2, 1).

Q6. (MEDIUM) Suppose you are playing a game in turn with the computer. Total n number of
sticks are to be picked up in this game. Whoever picks the last one loses the game. Neither
the computer nor you can pick up more than 3 sticks at a time. Nobody can skip a turn, i.e.
at least one stick is to be picked up in a turn. Write a program to ensure that the computer
wins optimally (whenever there is a chance) irrespective of the turn.

1
Q7. (MEDIUM) Write a program to print the following pattern given the line number as user
input.

*
* * *
* * *
* * *
* * *
* * * * * * * *
* * *
* * *
* * *
* * *
*

Q8. (HARD) A group of n friends F1 , F2 , . . . , Fn decide to try their luck at a lottery. Each person
Fi buys a number Xi of lottery tickets. Each lottery ticket has a digit (0-9) printed on it.
The rule for winning the jackpot is as follows. Each person is asked to announce the largest
multiple of 3 that can be formed by selecting and arranging the digits on his lottery tickets.
The person who has the highest such multiple wins the jackpot. Note that a person’s tickets
may not have distinct digits on them.

Q9. (HARD) There are N petrol pumps P1 , P2 , . . . , PN arranged in a clockwise direction along a
circular road. Consider a truck with a fuel tank that is initially empty, but which has infinite
capacity. The truck will initially fuel up at some Pi and move in a clockwise direction along
the circular road. Each time it reaches a petrol pump, it will take up all the petrol available
at that pump. Write a program to determine the first Pi such that if the truck starts from Pi ,
it will be able to completely traverse the circle and return to Pi . The amount of petrol that
every petrol pump has (in litres), and the distance from that petrol pump to the next petrol
pump (in km) will be given to you as inputs. Assume that the truck needs 1 litre of fuel to
travel 1 km.

Q10. (HARD) Let us define a sequence a0 , a1 , a2 , . . . , an−1 as Λ-bitonic if there exists a j, 0 ≤ j < n,
such that a0 < a1 < · · · < aj > aj+1 > · · · > an−1 . Consider an m × n matrix A consisting of
integer entries, such that each row and each column of the matrix forms a Λ-bitonic sequence.
Write a program to efficiently find the largest element of the matrix.

2
Model Answers:
To be added

You might also like