Sequences & Data abstraction
Sequences
: a sequence is an ordered collection of values
- strings : sequence of characters
- lists : sequence of values of any data type
Sequence Abstraction
: All sequences have finite length
: Each element in a sequence has a discrete integer index
: sequence share common behaviors based on the shared trait of having a finite length and indexed elements
- Retrieve an element at a particular position
- Create a copy of a subsequence
- Check for membership
- Concatenate two sequences together
What can you do with sequences?
Get item : get the ith element
slice a subsequence : create a copy of the sequence from i to j
check membership : check if the value of <expr> is in <seq>
concatenate : combine two sequences into a single sequence
Sequence Processing
Iterating through sequences
: you can use a for statement to iterate through the elements of a sequence
Range
: the range function creates a sequence containing the values within a specified range
range( <start>, <end>, <skip> )
: creates a range object from <start> (inclusive) to <end> (exclusive), skipping every <skip> element
List comprehensions
: you can create out a list out of a sequence using a list comprehension
[ <expr> for <name> in <seq> if <cond> ]
- rules for execution
1. create an empty result list that will be the value of the list comprehension
2. for each element in <seq>:
A. bind to that element to <name>
B. If <cond> evaluates to a true value, then add the value of <expr> to the result list
Data Abstraction
- Compound values combine other values together
: a date _ a year, a month, and a day
: a geographic position _ latitude and longitude
- Data abstraction lets us manipulate compound values as units
- Isolate two parts of any program that uses daya
: how data are represented ( as parts )
: how data are manipulated ( as units )
- data abstraction : a methodology by which functions enforce an abstraction barrier between representation and use
Rational Numbers
: Exacy representation as fraction
: a pair of integers
: as soon as division occurs, the exact representation my be lost ! (demo)
: assume we can compose and decompose rational numbers
Rational Numbers Arithmetic
Rational Numbers Arithmetic Implementation
Representing Rational Numbers
Return to Lowest Terms
Abstraction Barriers
- use rational numbers to perform computation -> whole data values
: add_rational, mul_rational, rationals_are_equal, print_rational
- create rationals or implement rational operations -> numerators and denominators
: rational, numer, denom
- implement selectors and constructor for rationals -> two-element lists
: list literals and element selection
Violating Abstration Barriers
'University of California, Berkeley > ElectricalEngineering & ComputerSciences' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Functional Decomposition & Debugging (0) | 2019.07.10 |
---|---|
LAB_day 4 (0) | 2019.07.09 |
Week 3 (0) | 2019.07.09 |
LAB_day 3 (0) | 2019.07.04 |
Recursion Examples (0) | 2019.07.04 |