0% found this document useful (0 votes)
5 views

05.4.CoinChange_RecursionWithMemoization_Challenge_ProgrammingExercises

The document outlines a series of programming exercises for students at Atlas SkillTech University, covering topics such as palindromes, valid sequences, coin change, Fibonacci calculations, and maximizing stolen goods. Each exercise includes a description, input/output specifications, and sample runs for clarity. The exercises are designed to enhance students' programming skills and problem-solving abilities.

Uploaded by

Sonit Marwah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

05.4.CoinChange_RecursionWithMemoization_Challenge_ProgrammingExercises

The document outlines a series of programming exercises for students at Atlas SkillTech University, covering topics such as palindromes, valid sequences, coin change, Fibonacci calculations, and maximizing stolen goods. Each exercise includes a description, input/output specifications, and sample runs for clarity. The exercises are designed to enhance students' programming skills and problem-solving abilities.

Uploaded by

Sonit Marwah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Atlas SkillTech University, Fall 2022

Instructor: Prof. Ashwin Ganesan


Programming Exercises

Contents
1 Even palindromes 2

2 Valid sequences of if-else keywords 3

3 Valid strings 4

4 Coin change 1 5

5 Fibonacci remainder 6

6 Coin change 2 7

7 Maximize value of stolen goods 9

1
1. Even palindromes
Write a program which takes as input a binary string and outputs the string “True” (without double
quotes) if the string is a palindrome of even length, and outputs “False” otherwise. Recall that a
palindrome is a string which reads the same right to left as left to right (ie, a palindrome is a string
which is equal to its reverse).

Input: a nonempty string of 0’s and 1’s. The length of the string is at most 1000.

Output: If the string is a palindrome of even length, output “True” (without the double quotes).
Otherwise, output “False”.

Sample runs:

• Input:
11

Output:
True
Explanation: The string “11” is a palindrome, and it has length 2, which is even.

• Input:
10101

Output:
False
Explanation: The input string is a palindrome, but its length is not even.

• Input:
1010

Output:
False
Explanation: The string is not a palindrome.

2
2. Valid sequences of if-else keywords
A sequence of if-else keywords (such as “if, if, else, if, else, else”) is said to be valid whenever it can
arise as a sequence of if-else keywords in a program. Call a string of characters over the alphabet
{i, e} valid if for each prefix of the string, the number of i’s is greater than or equal to the number
of e’s in the prefix. (Recall that a prefix of a string a1 a2 · · · an is a substring of the form a1 · · · ak
for some k.)
Write a program which takes as input a string over the alphabet {i, e}, and which outputs “Valid”
(without double quotes) if the string is valid, and “Invalid” otherwise.

Input: a nonempty string of i’s and e’s. The length of the string is at most 1000.

Output: If the string is valid, output “Valid” (without double quotes), otherwise output “Invalid”

Sample runs:

• Input:
iie

Output:
Valid
Explanation: The number of i’s is greater than or equal to the number of e’s in each prefix of
the string.
• Input:
iieee

Output:
Invalid
Explanation: The string has more e’s than i’s.
• Input:
iiieeeei

Output:
Invalid
Explanation: While the string has at least as many i’s as e’s, there is a prefix for which the
number of e’s exceeds the number of i’s.

3
3. Valid strings
A string of 0’s, 1’s and 2’s is said to be valid if it is of the form 0n 1n 2n for some n ≥ 1. In other
words, a string is valid if it is nonempty, and consists of some number of 0’s followed by the same
number of 1’s and then the same number of 2’s. Write a program which takes as input a string over
the alphabet {0, 1, 2} and which outputs “Valid” if the string is valid, and “Invalid” otherwise.

Input: a nonempty string over the alphabet {0, 1, 2}. The length of the string is at most 1000.

Output: If the string is of the form 0n 1n 2n for some n ≥ 1, then output “Valid” (without double
quotes), otherwise output “Invalid”.

Sample runs:

• Input:
001122

Output:
Valid
Explanation: The string is exactly 02 12 22 .

• Input:
012012

Output:
Invalid
Explanation: The string contains an equal number of 0’s, 1’s and 2’s but is not of the given
form because some 0’s and 1’s follow a 2.
• Input:
0

Output:
Invalid
Explanation: The string does not contain an equal number of 0’s, 1’s and 2’s.

4
4. Coin change 1
A currency has coin denominations 1, 10 and 100. Write a program which inputs a positive integer
n and outputs the smallest number of coins needed to make change n. For example, if the input is
23, then the output is “5’ because 23 = 10 + 10 + 1 + 1 + 1, and so 23 can be expressed using 5 coins,
and it is not possible to obtain 23 using fewer than 5 coins.

Input: A positive integer n, where n ≤ 106 .

Output: The smallest number of coins of denominations 1, 10 and 100 needed to make change n.

Sample runs:

• Input:
23

