App Academy Prep Flashcards

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

Add, subtract, multiply, and divide numbers

A

3 + 3 == 6
3 * 4 == 12
4 - 3 == 1
8 / 2 == 4

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

create variable, assign it 3

A

my_favorite_number = 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
# recall my_favorite_number, use it in an expression, save it
# to a variable
A

my_favorite_square = my_favorite_number * my_favorite_number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
# block of code that will take in a number, and return its
# square
A
def square(num)
  # squares num; the last expression in a method gets returned
  # implicitly
  num * num
end

square(3) == 9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
# search for `target_item` in `items`; return the array index
# where it first occurs
A
def find_item(items, target_item)
  i = 0
  while i < items.count
    current_item = items[i]
    if current_item == target_item
      # found item; return its index; stop here and exit the
      # method
      return i
    end
    i += 1
  end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

return nil to mean item not found

A

nil

end

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

Use if, else, elsif to describe negative, small, and big numbers

A
def how_big(num)
  if num < 0
    "negative"
  elsif num < 10
    "pretty small"
  elsif num < 100
    "not too big"
  elsif num < 1000
    "plenty big"
  else
    "way too big"
  end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is an array?

A

Arrays are lists of items.

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

How do you create an empty array

A

empty_array = []

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

Create an array with some_prime numbers

A

some_primes = [2, 3, 5, 7]

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

some_primes = [2, 3, 5, 7]

fetch 2 and 3 from this array

A

some_primes[0] == 2 # first prime

some_primes[1] == 3 # second prime

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

Set the first element of the array to 2

A

some_primes[0] = 2 # set the first element of the array to 2

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

Add an element to the end of an array

A

some_primes &laquo_space;11 # add the next prime to the array

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

get the number of items in an array

A

[2, 3, 5, 7].count == 4

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

What is a while loop?

A

A while loop repeatedly executes a block as long as its “condition” is true

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

How do you loop through items in an array?

A

To loop through the items in an array, use an “index variable” (often named i) and go one-by-one through the positions in the array.
i = 0
while i < items.count
current_item = items[i]

# Do something with current_item here…

i += 1
end

17
Q

Q: Write a method, pow, that takes two (non-negative, integer) numbers, base and exponent and returns base raised to the exponent power. (No fair using Ruby’s base ** exponent notation!).

A
def pow(base, exponent)
  result = 1
  i = 1
  while i <= exponent
    result = result * base
    i += 1
  end

result
end

18
Q

Q: Write a method, sum which takes an array of numbers and returns the sum of the numbers.

A
def sum(nums)
  total = 0

i = 0
while i < nums.count
total += nums[i]

i += 1   end

# return total
total
end

19
Q

Q: Write a method, is_prime?, that takes a number num and returns true if it is prime and false otherwise.

You may wish to use the modulo operation: 5 % 2 returns the remainder when dividing 5 by 2: 1. If num is divisible by i, then num % i == 0. (You would not be expected to already know about modulo for the challenge)

A

Works for values greater than 1

def is_prime?(num)
  i = 2
  while i
20
Q

Q: Using your is_prime? method, write a new method, primes that takes a (non-negative, integer) number max and returns an array of all prime numbers less than max.

A
def primes(max)
  primes_arr = []
  i = 2
  while i < max
    if is_prime?(i)
      # i is prime; add it to the array
      primes_arr << i
    end
i += 1   end

# return primes_arr
primes_arr
end