Q.For some calculations, we want an algorithm that accepts only positive integers upto 100.
Accept_1to100_Algo
INPUT Number
IF (0<= Number) AND (Number <= 100)
ACCEPT
Else
REJECT
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The check 0 <= Number <= 100 wrongly accepts 0 (not positive) and wrongly accepts fractions like 2.5 (not integers); the corrected condition is "Number is an integer AND 1 <= Number <= 100".
The idea (matching the condition to the specification). The specification is precise: accept positive integers up to 100, i.e. exactly the whole numbers 1, 2, …, 100. Verifying an algorithm means comparing what the condition actually admits against that set — every extra value admitted is a failure case.
a) On what values does it fail?
| Input | Algorithm's verdict | Correct verdict | Failure? |
|---|---|---|---|
| 0 | 0 <= 0 <= 100 → ACCEPT | reject (0 is not positive) | yes |
| 2.5 | 0 <= 2.5 <= 100 → ACCEPT | reject (not an integer) | yes |
| 99.99 | ACCEPT | reject (not an integer) | yes |
| -5 | 0 <= -5 false → REJECT | reject | no |
| 100 | ACCEPT | accept | no |
| 101 | REJECT | reject | no |
So the algorithm fails on 0 (the lower bound is wrong — it should start at 1) and on all non-integer values between 0 and 100 (there is no integrality check at all).
b) Improved algorithm
Accept_1to100_Algo (improved)
INPUT Number
IF (Number is an integer) AND (1 <= Number) AND (Number <= 100)
ACCEPT
Else
REJECT
Python verification
def accept(number):
if number == int(number) and 1 <= number <= 100:
return "ACCEPT"
return "REJECT" …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.