Lesson 5/48 ยท ๐ง The Programmer's Mindset
๐ง The Programmer's MindsetLesson 5/48
Phase 0 ยท The Programmer's Mindset15 min
How to Read a Problem
Before writing a single line of code, learn to dissect any problem statement
Every programming problem is a puzzle. The #1 mistake beginners make is jumping straight to code without understanding what they're actually being asked to do.
Great programmers spend more time *reading* than *typing*.
Great programmers spend more time *reading* than *typing*.
The 4 questions to ask every time you see a problem:1. What is the input?, What information am I given?2. What is the output?, What do I need to produce?3. What are the constraints?, Any limits or special rules?4. What are the edge cases?, What are unusual or tricky situations?
Example: *"Write a program that takes two numbers and prints their sum."*
Let's apply the 4 questions:Input: Two numbers Output: Their sum printed to the screen Constraints: None mentioned Edge cases: What if they're negative? Decimals? Zero?
Now you understand the problem completely before writing any code.
Let's apply the 4 questions:
Now you understand the problem completely before writing any code.
Always draw an example first. If input is
5 and 3, what should output be? Write it down: 5 + 3 = 8. Seems obvious, but doing this for every problem prevents 90% of bugs.Practice Exercises
0/2 solvedExercise 1 of 2easy
โฑ 00:00Add Two Numbers
Write a function
Before coding: What is the input? What is the output? Write it in a comment first.
add(a, b) that returns the sum of two numbers.Before coding: What is the input? What is the output? Write it in a comment first.
solution.py
1 / 2
Exercise 2 of 2easy
โฑ 00:00Is It Even?
Write a function
Hint: A number is even if the remainder when divided by 2 is 0. In Python, use
is_even(n) that returns True if n is even, False otherwise.Hint: A number is even if the remainder when divided by 2 is 0. In Python, use
% for remainder.solution.py
2 / 2
Solve all 2 exercises to unlock completion