Week 1 Practice problems Flashcards
1
Q
### implement your own include version of string.include? # easy mode: you can assume search_term is one character # challenge mode: search for more than one character # my_include("chris","is") -> true
A
# def my_include?(string, search_term) # end # p my_include?("avatar aang","a") # true # p my_include?("Fullmetal Alchemist","f") # false # p my_include?("avatar aang","tar") # true # p my_include?("teacher","cheat") # false # p my_include?("manslaughter","laughter") # true
2
Q
# given an array of integers # return an array of the squares of those integers
A
def square_array(arr) # thats why you're here! squared = [] i = 0 while i < arr.length squared << arr[i] * arr[i] i += 1 end return squared end # p square_array([1, 2, 3, 4]) # [1, 4, 9, 16] # p square_array([1, 3, 5, 9]) # [1, 9, 25, 81]
3
Q
# given a string # return a string of ONLY the words which are length 5 or greater # (hint: how can we break a string up into individual words?)
A
def big_words(str)
big_words_arr = []
# split initial string into individual words
splitted_str = str.split(“ “)
i = 0
while i < splitted_str.length
# check if the word is length 5 or greater
if splitted_str[i].length >= 5
big_words_arr «_space;splitted_str[i]
end
i += 1
end
return big_words_arr.join(“ “)
end
p big_words(“the quick brown fox jumped over the lazy dog”)
# => “quick brown jumped”
p big_words(“kevin is an absolute unit!!!!! The sheer size of this madman!”)
# => “kevin absolute unit!!!!! sheer madman!”
4
Q
# given an array, return every unique el in the array (in order of appearance) # cannot use .uniq method
A
# def unique_items(arr) # end # unique_items([0, 2, 0, 0, "dog", 2]) -> [0, 2, "dog"]
5
Q
# given an array of numbers # return the SECOND biggest number # without using the sort method
A
# def second_largest_number(arr) # without using the sort method # end # p second_largest_number([6,7,8,9,10,1,2,3,4,5]) # 9 # p second_largest_number([5,4,5,4]) # 5 # p second_largest_number([1,2,2,3]) # 2 # p second_largest_number([19]) # nil
6
Q
# A magic number is a number whose digits, when added together, sum # to 7, e.g., 34. Define a method that checks if a number is a magic number.
A
# def magic_number?(num) # end # p magic_number?(7) # true # p magic_number?(4201) # true # p magic_number?(82) # false
7
Q
# magic_numbers method return an array of the first n magic numbers # magic_numbers(3) => [7, 16, 25]
A
# def magic_numbers(n) # end # p magic_numbers(3) # p magic_numbers(50)
8
Q
# given 2 sorted arrays # merge the 2 arrays into a single, sorted array # no .sort # (hint: how can we take advantage of the fact that our arrays come pre-sorted?) # (hint: the smallest number in our merged array started at which position?)
A
# def merge_sorted_arrays(arr1,arr2) # end # p merge_sorted_arrays([1,2,4],[3,6,10,11]) # [1, 2, 3, 4, 6, 10, 11] # p merge_sorted_arrays([1,2,3],[4,5]) # [1, 2, 3, 4, 5] # p merge_sorted_arrays([1,1,2,6],[1,2,2]) # [1, 1, 2, 2, 2, 6]
9
Q
### Titleize # # Write a method that capitalizes each word in a string like a book title. # Do not capitalize the words: 'a', 'and', 'of', 'over' or 'the'.
A
# def titleize(title) # end # puts titleize("basketball") == "Basketball" # puts titleize("stephen curry") == "Stephen Curry" # puts titleize("war and peace") == "War and Peace" # puts titleize("the bridge OVER the river kwai") == "The Bridge over the River Kwai"