Introduction to Computing, Problem Solving, and Program Design

Introduction to Computing and Problem Solving

  • General Overview: This course module serves as an introduction to basic methodology for solving problems using computer programs, specifically focusing on program design and the Python programming language.
  • Preparation Credits: Prepared by Suhaini Nordin, with specific acknowledgments to Ban Kar Weng (William) for original pedagogical materials.

Questions & Discussion

  • How do we communicate with the computer?: Through the use of programming languages.
  • How do we get computers to perform complicated tasks?: These tasks are broken down systematically into a sequence of small, manageable instructions.
  • Why Python?: Python is popular because it is powerful, easy to download, easy to write, and easy to read.
  • How did the language Python get its name?: It was named after the British comedy group "Monty Python."
  • What is IDLE?: IDLE stands for Integrated DeveLopment Environment. It includes the Python Shell and a text editor used by programmers to write and execute scripts.
  • What is an interpreted language?: A language that utilizes an interpreter, which translates high-level code into machine language one statement at a time and executes it immediately.
  • What are the meanings of the terms "programmer" and "user"?:
    • Programmer: A person who identifies problems and solves them by writing instructions for a computer.
    • User: Any individual who runs or interacts with a program created by a programmer.
  • What is the meaning of the term "code"?: Code refers to the specific Python statements written by the programmer to perform logic.
  • Are there certain characteristics that all programs have in common?: Yes, they all follow the fundamental sequence of Input, Processing, and Output (IPO).
  • What are the meanings of the terms "hardware" and "software"?:
    • Hardware: The physical, tangible components of the computer system.
    • Software: The digital instructions or program that runs on the hardware.
  • How are problems solved with a program?: By devising a step-by-step procedure designed to process data inputs and produce the desired output.
  • What is a zero-based numbering system?: A system where the counting or indexing sequence begins with the digit 00 rather than 11.
  • What are the prerequisites to learning Python?: Students should be familiar with computer file and folder management (operating system basics).

Program Development Cycle

  • The problem-solving process typically flows through three stages:

    1. Input: Receiving data into the system.
    2. Processing: Executing logic or calculations on the input data.
    3. Output: Displaying the results of the processing.
  • Detailed Program Planning Stages:

    • Analyze: Define the problem clearly. This involves asking questions and checking for all possible edge cases or scenarios.
    • Design: Plan the solution strategy. This involves writing pseudocode or drawing a flowchart to visualize logic before writing code.
    • Code: Translate the planned algorithm into a specific programming language (e.g., Python).
    • Test, Debug, and Correct: Locate, isolate, and remove errors (bugs) within the program to ensure it functions as intended.
    • Complete Documentation: Organize all materials, descriptions, and logic summaries that explain how the program works for future users or developers.

Program Design vs. Coding Example

  • Scenario: Checking if a provided number is positive, negative, or zero.
  • Solution Logic:
    • Ask the user to provide a number.
    • If the number is 00, it is identified as "zero."
    • If the number is greater than 00, it is identified as a "positive number."
    • If the number is less than 00, it is identified as a "negative number."
  • Design Steps:
    1. Get number.
    2. If number =0= 0 then print "Zero".
    3. If number >0> 0 then print "Positive number".
    4. If number <0< 0 then print "Negative number".

Problem-Solving Skill Test: Jack's Path

  • Objective: Guide a character named "Jack" from a starting point to a destination using only simple commands: Move forward, Turn left, Turn right, and Stop.
  • Logic Steps:
    1. Move forward 44 steps.
    2. Turn right.
    3. Move forward 44 steps.
    4. Turn left.
    5. Move forward 44 steps.
    6. Turn right.
    7. Move forward 22 steps.
    8. Turn right.
    9. Move forward 22 steps.
    10. Stop.

Programming Tools: Definitions and Symbols

  • Algorithm: A step-by-step procedure for solving a problem.

  • Pseudocode: An abbreviated version of actual computer code written in plain English. It replaces flowchart symbols with English-like statements, allowing the programmer to focus on logic rather than syntax.

  • Flowchart: A visual representation of an algorithm using standard symbols.

  • Flowchart Symbols:

    • Flowline: Represented by an arrow; indicates the direction of logic flow.
    • Terminal: Represented by a rounded rectangle (oval); indicates the Start or End of a program.
    • Input/Output: Represented by a parallelogram; used for reading input from the user or displaying output results.
    • Processing: Represented by a rectangle; used for calculations or data manipulation.
    • Decision: Represented by a diamond; used for branching logic (Yes/No or True/False conditions).
    • Connector: Represented by a small circle; used to connect different parts of a flowchart across pages or sections.
    • Annotation: Used for adding descriptive notes or comments within the flowchart.

