Week 2 Practice Problems Flashcards

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

Define a method that returns the longest word in its argument.

A
def longest_word(str)
  longest = ""

splitted_str = str.split(“ “) # [“is”, “erica”, “nicer”]

  splitted_str.each do |str| #auto index
    if str.length > longest.length
      longest =  str 
    end
  end
  return longest 
end
p longest_word("is erica nicer") #erica
p longest_word("is nick a genius") #genius
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
# Define a method that, given an array of numbers, returns another array with
# each of the argument's numbers multiplied by two. Do not modify the original array.
A
def array_times_two(arr)
  doubled = []
  arr.each do |num|
    doubled << num * 2
  end
  return doubled
end
# p array_times_two([1, 5, 9])
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
# Define a method that reverses the digits of its argument and returns the
# resulting number.
A
def reverse_digits(int)
  # return int.to_s.reverse.to_i
  # return int.digits.reverse.join.to_i
  # reversed = []
  # digs = int.digits
  # digs.each do |num|
  #   reversed.unshift(num)
  # end
  # return digs.join('').to_i
end
# p reverse_digits(1234) # => 4321
# p reverse_digits(456785432) # => 234587654
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
# The Fibonacci Sequence follows a simple rule: the next number in the sequence
# is the sum of the previous two. The sequence begins with [0, 1]. One computes
# the third number by summing the first and  second (0 + 1 == 1),
# yielding [0, 1, 1], one computes the fourth number by summing the second and
# the third, yielding [0, 1, 1, 2], and so on.
# Define a method, #fibs, that accepts an integer as an argument. The method should return an array of the first n Fibonacci numbers.
# fibs(1) => [0]
# fibs(2) => [0, 1]
# fibs(3) => [0, 1, 1]
# fibs(6) => [0, 1, 1, 2, 3, 5] 8 13 21 34
A

def fibs(n)

end
puts "Fibs\n\n"
p fibs(1)
p fibs(2)
p fibs(3)
p fibs(6)
p fibs(15)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
# write a method longest_pause(string) that takes a string and counts the
# longest "ummmm". For example:
# longest_pause("ummmmmmm") => 8
A
# def longest_pause(sentence)
# end
# puts longest_pause("ummmmmmm")
# puts longest_pause("Hi there, I'm um here for the interview")
# puts longest_pause("um I'm uum not sure.")
# puts longest_pause("ummmmm, I'm umm not sure.")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
# A magic number is a number whose digits, when added together, sum
# to 7, e.g., 34. Define a method that returns an array of the first n magic
# numbers. You may wish to write a helper method that returns a boolean
# indicating whether a number is magic.
# magic_numbers(3) => [7, 16, 25]
A
# def magic_number?(n)
# end
# def magic_numbers(n)
# end
# puts "Magic Number\n\n"
# p magic_numbers(3)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
# Write a method - five_ws, that takes one or more sentences and
# returns them as questions if they contain the words: why, what, where,
# who,  when.
# def five_ws(string)
# end
A
# def five_ws(string)
# end
# puts five_ws("Where can I find new shoes. I'm missing my favorite pair.") == "Where can I find new shoes? I'm missing my favorite pair."
# puts five_ws("Who is at the door.") == "Who is at the door?"
# puts five_ws("Whenever you are ready I'll meet you at the shop.") == "Whenever you are ready I'll meet you at the shop."
# puts five_ws("Who. What. Where. When.") == "Who? What? Where? When?"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly