COL100
Introduction to Computer Science
Keys to Success
‣ Ask questions — repeatedly
• In class, In lab, online, outside class
• Seek help (Instructors and TAs)
‣ Practice programming in the lab and outside
• Learn to recognize common errors (and parse error messages)
- Try again and again .. (sometimes a short break helps)
‣ Attend, Take notes, Review slides and in-class programs
• Be regular — Catching up later is harder
‣ Do not miss emails; follow instructions carefully Keep your institute login ID working
Problem Specification
‣ A well-posed problem indicates the solution under all circumstances
• Directly or indirectly describes the property of the solution
• The solution must be veri able Unique or not
• Also describes what is known (given, input)
‣ There are assumed axioms (shared assumptions)
• Problem speci er and solution provider must agree on these axioms
‣ Given two numbers, produce their sum
may be a part of the axiom
☚
‣ Given two numbers, n1 and n2 s.t. 0 ≤ n1 ≤ m and 0 ≤ n2 ≤ m, produce n1 + n2 ,
• We share the understanding of ‘number’ ‘+’ ‘giving’ ..
Complex number? Real number? Positive integer?
fi
fi
Trivial Problem Specification: I/O listing
x y .. Heading Pitch Speed ..
3 1000 NNW 43 49
In nite Table?
fi
More Problem Specifications
‣ Explicit form: Output = f(Input)
• x(ρ, θ), y(ρ, θ) = ρ sin θ, ρ cos θ , given ρ, θ
• Sometimes f is described semi-formally, but needs to have a unique meaning
2 2
‣ Implicit form: Find x s.t. x + y = 9, given y
• Describes the properties of the solution Not always given as a
mathematical formula
‣ Recursive form: f(Input) = f(some function of input)
But we want to model it
n=0: 0 mathematically
f(n), n ∈ ℕ0 = n=1: 1
n > 1 : f(n − 1) + f(n − 2)
{n > 0 : f(n − 1) + d
n=0: a Base case
f(n), n ∈ ℕ0 =
Example Mathematical Problem Specifications 14
L 5 7
3 4
j=i 3 1 1 2 7 N
∑
Produce a list [L] such that Li = Nj, given list N, where 0 ≤ i < n and 0 ≤ Ni ≤ m
‣ j=0 Ni is the ith member of list N
Given m ⇒ N and L are ordinal lists
and |[L]|
‣ Produce list [L] , such that GCD(Li, n) = 1 ∀n, 1 ≤ n < Li, and Li < m, n, Li ∈ ℕ
natural
• Produce a list of all prime numbers less than m
• How do you verify the answer? Remainder, Modulus, Mod
‣ GCD(a, b) = max([D]), where ((a % Di = 0 ) and (b % Di = 0 )) assumed precedence
and ( max([D]) = Dj, s.t. Dj ≥ Di ∀i, 0 ≤ i < | [D] | )
• i.e., ∀x ∈ [a, b], x ∈ [D] ⇔ a % x = 0 and b % x = 0 a and b positive integers?
ℕ0
Square Root Problem Specification
‣ Find the square-root of a given integer n
• Can n be negative?
• What is the required precision?
‣ Find real number x, s.t. |x2 - n| < ε
Reverse a Number Problem Specification
‣ Given number n, Produce m that has digits in the opposite order of m
• Given two digits of m: mi, mj s.t. i<j, (mi = nk and mj = nl) (l<k) ?
IF mi = nk and mj = nl then l<k ?
• mi = nD-i-1? (where D = number of digits in n)
- Base 10?
Traffic Police Signal Problem Specification
‣ Only allow non-collision paths Forced enumeration
• {Ini}: Set of incoming directions and {Outi}: Set of outgoing directions
• {Path} = {In} X {Out} = {(Ini,Outj)} = {Pathm}
Assign numeric codes
- Is some (Ini, Outj) disallowed? Always Open?
• Allow path (Pathm, Pathn) if Pathm does not intersect Pathn 2
- Only two paths at a time? 1 4
- Number of lanes? Width of lanes and intersection? 3
- Fairness?
- Does Left-Side vs Right-Side driving matter?
Design Elevator Control Problem Specification
‣ Input:
• Up/Down button-press at Floor f, 0 ≤ f ≤ T
• Elevator button-press: destination oor, Open, Close
‣ Output: Open door, Close door, Up one oor, Down one oor, Stop
‣ Additional Conditions:
• Input can be asynchronous (Remember all input, Maintain safety?)
- Disallow some input? Validate.
• Output must be safe (Some output sequences are disallowed)
• Handle faults (what type of faults)
fl
fl
fl
Fuzzy Specification?
‣ Provide examples of input/output
• Given partial I/O table, ‘ ll up’ the rest
- How do we know the lling is ‘good?’
‣ Solution is a sample from a probability distribution
• Or, satis es a speci ed property with a high probability
‣ Usually divided into concretely speci ed sub-problems
• e.g., Apply statistical methods on data
fi
fi
fi
fi
fi
Specification Lessons
‣ Informally:
• Establish that the input and output are clear
• Establish that output for each given input can be veri ed to be correct
‣ Formally:
• Use only commonly understood axioms
• Understand and use standard mathematical notation
• f : ℕ × ℜ → ℕ, s . t . f(a, b) = a × b, where 0 ≤ a ≤ Nmax ∧ a % 2 = 0
‣ Often need to model the problem in terms of numbers (and symbols)
‣ ∃ complete formal speci cation languages; we will not get into those in COL100
fi
fi
Problem Specification and Solution
‣ Some formulations may admit multiple solutions
‣ Some formulations directly provide the solution or a method to solve
2 2
• Find distance D, where D((x1, y1), (x2, y2)) = (x1 − x2) + )(x1 − x2)
{n > 1 : f(n − 1) + 1
n=1: 1
• Find f, where f(n), n ∈ ℕ =
‣ Others require some derivation
{n > 1 : g(n + 1) − 1
n=1: 1
• g(n), n ∈ ℕ = Solution not unique
{n ≥ 1
n = 100 : 100
: g(n + 1) − 1
Speci cation not unique
fi