Computational Thinking & Problem Solving
গণনা চিন্তা ও সমস্যা সমাধান — কোড লেখার আগে কীভাবে ভাবতে হয়
1. What Is Computational Thinking?
Computational thinking is the skill of framing a real-world problem in a way a computer can solve. It has nothing to do with any particular programming language — it is about how you think, not how you type. Before a programmer writes a single line of Python, she has already decomposed the problem into smaller pieces, recognized patterns, abstracted away the unimportant details, and sketched an algorithm.
This module teaches five core habits: abstraction, decomposition, algorithmic thinking, pseudocode, and invariants. Master these, and every future module will feel natural.
2. The Four Pillars of Computational Thinking
Jeannette Wing — the computer scientist who coined the modern definition in 2006 — described four pillars. Every good solution uses all four.
A good example: suppose you are asked to build a program that checks whether a Bangladeshi National ID number is valid. You would decompose (separate length check, digit check, checksum); you would recognize patterns (the last digit is often a checksum, similar to credit cards); you would abstract (treat the ID as a string of digits, not as a person); and finally you would write an algorithm — a clear sequence of steps.
3. Pseudocode — Programming in English (and Bangla)
Pseudocode is a made-up shorthand — half English, half code — that lets you describe an algorithm without worrying about syntax. It is the bridge between a human idea and a Python program. A good pseudocode can be translated to Python, Java, or C with minimal effort.
Example: find the largest number in a list.
# Pseudocode — not Python yet
INPUT : a list of numbers called nums
OUTPUT : the largest number
set largest = first element of nums
for each number n in nums:
if n is greater than largest:
set largest = n
return largest
Now the same idea — translated to real Python:
def largest(nums):
largest_so_far = nums[0]
for n in nums:
if n > largest_so_far:
largest_so_far = n
return largest_so_far
print(largest([4, 19, 7, 42, 3, 17]))
4. Flowcharts — A Picture of Your Algorithm
A flowchart uses shapes to show the flow of control: rectangles for actions, diamonds for decisions, ovals for start/end. Even today, senior engineers sketch flowcharts on whiteboards before any coding happens.
n = 17
if n % 2 == 0:
print("Even")
else:
print("Odd")
5. Loop Invariants — The Secret of Correct Loops
A loop invariant is a statement that remains true every time the loop reaches the top. If you can state the invariant clearly, your loop is almost certainly correct. Great programmers think in invariants, not in line-by-line execution.
Example: summing a list. The invariant is: after i iterations, total equals the sum of the first i elements.
nums = [3, 1, 4, 1, 5, 9, 2, 6]
total = 0
for i, x in enumerate(nums):
total = total + x
# Invariant: total == sum of nums[0..i]
print(f"after {i+1} steps, total = {total}")
print(f"Final sum: {total}")
6. Estimating Time and Space
Before running on millions of items, a good programmer estimates how long the algorithm will take and how much memory it will use. The standard notation is Big-O — a measurement of how the work grows as the input grows.
| Big-O | Name | Example | বাংলায় |
|---|---|---|---|
O(1) | Constant | Look up d[key] | ইনপুটের আকার যাই হোক, একই সময়। |
O(log n) | Logarithmic | Binary search | ইনপুট দ্বিগুণ হলে কাজ মাত্র এক ধাপ বাড়ে। |
O(n) | Linear | Sum a list | ইনপুটের সমানুপাতিক সময়। |
O(n log n) | Linearithmic | sorted() | ভালো sort algorithm। |
O(n²) | Quadratic | Nested loops | প্রতিটি জোড়া দেখা — ছোট ইনপুটে ঠিক, বড় ইনপুটে ধীর। |
O(n²) algorithm
on n = 100,000 will take about 1000 seconds — too slow. Always estimate before coding.
একটি আধুনিক CPU-তে Python প্রায় ১ কোটি সহজ operation/সেকেন্ডে চলে। তাই n = ১ লাখ হলে O(n²) algorithm প্রায় ১০০০ সেকেন্ড নেবে — অনেক ধীর।
7. Vocabulary (শব্দভাণ্ডার)
| Term | Meaning | বাংলায় |
|---|---|---|
| Algorithm | A finite, step-by-step procedure that solves a problem. | একটি নির্দিষ্ট ধাপে-ধাপে পদ্ধতি যা সমস্যা সমাধান করে। |
| Abstraction | Hiding irrelevant details, keeping only the essentials. | অপ্রয়োজনীয় বিবরণ বাদ দিয়ে শুধু মূল বিষয় রাখা। |
| Decomposition | Breaking a large problem into smaller sub-problems. | বড় সমস্যা ছোট ছোট অংশে ভাঙা। |
| Pseudocode | Informal description of an algorithm in near-English. | প্রায়-ইংরেজিতে algorithm-এর অনানুষ্ঠানিক লিখন। |
| Invariant | A condition that stays true throughout a loop. | একটি শর্ত যা loop-এ সবসময় সত্য থাকে। |
| Big-O | Notation for how an algorithm's work grows with input size. | ইনপুট বাড়ার সাথে কাজ কতটা বাড়ে তার সংক্ষিপ্ত লিখন। |
8. Practice Problems
-
Write pseudocode (no Python yet) for: given a list of students and their marks, find the average mark.একটি pseudocode লিখুন (Python নয়): একদল ছাত্রের নাম ও নম্বর থেকে গড় নম্বর বের করার।
✨ Show Answer (উত্তর দেখুন)
INPUT : list of (name, mark) pairs OUTPUT : the average mark set total = 0 set count = 0 for each (name, mark) in list: total = total + mark count = count + 1 if count == 0: return 0 return total / count -
Translate the pseudocode from Problem 1 into working Python.Problem 1-এর pseudocode-কে চালু Python কোডে রূপান্তর করুন।
✨ Show Answer (উত্তর দেখুন)
avg.pystudents = [("Ayesha", 82), ("Rakib", 74), ("Nabila", 91), ("Tanvir", 66)] total = 0 count = 0 for name, mark in students: total += mark count += 1 average = total / count if count else 0 print(f"Average mark = {average:.2f}") -
Estimate the Big-O of this code: two nested for-loops over the same list of n items.একই list-এর উপর দুটি nested for loop-এর Big-O নির্ণয় করুন।
✨ Show Answer (উত্তর দেখুন)
Answer: The outer loop runs n times, and the inner loop runs n times for each outer iteration — so the total work is n × n =
O(n²). For n = 1,000 that is already a million operations; for n = 100,000 it is ten billion — far too slow. Whenever you see nested loops over the same data, think hard about whether a dict or set can reduce it to O(n).বাইরের loop n বার, ভেতরের loop প্রতিটি বাইরের iteration-এ n বার — মোট n × n =
O(n²)। n = ১০০০ হলে ১০ লক্ষ operation, n = ১ লাখ হলে ১০০০ কোটি — অনেক ধীর। -
State the loop invariant for a program that counts how many times the letter 'a' appears in a word.একটি শব্দে 'a' অক্ষর কতবার আছে তা গোনার loop-এর invariant বলুন।
✨ Show Answer (উত্তর দেখুন)
Invariant: After processing the first i characters of the word,
countequals the number of 'a' characters among those i characters.count_a.pyword = "banana" count = 0 for i, ch in enumerate(word): if ch == "a": count += 1 # Invariant: count == number of 'a' in word[0..i] print(f"'a' appears {count} times in '{word}'") -
Decompose the problem "build a library book return system" into at least four sub-problems."লাইব্রেরি বই ফেরত সিস্টেম" সমস্যাটি অন্তত ৪টি ছোট সমস্যায় ভাঙুন।
✨ Show Answer (উত্তর দেখুন)
Answer: (1) Identify the member — who is returning the book? (2) Look up the book — does the library have a record of the lending? (3) Check the return date — is it late, and is there a fine? (4) Update the inventory — mark the book as available again. (5) Send a receipt. Each sub-problem can be coded as a small function, then composed into a single
return_book()function.(১) সদস্য সনাক্ত (২) বই খোঁজা ও lending record (৩) ফেরতের তারিখ ও জরিমানা (৪) inventory আপডেট (৫) রসিদ প্রদান — প্রতিটি ছোট function হিসেবে লেখা যাবে।
Summary — Module 02
Computational thinking is the habit of solving problems in a way a computer can execute: decompose a big problem, recognize patterns, abstract away noise, and design an algorithm. Use pseudocode and flowcharts before coding; state loop invariants as you write; estimate time and space with Big-O. These habits will remain valuable for the rest of your career — long after today's Python syntax is forgotten.