Output:
5
Explanation: 23 = 10 + 10 + 1 + 1 + 1 and 23 cannot be expressed as a sum of fewer than 5
terms if each term must be 1, 10 or 100.

5
5. Fibonacci remainder
Recall that the Fibonacci sequence f (n) is defined by f (0) = 0, f (1) = 1, and f (n) = f (n − 1) +
f (n − 2) for all n ≥ 2. Given positive integers n and m, write a program which outputs f (n)
(mod m), i.e. the remainder obtained upon dividing the nth fibonacci number f (n) by m.

Input: The input is a single line consisting of the two numbers n and m separated by space. Assume
n, m ≤ 105 .

Output: The remainder f (n) (mod m).

Sample runs:

• Input:
8 6

Output:
3
Explanation: The fibonacci number f (8) is equal to 21 and the remainder obtained when we
divide 21 by 6 is 3.

6
6. Coin change 2
Given that a currency has a particular set of denominations of coins, find the smallest number of coins
needed to make change. For example, suppose a particular currency has coins with denominations
1, 2, 5 and 10. What is the minimum number of coins needed to make a change of 23? The answer
is 4 because 23 = 10 + 10 + 2 + 1 and we cannot obtain 23 using three or fewer coins.
Write a program that obtains the minimum number of coins to make change. The input consists
of a sequence of integers separated by a space. The first integer in the input sequence is the number
of denominations (which is 4 in the above example). This is followed by the set of denominations
(which is 1, 2, 5, 10 in the above example). The last integer is the change amount (which is 23
in the above example). Your program should output either the minimum number of coins needed
to make change, or output “Impossible” (without the double quotes) if it is not possible to make
change with the given denominations.

Input: The input has just one line consisting of a sequence of integers (whose consecutive elements
are separated by a space). The first integer in the sequence is the number m of denominations. The
next m numbers are distinct integers which represent the available coin denominations. The last
integer in the sequence is the change amount n. Assume 1 ≤ m ≤ 10 and 0 ̸= n ≤ 106 .

Output: The minimum number of coins needed to make change (or the word “Impossible”).

Sample runs:

• Input:
4 1 2 5 10 23

Output:
4
Explanation: The input sequence says there are 4 coin denominations, namely 1, 2, 5 and 10.
The change amount 23 can be expressed using 4 coins as 10 + 10 + 2 + 1 and 23 cannot be
expressed using fewer than 4 coins.
• Input:
4 5 10 20 25 40

Output:
2
Explanation: 40 = 20 + 20.

• Input:
4 5 10 20 25 100

Output:
4

• Input:
4 5 10 20 25 3

Output:
Impossible
Explanation: It is not possible to express 3 as a sum of 5’s, 10’s, 20’s and 25’s.

• Input:
3 6 1 5 9

7
Output:
4
• Input:
4 3 5 7 9 2000

Output:
224
• Input:
4 1 2 5 10 1000000

Output:
100000
• Input:
3 1 2 3 0

Output:
0
Explanation: 0 is a sum of zero terms.
• Input:
6 7 11 13 17 19 23 987654

Output:
42942

8
7. Maximize value of stolen goods
A thief breaks into a locker and finds various precious metals in powder form. His backpack allows
him to carry only a certain amount of weight. Each item in the locker has a certain weight (in kg)
and a certain value per kg. The thief needs to decide which items to take with him and how much
of each item to take, so that the value of the stolen goods is maximized.
For example, suppose the backpack has a capacity of 20 kg. The locker contains five items, call
them a, b, c, d and e. Suppose the weights of these five items (in kg) are 10, 10, 5, 20 and 3 and the
values of these items (in lakh Rupees per kg) are 6, 5, 3, 2 and 10, respectively. Then, the thief can
decide to take all 10 kg of item a, 7 kg of item b and all 3 kg of item e. The value of the stolen
goods is then 10 × 6 + 7 × 5 + 3 × 10 = 125. It turns out that it is not possible to get a value more
than 125.

Input: The input consists of a single sequence of numbers (two consecutive numbers in this sequence
are separated by a space). The first number in the sequence is number of items m in the knapsack,
where 1 ≤ m ≤ 10. The next 2m numbers represent the weight and value of the m items - every
two subsequent numbers wi , vi correspond to the weight (in kg) and value (in Rupees per kg) of the
ith item, respectively. The last number in the sequence is the the capacity of the knapsack (in kg).
Assume all numbers in the input sequence are integers.

Output: The maximum possible value of the stolen goods, rounded to the nearest integer.

Sample run:

• Input:
5 10 6 10 5 5 3 20 2 3 10 20

Output:
125
Explanation: The input says that there are 5 items available, and the (wi , vi ) values for the
five items are (10, 6), (10, 5), (5, 3), (20, 2) and (3, 10), respectively. The knapsack capacity is
20 kg. There exists a subset of these items whose value is 125. It can be shown that no subset
has a value larger than 125.

You might also like