From 0f6aa231f38021d40c30f46ed92cfd4d4fd79427 Mon Sep 17 00:00:00 2001 From: shirley1chu <41248353+shirley1chu@users.noreply.github.com> Date: Fri, 8 Feb 2019 10:22:31 -0800 Subject: [PATCH 1/2] Create generator.rb I understood the portmanteau rules retaining everything before (but not including) the first vowel of the first word, and everything after (and including) the first vowel fo the last word --- generator.rb | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 generator.rb diff --git a/generator.rb b/generator.rb new file mode 100644 index 0000000..4474946 --- /dev/null +++ b/generator.rb @@ -0,0 +1,52 @@ +def length_validation(word) + while word.length < 2 + puts "Please enter a word greater than one character long" + word = gets.chomp + end + word = word +end + +def is_vowel?(letter) + if letter == "a" || letter == "e" || letter == "i" || letter == "o" || letter == "u" + return true + else + return false + end +end + +def find_first_vowel(word) + word_index = -1 + word.each_char do |letter| + word_index += 1 + if is_vowel?(letter) == true + break + end + end + return word_index +end + +def run_generator + puts "What is the first word to combine?" + first_word = length_validation(gets.chomp) + puts "What is the second word to combine?" + second_word = length_validation(gets.chomp) + puts "The first word is #{first_word}. The second word is #{second_word} in the run_generator method" + index_1 = nil + # if loop to address special case of all consonants in first_word + if first_word.include?("a" || "e" || "i" || "o" || "u") + index_1 = find_first_vowel(first_word) + else + index_1 = -1 + end + index_2 = nil + if second_word.include?("a" || "e" || "i" || "o" || "u") + index_2 = find_first_vowel(second_word) + else + index_2 = 0 + end + first_portion = first_word.slice(0...index_1) + second_portion = second_word.slice(index_2..-1) + puts "#{first_portion}" + "#{second_portion}" +end + +run_generator From cb30bd5080dda3060d7ce9ecdbc212bb4d47e9bd Mon Sep 17 00:00:00 2001 From: shirley1chu <41248353+shirley1chu@users.noreply.github.com> Date: Sun, 10 Feb 2019 15:28:03 -0800 Subject: [PATCH 2/2] Update generator.rb --- generator.rb | 61 ++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/generator.rb b/generator.rb index 4474946..e9106d3 100644 --- a/generator.rb +++ b/generator.rb @@ -1,28 +1,27 @@ -def length_validation(word) - while word.length < 2 - puts "Please enter a word greater than one character long" - word = gets.chomp - end - word = word +def is_vowel?(letter) + return ["a", "e", "i", "o", "u"].include?(letter) end -def is_vowel?(letter) - if letter == "a" || letter == "e" || letter == "i" || letter == "o" || letter == "u" - return true - else - return false +def length_validation(word) + until word.length >= 2 && word.count("aeiou") > 0 + puts "Please enter a word greater than one character long that also has at least one vowel" + word = gets.chomp end + return word end -def find_first_vowel(word) - word_index = -1 - word.each_char do |letter| - word_index += 1 - if is_vowel?(letter) == true +# method to find the index of desired vowel, whether it be first or last vowel +# char_idx represents index for the letter that you want to start iterating at +def find_vowel_index(word, char_idx, count_direction) + vowel_index = char_idx + word.length.times do + if is_vowel?(word[char_idx]) + vowel_index = char_idx break end + char_idx += count_direction end - return word_index + return vowel_index end def run_generator @@ -31,22 +30,22 @@ def run_generator puts "What is the second word to combine?" second_word = length_validation(gets.chomp) puts "The first word is #{first_word}. The second word is #{second_word} in the run_generator method" - index_1 = nil - # if loop to address special case of all consonants in first_word - if first_word.include?("a" || "e" || "i" || "o" || "u") - index_1 = find_first_vowel(first_word) - else - index_1 = -1 - end - index_2 = nil - if second_word.include?("a" || "e" || "i" || "o" || "u") - index_2 = find_first_vowel(second_word) + first_word_last_vowel = find_vowel_index(first_word, -1, -1) + # addresses special case that the last letter in the first word is a consonant, since it should not be chopped off + if first_word_last_vowel == -1 && is_vowel?(first_word[-1]) == false + first_portion = first_word else - index_2 = 0 + first_portion = first_word[0...first_word_last_vowel] end - first_portion = first_word.slice(0...index_1) - second_portion = second_word.slice(index_2..-1) - puts "#{first_portion}" + "#{second_portion}" + second_word_first_vowel = find_vowel_index(second_word, 0, 1) + second_portion = second_word[second_word_first_vowel..-1] + puts "Portmanteau: #{first_portion}" + "#{second_portion}" end run_generator + +puts "\nWould you like to continue the generator? Please enter yes to continue, or enter anything else to exit" +while gets.chomp.downcase == "yes" + run_generator + puts "\nWould you like to continue the generator? Please enter yes to continue, or enter anything else to exit" +end