Week-2, PPAs - Answers
This document contains the answers to the Week-2 PPAs.
PPA-1
An incomplete outline of the solution is given below:
if x > 0:
print('positive')
Can you complete it?
Answer: The solution can be completed by adding another condition to check for when x is less than 0 (negative) or equals 0:
if x > 0:
print('positive')
elif x < 0:
print('negative')
else:
print('zero')
PPA-2
Real numbers are represented as float values. So, you would have to accept the input as follows:
x = float(input())
The output in this case is the value of $f(x)$, which is again to be treated as a float value according to the question. Here is an incomplete code snippet that you can use to complete the solution:
if 0 < x < 10:
print(x + 2)
• If you use print statements within the conditional blocks, like the way it is shown here, how many of them would you require to solve this problem?
• Is there a way to solve this problem that requires just one print statement? Can you come up with that solution if it exists?
Answer: We can have multiple print statements in each condition, but we can solve this with just one print statement by using if-elif-else structure and directly printing the result:
if 0 < x < 10:
print(x + 2)
elif x >= 10:
print(x * 2)
else:
print('out of range')
PPA-3
How is this problem related to the previous problem? Do they have the same structure?
Think about the following code snippets:
Snippet-1
if T < 0:
print('INVALID')
elif 0 <= T <= 5:
print('NIGHT')
Snippet-2
if T < 0:
print('INVALID')
if 0 <= T <= 5:
print('NIGHT')
Come up with two different solutions to the problem, one that develops on snippet-1 and the other which develops on snippet-2. What is the difference between these two solutions that you arrive at? Which one is better?
Answer: In Snippet-1, the second condition will be checked only if the first one fails. In Snippet-2, the second condition is checked regardless of the first. Snippet-1 is better as it prevents unnecessary checks and follows a logical flow:
if T < 0:
print('INVALID')
elif 0 <= T <= 5:
print('NIGHT')
else:
print('DAY')
PPA-4
For a point (x, y), you have to construct a table such as this:
| Condition | Result |
|---|---|
Both x and y are positive |
first |
x is positive and y is negative |
fourth |
Your task is twofold. First, complete this table. How many rows does it have? Secondly, convert the conditions into Python expressions. Now, you just have to write a sequence of conditional statements and print the appropriate string. Here is a follow-up question to help you with the second task:
Snippet-1
if x > 0:
if y > 0:
print('first')
Snippet-2
if x > 0 and y > 0:
print('first')
Snippet-3
if x > 0 or y > 0:
print('first')
Answer: The table should include the following conditions:
| Condition | Result |
|---|---|
Both x and y are positive |
first |
x is positive and y is negative |
fourth |
x is negative and y is positive |
second |
Both x and y are negative |
third |
The correct snippet to use is Snippet-2:
if x > 0 and y > 0:
print('first')
elif x > 0 and y < 0:
print('fourth')
elif x < 0 and y > 0:
print('second')
else:
print('third')
PPA-5
There are two aspects to this problem. One is mathematical, the other is computational. First, try to figure out the mathematical part. The questions you have to ask yourself are the following:
- For what collection of points does the line joining them become a vertical line?
- If a line is not vertical, then how does one compute the slope?
- What is the slope of a horizontal line?
Once you have answered these questions, you have all the ingredients necessary to solve the problem computationally. Some of the steps in the computational process:
- Accept inputs as real numbers. You know how to do this from PPA-2.
- Calculate the slope.
- Find the y-coordinate of the point.
There are important details that I have deliberately missed out. These are for you to fill.
Answer: A vertical line occurs when the x-coordinates of two points are the same. The slope is computed as slope = (y2 - y1) / (x2 - x1). If x1 == x2, the line is vertical and the slope is undefined. If y1 == y2, the slope is 0 (horizontal line).
PPA-6
In case you are confused with the terminology, a period is another term for the full-stop.
This problem can be split into two parts:
- Get a string of odd length.
- Extract the required substring.
If the string is already of odd length, you don't have to do much. If not, then you have to follow the procedure given in the question. A small snippet is given to help you; where do you think you would find this useful?
if word[-1] == '.':
new_word = word[: -1]
Answer: This snippet helps remove the period if it exists at the end of the string. To find the middle character, you can use the index len(word) // 2.
if len(word) % 2 == 1:
middle = word[len(word)//2]
else:
word = word[:-1]
middle = word[len(word)//2]
PPA-7
The in keyword is a powerful tool. For example, to see if a string word1 is a substring of another string word2, you just need to type:
word1 in word2
If word1 is a substring of word2 then this expression evaluates to True. If not, it evaluates to False. Now, it is just a question of repeatedly applying this condition across the sequence.
Answer: You can use the in operator within a loop to check for multiple substrings. Example:
words = ['apple', 'banana', 'cherry']
for word in words:
if 'a' in word:
print(word)
PPA-8
The movement of the bishop on the chess board is given below (image borrowed from chess.com):
The bishop is capable of only diagonal moves. In the problem statement, you have to figure out if the bishop can move from start to end in exactly one move. So, you have to see if this movement is along a diagonal or not. What are the characteristics of a diagonal? Try to define it mathematically and the answer will jump out at you.
Answer: A bishop moves diagonally, which means that the difference in x-coordinates equals the difference in y-coordinates. You can check this with the condition:
if abs(x1 - x2) == abs(y1 - y2):
print("Diagonal move")
else:
print("Not a diagonal move")
PPA-9
There are three statements given to you here:
(a > 0) and (b > 0) and (c > 0)
(a != b) and (b != c) and (c != a)
a + b + c - n == 0
Now, make use of these three statements to construct a solution. Consider another set of three statements:
(a == 0) or (b == 0) or (c == 0)
(a == b) or (b == c) or (c == a)
a + b + c - n != 0
Now, make use of these three statements to construct another solution. How are these two solutions different from each other?
Answer: The first solution checks if all values are positive, all values are distinct, and their sum equals a constant. The second solution checks for zero values, equality between any two variables, and ensures the sum doesn't equal the constant. The difference is in the conditions for equality and the constraints on the sum.
PPA-10
The crux of this problem is to look at two cases. What happens when $x$ is positive? What happens when $x$ is negative? Here is another hint:
f = int(3.3)
print(f)
int(3.3) retains the integer part while truncating whatever comes after the decimal, and also the decimal. Try out the following:
f = int(-3.3)
print(f)
Answer: The function int() truncates the decimal part, so for positive values it gives the floor value, and for negative values, it truncates towards zero. This is equivalent to the floor function for positive values and the ceiling function for negative values.