Case Study: The Postage-Stamp Problem

  • Problem: Determine the number of stamps needed for a letter.
  • Rule of Thumb: Provide 11 stamp for every 55 sheets of paper.
  • Algorithm steps:
    1. Get the total sheets of paper.
    2. Divide the number of sheets by 55.
    3. Round the quotient up to the next whole number; store this as the number of stamps.
    4. Display final stamp count.
  • Processing breakdown for 16 sheets:
    • Input: 1616
    • Processing: 165=3.2\frac{16}{5} = 3.2
    • Process (Rounding): Rounding 3.23.2 up gives 44.
    • Output: 44
  • Pseudocode:
    • Read Sheets
    • Set stamps = Sheets / 55
    • Round stamps up to the next whole number
    • Display stamps

Case Study: New York City Street Directions

  • Objective: Determine if a one-way street in NYC runs Eastbound or Westbound based on the street number.
  • Rule: Even-numbered streets run Eastbound; odd-numbered streets run Westbound.
  • IPO Breakdown:
    • Input: Street number (SS).
    • Processing: Check if SS is divisible by 22 (i.e., check if S(mod2)=0S \pmod 2 = 0).
    • Output: "Eastbound" if even, "Westbound" if odd.
  • Pseudocode:
    • Get street
    • if street is even
      • Display Eastbound
    • else
      • Display Westbound

Case Study: Class Average Calculation

  • Objective: Calculate and report the average grade for a class.
  • Logic Required:
    • A loop is needed to read grades and accumulate a sum.
    • A counter is needed to track the number of students.
  • IPO Breakdown:
    • Input: Student grades.
    • Processing:
      • Initialize Counter=0Counter = 0 and Sum=0Sum = 0.
      • While data exists: Add grade to Sum, add 11 to Counter.
      • Average=SumCounter\text{Average} = \frac{\text{Sum}}{\text{Counter}}
    • Output: Average grade.
  • Pseudocode:
    • Initialize Counter and Sum to 00
    • while there are more data
      • Get the next Grade
      • Increment the Counter
      • Add the Grade to the Sum
    • Compute Average = Sum / Counter
    • Display Average

The Python Language

  • History: Conceived by Guido van Rossum in the 1980s. Its name originates from the "Monty Python’s Flying Circus" comedy sketch show.
  • Growth Drivers:
    • Simplicity and development speed.
    • Significant adoption by the Data Science and Machine Learning communities.
    • Availability of extensive libraries that extend core functionality.
  • Industry Use: Major companies using Python include Spotify, Facebook, Amazon, Google, and YouTube.
  • Example Code (Factorial Calculation):
    • File: factorial.py
    • Logic:
      • F=1F = 1
      • N=int(input("Enter N: "))N = \text{int}(\text{input}("\text{Enter N: }"))
      • for i in range(1,N+1):\text{for } i \text{ in range}(1, N+1):
        • F=F×iF = F \times i
      • print(N,"!=",F)\text{print}(N, "! = ", F)
    • Console Result: If input is 55, output is 5!=1205! = 120.

Language Translators

  • Translator: A tool that translates high-level programming language (Basic, C++, Java, Python, etc.) into machine language (1s1s and 0s0s) so the computer can execute it.
  • Types of Translators:
    1. Interpreter:
      • Reads code, parses it, and executes instructions "on the fly."
      • Examples: Python, JavaScript, PHP.
    2. Compiler:
      • Translates the entire source program into machine language at once.
      • Saves the translation into a file (e.g., .exe or .dll) for subsequent execution.
      • Examples: C, C++.

Educational Tools for Programming

  • Blockly Games: A Google-supported visual programming tool (block-based) for learning logic (Puzzle, Maze, Logic, Loops, Math).
  • Lightbot: A game-based platform designed to teach sequencing, procedures, and loops visually without writing syntax.
  • Evolution of Languages: Mentions the shift from older languages (COBOL) to modern, widely accessible languages like Swift, Kotlin, Dart, and Python.