Computational Thinking & Problem Solving

গণনা চিন্তা ও সমস্যা সমাধান — কোড লেখার আগে কীভাবে ভাবতে হয়

Read: ~25 min Beginner 5 practice problems Live code runner

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.

Computational thinking হলো বাস্তব জগতের একটি সমস্যাকে এমনভাবে প্রকাশ করার দক্ষতা, যাতে কম্পিউটার সেটি সমাধান করতে পারে। এটি কোনো নির্দিষ্ট প্রোগ্রামিং ভাষার উপর নির্ভর করে না — এটি নির্ভর করে আপনি কীভাবে চিন্তা করেন তার উপর। একজন অভিজ্ঞ প্রোগ্রামার Python-এ একটি লাইনও লেখার আগে সমস্যাটি ছোট ছোট অংশে ভেঙে নেন, সাধারণ প্যাটার্ন খুঁজে বের করেন, অপ্রয়োজনীয় বিষয়গুলো বাদ দেন এবং একটি অ্যালগরিদম খসড়া করেন।

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.

Decomposition Break a big problem into smaller pieces ছোট অংশে ভাঙা Pattern recognition Spot what repeats or looks similar প্যাটার্ন চেনা Abstraction Hide the noise, keep the essence অপ্রয়োজনীয় বাদ Algorithms A step-by-step recipe that works ধাপে-ধাপে পদ্ধতি Figure 2.1 — Computational thinking-এর চারটি স্তম্ভ।

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.

ধরুন, আপনাকে NID নম্বর valid কিনা পরীক্ষা করার program লিখতে বলা হলো। প্রথমে decompose — দৈর্ঘ্য, প্রতিটি digit, checksum — আলাদা করে ভাবুন। তারপর pattern চিনুন — শেষ digit সাধারণত checksum। এরপর abstract — NID-কে শুধু একটি digit string হিসেবে দেখুন। সবশেষে লিখুন একটি algorithm।

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.

Pseudocode হলো একটি কাল্পনিক সংক্ষিপ্ত লিখন — অর্ধেক ইংরেজি, অর্ধেক code — যা syntax-এর চিন্তা ছাড়াই algorithm বোঝাতে সাহায্য করে। এটিই মানবিক ধারণা এবং Python program-এর মধ্যে সেতু। ভালো pseudocode থেকে Python, Java, বা C — যেকোনো ভাষায় সহজে অনুবাদ করা যায়।

Example: find the largest number in a list.

pseudocode.txt
# 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:

largest.py
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.

Start Read n n % 2 == 0 ? Yes Print "Even" No Print "Odd" End Figure 2.2 — একটি সহজ flowchart: সংখ্যাটি even না odd?
even_odd.py
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.

Loop invariant হলো এমন একটি statement যা loop-এর শুরুতে প্রতিবার সত্য থাকে। invariant স্পষ্টভাবে বলতে পারলে, আপনার loop প্রায় নিশ্চিতভাবেই সঠিক। অভিজ্ঞ programmer-রা line-by-line execution-এর বদলে invariant-এর মাধ্যমে চিন্তা করেন।

Example: summing a list. The invariant is: after i iterations, total equals the sum of the first i elements.

sum_invariant.py
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-ONameExampleবাংলায়
O(1)ConstantLook up d[key]ইনপুটের আকার যাই হোক, একই সময়।
O(log n)LogarithmicBinary searchইনপুট দ্বিগুণ হলে কাজ মাত্র এক ধাপ বাড়ে।
O(n)LinearSum a listইনপুটের সমানুপাতিক সময়।
O(n log n)Linearithmicsorted()ভালো sort algorithm।
O(n²)QuadraticNested loopsপ্রতিটি জোড়া দেখা — ছোট ইনপুটে ঠিক, বড় ইনপুটে ধীর।
Rule of thumb On a modern CPU, Python does roughly 10 million simple operations per second. So an 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 (শব্দভাণ্ডার)

TermMeaningবাংলায়
AlgorithmA finite, step-by-step procedure that solves a problem.একটি নির্দিষ্ট ধাপে-ধাপে পদ্ধতি যা সমস্যা সমাধান করে।
AbstractionHiding irrelevant details, keeping only the essentials.অপ্রয়োজনীয় বিবরণ বাদ দিয়ে শুধু মূল বিষয় রাখা।
DecompositionBreaking a large problem into smaller sub-problems.বড় সমস্যা ছোট ছোট অংশে ভাঙা।
PseudocodeInformal description of an algorithm in near-English.প্রায়-ইংরেজিতে algorithm-এর অনানুষ্ঠানিক লিখন।
InvariantA condition that stays true throughout a loop.একটি শর্ত যা loop-এ সবসময় সত্য থাকে।
Big-ONotation for how an algorithm's work grows with input size.ইনপুট বাড়ার সাথে কাজ কতটা বাড়ে তার সংক্ষিপ্ত লিখন।

8. Practice Problems

  1. 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
  2. Translate the pseudocode from Problem 1 into working Python.
    Problem 1-এর pseudocode-কে চালু Python কোডে রূপান্তর করুন।
    ✨ Show Answer (উত্তর দেখুন)
    avg.py
    students = [("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}")
  3. 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 = ১ লাখ হলে ১০০০ কোটি — অনেক ধীর।

  4. 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, count equals the number of 'a' characters among those i characters.

    count_a.py
    word = "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}'")
  5. 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.

Computational thinking মানে সমস্যাকে এমনভাবে সমাধান করা, যেন কম্পিউটার তা চালাতে পারে — ভাঙুন, প্যাটার্ন চিনুন, অপ্রয়োজনীয় বাদ দিন, algorithm লিখুন। pseudocode ও flowchart কোডের আগে, invariant কোডের সময়, এবং Big-O কোডের পরে।

Next Module → Setup — Python, pip, venv, IDE — আপনার লেখার পরিবেশ তৈরি।