Simple Programs Flashcards

Week 2 of Intro to computer science and programming using Python

You may prefer our related Brainscape-certified flashcards:
1
Q

Strings

A
  • A sequence of case sensitive characters
  • Can compare strings with ==, etc.
  • len() is a function to retrieve the lenght of the string
  • square brackets used to perform indexing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

More on strings

A
  • Can slice strings using [start:stop:step]

- Strings are “immutable” - cannot be modified

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Approximate solutions

A
  • can’t guarantee exact answer
  • start with exhaustive enumeration
    • take small steps to generate guesses in order
    • check to see if close enough
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Approximate solutions - continued

A
  • Good enough solution
  • Start with a guess and increment by some small value
  • Use small epsilon
    (decreasing increment size -> slower program)
    (increasing epsilon -> less accurate answer)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Bisection search

A
  • Radically reduces computation time - being smart about generating guesses is important.
  • Works well on problems with ‘ordering’ property, value of function being solved varies monotonically with input value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Floats

A
  • Floats approximate real numbers

- Internally computers represents numbers in binary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Decimal number

A

302 = 310n2 + 010n1 + 2*10n0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Binary number

A

10011 = 12n4 + 02n3 + 02n2 + 12n1 + 1*2n0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Convert decimal integer to binary

A
  • take remainder relative to 2 (x%2) of number, that gives us the last binary bit
  • divide x by 2 (x//2), all the bits get shifted right
  • keep doing successive divisions, now remainder gets next bit and so on
  • then convert to binary form
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Fractions

A

Multiple by a power of 2 big enough to convert into a whole number. Can convert to binary and then divide by the same power of 2.

  • 0.375 * (2**3 = 3 (decimal)
  • convert 3 to binary (now 11)
  • divide by 2**3 (shift right) to get 0.011 (binary)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Implications

A
  • if no integer p such that x*(2**p) is a whole number, then internal representation is always approximation
  • Suggest testing equality of floats is not exact
    Use abs(x-y) < some small number, rather than x == y
  • print(0.1) returns 0.1 because Python designers set it up this way to automatically round.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Newton-Raphson - definition

A

General approximation algorithm to find roots of polynomial in one variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Newton-Raphson

A
  • Simple case cxn2 + k
  • First derivative 2cx
  • So if polynomial is xn2 + k, then derivative is 2x
  • Newton-Raphson says given a guess g for root, a better guess is: g-(gn2 -k)/2g
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Good Programming

A
  • more code not necessarily a good thing
  • measure good programmers by the amount of functionality
  • Mechanism to achieve decomposition and abstraction
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Decomposition

A

Break problems into different, self-contained, pieces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Abstraction

A

Suppress details of method to compute something from use of that computaion

17
Q

Modules

A
  • self-contained
  • break up code
  • reusable
  • keep code organized
  • keep code coherent
18
Q

Achieve decomposition

A
  • with functions

- with classes

19
Q

Abstraction in detail

A

Think of piece of code as a black box

  • cannot see details
  • do not need to see details
  • do not want to see details
  • hide tedious coding details
20
Q

Achieve abstraction

A
  • with function specification

- with docstrings

21
Q

Functions

A
  • called or invoked
  • has a name
  • has parameters
  • has a docstring (optional)
  • has a body
22
Q

Variable scope

A
  • formal parameter (gets bound to the value of..)
  • actual parameter (when function is called)
  • new scope/frame/env. created when enter a function
  • scope is mapping of names to objects
23
Q

return only

A
  • only inside a function
  • one return executed
  • code after a return is not executed!
  • value associated is given back to function caller
24
Q

print

A
  • outside and inside functions
  • execute many prints statements
  • code will be executed after print statement
  • value associated is outputted to the console
25
Q

Function specification

A

a contract between the implementer and the client.

  • Assumption: conditions that must be met by clients
  • Guarantees: conditions that must be met by the function
26
Q

Recursion

A
  • divide-and-conquer
  • decrease-and-conquer
  • a function that calls itself
  • must have 1 or more base cases
  • must solve the same problem on some other input
27
Q

Recursion observations

A
  • each recursive call creates its own scope/environment
  • bindings of variables in a scope is not changed
  • flow of control passes back to previous scope once function call returns a value
28
Q

Iteration
vs
Recursion

A
  • recursion may be simpler, more intuitive
  • recursion may be efficient from programmer POV
  • recursion may not be efficient from computer POV