Lab 1 Questions
Lab 1 Questions
● All your methods must be written in one single .java or .py or .pynb file. DO NOT
CREATE separate files for each task.
● If you are using JAVA, you must include the main method as well which should test
your other methods and print the outputs according to the tasks.
● If you are using PYTHON, then follow the coding templates shared in this folder.
NOTE:
● YOUR CODE SHOULD WORK FOR ANY VALID INPUTS. [Make changes to
the Sample Inputs and check whether your program works correctly]
● Use Numpy array to initialize the array. You are allowed to use the shape
parameter of the numpy array as well.
1. Merge Lineup:
Now you guys are playing a 2v2 fierce pokemon battle. At one point your and
your teammate's pokemons have very low hp. So you created a new rule to merge
the hp of your and your teammate's pokemons and create a new pokemon lineup.
But the opposite team placed a special condition. You and your teammate have to
add the hp of your team's pokemons from opposite directions. That is, your first
pokemon's hp will be added to your teammate's last pokemon's hp; your second
pokemon's hp will be added to your teammate's second last pokemon's hp and so
on and so forth. The None elements denote 0 hp. You and your teammate have the
same number of pokemons. Implement the scenario using a proper method which
takes your and your teammate's pokemon hp as two arrays (as parameters); and
returns the resulting arr.
Sample Output:
[4,17,6,27,2]
Explanation:
4+None(0) = 4 {pokemon_1[0]+pokemon_2[4]},
5+12 = 17 {pokemon_1[1]+pokemon_2[3]},
-1+7 = 6 {pokemon_1[2]+pokemon_2[2]},
None(0)+27 = 27 {pokemon_1[3]+pokemon_2[1]},
None(0)+2 = 2 {pokemon_1[4]+pokemon_2[0]}
2. Discard Cards:
You and your friends are playing a card game. You pick n numbers of UNO cards
from the deck. The rule of the card game is- one person says a number, t and you
have to discard the alternative cards with the same number from your hand
without changing the relative position of other cards. Implement the scenario using
a proper method which takes the cards in your hands as an array and a number as
parameters; and returns the resulting array. The empty spaces are denoted as 0 in
the array.
Start from deleting the first t.
Explanation: Explanation:
[1,3,7,20,5,21,22,23,0] [50, 51, 52,0,0]
Starting from the first 2, the Starting from the first 5, the
alternative 2’s are 20 and 22. alternative 5’s are 50 and 52.
After removing them, the After removing them, the
resulting array is - resulting array is -
[1,3,7,5,2,2,0,0,0] [5,0,0,0,0]
3. Decryption Process:
Suppose you're working as a cryptographer for a secret intelligence agency. You
are given an encrypted matrix as an input. You have to decrypt it after some processing of
the given matrix.
To decrypt this message efficiently, you have a task to construct a method called
decrypt_matrix(matrix) which takes an encrypted matrix as input and returns a
decrypted linear array. The process of finding the decrypted linear array is given below:
You have to find out the column-wise summations for each column and store the
difference of subsequent column-wise summations in a new linear array.
OR
Now suppose you are given a 2D array which resembles the tiled floor.
Each tile has a number. Can you write a method that will print your
walking sequence on the floor?
Constraint: The first tile of the room is always black.
Hint: Look out for the number of rows in the matrix [Notice the
transition from column 0 to column 1 in the above figures]
Sample Input Sample Output
-------------------- 391
|3|8|4|6|1| 12
-------------------- 472
|7|2|1|9|3| 49
-------------------- 186
|9|0|7|5|8|
--------------------
|2|1|3|4|0|
--------------------
|1|4|2|8|6|
--------------------
-------------------- 39
|3|8|4|6|1| 12
-------------------- 47
|7|2|1|9|3| 49
-------------------- 18
|9|0|7|5|8|
--------------------
|2|1|3|4|0|
--------------------
5. Row Rotation Policy of BRACU Classroom:
You are no longer permitted to choose your own seat on the new campus of
BRACU. You must abide by the new regulations, which state that if you sit in the
first row for the first week, you must shift to the second row for Week2, the third
row for the week3, and so on. In your classroom, there are a total of 6 rows and 5
columns. Your friend “AA” wants to know from you that on the upcoming exam
week in which row he will be in. Your task is to implement a function
row_rotation(exam_week, seat_status) that takes the exam_week and a 2D array
of current seat status as input and returns the row number in which your friend
“AA” will be seated and print the seat status for that week.
A B C D E Z AA BB CC DD
F G H I J A B C D E
K L M N O F G H I J
P Q R S T K L M N O
U V W X Y P Q R S T
6. Matrix Compression:
7. Game Arena:
Suppose you and your friends are in the world of ‘Alice in Borderland’
where you decided to take part in a game and entered the game arena.
As a team, you need to gain at least 10 points in order to keep surviving
in the borderland. Otherwise, you will be out of the game and your team
will be banished for good. Now, the arena has a 2D array like structure
where players of a team are given certain positions with values that are
multiples of 50. By staying in these positions, every player can gain
points from the cells above, below, left and right (not diagonally) only if
those cells contain 2 [The cells containing 1s and 0s are to be avoided].
For each player, add from these cells containing 2s to your total points
for the team to keep on surviving in borderland. Be careful about
corner cases. Your task is to write a method which tells us whether
your team is out or has survived the game.
Sample Input 1 Sample Output 2
Explanation:
Player with value 50 has 2 in the cell below him (1 cell). Player with value 100 has 2 in
the cell above and in the right cell (2 cells). So in total, they got (1+2)*2 = 6 points
which was not enough to survive the game.
Explanation:
Player with value 50 has 2 in the cell above, cell below and the right cell (3 cells).
Player with value 100 has 2 in the cell above and the cell below (2 cells). Player with
value 200 has 2 in the above cell and right cell (2 cells). So in total, they gained
(3+2+2)*2 = 14 points and survived the game.
Note: For the cell with value 2 that is common between 2 players in position (2,1), both
gained 2 points each, so it's not like if one player already added those 2 points, another
player cannot.