Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sockets - Shubha #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# run_generator prompts user for 2 words and prints a portmanteau of the words
# to the screen
def run_generator
# prompt user for 2 words to combine
puts "What is the first word to combine?"
word1 = get_word
puts "What is the second word to combine?"
word2 = get_word

# WAVE 1 CODE
# puts "Word 1: #{word1}"
# puts "Word 2: #{word2}"
# test_vowel = "i"
# puts "Is #{test_vowel} a vowel?"
# puts is_vowel?(test_vowel)
# test_consonant = "g"
# puts "Is #{test_consonant} a vowel?"
# puts is_vowel?(test_consonant)
# puts "in the run_generator method"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would delete this dead code! It just muddies the waters when someone goes to look through it.


# calculate the end index of the first word by counting backwards. Default
# value is word length so that the whole word is included if there are no vowels
word1_end = word1.length
index = word1.length - 1
while (index >= 0)
if is_vowel?(word1[index])
word1_end = index
break
end
index -= 1
end

# calculate the start index of the second word. Default value is 0
# so that the whole word is included if there are no vowels
word2_start = 0
index = 0
until (index == word2.length)
if is_vowel?(word2[index])
word2_start = index
break
end
index += 1
end

# slice words and make the portmanteau
portmanteau = word1[0...word1_end] + word2[word2_start...word2.length]

puts "#{word1} + #{word2} = #{portmanteau}"
end

# get_word checks to see if the word length is greater than or equal to 2
def get_word
word = gets.chomp
until (word.length >= 2)
puts "Please enter a word that is at least 2 characters long"
word = gets.chomp
end
return word
end

# is_vowel checks to see if a specific character is a vowel
def is_vowel?(letter)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, personal style note, I'd move this function to the top of the code, because it gives context to what happens in your main program. Remember, your jobs are to 1) make it work good and 2) make it as easy to grok as possible.

vowels = ["a", "e", "i", "o", "u"]
if vowels.include?(letter.downcase)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice trick!

return true
else
return false
end
end

run_generator
71 changes: 71 additions & 0 deletions generator_v2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# run_generator prompts user for 2 words and prints a portmanteau of the words
# to the screen
def run_generator
puts "What is the first word to combine?"
word1 = get_word
puts "What is the second word to combine?"
word2 = get_word

# WAVE 1 CODE
# puts "Word 1: #{word1}"
# puts "Word 2: #{word2}"
# test_vowel = "i"
# puts "Is #{test_vowel} a vowel?"
# puts is_vowel?(test_vowel)
# test_consonant = "g"
# puts "Is #{test_consonant} a vowel?"
# puts is_vowel?(test_consonant)
# puts "in the run_generator method"


# calculate the end index of the first word. Default value is word length
# so that the whole word is included if there are no vowels
index = 0
word1_end = word1.length
(word1.reverse).each_char do |letter|
if is_vowel?(letter)
word1_end = (word1.length - 1) - index
break
end
index += 1
end

# calculate the start index of the second word. Default value is 0
# so that the whole word is included if there are no vowels
index = 0
word2_start = 0
(word2).each_char do |letter|
if is_vowel?(letter)
word2_start = index
break
end
index += 1
end

# slice words and make the portmanteau
portmanteau = word1[0...word1_end] + word2[word2_start...word2.length]

puts "#{word1} + #{word2} = #{portmanteau}"
end

# get_word checks to see if the word length is greater than or equal to 2
def get_word
word = gets.chomp
until (word.length >= 2)
puts "Please enter a word that is at least 2 characters long"
word = gets.chomp
end
return word
end

# is_vowel? checks to see if a specific character is a vowel
def is_vowel?(letter)
vowels = ["a", "e", "i", "o", "u"]
if vowels.include?(letter.downcase)
return true
else
return false
end
end

run_generator
89 changes: 89 additions & 0 deletions generator_with_enhancements.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# run_generator prompts user for 2 words and prints a portmanteau of the words
# to the screen
def run_generator
puts "What is the first word to combine?"
word1 = get_word
puts "What is the second word to combine?"
word2 = get_word

# WAVE 1 CODE
# puts "Word 1: #{word1}"
# puts "Word 2: #{word2}"
# test_vowel = "i"
# puts "Is #{test_vowel} a vowel?"
# puts is_vowel?(test_vowel)
# test_consonant = "g"
# puts "Is #{test_consonant} a vowel?"
# puts is_vowel?(test_consonant)
# puts "in the run_generator method"

# calculate the end index of the first word. Default value is word length
# so that the whole word is included if there are no vowels
word1_end = word1.length
index = word1.length - 1
while (index >= 0)
if is_vowel?(word1[index])
word1_end = index
break
end
index -= 1
end

# calculate the start index of the second word. Default value is 0
# so that the whole word is included if there are no vowels
word2_start = 0
index = 0
until (index == word2.length)
if is_vowel?(word2[index])
word2_start = index
break
end
index += 1
end

# slice words and make the portmanteau
portmanteau = word1[0...word1_end] + word2[word2_start...word2.length]

puts "#{word1} + #{word2} = #{portmanteau}"
end

# has_vowel? checks to see if a word has at least one vowel
def has_vowel?(word)
word.each_char do |letter|
if is_vowel?(letter)
return true
end
end
return false
end

# get_word checks to see if the word length is greater than or equal to 2
def get_word
word = gets.chomp
until (word.length >= 2) && has_vowel?(word)
puts "Please enter a word that is at least 2 characters long and has at least 1 vowel."
word = gets.chomp
end
return word
end

# is_vowel? checks to see if a specific character is a vowel
def is_vowel?(letter)
vowels = ["a", "e", "i", "o", "u"]
if vowels.include?(letter.downcase)
return true
else
return false
end
end

# while loop continues to run generator until user inputs something other than
# yes to "continue" prompt
continue = "yes"
while (continue == "yes")
run_generator
puts "Would you like to continue?
Enter 'yes' to continue.
Enter any other input to exit."
continue = gets.chomp.downcase
end