App Academy Prep Flashcards
Add, subtract, multiply, and divide numbers
3 + 3 == 6
3 * 4 == 12
4 - 3 == 1
8 / 2 == 4
create variable, assign it 3
my_favorite_number = 3
# recall my_favorite_number, use it in an expression, save it # to a variable
my_favorite_square = my_favorite_number * my_favorite_number
# block of code that will take in a number, and return its # square
def square(num) # squares num; the last expression in a method gets returned # implicitly num * num end
square(3) == 9
# search for `target_item` in `items`; return the array index # where it first occurs
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
return nil to mean item not found
nil
end
Use if, else, elsif to describe negative, small, and big numbers
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
What is an array?
Arrays are lists of items.
How do you create an empty array
empty_array = []
Create an array with some_prime numbers
some_primes = [2, 3, 5, 7]
some_primes = [2, 3, 5, 7]
fetch 2 and 3 from this array
some_primes[0] == 2 # first prime
some_primes[1] == 3 # second prime
Set the first element of the array to 2
some_primes[0] = 2 # set the first element of the array to 2
Add an element to the end of an array
some_primes «_space;11 # add the next prime to the array
get the number of items in an array
[2, 3, 5, 7].count == 4
What is a while loop?
A while loop repeatedly executes a block as long as its “condition” is true
How do you loop through items in an array?
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
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!).
def pow(base, exponent) result = 1
i = 1 while i <= exponent result = result * base i += 1 end
result
end
Q: Write a method, sum which takes an array of numbers and returns the sum of the numbers.
def sum(nums) total = 0
i = 0
while i < nums.count
total += nums[i]
i += 1 end
# return total
total
end
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)
Works for values greater than 1
def is_prime?(num) i = 2 while i
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.
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