PE1 (AY21/22)
Problems
1. Herd
Note the and are valid vaccination rates. Nothing else. This is a "give-away" question.
2. Insert*
Using wishful thinking, to insert into at position , it is the same as inserting into at position , take the result, and append the last digit . So the function can be written as below:
There is one more complication, it does not work for negative numbers. But that can be easily fixed by changing the sign of the input digit d
.
3. Fraction
This problem is an excellent example to test your ability to divide a problem into several "sub-problems" to solve.
See Prof Ooi Wei Tsang's comment. The idea is similar.
Some Useful functions
The greatest common divisor (gcd), which is also known as the greatest common factor.
The least common multiple (lcm)
The iterative way to get the number of digtis of a given number
4. Almost
5. Stone
The tricker recursion question, I haven't solved it at first time because I ignore that I don't need to print 1 or 2 only. I can treate the path a number and right shift it!!!
In this problem, we just need to know how to form each "path" (treat it as a number). And the easiest way is to append the path at the last digit, which should be path * 10 + 1
or path * 10 + 2
. The final code should be following:
Tips
Last updated