Computer science you might (not) want to know
Andy Andrea�Rails Conf 2022
Who I am and who I am not
Things to know
Part 1: Numbers
What exactly is a number anyway?
Base 10
7053
7*103 + 0*102 + 5*101 + 3*100
7*1000 + 0*100 + 5*10 + 3*1
What exactly is a number anyway?�Bases
Base 2/Binary | Base 10/Decimal | Base 12/Dozenal/Duodecimal |
0 | 0 | 0 |
1 | 1 | 1 |
10 | 2 | 2 |
11 | 3 | 3 |
100 | 4 | 4 |
101 | 5 | 5 |
110 | 6 | 6 |
111 | 7 | 7 |
1000 | 8 | 8 |
1001 | 9 | 9 |
1010 | 10 | a |
1011 | 11 | b |
1100 | 12 | 10 |
1101 | 13 | 11 |
Binary
11112
1*23 + 1*22 + 1*21 + 1*20
8 + 4 + 2 + 1
1510
111110
1*103 + 1*102 + 1*101 + 1*100
1000 + 100 + 10 + 1
111110
Fractions: decimal versus bicimal
481.235 in decimal
110.011 in bicimal (6.375 in decimal)
102 | 101 | 100 | . | 10-1 | 10-2 | 10-3 |
100 | 10 | 1 | . | 1/10 | 1/100 | 1/1000 |
4 | 8 | 1 | . | 2 | 3 | 5 |
22 | 21 | 20 | . | 2-1 | 2-2 | 2-3 |
4 | 2 | 1 | . | 1/2 | 1/4 | 1/8 |
1 | 1 | 0 | . | 0 | 1 | 1 |
Fractions
🔎 Why base 12 is better�🔎 How floating point numbers are stored (exponent, significand, etc)
The float problem
Solution 1: truncate to a certain number of digits and round
The float problem
Solution 2: Store fractions as an equation, e.g. ⅓
The float problem
Other issues with numbers
0111 + 1
-8..7
0111
🔎 Two’s Complement
🔎 JavaScript’s Number.MAX_VALUE vs Number.MAX_SAFE_INTEGER
🔎 Ruby’s Integer (>= Ruby 2.4) and Bignum (< 2.4)
🔎 Gandhi Civilization Overflow
Why do you want to know: Binary
🔎Bitwise operators and methods e.g.
Why do you not need to know: Binary
Part 2: Algorithms
“Algorithm” defined
“A process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer”
Sorting algorithms
Comparing algorithms
Algorithmic analysis
Time/space complexity
Constant time (O(1))
One line of code gets executed one time regardless of input
Linear time (O(n))
The two lines within our block get executed n (or numbers.length) times giving us 2n
The puts gets executed once giving us 1
0
Linear time (O(n))
Algorithms in real life
Algorithms in real life: O(n) example
Algorithms in real life: Improving on O(n)
Algorithms in real life: Improving on O(n)
Algorithms in real life: Improving on O(n)
The binary search algorithm
🔎Depth-first search
🔎Breadth-first search
Binary search in the wild: git bisect
man git-bisect
🔎RSpec bisect
Why do you want to know: Algorithms
Why do you not want to know: Algorithms
Conclusion: why do you want to know computer science?
Conclusion: why do you not want to know computer science?
Conclusion: why do you not want to know computer science?