Last active
December 24, 2015 01:19
-
-
Save gregeng/6723087 to your computer and use it in GitHub Desktop.
Lab: Conference Badges and Schedules // Lab: Different Ways to Discover what's true
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You're hosting a conference and need to print badges for the speakers. Each badge should say: "Hello, my name is _____." | |
# Write a method that will create and return this message, given a person's name. | |
# Now the list of speakers is finalized, and you can send the list of badges to the printer. Remember that you'll need to give this list to the printer, so it should be accessible outside of the method. | |
# Modify your method so it can take a list of names as an argument and return a list of badge messages. | |
# Your conference speakers are: Edsger, Ada, Charles, Alan, Grace, Linus and Matz. How you scored these luminaries is beyond me, but way to go! | |
# You just realized that you also need to give each speaker a room assignment. You have rooms 1-7. You'll need to print this for the speakers, so make sure to return a list of room assignments in the form of: "Hello, _____! You'll be assigned to room _____!" | |
# Write a method that assigns each speaker to a room, and make sure that each room only has one speaker. Return a list of room assignments | |
# Now you have to tell the printer what to print. Create a method that will output the results of the badge method and schedule method to the screen so the printer can do his thing. | |
# Write a method to run the rest of your program and print the results to the screen. No other method should print to the screen. | |
@nametag = [] | |
@room_assign = [] | |
@speakers = | |
@rooms = 1 | |
def my_name_is(*name) | |
name.each do |x| | |
x.capitalize! | |
@speakers = name | |
@nametag << "Hello, my name is #{x}!" | |
end | |
end | |
def room(speakers) | |
@speakers.each do |x| | |
@room_assign << "Hello #{x}! You'll be assigned to room #{@rooms}!" | |
@rooms += 1 | |
end | |
end | |
def print | |
puts @nametag | |
puts @room_assign | |
end | |
def run_program | |
my_name_is("Edsger", "Ada", "Charles", "Alan", "Grace", "Linus", "Matz") | |
room(@speakers) | |
end | |
run_program |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let's go back to the exercise where we determined what is and isn't a vowel. With ruby, there's always more than one way to do something and get the same result. | |
# Assuming vowels a,e,i,o,u: | |
#1 Write a method that returns whether a given letter is a vowel, using if and elsif | |
#2 Write a method that returns whether a given letter is a vowel, using case | |
#3 Write a method that returns whether a given letter is a vowel, using if with no else, all on a single line | |
#4 Write a method that returns whether a given letter is a vowel without using if or case while all on a single line | |
#5 Write a method that returns whether a given letter is a vowel without checking equality, or the use of if, using the array of vowels | |
#6 Write a method that will evaluate a string and return the first vowel found in the string, if any | |
#7 Write a method that will evaluate a string and return the ordinal position (index) of the string where the first vowel is found, if any | |
# hint: remember that every line of ruby code has a return value, and that a method will return the result of the last operation | |
#1 | |
def vowel(letter) | |
if letter == "a" | |
"vowel" | |
elsif letter == "e" | |
"vowel" | |
elsif letter == "i" | |
"vowel" | |
elsif letter == "o" | |
"vowel" | |
elsif letter == "u" | |
"vowel" | |
else | |
"consonant" | |
end | |
end | |
# 2 | |
def vowel2(letter) | |
case letter | |
when "a" , "e", "i", "o", "u" | |
"vowel" | |
else | |
"consonant" | |
end | |
end | |
#3 | |
def vowel3(letter) | |
if letter == "a" || if letter == "e" || if letter == "i" || if letter == "o" || if letter == "u" | |
"vowel" | |
end | |
end | |
#4 | |
def vowel4?(letter) | |
if "aeiou".include?(letter) | |
"vowel" | |
end | |
end | |
#5 | |
def vowel5(letter) | |
if letter.match(/a|e|i|o|u/) | |
"vowel" | |
end | |
end | |
#6 | |
def vowel6(string) | |
string.match(/a|e|i|o|u/) | |
end | |
#7 | |
def vowel7(string) | |
string.index(/a|e|i|o|u/) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment