Hey Folks! 👋
Good Day.
Welcome back to Day 2 of my Python Problem Solving journey.
A few days ago, our trainer assigned us 15 Python programming problems . Today, I solved another three problems from that list.
One thing I've started realizing is that programming is less about syntax and more about problem-solving. Once the logic becomes clear, writing the code becomes much easier.
In this article, I'll walk through three Python problems I solved and share the thinking process behind each solution.
Problem 1: Factorial of a Number
Understanding the Problem
A factorial is the product of all positive integers from a given number down to 1.
For example:
5! = 5 × 4 × 3 × 2 × 1
Result:
120
Before writing any code, I asked myself:
If I were solving this manually on paper, what exact steps would I follow?
The answer became the algorithm itself.
- Start with 1.
- Multiply by the current number.
- Move to the next smaller number.
- Repeat until reaching 1.
Python Solution Using While Loop
no = 5
factorial = 1
while no > 0:
factorial = factorial * no
no -= 1
print(factorial)
Output
120
Pseudocode
START
number = 5
factorial = 1
WHILE number > 0
factorial = factorial × number
number = number - 1
END WHILE
PRINT factorial
STOP
Flowchart
Start
|
v
number = 5
factorial = 1
|
v
number > 0 ?
|
Yes
|
factorial = factorial * number
|
number = number - 1
|
+------> Repeat
|
No
|
Print factorial
|
End
Alternative Solution Using For Loop
no = 5
factorial = 1
for i in range(1, no + 1):
factorial = factorial * i
print(factorial)
Output
120
Key Takeaway
When you already know how many iterations are required, a for loop is usually cleaner and easier to read.
Problem 2: Print Odd Numbers First and Then Even Numbers
Expected Output
1 3 5 7 9 2 4 6 8 10
At first glance, most people would solve this using two separate loops:
- One loop for odd numbers
- One loop for even numbers
I wanted to see whether I could achieve the same result using a single loop.
My Thought Process
I imagined the output as two separate queues.
Odd numbers:
1 → 3 → 5 → 7 → 9
Even numbers:
2 → 4 → 6 → 8 → 10
The challenge wasn't generating the numbers.
The challenge was switching from the first sequence to the second without creating another loop.
Python Solution
no = 1
while no <= 10:
print(no, end=" ")
no += 2
if no == 11:
no = 2
Output
1 3 5 7 9 2 4 6 8 10
How the Logic Works
The loop starts at 1 and keeps incrementing by 2.
This generates:
1 3 5 7 9
After printing 9:
no += 2
changes the value to:
11
At that point:
if no == 11:
no = 2
resets the sequence and begins printing even numbers.
A small condition completely changes the direction of the program.
Pseudocode
START
number = 1
WHILE number <= 10
PRINT number
number = number + 2
IF number == 11
number = 2
END WHILE
STOP
Flowchart
Start
|
v
number = 1
|
v
number <= 10 ?
|
Yes
|
Print number
|
number = number + 2
|
number == 11 ?
/ \
Yes No
|
number = 2
|
+------> Repeat
|
No
|
End
Key Takeaway
Sometimes a simple condition can eliminate the need for an additional loop.
Always ask yourself:
Can I simplify the solution without making it harder to understand?
Problem 3: Perfect Number Program in Python
What Is a Perfect Number?
A perfect number is a number whose proper divisors add up exactly to the original number.
Example:
6
Divisors:
1, 2, 3
Sum:
1 + 2 + 3 = 6
Since the sum equals the original number, 6 is called a Perfect Number.
Real-World Analogy
Imagine a group of friends contributing money to buy a gift.
If the total contribution exactly matches the target amount, everything balances perfectly.
That's exactly how a perfect number works.
The sum of all valid contributors (divisors) equals the target amount (the number itself).
Python Solution
no = 496
sum = 0
i = 1
while i < no:
if no % i == 0:
sum += i
i += 1
if sum == no:
print("Perfect")
else:
print("Not Perfect")
How the Logic Works
The program checks every number from 1 to n - 1.
If a number divides the original number without leaving a remainder:
no % i == 0
it gets added to the running total.
After all checks are complete, we compare:
sum == no
If both values match, the number is perfect.
Dry Run Using 6
| Divisor | Added? | Running Sum |
|---|---|---|
| 1 | Yes | 1 |
| 2 | Yes | 3 |
| 3 | Yes | 6 |
| 4 | No | 6 |
| 5 | No | 6 |
Final Check:
6 == 6
Result:
Perfect Number
Pseudocode
START
number = n
sum = 0
FOR each value from 1 to n - 1
IF value divides number
sum = sum + value
END FOR
IF sum equals number
PRINT Perfect Number
ELSE
PRINT Not Perfect Number
STOP
Flowchart
Start
|
v
sum = 0
i = 1
|
v
i < number ?
|
Yes
|
number % i == 0 ?
/ \
Yes No
|
sum += i
|
i += 1
|
+------> Repeat
|
No
|
sum == number ?
/ \
Yes No
| |
Perfect
|
Not Perfect
What I Learned from These Problems
Although these programs are small, they helped me strengthen some important programming fundamentals:
- Loops
- Conditional Statements
- Mathematical Logic
- Pattern Generation
- Problem Decomposition
- Dry Running Code
* Converting Logic into Programs
My Current Problem-Solving Workflow
Problem
↓
Understand the Requirement
↓
Break It Into Smaller Steps
↓
Write Pseudocode
↓
Code
This workflow has helped me reduce mistakes and understand programming concepts much more deeply.
If you're new to this series, check out Part 1:
Top comments (0)