Enumerables & Ranges Flashcards

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

To Initials

Write a method to_initials that takes in a person’s name string and returns a string representing their initials.

A
def to_initials(name)
  initials = ""

split = name.split(“ “)
split.each { |part| initials += part[0] }

return initials

end

puts to_initials("Kelvin Bridges")      # => "KB"
puts to_initials("Michaela Yamamoto")   # => "MY"
puts to_initials("Mary La Grange")      # => "MLG"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

First In Array
Write a method first_in_array that takes in an array and two elements, the method should return the element that appears earlier in the array.

A

iterate through arr to find the index # of el1 && el2, return the lower index

def first_in_array(arr, el1, el2)
   if arr.index(el1) < arr.index(el2)
    return el1
  else
    return el2
  end
end
puts first_in_array(["a", "b", "c", "d"], "d", "b"); # => "b"
puts first_in_array(["cat", "bird" ,"dog", "mouse" ], "dog", "mouse"); # => "dog"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Abbreviate Sentence
Write a method abbreviate_sentence that takes in a sentence string and returns a new sentence where every word longer than 4 characters has all of it’s vowels removed.

A
def abbreviate_sentence(sent)
  words = sent.split(" ")
  new_words = []
  words.each do |word|
    if word.length > 4
      new_word = abbreviate_word(word)
      new_words << new_word
    else
      new_words << word
    end
  end

return new_words.join(“ “)
end

def abbreviate_word(word)
  vowels = "aeiou"
  new_word = ""
  word.each_char do |char|
    if !vowels.include?(char)
      new_word += char
    end
  end

return new_word
end

puts abbreviate_sentence("follow the yellow brick road") # => "fllw the yllw brck road"
puts abbreviate_sentence("what a wonderful life")        # => "what a wndrfl life"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Format Name

Write a method format_name that takes in a name string and returns the name properly capitalized.

A
def format_name(str)
  parts = str.split(" ")
  new_parts = []

parts.each do |part|
new_parts &laquo_space;part[0].upcase + part[1..-1].downcase
end

return new_parts.join(“ “)
end

puts format_name("chase WILSON") # => "Chase Wilson"
puts format_name("brian CrAwFoRd scoTT") # => "Brian Crawford Scott"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Is Valid Name

Write a method is_valid_name that takes in a string and returns a boolean indicating whether or not it is a valid name.

A

def is_valid_name(str) #is each ele of a str capitalized with following lowercase chars

split_str = str.split #separate each part

if split_str.length < 2
  return false
end
  split_str.each do |word|
    if !is_capitalized(word)
      return false
    else
      return true
    end
  end
end
def is_capitalized(word)
   if word[0] == word[0].upcase &amp;&amp; word[1..-1] == word[1..-1].downcase
    return true
  else
    return false
  end
end
puts is_valid_name("Kush Patel")       # => true
puts is_valid_name("Daniel")           # => false
puts is_valid_name("Robert Downey Jr") # => true
puts is_valid_name("ROBERT DOWNEY JR") # => false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Reverse Words
Write a method reverse_words that takes in a sentence string and returns the sentence with the order of the characters in each word reversed. Note that we need to reverse the order of characters in the words, do not reverse the order of words in the sentence.

A
def reverse_words(sent)
  split = sent.split(" ")

reversed = []
split.each { |word| reversed &laquo_space;word.reverse }

return reversed.join(“ “)
end

puts reverse_words('keep coding') # => 'peek gnidoc'
puts reverse_words('simplicity is prerequisite for reliability') # => 'yticilpmis si etisiuqererp rof ytilibailer'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Rotate Array
Write a method rotate_array that takes in an array and a number. The method should return the array after rotating the elements the specified number of times. A single rotation takes the last element of the array and moves it to the front.

A
def rotate_array(arr, num)
  num.times do
    popped = arr.pop()
    arr.unshift(popped)
  end

return arr

end

print rotate_array([ "Matt", "Danny", "Mashu", "Matthias" ], 1) # => [ "Matthias", "Matt", "Danny", "Mashu" ]
puts
print rotate_array([ "a", "b", "c", "d" ], 2) # => [ "c", "d", "a", "b" ]
puts
How well did you know this?
1
Not at all
2
3
4
5
Perfectly