Back to marketplace

Simple Programs

No description yet. This curated deck is ready for daily study.

Total cards: 24•Content version: v4•Public deck•Updated: 8/1/2026

Card preview (24)

#1
Front (Question)

Strings

Back (Answer)

• 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

#2
Front (Question)

More on strings

Back (Answer)

• Can slice strings using [start:stop:step] - Strings are “immutable” - cannot be modified

#3
Front (Question)

Approximate solutions

Back (Answer)

• can’t guarantee exact answer • start with exhaustive enumeration • take small steps to generate guesses in order • check to see if close enough

#4
Front (Question)

Approximate solutions - continued

Back (Answer)

• 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)

#5
Front (Question)

Bisection search

Back (Answer)

• 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.

#6
Front (Question)

Floats

Back (Answer)

• Floats approximate real numbers - Internally computers represents numbers in binary

#7
Front (Question)

Decimal number

Back (Answer)

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

#8
Front (Question)

Binary number

Back (Answer)

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

#9
Front (Question)

Convert decimal integer to binary

Back (Answer)

• 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

#10
Front (Question)

Fractions

Back (Answer)

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)

#11
Front (Question)

Implications

Back (Answer)

• 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.

#12
Front (Question)

Newton-Raphson - definition

Back (Answer)

General approximation algorithm to find roots of polynomial in one variable

#13
Front (Question)

Newton-Raphson

Back (Answer)

• 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

#14
Front (Question)

Good Programming

Back (Answer)

• more code not necessarily a good thing • measure good programmers by the amount of functionality • Mechanism to achieve decomposition and abstraction

#15
Front (Question)

Decomposition

Back (Answer)

Break problems into different, self-contained, pieces

#16
Front (Question)

Abstraction

Back (Answer)

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

#17
Front (Question)

Modules

Back (Answer)

• self-contained • break up code • reusable • keep code organized • keep code coherent

#18
Front (Question)

Achieve decomposition

Back (Answer)

• with functions - with classes

#19
Front (Question)

Abstraction in detail

Back (Answer)

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
Front (Question)

Achieve abstraction

Back (Answer)

• with function specification - with docstrings

#21
Front (Question)

Functions

Back (Answer)

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

#22
Front (Question)

Variable scope

Back (Answer)

• 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
Front (Question)

return only

Back (Answer)

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

#24
Front (Question)

print

Back (Answer)

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