Creative problem solving lab file ad
Creative problem solving lab file ad
Deemed University
(Declared under Distinct Category by Ministry of Education, Government of India)
NAAC Accredited with A++ Grade
Lab File
Of
Creative Problem Solving
(150703)
Submitted by:
Anirban Das
(0901CS211140)
Faculty Mentor:
Dr. Jaimala Jha
Assistant Professor
Submitted to:
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
MADHAV INSTITUTE OF TECHNOLOGY & SCIENCE
GWALIOR (474005) (MP) est.1957
JULY-DEC 2024
1
2
QUESTION-1
You wish to build a mansion beside a long road. The far side of the road is filled with n
houses, each containing a given number of people. Your mansion is as long as w houses
combined. Your task is to position the mansion so that as many people as possible live
across the road from you.
For instance, consider the road illustrated below, with 7 and w 4. Here the seven houses
contain 3, 2. 5, 1, 4, 1 and 3 people respectively. The first diagram places the mansion across
from 2+5+1+4-12 people, whereas the second diagram places it across from 5+1+ 4+1 11
people. Indeed, of all the possible locations for the mansion, the largest possible number
of people living across the road is 12.
Input: Your program must read its input from the file manin.txt. The first line of this file will
give the integers n and w, and the following in lines will give the number of people living in
each house.
Output: Your program must write its output to the file manout.txt. This file must give the
largest possible num- ber of people living across from the mansion.
Limits: Your program must run within 1 second. The input integers are guaranteed to lie in
the range 1≤ w≤ <100000.
Sample Input and Output: The sample input and output files below correspond to the
example given above.
manin.txt: manout.txt:
74 12
2
6
1
4
1
3
3
Solution:
1. Read Input:
• Read n and w (total number of houses and mansion width) from the file.
• Read the list of integers that represent the number of people in each house.
2. Sliding Window Technique:
• Start by calculating the sum of the first w houses as the initial sum.
• Slide the window one house at a time, updating the sum by adding the next
house and subtracting the house that's no longer in the window.
Code:
# Read input from "manin.txt"
with open("manin.txt", "r") as f:
# Read n and w
n, w = map(int, f.readline().strip().split())
4
# Write output to "manout.txt"
with open("manout.txt", "w") as f:
f.write(f"{max_sum}\n")
Input:
Output:
5
QUESTION-2
You are faced with the unenviable task of organising dinner for an international conference.
Several countries are represented at the conference, each with a given number of
delegates. You have also identified several restaurants in the neighbourhood, each with a
different number of seats.
In order to break down international barriers, you can- not seat two people from the same
country at the same restaurant. Your task is to find an arrangement that seats as many
people as possible.
As an example. suppose there are three countries with 4, 3 and 3 delegates respectively,
and three restaurants with 5, 2 and 3 seats respectively. You can seat most of the delegates
by placing one delegate from the second country and one delegate from the third country
in every restaurant, and by placing two delegates from the first country in the first and third
restaurants. This leaves two delegates without dinner, which is the best you can do.
Input: Your program must read its input from the file restin.txt. The first line of this file will
give the number of countries, and the second line will give the number of delegates from
each country. Likewise, the third line will give the number of restaurants, and the fourth
(and final) line will give the number of seats in each restaurant.
Output: Your program must write its output to the file restout.txt. This file must give the
smallest possible number of delegates who cannot be seated.
Limits: Your program must run within 1 second. There will be at most 5000 countries and
5000 restaurants.
Sample Input and Output: The sample input and. output files below correspond to the
example described above.
restin.txt: restout.txt:
3 2
433
3
523
Solution:
1. Sort the Delegates and Seats:
o Sort the list of delegates from each country in descending order so that
countries with more delegates are processed first.
6
o Sort the list of seats in each restaurant in descending order, prioritizing
restaurants with more seats.
2. Use a Greedy Approach:
o For each restaurant, try to seat as many delegates from different countries
as possible, ensuring that no two delegates from the same country are
seated at the same restaurant.
o Use a min-heap or similar structure to keep track of remaining delegates
from each country so that we can allocate delegates efficiently.
3. Track Unseated Delegates:
o After trying to seat delegates in all restaurants, count the remaining
delegates who could not be seated and write this number to the output file.
Time Complexity:
This greedy, max-heap-based solution efficiently manages the seating arrangement within
the 1-second time limit due to its O((n+m)log n) complexity, making it well-suited for the
input limits.
Code:
import heapq
# Number of restaurants
num_restaurants = int(f.readline().strip())
7
delegate_heap = [(-count, count) for count in delegates if count > 0]
heapq.heapify(delegate_heap)
Input:
8
Output:
9
QUESTION-3
You are wandering through the desert with a map, which shows the desert as an (x, y)
coordinate plane. You begin your journey at (0,0) facing north. In your hands are directions to
an oasis, written as a sequence of letters.
The possible letters are:
• F, indicating that you should walk forwards one kilo- metre in the direction you are
currently facing;
• L, indicating that you should turn 90° to the left;
• R, indicating that you should turn 90° to the right.
Alas, the directions contain a critical mistake one of the right hand turns has been deleted.
Fortunately, your map also shows the coordinates of the oasis, and so you hope to use this
information to work out where the missing right hand turn should be.
For example, suppose the directions are R F L F F F and the oasis is at (2,2). The first diagram
below illustrates this path, which ends at the incorrect location (1,3).
With some thought it can be seen that the directions should be R F L F F R F. That is, the missing
right hand turn takes place just before the final walk forwards, as shown in the second diagram
above.
Each scenario below lists a series of directions, followed by the location of the oasis. For each
scenario, how many letters appear before the missing R must be inserted?
1. R F F L F L F F F R F F → (3,3)
2. R F F L F R F F L F F L F L F R F F R → (5,5)
FFLFLFRFRFFRFLFFLF
3. R F F F L F F R F F F R F R F F R F F F F → (8,8)
FFLFFLFFFLFLFFFFFLFF
Solution:
1. Simulate Movement: Define the movement logic based on the current direction
(north, east, south, or west).
• F (Forward): Move one step in the direction you're currently facing.
• For each possible position in the directions string, simulate the journey by
inserting an "R" at that position.
• After each insertion, check if the final coordinates match the oasis coordinates.
3. Identify the Correct Insertion Point:
11
QUESTION-4
A token (marked 'X' in the diagram) is in a maze. You may move the token around according to
the following rule: in each move the token may travel any distance either horizontally or
vertically, but it cannot pass over or stop on a shaded square.
For example, from its starting position the token could travel either one square right, one square
down, two squares down or three squares down in a single move. To reach any other square
would require more than one move.
What is the minimum number of moves that you need to ensure that the token can reach any
white square from its starting position?
(A) 8 (B) 9 (C) 10 (D) 11 (E) 12
Solution:
1. Understand Movement Constraints:
• The token can move any distance horizontally or vertically in a single move, but
it cannot cross or land on shaded squares (1s).
2. Identify Starting and Target Squares:
• The token starts at the position marked by X, which in this grid is at (2, 2) (0-
based index).
• We need to ensure the token can reach every white square (0) in the maze.
3. Minimum Move Calculation:
• Since the token can move any number of squares in each move, we need to
calculate how many moves it would take to reach all the white squares from the
starting position without violating the constraints.
12
• This problem is similar to a flood-fill algorithm in which we expand outward from
the starting point, marking reachable squares until all accessible squares are
covered.
4. Breadth-First Search (BFS):
• Use BFS starting from (2, 2) to explore all accessible white squares.
• Each time we move in a direction, we can travel up to the nearest obstacle or
boundary.
• Count each distinct move required to reach new areas.
Answer:
• Move 1: Starting from (2, 2), the token can move right to (2, 3), down to (3, 2), left to (2,
1), or up to (1, 2).
• Move 2: From each new position, explore all directions as per the BFS algorithm.
After 10 distinct moves, we can confirm that all reachable unshaded squares are covered.
After carefully examining the maze and checking reachable squares, we find that 10 moves are
required to ensure the token can reach any unshaded square.
(C) 10
13
QUESTION-5
14
• Increase the 0 to 1, resulting in the sequence: 111
• We now have a palindromic triplet 111.
9. Third Application of Rule (i)
• Remove the palindromic triplet 111, leaving the sequence empty.
In total, we applied Rule (ii) 5 times to create palindromic triplets, which were then removed
using Rule (i).
The minimum number of times that Rule (ii) must be used to remove all digits is: 5
15
QUESTION-6
The land of Pitopia is centred upon a large circular lake. Around this lake is a circular highway,
with five cities placed along the highway. The distances between the cities are as follows:
Note that there are always two different ways of travelling from one city to another
(corresponding to the two different directions around the lake); the table above lists the shorter
distance in each case.
You are travelling along the highway in a constant direction around the lake. In which order
might you travel past the five cities?
(A) P, Q, S, T, R (B) P, R, S, T, Q (C) P, R, Q, T, S
(D) P, S, Q, T, R (E) P, T, S, Q, R
Solution:
1. Analyze the distance matrix: The table shows us the shortest distances between each
pair of cities.
2. Identify sequences that minimize distances: Since there are two ways to travel
between any two cities, we need to find the sequence that goes around the circle,
using the shortest available distances between consecutive pairs of cities.
3. Check each option: By calculating the distances between each pair of cities in each
option, we can verify which option follows the shortest distance between consecutive
cities.
Now calculate each consecutive pair in this sequence to confirm that it follows the shortest
available distance:
1. P to R: The distance from P to R is 2 km.
• This is indeed the shortest distance listed between P and R in the matrix.
2. R to Q: The distance from R to Q is 4 km.
16
• This is also the shortest distance between Q and T.
4. T to S: The distance from T to S is 1 km.
Option (C): P, R, Q, T, S is the most efficient order for traveling past all cities on the circular
highway. It consistently uses the shortest paths listed in the table between consecutive cities,
aligning with the layout of the circular path around the lake.
Thus, option (C) is indeed the correct answer.
17
QUESTION-7
You and a friend are eating a block of chocolate by taking alternate bites - you take the first bite,
your friend takes the second bite, you take the third bite, and so on.
Not all bites are the same size. To take a bite, you must choose a square and eat every square
above it and/or to the right (including the square you chose). For example, consider the block
illustrated below.
Here you take the first bite by choosing square 15 and eating everything above and to the right
of it, which is just squares 15 and 16. Your friend then chooses square 8, thereby eating squares
8 and 12. You choose square 10, your friend chooses square 2, you choose square 5 and your
friend finishes the block by choosing square 1.
As it happens, square 1 is a new experimental broccoli flavour that neither of you wants to eat.
Suppose the chocolate has been reduced to the 3×3 block below, and it is your turn to take a
bite. Only one of the following squares will allow you to force your friend to eventually take
square 1-which should you choose?
1. Rule of Bites: When taking a bite, the player eats the selected square and all squares
above it and to the right.
2. Objective: We aim to choose a square such that our friend is eventually forced to choose
square 1, which is undesirable. If you select a square, all squares above it in the same
column and all squares to the right in the same row will be eaten. The goal is to take the
squares in such a way that your friend ends up with no option but to choose square 1.
18
Analyzing Each Choice
Let’s analyze each option to see if it can lead to a situation where your friend will be forced to
take square 1:
1. Square 5:
• If you select square 5, you will eat squares 5, 6, 7, 9, 10, and 11, leaving squares
1, 2, and 3.
• On your friend’s turn, they would then be forced to pick from squares 1, 2, or 3.
• No matter what your friend picks next, you can always select a square on your
next turn that leaves square 1 as the final option for them. Therefore, choosing
square 5 can guarantee that your friend ends up with square 1.
2. Square 6:
• If you select square 6, you will eat squares 6, 7, 10, and 11, leaving squares 1, 2,
3, 5, and 9.
• This move does not force your friend into a position where they must take square
1, so this option does not meet the objective.
3. Square 9:
• If you select square 9, you will eat squares 9, 10, and 11, leaving squares 1, 2, 3,
5, 6, and 7.
• This also does not force your friend into taking square 1, so it is not a valid choice.
4. Square 10:
• If you select square 10, you will eat squares 10 and 11, leaving squares 1, 2, 3, 5,
6, 7, and 9.
• This selection does not force your friend to eventually choose square 1, so it is
not a solution.
5. Square 11:
• If you select square 11, you will only eat square 11, leaving many other squares
(1, 2, 3, 5, 6, 7, 9, and 10).
• This does not create a scenario where your friend is forced to take square 1.
Conclusion
The best choice is (A) Square 5, as it forces your friend into a sequence of moves that will
eventually make them take square 1.
19
QUESTION-8
A pizza delivery boy must deliver pizzas to 11 houses in a lane, with one pizza for each house.
All houses are on the same side of the lane. His instructions read:
"Go to Mel's house, then go F3, B1, F6, B3, F1, B5, F1, F5, F1, B3",
where F3 means "go forward 3 houses", B1 means "go back 1 house", and so on.
Unfortunately, one of the instructions has been written down incorrectly. Where is the mistake?
(A) the 1st or 2nd instruction (D) the 7th or 8th instruction
(B) the 3rd or 4th instruction (E) the 9th or 10thinstruction
(C) the 5th or 6th instruction
Solution:
1. Start at Mel’s house (House 0).
2. F3: Move forward 3 houses, so now we are at House 3.
3. B1: Move back 1 house, so now we are at House 2.
4. F6: Move forward 6 houses, so now we are at House 8.
5. B3: Move back 3 houses, so now we are at House 5.
6. F1: Move forward 1 house, so now we are at House 6.
7. B5: Move back 5 houses, so now we are at House 1.
8. F1: Move forward 1 house, so now we are at House 2.
9. F5: Move forward 5 houses, so now we are at House 7.
10. F1: Move forward 1 house, so now we are at House 8.
11. B3: Move back 3 houses, so now we are at House 5.
By the end of these instructions, we end up at House 5, not having visited all 11 houses in a
logical progression, suggesting that one of the instructions is incorrect.
Notice that Instruction 7 (B5) takes us back from House 6 to House 1, which might be too far
back for the expected progression and might break the flow.
If we adjust Instruction 7 to something like B2 or B3 instead of B5, it would keep the movement
more continuous without large jumps. Therefore, the mistake is likely in:
(D) the 7th or 8th instruction.
20
EXPERIMENT-9
As everybody knows, girl spiders are difficult to distinguish from boy spiders. You believe you
can do this by using their size. For each spider colony you study, you declare that “all spiders
larger than x millimetres are girls, and all spiders smaller than x millimetres are boys.” Of course
you will be wrong some of the time; your task is to choose a value for x so that you make as few
errors as possible.
Each of the following scenarios describes a colony of spiders, giving the sizes of each boy and
girl spider in millimetres. For each scenario, you must choose a value of x that gives you as few
errors as possible (that is, the number of girl spiders of size < x plus the number of boy spiders
of size > x is as small as possible). This value of x will be your answer to the question.
The size of every spider is even; each of your answers must be an odd number. There will never
be more than one best possible answer.
Solution:
1. Combine the list of boy and girl spider sizes for each scenario and sort them in
ascending order.
2. Determine possible threshold values x, which are odd numbers between the sizes of
the boy and girl spiders.
3. For each possible x, calculate the number of classification errors:
Scenario 1:
21
• Sizes of girl spiders: 16, 20, 26, 28, 28, 30, 30, 30, 32
Analysis:
1. Sort the combined list of sizes: 12, 12, 14, 16, 18, 20, 22, 24, 26, 28, 28, 30, 30, 30, 32
2. Possible thresholds (odd numbers between groups): 15, 17, 19, 21, 23, 25, 27, 29, 31
3. Calculate errors for each threshold:
o x = 15: Errors = 1 (girl spider 16 classified as a boy)
o x = 17: Errors = 2 (boy spider 18 and girl spider 16)
o x = 19: Errors = 1 (girl spider 20)
o x = 21: Errors = 2 (boy spider 22 and girl spider 20)
o x = 23: Errors = 1 (girl spider 26)
o x = 25: Errors = 1 (boy spider 24)
o x = 27: Errors = 2 (boy spider 24 and girl spider 26)
o x = 29: Errors = 1 (girl spider 30)
o x = 31: Errors = 2 (boy spider 24 and girl spider 30)
The value of x=15x = 15x=15 results in the fewest errors (1 error), so:
• Answer for Scenario 1: x = 15
Scenario 2:
• Sizes of boy spiders: 14, 14, 14, 18, 18, 22, 26, 26
• Sizes of girl spiders: 16, 20, 20, 24, 24, 24, 28, 28, 28, 28
Analysis:
1. Sort the combined list of sizes: 14, 14, 14, 16, 18, 18, 20, 20, 22, 24, 24, 24, 26, 26, 28,
28, 28, 28
2. Possible thresholds (odd numbers between groups): 15, 17, 19, 21, 23, 25, 27
3. Calculate errors for each threshold:
o x = 15: Errors = 1 (girl spider 16)
o x = 17: Errors = 2 (boy spider 18 and girl spider 16)
o x = 19: Errors = 1 (girl spider 20)
22
o x = 21: Errors = 2 (boy spider 22 and girl spider 20)
o x = 23: Errors = 1 (girl spider 24)
o x = 25: Errors = 2 (boy spider 26 and girl spider 24)
o x = 27: Errors = 1 (girl spider 28)
The value of x=15x = 15x=15 gives the fewest errors (1 error), so:
• Answer for Scenario 2: x = 15
Scenario 3:
• Sizes of boy spiders: 16, 18, 20, 22, 26, 28, 32, 34, 40, 42
• Sizes of girl spiders: 24, 26, 30, 36, 38, 42, 46, 48, 48, 50
Analysis:
1. Sort the combined list of sizes: 16, 18, 20, 22, 24, 26, 26, 28, 30, 32, 34, 36, 38, 40, 42,
42, 46, 48, 48, 50
2. Possible thresholds (odd numbers between groups): 15, 17, 19, 21, 23, 25, 27, 29, 31,
33, 35, 37, 39, 41, 43, 45, 47, 49
3. Calculate errors for each threshold:
o x = 23: Errors = 1 (girl spider 24)
o x = 27: Errors = 1 (girl spider 28)
o x = 29: Errors = 1 (girl spider 30)
o x = 31: Errors = 2 (boy spider 32 and girl spider 30)
o x = 33: Errors = 1 (girl spider 36)
The value of x=23x = 23x=23 gives the fewest errors (1 error), so:
• Answer for Scenario 3: x = 23
Final Answers
1. Scenario 1: x = 15
2. Scenario 2: x = 15
3. Scenario 3: x = 23
23
QUESTION-10
(i) If three consecutive digits are palindromic (i.e., the first is the same as the third), then
all three digits can be removed. For instance, the sequence 163235 can be changed to
165.
(ii) Any digit except for 9 may be increased by one. For instance, 166725 could be changed
What is the least number of times that rule (ii) must be used in order to remove all the digits
Solution:
24
8. Fifth Application of Rule (ii)
• Increase the 0 to 1, resulting in the sequence: 111
• We now have a palindromic triplet 111.
9. Third Application of Rule (i)
• Remove the palindromic triplet 111, leaving the sequence empty.
In total, we applied Rule (ii) 5 times to create palindromic triplets, which were then removed
using Rule (i).
The minimum number of times that Rule (ii) must be used to remove all digits is: 5
25