w1d3 Flashcards

1
Q

What’s a good way to visualize ruby’s modulo arithmetic?

A

Imagine a wheel with numbers written clockwise.

Count clockwise around the wheel for positive modulo.

Count counter-clockwise for negative modulo.

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

What’s a good way to get better debug information from an object?

A

Override the #inspect method

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

What’s a benefit to using a recursive solution over an iterative solution?

A

It can be easier to prove the correctness of a recursive algorithm.

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

High-level: quicksort

Recursive or iterative?

A

Choose a pivot.

Place all items less than the pivot to the left of the pivot.

Place all items greater than the pivot to the right of the pivot.

left = quicksort the left side
right = quicksort the right side

Put these back in order:
left + pivot + right

(it’s recursive)

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

What’s a common cause of a StackOverflowError ?

A

Broken base case in recursive call

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

What’s the name of the highest level stack frame?

A

main

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

How do you refer to the smaller and smaller-sized problems that a recursive algorithm breaks its input into?

A

subproblems

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

What type of mathematical proof lends itself to recursion?

A

mathematical induction

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

What type of function lends itself to mathematical induction?

A

recursion

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

What’s mathematical induction in a nutshell?

A

If we can solve for a base case and we can solve for a general (or nth) case, then we have solved for all the cases.

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

List a 5 step process for programming recursively

A

Map out a recursive decomposition.

Identify the base case(s).

Think one level up from the base case.

Ensure that the return values from all cases are always of the same type.

Get a stack trace (debuggin).

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

How do you change the number of recursive calls Ruby will allow?

A

MAX_STACK_SIZE = num

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

What does MAX_STACK_SIZE let you control?

A

The maximum number of recursive calls allowed.

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

What are some semantic HTML5 containers?

0-2: 1 star
3-4: 2 star
5-6: 3 star
6-7: 4 star
8-9: 5 star
A
header
fooder
nav
article
aside
figure
figcaption
section
div
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How does rails use the method_missing function?

A

dynamic finders such as

User.find_by_username_and_state(‘bob’, ‘california’)

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

Why would you use an underscore as a block argument?

A

Readability. To indicate to future users of the code not to care about that argument.

17
Q

Consider this code:

x = 7

def foo(x)
  x = 10
end

foo(x)
p x

What value is displayed? Why?

A
  1. The x within the foo method is a new local variable, separate from the x defined on the first line.
18
Q

Why does Enumerable#each return its receiver?

A

Each is intended to be used solely for its side effects.

19
Q

What’s the difference between:

a = "hello" ; a << "!"
a = "hello" ; a += "!"
A

The first one mutates the string by pushing a new object onto the end.

The second one reassigns the a pointer to point to a new string.

20
Q

How do you access the last result in pry?

A

_ (underscore)

21
Q

What does the & operator do in a method definition?

A

Converts a block into a proc

Proc-ifies a block.

22
Q

What does the & operator do in a method call?

A

Converts a proc into a block.

Block-ifies a proc.

23
Q

How do you pass a proc to a method that expects a block?

A

Use the & operator to toggle the proc back into a block.

24
Q

What’s the proper way to set up a new hash that stores mutable objects?

A

hash = Hash.new { |hash, key| hash[key] = }

25
Q

Why is it useful to use reader/writers internally within a class?

A

It prevents typos from generating new instance methods. Instead, they generate NoMethodError’s.

self.lmonade throws error
@lmonade creates new instance var

26
Q

How do you look at the class methods native to a class? How about instance methods?

A

class.methods - Object.methods

instance_of_class.methods - Object.methods

27
Q

Where can you use & to blockify a proc?

A

In a method call.

28
Q

Where can you use & to procify a block?

A

In a method definition

29
Q

If a block is passed into a method (as a block, not a proc), what is the only way to call it?

A

yield