Lab Worksheet 4: Iteration: CS 101 Algorithmic Problem Solving Fall 2024
Lab Worksheet 4: Iteration: CS 101 Algorithmic Problem Solving Fall 2024
Name(s):
HU ID (e.g., xy01042) :
• 1 ≤ N ≤ 108
Interaction
The input contains a single integer value denoting the number whose digits you have to sum
The output would contain the sum of the digits.
Sample
Input Output
1829 20
23 5
In the first case, N = 1829, therefore the answer would be 1 + 8 + 2 + 9 = 20
In the second case, N = 23, therefore the answer would be 2 + 3 = 5
Exercise
In the space provided, indicate the outputs for the given inputs.
Input Output
2872
98723
Pseudocode
1
CS 101, Fall 2024 LW4: Iteration
Dry Run
Using any one of the inputs provided in the Exercise section above, dry run your psuedocode
in the space below.
2. Party Invites
Your friend is hosting a party at his house. He’s prepared X burgers and Y sandwiches for
the event. He needs your help in figuring out how many people to invite. Ideally, he wants
that everyone gets the same amount of burgers and sandwiches, and no food is leftover.
Given values of X and Y , determine the maximum number of guests your friend can invite.
Constraints
• X, Y ∈ N
• 1 ≤ X, Y ≤ 105
Interaction
The input comprises of a single line containing two space-separated integers denoting the
values of X and Y respectively.
The output must be a single integer denoting the maximum number of guests your friend
can invite.
Sample
Input Output
12 16 4
99 9
In the first case, (X, Y ) = (12, 16), the maximum number of guests your friend can invite is
4; each guest can have 3 burgers and 4 sandwiches.
In the second case, (X, Y ) = (9, 9), the maximum number of guests your friend can invite is
9; each guest can have 1 burger and 1 sandwich.
Exercise
In the space provided, indicate the outputs for the given inputs.
Input Output
16 17
32 52
256 128
Page 2 of 10
CS 101, Fall 2024 LW4: Iteration
Pseudocode
Dry Run
Using any one of the inputs provided in the Exercise section above, dry run your pseudocode
in the space below.
3. Magic Potions
Aimen plays a game in which the character competes in a hurdle race. Hurdles are of varying
heights, and the characters have a maximum height h they can jump. There is a magic potion
they can take that will increase their maximum jump height by 1 unit for each dose.
Given the number of jumps n and maximum height h, figure out how many doses of the
potion must Aimen’s character take to be able to jump all of the hurdles. If Aimen can
already clear all of the hurdles without taking the potion, return 0.
Constraints
• 1 ≤ n, h ≤ 25
Interaction
The input comprises a line containing 2 space-separated integers denoting the values of n
and h respectively. Followed by a line containig n integers separated by space, each denoting
the height of a hurdle.
Page 3 of 10
CS 101, Fall 2024 LW4: Iteration
The output must contain a single number denoting the total doses of magic potions needed.
Sample
Input Output
43 0
1232
57 3
2 7 8 10 3
In the first case, all hurdles are lower than the maximum height the character can jump.
Hence, no magic potion needed.
In the second case, the tallest hurdle is 10 hence 3 doses of the magic potion are needed. If
they are taken, the character will be able to jump the hurdles 8 and 10 which are greater
than the maximum height.
Exercise
In the space provided, indicate the outputs for the given inputs.
Input Output
43
9612
68
1 3 8 9 10 12
Pseudocode
Page 4 of 10
CS 101, Fall 2024 LW4: Iteration
Dry Run
Using any one of the inputs provided in the Exercise section above, dry run your psuedocode
in the space below.
4. Password
You’ve made a website that hosts information about upcoming events taking place at your
university. You’re working on the backend of the login system and have decided that the
user passwords must be very difficult to crack so that nobody can impersonate others and
misuse your website. You’ve come up with a set of parameters that a password must satisfy
in order to be accepted. These are:
• The password must contain a lowercase character.
• The password must contain an uppercase character.
• The password must contain a number.
• The password must not contain any symbols or spaces.
Given a string S, determine if it is an acceptable password or not.
Constraints
• 1 ≤ len(S) ≤ 20
Interaction
The input comprises of a single line containing a string S denoting the password.
The output must be “YES” if the string is an acceptable password, and “NO” otherwise.
Sample
Input Output
“seVenteen17” “YES”
“H@B1Buni” “NO”
In the first case, S = “seVenteen17” which satisfies all the given parameters and is an
acceptable password.
In the second case, S = “H@B1Buni” which contains a special character and is not an
acceptable password.
Exercise
In the space provided, indicate the outputs for the given inputs.
Page 5 of 10
CS 101, Fall 2024 LW4: Iteration
Input Output
“pass Word23”
“29thFeb2004”
“14th Aug”
Pseudocode
Dry Run
Using any one of the inputs provided in the Exercise section above, dry run your pseudocode
in the space below.
• 0 ≤ R1, R2 ≤ 100
• 1 ≤ N ≤ 100
Page 6 of 10
CS 101, Fall 2024 LW4: Iteration
Interaction
The input comprises a single line containing 3 space-separated integers denoting the values
of N, R1 and R2 respectively.
The output must contain a single number denoting the maximum distance.
Sample
Input Output
504 2
367 0
In the first case, there are a total of 5 planets. Planet 0 and 4 have the distance of 0 to the
nearest power source. Planet 1 and 3 have power source 0 and 4 respectively at a distance
of 1 km. Planet 2 is 2 km away from both the power sources hence, the maximum distance
for all 5 planets is 2 km.
In the second case, there are 3 planets but stations are said to be at planets numbered 6 and
7 which do not exist, hence the output is 0.
Exercise
In the space provided, indicate the outputs for the given inputs.
Input Output
704
496
11 2 10
Pseudocode
Page 7 of 10
CS 101, Fall 2024 LW4: Iteration
Dry Run
Using any one of the inputs provided in the Exercise section above, dry run your psuedocode
in the space below.
6. Iterative exponent
You have studied exponents both early in this class and in secondary school, and your friends
challenge you to find the exponent using multiplications only. Given m and n two integers
your job is to find the exponent is the form mn
Constraints
• 1 ≤ m, n ≤ 103
Interaction
The input contains two space-separated integers m and n.
Output would contain a single integer which is mn
Sample
Input Output
3,4 81
0,2 0
In the first case, m, n = 3, 4, 34 = 81 = 3 ∗ 3 ∗ 3 ∗ 3
In the second case, m, n = 0, 2, 02 = 0 = 0 ∗ 0
Proposed Solution
1 product = m
2 if m == 0:
3 print(0)
4
5 elif n == 0:
6 print(0)
7
8 else:
9 for i in range(0,n-1):
10 product = product**m
11
12 print(product)
Page 8 of 10
CS 101, Fall 2024 LW4: Iteration
Dry Run
Using the inputs provided in the Sample section above, dry run the proposed code solution
below.
Error Identification
Briefly explain the errors you identified in the proposed code solution. Mention the line
number and the errors in each line.
Correct Solution
Rewrite the lines of code you mentioned above with their errors corrected.
7. Intoduction to Hackerrank
The guide to login/sign-up on Hackerrank tests is available here.
Problems are available on this link as a short introduction to programming:
https://hr.gs/cs101-lw4-fall24
Page 9 of 10
CS 101, Fall 2024 LW4: Iteration
Rough Work
Page 10 of 10