+ All Categories
Home > Documents > Introduction to CS - Indian Institute of Technology Delhihimani/COL100/col100.pdf · Introduction...

Introduction to CS - Indian Institute of Technology Delhihimani/COL100/col100.pdf · Introduction...

Date post: 01-Dec-2018
Category:
Upload: vuongtram
View: 235 times
Download: 0 times
Share this document with a friend
22
Introduction to CS Not a study of computers but computation But we do study computers as well Computability How to formally specify and abstract problems And design and evaluate solutions Algorithms and data management Efficient computation
Transcript

Introduction to CS

• Not a study of computers but computation

• But we do study computers as well

• Computability • How to formally specify and abstract problems

• And design and evaluate solutions

• Algorithms and data management

• Efficient computation

What is an Algorithm

• Self-contained set of actionable steps that lead to a solution

• with order among (some) steps clearly spelled out

• Incomplete without

• an understanding of steps

• an understanding of input requirements

• an understanding of output requirements

R1←@R2

Von-Neumann Model

Registers

PC

Memory

0x1010101010

0x0000000101

Instructions

0x0101111100

0x0101000100 0x0101010101

Execution unit 0x0000000101

0x0111011110

+ *

Registers

PC

0x0101111100

0x0101000100 0x0101010101

Execution unit

0x0111011110

+ *R1←R1+R2

Memory SharedMemory

<A1, A2, J>

Von-Neumann Model

Registers

PC

0x0101111100

0x0101000100 0x0101010101

Execution unit

0x0111011110

+ *

Registers

PC

0x0101111100

0x0101000100 0x0101010101

Execution unit

0x0111011110

+ *

SharedMemory

Instructions

Instructions

R1←@R2Instruction:

What is Programming?

• Express the problem formally

• Then say “Solve(Problem)”

• Formal vs Natural language • High level language

• Preferably Turing complete

• Declarative style

• Imperative Style

• Object-oriented

• Syntax & Semantics

Language Styles

x, where x*x = n

Declarative

x = 0while( x < n): if(x*x == n): return x x = x+1

Imperative

x = n.sqrt()

Object method sqrt: y = 0while( y < me): if(y*y == me): return y x = x+1

Object-oriented

(functional)

Program as A Graph of Simpler Tasks

SUM: [1, 3, 6, 11, 21, 0]

1 3

+ 6

+ 11

+

0

21

+

+

1 3

+

6

+

11

+

+

021

+

Flow Chart

T < 20T = Temperature

L = Load

L < 20%

Turn AC off

Start

Turn AC on

Y

N

Y

End

N

Programming is Problem-Solving

• Understand the problem: output for each input

• Formalize problem specification

• Formulate the over-all structure of the algorithm

• Coarse steps first

• Refine each step into simpler steps

• Until you know how to implement those steps

• Implement, Test & Maintain

IsThere(v, list)?

IsThere(v, list-left-half) IsThere(v, list-right-half)

Ans1 OR Ans2

return isThere(v, list.leftHalf) OR isThere(v, list.rightHalf)

IsThere(v, list)?

IsThere(v, list-1st-half)

IsThere(v, list-2nd-Half)

Ans1 OR Ans2

IsThere(v, list-1st-Half)

IsThere(v, list-2nd-Half)

Ans1 OR Ans2

Ans1 Ans2 Ans1 Ans2

Ans1 Ans2Ans1 OR Ans2

IsThere(v, list)?

IsEqual(v, list-1st-element)

IsEqual(v, list-2nd element)

IsEqual(v, list-ith-element)

IsEqual(v, list-last-element)

ORfor all i ∈ [0..n) if(list[i] == v) return TRUE

return FALSE

How does a Program Look?

• A set of instructions in a “programming language” • May maintains state

• Notion of Variables

• Or, name and binding

• Collections

• May take action based on (some part of) current state • May repeatedly take action • May interact with other programs, people, or devices

Programming Steps

• Understand specification

• Formulate as formally as you can

• Devise the test plan • Algorithmic design

• Analyse the performance

• Refine the test plan • Implement incrementally

• Test each time

• Error debugging + performance debugging

Repeat as necessary

Programming Errors

• Syntax/ Semantic errors • Crash

• Exception, Illegal access, Resource unavailability, System fault

• Hang • Wrong answer

• Occasionally wrong

Termination

• Any program without loops or recursion terminates • For loop

• Find an integer function of some program variables

• Integer value is non-negative at the start of the loop

• Integer value is zero at the end of the loop

• Value of guaranteed to decrease progressively

• For a recursive function:

• every recursive call will eventually reach a basis case

Every

exec

ution

Test Termination

def factorial(n): fact = 1 for i in xrange(1, n+1): fact = fact * i return fact

def isThere(v, list): for l in list: if(l == v): return TRUE return FALSE

v = length of list - position of l in list - 1

v = n - i if n > 00 otherwise

Programming Errors

• Syntax/ Semantic errors • Crash

• Exception, Illegal access, Resource unavailability, System fault

• Hang • Wrong answer

• Occasionally wrong

Program Correctness

• Starts with a correct specification of the requirements • Correctness can only be with respect to the specification

• Correct design

• Algorithmically correct

• Correct implementation

• Re-use of already correct code helps

Safe Coding Style

• Indicative names

• Comment

• Always check for error value returned by functions

• Handle exception

• Validate user input • Assert known state

Prove Correctness

def factorial(n): fact = 1 for i in xrange(1, n+1): fact = fact * i return fact

def isThere(v, list): for l in list: if(l == v): return TRUE return FALSE

Check all termination conditions

Induction on n

fact = (i-1)!

fact = i!

Python: Understand Error Report

• SyntaxError • IndentationError • TypeError • NameError • IndexError • UnboundLocalError • AssertionError


Recommended