What is Comp Sci? CS 160: Computer Science Orientation

advertisement

What is Comp Sci?

CS 160: Computer Science Orientation

Today’s Schedule

• What is Computer Science?

• Algorithms

• Hardware

• Software

• Computational Problem Solving

• Python

• Live Coding

1

April 12, 2020

What is Computer Science?

• Not math

2

April 12, 2020

What is Computer Science?

• Not “just” programming

3

April 12, 2020

What is Computer Science?

• Computers are only tools

“Computer science is no more about computers than astronomy is about telescopes.”

- Dijkstra

4

April 12, 2020

What is Computer Science?

• Computer Science = computational problem solving

• What is computation?

• Any calculation that follows a well-defined model (e.g., algorithm)

• To be solved computationally, a problem needs:

Representation of all aspects of problem

Algorithm that solves problem based on representation

5

April 12, 2020

What is Computer Science

• Example problem: man, cabbage, goat, wolf

6

April 12, 2020

• Only one can cross at a time

Cabbage cannot be left alone with goat

Goat cannot be left alone with wolf

• How to solve this?

• Information must be abstracted. e.g., { M = E, C = E, G = E, W = E }

• Algorithm- e.g., “brute force” vs. “search”

7

April 12, 2020

What is Computer Science?

• There are limits to computation

• E.g., Traveling Salesman Problem

• Find shortest possible route of travel between set of cities

• “Brute force” approach (try all possible routes)

• 10 cities => 10! = ~3.5 million routes

• 20 cities => 20! = ~2.5 x 10 18 routes (2.5 quintillion!!)

• If computer could estimate 1,000,000 routes per second,

• It would take ~77,000 years to compute solution for 20 cities!!

8

April 12, 2020

Today’s Schedule

• What is Computer Science?

• Algorithms

• Hardware

• Software

• Computational Problem Solving

• Python

• Live Coding

9

April 12, 2020

Algorithms

Algorithm- finite number of clearly described, unambiguous, “doable” steps that can be systematically followed to produce a desired result for given input in a finite amount of time.

10

April 12, 2020

Algorithms

• Solve general problems

• E.g., algorithm to sort a list of numbers

• (should work for all lists of numbers)

• Computer is only as smart as the algorithm chosen

11

April 12, 2020

Algorithms

• Computer are fast and don’t make errors

• Great for algorithms!!

• Errors are human caused!!

12

April 12, 2020

Today’s Schedule

• What is Computer Science?

• Algorithms

• Hardware

• Software

• Computational Problem Solving

• Python

• Live Coding

13

April 12, 2020

Hardware

• Physical components of a computer

• Fundamental components:

• CPU

• main memory

• secondary memory

• input / output devices

• buses

14

April 12, 2020

CPU – Central Processing Unit

• “Brain” of computer

• Uses digital logic to interpret and execute instructions

15

April 12, 2020

Main Memory

• Where executing programs reside

• CPU accesses directly

• Fast

• Volatile (data lost on power cycle)

16

April 12, 2020

Secondary Memory

• Large, persistent storage

• Slower than main memory

• Non-volatile (data persists on power cycle)

17

April 12, 2020

Input / Output Devices

• Keyboard

• Mouse

• Monitor

• Printer

• etc.

18

April 12, 2020

Buses

• Connect everything together

19

April 12, 2020

Operating System

• Software that manages and interacts with hardware resources

• Acts as “middle man” between hardware and applications

• Also known as system software

20

April 12, 2020

Today’s Schedule

• What is Computer Science?

• Algorithms

• Hardware

• Software

• Computational Problem Solving

• Python

• Live Coding

21

April 12, 2020

Software

• Set of instructions, data, and documentations that can be executed by a computer

Application software fulfills a user’s needs

• Different from system software

• E.g., iTunes, iMovie, etc.

22

April 12, 2020

Syntax vs. Semantics

Syntax – set of characters and acceptable arrangements

Semantics – meaning associated with each syntactically correct sequence of characters

23

April 12, 2020

Syntax vs. Semantics

• Syntax errors

• Caused by invalid syntax

• E.g., prnt instead of print

• Semantic (logic) errors

• Errors in program logic

• Computers do not understand what program is meant to do

• Only follow instructions

24

April 12, 2020

Program Translation

• Translates human-readable source code into machine code

• Instructions in 1s and 0s

• Most programs written in a high-level language

• E.g., Python, C++, Java, Ruby, etc.

High-level language translated into machine code via:

• Compiler

• Interpreter

25

April 12, 2020

Compiler vs. Interpreter

• Compiler is translator program

• Translates source code into machine code for CPU

• Interpreter

• Executes program instructions in place of (“on top of”) CPU

• “Interactive” mode

26

April 12, 2020

Today’s Schedule

• What is Computer Science?

• Algorithms

• Hardware

• Software

• Computational Problem Solving

• Python

• Live Coding

27

April 12, 2020

Computational Problem Solving

• Four main steps:

1) Analyze

2) Design

3) Implement

4) Test

28

April 12, 2020

Computational Problem Solving

• Four main steps:

1) Analyze – understand problem

2) Design

3) Implement

4) Test

29

April 12, 2020

Computational Problem Solving

• Four main steps:

1) Analyze

2) Design – determine abstraction and algorithm

3) Implement

4) Test

30

April 12, 2020

Computational Problem Solving

• Four main steps:

1) Analyze

2) Design

3) Implement – write code

4) Test

31

April 12, 2020

Computational Problem Solving

• Four main steps:

1) Analyze

2) Design

3) Implement

4) Test – validate and verify code solves problem

32

April 12, 2020

Today’s Schedule

• What is Computer Science?

• Algorithms

• Hardware

• Software

• Computational Problem Solving

• Python

• Live Coding

33

April 12, 2020

Python

• Invested by Guido van Rossum in early 1990s

• Simple syntax

• Easy to read

• Very powerful

• Used by YouTube, Google, Yahoo, NASA, etc.

• Free

• Has many built-in modules

• e.g., import math

34

April 12, 2020

Python Basics

• Variables

• Arithmetic operators

• Input and output

35

April 12, 2020

Variables

• A name that is assigned to a value, e.g., n = 5

(variable n assigned to value 5)

• Variables can be used and reassigned, e.g.,

• n + 20 = ?

• n = 10

36

April 12, 2020

Arithmetic Operators

*

/

-

+ (addition)

(subtraction)

(multiplication)

(division)

2 + 5

2 - 5

2 * 5

2 / 5

** (exponentiation) 2**5

37

April 12, 2020

Input and Output (using Python 2.7) print( value )

• Python function to write output (i.e., value) to the terminal

value can be

• literal (e.g., 10, 44/2, “Hello”)

• Variable (e.g., name)

E.g., print(‘hello class!!!’)

38

April 12, 2020

Input and Output (using Python 2.7) retval = raw_input( prompt )

• Python function to read input from the user

• Value entered stored in retval

prompt is message displayed to user..

E.g., name = raw_input(“what is your name? “);

NOTE: The book uses Python 3.0, which is simply input()

39

April 12, 2020

Today’s Schedule

• What is Computer Science?

• Algorithms

• Hardware

• Software

• Computational Problem Solving

• Python

• Live Coding

40

April 12, 2020

Live Coding

• Variables

• Arithmetic operators

• Input and output

• Common errors

41

April 12, 2020

Download