1.
Write down driving directions from your school to your home with the
precision required from an algorithm's description
ANS:
Step 1:
From any location on the school campus, locate the E-Library building.
Step 2:
Turn your back to the entrance of the E-Library and face the opposite direction.
Step 3:
Drive forward until you reach the Main Gate lantern monument.
Step 4:
Turn right and drive forward until you are approximately 25 meters away from a monument of two
hands clasped together.
Step 5:
Observe a street to your right and drive into it until you reach a T-junction.
Step 6:
Turn right at the T-junction and drive until you reach a transformer.
Step 7:
Observe a street next to the transformer and drive into it until you reach a caged shop beside a red gate.
Step 8:
Stop.
2. Write down the recipe for cooking your favourite dish with precision
required by an algorithm.
ANS:
Step 1: Prepare the Ingredients
Wash and slice the Afang leaves and water leaves. Set aside.
Soak the dried fish or stockfish in warm water for 10 minutes to soften. Rinse and debone if necessary.
Wash the assorted meat thoroughly and place it in a pot.
Step 2: Cook the Meat
Add chopped onions, seasoning cubes, and salt to the pot with the meat.
Add enough water to cover the meat and cook on medium heat for 20-30 minutes or until tender.
Add the soaked dried fish or stockfish to the pot and cook for an additional 5 minutes.
Remove the cooked meat and fish from the pot and set aside. Reserve the stock.
Step 3: Prepare the Soup Base
In the same pot, add palm oil and heat for 1-2 minutes.
Add the ground crayfish and scotch bonnet peppers (if using). Stir and cook for 2 minutes.
Pour the reserved stock into the pot and bring to a boil.
Step 4: Add the Vegetables
Add the sliced water leaves to the pot and cook for 5 minutes.
Add the shredded Afang leaves and stir well.
Add the cooked meat and fish back into the pot. Stir and cook for another 5-7 minutes.
Taste and adjust seasoning if necessary. Simmer for 2-3 minutes, then turn off the heat.
Step 5: Prepare the Fufu
In a clean pot, bring water to a boil.
Gradually add cassava flour or fufu powder to the boiling water while stirring continuously to avoid
lumps.
Reduce the heat and continue stirring until the mixture thickens and forms a smooth, stretchy dough.
Turn off the heat and mold the fufu into serving-sized balls.
Step 6: Serve
Serve the Afang soup hot with the fufu.
3. Find the gcd(31415, 14142) by applying Euclid's algorithm
ANS:
STEP 1: Divide the larger number by the smaller number and find the remainder:
31415 ÷ 14142 = 2 with a remainder of 31415 - 2 × 14142 = 3131
So, gcd(31415, 14142) = gcd(14142, 3131).
STEP 2: Now, divide 14142 by 3131:
14142 ÷ 3131 = 4 with a remainder of 14142 - 4 × 3131 = 14142 - 12524 = 1618
So, gcd(14142, 3131) = gcd(3131, 1618).
STEP 3: Divide 3131 by 1618:
3131 ÷ 1618 = 1 with a remainder of 3131 - 1 × 1618 = 1513
So, gcd(3131, 1618) = gcd(1618, 1513).
STEP 4: Divide 1618 by 1513:
1618 ÷ 1513 = 1 with a remainder of 1618 - 1 × 1513 = 105
So, gcd(1618, 1513) = gcd(1513, 105).
STEP 5: Divide 1513 by 105:
1513 ÷ 105 = 14 with a remainder of 1513 - 14 × 105 = 1513 - 1470 = 43
So, gcd(1513, 105) = gcd(105, 43).
STEP 6: Divide 105 by 43:
105 ÷ 43 = 2 with a remainder of 105 - 2 × 43 = 105 - 86 = 19
So, gcd(105, 43) = gcd(43, 19).
STEP 7: Divide 43 by 19:
43 ÷ 19 = 2 with a remainder of 43 - 2 × 19 = 43 - 38 = 5
So, gcd(43, 19) = gcd(19, 5).
STEP 8: Divide 19 by 5:
19 ÷ 5 = 3 with a remainder of 19 - 3 × 5 = 19 - 15 = 4
So, gcd(19, 5) = gcd(5, 4).
STEP 9: Divide 5 by 4:
5 ÷ 4 = 1 with a remainder of 5 - 1 × 4 = 1
So, gcd(5, 4) = gcd(4, 1).
STEP 10: Divide 4 by 1:
4 ÷ 1 = 4 with a remainder of 4 - 4 × 1 = 0
So, gcd(4, 1) = 1.
4. Estimate how many times faster it will be to find the gcd of (31415,
14142) by Euclid's algorithm compared with the algorithm based on
checking consecutive integer from minimum of m and n down to gcd of m
and n.
ANS:
To estimate how many times faster Euclid's algorithm is compared to the consecutive integer checking
method for finding gcd(31415, 14142), we analyze the time complexity of both algorithms:
1. Euclid's Algorithm:
- Time complexity: O(log(min(m, n))).
- For gcd(31415, 14142), the number of steps is proportional to log(14142), which is approximately
9-10 steps (as seen in the earlier calculation).
2. Consecutive Integer Checking:
- Time complexity: O(min(m, n)).
- For gcd(31415, 14142), the worst case requires checking all integers from 14142 down to 1, which
is 14142 steps.
Estimate of Speedup:
- The speedup is approximately the ratio of the number of steps in the consecutive integer method to
the number of steps in Euclid's algorithm.
- Speedup ≈ 14142 / 10 ≈ 1414.
Conclusion:
Euclid's algorithm is roughly **1400 times faster** than the consecutive integer checking method for
finding gcd(31415, 14142).
5. A peasant finds himself on the river bank with a goat, a wolf and a head
of cabbage, he needs to transport all three to the other side of the river in
his boat., (However, the boat has room for only the peasant himself and
one other item, either the goat, the wolf or the cabbage)
In his absence the wolf will eat the goat, and the goat will eat the cabbage.
Solve this problem for the peasant or prove it has no solution.
Note, the peasant is a vegetarian but does not like cabbage and hence,
can't eat the wolf not the cabbage to help him solve the problem. And it
goes without saying that the wolf is a protected species.
ANS:
The peasant takes the goat across the river and leaves it on the other side.
The peasant returns alone to the original side.
The peasant takes the wolf across the river and leaves it on the other side.
The peasant brings the goat back to the original side.
The peasant takes the cabbage across the river and leaves it on the other side.
The peasant returns alone to the original side.
The peasant takes the goat across the river again.
Now, all three (the peasant, the goat, the wolf, and the cabbage) are safely on the other side of the river
without any of them being eaten.
This sequence ensures that:
The wolf and goat are never left alone together.
The goat and cabbage are never left alone together.
6. There are four people who want to cross a rickety bridge and they all
began on the same side, you have 17 minutes to get them all across to the
other side, it is night, and they have one flashlight. A maximum of two
people can cross at one time. Any party that crosses, either one or two
people must have a flashlight with them, the flashlight must be walked
back and forth, it cannot be thrown.
For example, person 1 takes 1 minute to cross the bridge, person 2 takes 2
mins, person 3 takes 5 minutes and person 4 need 10minutes.
A pair must walk together at the rate of the slower person's pace.
ANS:
Person 1 and Person 2 cross together (time taken: 2 minutes).
Total time elapsed: 2 minutes.
Flashlight is on the other side.
Person 1 returns with the flashlight (time taken: 1 minute).
Total time elapsed: 3 minutes.
Flashlight is back on the original side.
Person 3 and Person 4 cross together (time taken: 10 minutes).
Total time elapsed: 13 minutes.
Flashlight is on the other side.
Person 2 returns with the flashlight (time taken: 2 minutes).
Total time elapsed: 15 minutes.
Flashlight is back on the original side.
Person 1 and Person 2 cross together again (time taken: 2 minutes).
Total time elapsed: 17 minutes.
Flashlight is on the other side.
Now, all four people are on the other side of the bridge in 17 minutes, which is within the time limit.
This sequence ensures that the slowest people (Person 3 and Person 4) cross together only once, and the
faster people (Person 1 and Person 2) handle the flashlight returns efficiently.
7. Describe the standard algorithm used by your favourite ATM machine in
dispensing cash(English or pseudo code)
ANS:
Using Pseudo-code
BEGIN
WHILE card_inserted DO
PROMPT "Enter PIN"
READ entered_pin
IF entered_pin == stored_pin THEN
BREAK
ELSE
INCREMENT pin_attempts
IF pin_attempts >= 3 THEN
RETAIN_CARD
DISPLAY "Too many incorrect attempts. Card retained."
EXIT
END IF
END IF
END WHILE
DISPLAY_MENU
SELECTED_OPTION = READ "Select option"
IF SELECTED_OPTION == "Withdraw Cash" THEN
WHILE TRUE DO
PROMPT "Enter amount"
READ amount
IF amount % denomination != 0 OR amount > account_balance OR
amount > atm_cash THEN
DISPLAY "Invalid amount. Try again."
ELSE
BREAK
END IF
END WHILE
DISPENSE_CASH(amount)
UPDATE_ACCOUNT_BALANCE(amount)
UPDATE_ATM_CASH(amount)
EJECT_CARD
PROMPT "Do you want a receipt?"
IF receipt_requested THEN
PRINT_RECEIPT
END IF
DISPLAY "Thank you for using our ATM."
END IF
END