diff --git a/day_1/ex1.rb b/day_1/ex1.rb new file mode 100644 index 000000000..46a8196c4 --- /dev/null +++ b/day_1/ex1.rb @@ -0,0 +1,8 @@ +#puts "Hello World!" +#puts "Hello Again" +#puts "I like typing this." +#puts "This is fun." +#puts "Yay! Printing." +#puts "I'd much rather you 'not'." +#puts 'I "said" do not touch this.' +puts "Make your script print another line." diff --git a/day_1/ex2.rb b/day_1/ex2.rb new file mode 100644 index 000000000..dffb2b27c --- /dev/null +++ b/day_1/ex2.rb @@ -0,0 +1,9 @@ +# A comment, this is so you can read your program later. +# Anything after the # is ignored by ruby. + +puts "I could have code like this." # and the comment after is ignored + +# You can also use a comment to "disable" or comment out a piece of code: +# puts "This won't run." + +puts "This will run." diff --git a/day_1/ex3-1.rb b/day_1/ex3-1.rb new file mode 100644 index 000000000..cf0cd897f --- /dev/null +++ b/day_1/ex3-1.rb @@ -0,0 +1,4 @@ +# This is for Study Drills question 3 + +puts 6+5-2+20/2>=5 +# per terminal this = true diff --git a/day_1/ex3.rb b/day_1/ex3.rb new file mode 100644 index 000000000..dfcb9843c --- /dev/null +++ b/day_1/ex3.rb @@ -0,0 +1,23 @@ +puts "I will now count my chickens:" +# Brackets let you insert Ruby comps into text strings +puts "Hens #{25.0 + 30.0 / 6.0}" +puts "Roosters #{100.0 - 25.0 * 3.0 % 4.0}" +# Shows a string +puts "Now I will count the eggs:" +# Shows just the computation +puts 3.0 + 2.0 + 1.0 - 5.0 + 4.0 % 2.0 - 1.0 / 4.0 + 6.0 +# List everything out since it's contained in string, even the numbers +puts "Is it true that 3 + 2 < 5 - 7?" +# Shows computed equation +puts 3.0 + 2.0 < 5.0 - 7.0 +# Lines 14 and 15, same as lines 3 and 4 +puts "What is 3 + 2? #{3.0 + 2.0}" +puts "What is 5 - 7? #{5.0 - 7.0}" +# string +puts "Oh, that's why it's false." +# string +puts "How about some more." +# Similar to 3, 4, 14, and 15. Shows Question string trailed by computed answer +puts "Is it greater? #{5.0 > -2.0}" +puts "Is it greater or equal? #{5.0 >= -2.0}" +puts "Is it less or equal? #{5.0 <= -2.0}" diff --git a/day_1/ex4-1.rb b/day_1/ex4-1.rb new file mode 100644 index 000000000..225a0b060 --- /dev/null +++ b/day_1/ex4-1.rb @@ -0,0 +1,42 @@ +# STUDY DRILLS: + +#1. Explain error in your own words + +# -The variable carpool_capacity on line 14 was not properly defined on line +# 7. (This is what this error message means but without seeing the OG code +# this is the best guess at lines etc) + +#1. Using 4.0 + +# -It could be necessary, but with the current numbers it's not. I say it could +# be because, in very few situations it could impact the number of people +# you could transport. For example, assuming there were +# (9 drivers * 2.3 space_in_a_car the carpool_capacity would = 20.7). In this +# situation you would round down to 20 people that you could +# transport because you most likely wouldn't have less than one whole person. +# +# -If it was just 4 and not 4.0 then the resulting number for carpool_capacity +# would be a whole number with no decimal so. 120 not 120.0 +# +#2. Not reall a queston, more of a statement on floating points. +# +#3. See ex4.rb file. +# +#4. Statement about what "=" is called and that it gives data names. +# +#5. Statement about name of "_" +# +#6. See calculations below. + +i = 10 +x = 2 +j = 5 +people_invited = 50 +people_who_showed_up_at_party = 20 +number_of_people_at_party = people_invited - people_who_showed_up_at_party +number_of_invitations_made = people_invited + + +puts "There will be #{number_of_people_at_party} people at the party on Saturday." +puts "I sent out #{number_of_invitations_made} invitations to my friends and family." +puts i * j / x diff --git a/day_1/ex4.rb b/day_1/ex4.rb new file mode 100644 index 000000000..f2a9f4b3e --- /dev/null +++ b/day_1/ex4.rb @@ -0,0 +1,24 @@ +# The value or equivalent for cars is now defined as 100. +cars = 100 +# The value or equivalent for space_in_a_car is now defined as 4.0. +space_in_a_car = 4.0 +# The value or equivalent for drivers is now defined as 30. +drivers = 30 +# The value or equivalent for passengers is now defined as 90. +passengers = 90 +# The value or equivalent for cars_not_driven is now defined as cars(90)-drivers(30). +cars_not_driven = cars - drivers +# The value or equivalent for cars_driven is now defined as drivers(30). +cars_driven = drivers +# The value or equivalent for carpool_capacity is now defined as cars_driven(30) * space_in_a_car(4.0). +carpool_capacity = cars_driven * space_in_a_car +# The value or equivalent for average_passengers_per_car is now defined as passengers(90) / cars_driven(30). +average_passengers_per_car = passengers / cars_driven + + +puts "There are #{cars} cars available." +puts "There are only #{drivers} drivers available." +puts "There will be #{cars_not_driven} empty cars today." +puts "We can transport #{carpool_capacity} people today." +puts "We have #{passengers} to carpool today." +puts "We need to put about #{average_passengers_per_car} in each car." diff --git a/day_1/ex5.rb b/day_1/ex5.rb new file mode 100644 index 000000000..fb1fb0599 --- /dev/null +++ b/day_1/ex5.rb @@ -0,0 +1,21 @@ +name = 'Zed A. Shaw' +age = 35 +height = 74 +height_in_centimeters = height * 2.54 # New variable (Study Drills #2) +weight = 180 +weight_in_kilograms = weight * 0.453592 # New variable (Study Drills #2) +eyes = 'Blue' +teeth = 'White' +hair = 'Brown' + +puts "Let's talk about #{name}." +puts "He's #{height} inches tall." +puts "He's #{weight} pounds heavy." +puts "Actually that's not too heavy." +puts "He's got #{eyes} eyes and #{hair} hair." +puts "His teeth are usually #{teeth} depending on the coffee." +# +puts "He's #{height_in_centimeters} centimeters tall." # Proof of new variable +puts "He's #{weight_in_kilograms} kilograms heavy." # Proof of new variable + +puts "If I add #{age}, #{height}, and #{weight} I get #{age + height + weight}." diff --git a/day_1/ex6-1.rb b/day_1/ex6-1.rb new file mode 100644 index 000000000..5ede6ad80 --- /dev/null +++ b/day_1/ex6-1.rb @@ -0,0 +1,27 @@ +# Exercise 6 Study Drills from LRTHW webpage. + +# 1. Check ex6.rb +# +# 2. Lines: +# 10, 15, 17, and... +# 13 - I'm not sure if you would count this since it's a print command of a +# "string inside of a string". I'm adding it since the directions state to list +# "ALL the places where a string is put inside a string." +# +# 3. Yes, I'm sure. The only other possible lines are the following: +# +# 4 - I didn't count this because "technically" it's an integer inside of a string. +# 21 - I didn't count this because "technically" it's a boolean inside of a string. +# +# 4. You are adding the two strings together. +# +# 5. The single and double-quotes work "mostly" the same way. Normally you don't see them +# when using the puts command and running the file in ruby BUT, you can show +# them in ruby if you put them inside of each other. It works both ways as well. +# For example, +# if you want to see '' printed > puts "I like the 'way' you move." +# if you want to see "" printed > puts 'I like the "way" you move.' + +# The differences between the two is that the Single-Quotes can't hold/execute +# string interpolation inside of the them. They also can't hold/print escape +# sequences directly. diff --git a/day_1/ex6.rb b/day_1/ex6.rb new file mode 100644 index 000000000..405cd37f5 --- /dev/null +++ b/day_1/ex6.rb @@ -0,0 +1,29 @@ +# Defining the variable value. +types_of_people = 10 +# Creating string with a different string inside of it via a "#{}" +x = "There are #{types_of_people} types of people." +# Creating a string +binary = "binary" +# Creating a string +do_not = "don't" +#Creating string with a 2 different strings inside of it via a "#{}" +y = "Those who know #{binary} and those who #{do_not}." +# prints the string attached to the variable "X" and "y"on the following line +puts x +puts y +# prints string with "x" stirng inside of it. +puts "I said: #{x}." +# prints string with "y" stirng inside of it. +puts "I also said: '#{y}'." +# Defines variable value to "false" +hilarious = true +# Creates a string with a false variable inside of it. +joke_evaluation = "Isn't that joke so funny?! #{hilarious}" +# Prints above stated string with variable in it. +puts joke_evaluation +# Creates a string "w" variable. +w = "This is the left side..." +# Creates a string "e" variable. +e = "a string with a right side." +# Prints thestrings attached to the "w" and "e" variablein the PEMDAS order. +puts w + e diff --git a/day_1/ex7-1.rb b/day_1/ex7-1.rb new file mode 100644 index 000000000..f84ec1c5c --- /dev/null +++ b/day_1/ex7-1.rb @@ -0,0 +1,21 @@ +# Exercise 7 Study Drills from LRTHW webpage. + +# 1. gets.chomp is a two part method. +# gets(get string) method - retrieves user input via a string. When the user +# types a string and hits enter this enter is included in the users strings +# as a (\n) newline. +# +# chomp method - removes the (\n) newline character at the end of a string(s) +# +# 2. Yes, see bottom of file. +# +# 3. Please see form below: + +print "What color is your hair? " +hair = gets.chomp +print "How high can you jump? " +height = gets.chomp +print "How many large pizzas can you eat? " +pizza_count = gets.chomp + +puts "So, you're hair color is #{hair}, you can jump #{height} high, and eat #{pizza_count} large pizza(s)." diff --git a/day_1/ex7.rb b/day_1/ex7.rb new file mode 100644 index 000000000..8c4ef3d60 --- /dev/null +++ b/day_1/ex7.rb @@ -0,0 +1,15 @@ +print "How old are you? " +age = gets.chomp +print "How tall are you? " +height = gets.chomp +print "How much do you weight? " +weight = gets.chomp + +puts "So, you're #{age} old, #{height} tall and #{weight} heavy." + +# Answer to second question from strudy drills on webpage. + +# After you run it, type your name(hit enter) then it will insert your name +# into string when it's displayed/printed. +name = gets.chomp +puts "Hello #{name}, nice to meet you!" diff --git a/day_1/exercises/interpolation.rb b/day_1/exercises/interpolation.rb index c7f4f47df..27e746a65 100644 --- a/day_1/exercises/interpolation.rb +++ b/day_1/exercises/interpolation.rb @@ -15,11 +15,11 @@ speedy = "quick red fox" slow_poke = "lazy brown dog" -p # YOUR CODE HERE +p "The #{speedy} jumped over the #{slow_poke}" # Write code that uses the variables below to form a string that reads # "In a predictable result, the tortoise beat the hare!": slow_poke = "tortoise" speedy = "hare" -# YOUR CODE HERE +p "In a predictable result, the #{slow_poke} beat the #{speedy}!" diff --git a/day_1/exercises/loops.rb b/day_1/exercises/loops.rb index 90dc15ab1..e727245df 100644 --- a/day_1/exercises/loops.rb +++ b/day_1/exercises/loops.rb @@ -10,9 +10,10 @@ # Write code that prints the sum of 2 plus 2 seven times: 7.times do - # YOUR CODE HERE + p 2 + 2 end # Write code that prints the phrase 'She sells seashells down by the seashore' -# ten times: -# YOUR CODE HERE +10.times do + puts 'She sells seashells down by the seashore' +end diff --git a/day_1/exercises/numbers.rb b/day_1/exercises/numbers.rb index 9a5468a31..1b25b426a 100644 --- a/day_1/exercises/numbers.rb +++ b/day_1/exercises/numbers.rb @@ -7,10 +7,16 @@ p 2 + 2 # Write code that prints the result of 7 subtracted from 83: -p #YOUR CODE HERE +p 83 - 7 # Write code that prints the result of 6 multiplied by 53: -# YOUR CODE HERE +p 6 * 53 # Write code that prints the result of the modulo of 10 into 54: -# YOUR CODE HERE +# Since I'm not sure if you're asking for 54 mod 10 or 10 mod 54 +# I'm going to put both. I thinking you mean the first so I'll +# list that first as my official "answer", ha. + + +p 54 % 10 +p 10 % 54 diff --git a/day_1/exercises/strings.rb b/day_1/exercises/strings.rb index f2f903ffc..4e9dfe007 100644 --- a/day_1/exercises/strings.rb +++ b/day_1/exercises/strings.rb @@ -7,7 +7,7 @@ p "Alan Turing" # Write code that prints `Welcome to Turing!` to the terminal: -p #YOUR CODE HERE +p "Welcome to Turing!" # Write code that prints `99 bottles of pop on the wall...` to the terminal: -# YOUR CODE HERE +p "99 bottles of pop on the wall..." diff --git a/day_1/exercises/variables.rb b/day_1/exercises/variables.rb index a1e45bb26..c6697da93 100644 --- a/day_1/exercises/variables.rb +++ b/day_1/exercises/variables.rb @@ -1,6 +1,6 @@ # In the below exercises, write code that achieves # the desired result. To check your work, run this -# file by entering the following command in your terminal: +# file by entering the following command in your terminal: # `ruby day_1/exercises/variables.rb` # Example: Write code that saves your name to a variable and @@ -11,19 +11,20 @@ # Write code that saves the string 'Dobby' to a variable and # prints what that variable holds to the terminal: house_elf = "Dobby" -# YOUR CODE HERE +p house_elf # Write code that saves the string 'Harry Potter must not return to Hogwarts!' # and prints what that variable holds to the terminal: -# YOUR CODE HERE +elf_command = "Harry Potter must not return to Hogwarts!" +p elf_command # Write code that adds 2 to the `students` variable and # prints the result: students = 22 -# YOUR CODE HERE -p students +x = students + 2 +p x # Write code that subracts 2 from the `students` variable and # prints the result: -# YOUR CODE HERE -p students +j = students - 2 +p j diff --git a/day_1/questions.md b/day_1/questions.md index 73700e323..24b98608e 100644 --- a/day_1/questions.md +++ b/day_1/questions.md @@ -2,16 +2,39 @@ 1. How would you print the string `"Hello World!"` to the terminal? + - puts `"Hello World!"` + 1. What character is used to indicate comments in a ruby file? + - it's called an octothorpe or pound character + 1. Explain the difference between an integer and a float? + - integer has to be a whole number, float contains floating decimal place so it doesn't have to be a whole number + 1. In the space below, create a variable `animal` that holds the string `"zebra"` + - animal = "zebra" + 1. How would you print the string `"zebra"` using the variable that you created above? + - puts animal + 1. What is interpolation? Use interpolation to print a sentence using the variable `animal`. + - It's a method that allows you to embed/place Ruby code inside of a string by using the pound character with brackets `#{}` .(Assuming you mean string interpolation) + + - ```Ruby + animal = "zebra" + puts "I would love to pet a #{animal}." + ``` + 1. What method is used to get input from a user? + - The `gets` method. The specific method we used earlier was `gets and chomp` + methods combined. Which looks like this `gets.chomp` + 1. Name and describe two common string methods: + + - `.length` method - returns the string length of a variable in ruby. + - `.upcase` method - turns all letters in a string to uppercase. diff --git a/day_2/array_methods.md b/day_2/array_methods.md new file mode 100644 index 000000000..24269052d --- /dev/null +++ b/day_2/array_methods.md @@ -0,0 +1,53 @@ +## Array Methods + +### Creating Arrays + +You can create an array by putting data between square brackets `[]`. + + For example, `array = [1, "two", 3.0]` + +### Methods List + +1. **Append** + - This method adds/pushes an object(s) to the end of an array. It can do this + by using `.push` or **shovel operator** `<<` + +2. **Fetch** + - This method allows you to fetch an element at a specific position using `[]` + brackets. + +3. **Last** + - This method returns the last element in the array by using `.last`. + +4. **Sort** + - Sort will return a new array with sorted elements. If strings, it will be + alphabetical and numbers return in ascending order. + +5. **Each** + - This lets you iterate through an array. It takes the list/array and runs it through the block. It has two arguments and can be done using inline or multiline + blocks. Whatever block you apply the array to, it will return the OG array + after the results of your block. + + Syntax = `array/var.each {|obj| block }` + Return = OG array + +6. **Collect** + - Applies block of code on all elements in array and returns new array. + + Syntax = `array/var.collect {|obj| block }` + Return = New array + +7. **First** + - Returns first element in array. + + Syntax = array.first + +8. **Last** + - Returns last element in array. + + Syntax = array.last + +9. **Shuffle** + - Returns a new array where the elements are shuffled. + + Syntax = array.shuffle diff --git a/day_2/exercises/arrays.rb b/day_2/exercises/arrays.rb index f572a5ae6..a681d29b5 100644 --- a/day_2/exercises/arrays.rb +++ b/day_2/exercises/arrays.rb @@ -10,12 +10,13 @@ # Write code that stores an array of states in a variable, # then prints that array: -states = #YOUR CODE HERE +states = ["ohio", "texas", "colorado"] p states # Write code that stores an array of foods in a variable, # then prints that array: -# YOUR CODE HERE +foods = ["chicken", "rice", "onion"] +p foods # Example: Write code that prints the number of elements # in your above array of animals: @@ -23,18 +24,18 @@ # Write code that prints the number of elements # in your above array of foods: -# YOUR CODE HERE +p foods.count # Write code that prints "Zebra" from your animals array: -# YOUR CODE HERE +p animals.fetch(0) # Write code that prints the last item of your foods array: -# YOUR CODE HERE +p foods.last # Write code that adds "lion" to your animals array # and prints the result (Hint- use a method): -# YOUR CODE HERE +p animals << "lion" # Write code that removes the last element from your foods array # and prints the result (Hint- use a method): -# YOUR CODE HERE +p foods.delete("onion") diff --git a/day_2/exercises/boolean_practice.rb b/day_2/exercises/boolean_practice.rb new file mode 100644 index 000000000..57c45d157 --- /dev/null +++ b/day_2/exercises/boolean_practice.rb @@ -0,0 +1,11 @@ +# STUDY DRILLS + +# 1. (==) (!=)(>)(<)(>=)(<=)(<=>)(===)(.eql?)(equal?) + +# 2. (Equal) (Not equal) (Greater than) (Less than) (Greater than or equal to) +# (Less than or equal to) (Combined comparison operator) (Test equality) +# (True if two values are equal and of the same type) (True if two things are same object) + +# 3. Practice bollean operators in ruby + +# 4. Throw practice away diff --git a/day_2/exercises/iteration.rb b/day_2/exercises/iteration.rb index a801cb4fc..9b4e25659 100644 --- a/day_2/exercises/iteration.rb +++ b/day_2/exercises/iteration.rb @@ -13,16 +13,24 @@ # Write code that iterates through a list of animals and prints # "The is awesome!" for each animal: +["Zebra", "Giraffe", "Elephant"].each { |animals| p "The #{animals} is awesome!" } +OR animals.each { |animals| p "The #{animals} is awesome!" } -animals.each do |animal| - # YOUR CODE HERE -end - -# Write code that stores an array of foods in a variable, +# Write code that stores an array of foods in a variable, # then iterates over that array to print # "Add to shopping list" for each food item: -# YOUR CODE HERE +foods = ["pizza", "salad", "pasta"] -# Write code that stores an array of numbers in a variable, -# then iterates over that array to print doubles of each number: -# YOUR CODE HERE +foods.each do |foods| + x = "Add #{foods} to shopping list" + p x +end + +# Write code that stores an array of numbers in a variable, +# then iterates over that array to print doubles of each number: +numbers = [1, 2, 3, 4] + +numbers.each do |number| + n = "#{number * 2}" + puts n +end diff --git a/day_2/exercises/iteration_and_each_exercises.rb b/day_2/exercises/iteration_and_each_exercises.rb new file mode 100644 index 000000000..c3a7be867 --- /dev/null +++ b/day_2/exercises/iteration_and_each_exercises.rb @@ -0,0 +1,61 @@ +# 1. + +numbers = [1, 2, 3, 4] +numbers.each do |number| + n = "#{number * 2}" + puts n +end + +numbers.each do |number| + n = "#{number * 3}" + puts n +end + +# 2. + +numbers = [1, 2, 3, 4] +numbers.each do |number| + puts number if number.even? +end + +numbers.each do |number| + puts number if number.odd? +end + +# 3. + +numbers = [1, 2, 3, 4] +numbers.collect do |number| + n = "#{number * 2}" +end + +# 4. + +names = ["Alice Smith", "Bob Evans", "Roy Rogers"] +# print out the full names line by line +puts names +# print out only the first name +names.first +# print out only the last name +names.last +# print out only the only the initials +names.each do |name| + x = name.split.first[0, 1] + y = name.split.last[0, 1] + puts "#{x}#{y}" +end +# print out last name and how many characters are in it +names.each do |name| + x = name.split.last + y = name.split.last.length + puts "The last name is #{x} and has #{y} characters." +end +# create integer which reps the total number of characters in all names +names.each do |name| + x = names.join.gsub(/\s+/,"").length + puts "#{x}" +end +# if you didn't want the result 3 times you just wouldn't use .each method +# so it would just look like the following +x = names.join.gsub(/\s+/,"").length +puts "#{x}" diff --git a/day_2/questions.md b/day_2/questions.md index a179f0b04..fdce49773 100644 --- a/day_2/questions.md +++ b/day_2/questions.md @@ -2,16 +2,34 @@ 1. Create an array containing the following strings: `"zebra", "giraffe", "elephant"`. + `array = ["zebra", "giraffe", "elephant"]` + 1. Save the array you created above to a variable `animals`. + `animals = ["zebra", "giraffe", "elephant"]` + 1. Using the array `animals`, how would you access `"giraffe"`? + `animals.fetch(1)` + 1. How would you add `"lion"` to the `animals` array? + `animals << "lion"` + 1. Name and describe two additional array methods: + - **.sort** Sort will return a new array with sorted elements. If strings, it will be alphabetical and numbers return in ascending order. + + - **.first** Returns first element in array. + 1. What are the boolean values in Ruby? + `true & false` + 1. In Ruby, how would you evaluate if `2` is equal to `25`? What is the result of this evaluation? + `2 == 25` and `false` + 1. In Ruby, how would you evaluate if `25` is greater than `2`? What is the result of this evaluation? + + `25 > 2` and `true` diff --git a/day_3/exercises/else_and_if.rb b/day_3/exercises/else_and_if.rb new file mode 100644 index 000000000..70a2a25be --- /dev/null +++ b/day_3/exercises/else_and_if.rb @@ -0,0 +1,43 @@ +people = 20 +cars = 45 +trucks = 30 + +# is cars greater-than people? If yes print line 8. If not, print line +# 10. If that is false print line 12. +if cars > people + puts "We should take the cars." +elsif cars < people + puts "We should not take the cars." +else + puts "We can't decide." +end +# is trucks greater-than cars? If yes, print line 17. If not, print line +# 19. If false print linme 21. +if trucks > cars + puts "That's too many trucks." +elsif trucks < cars + puts "Maybe we could take the trucks." +else + puts "We still can't decide." +end +# is people greater-than trucks? If yes, print line 26. If not, print +# line 28. +if people > trucks + puts "Alright, let's just take the trucks." +else + puts "Fine, let's stay home then." +end + +# STUDY DRILLS + +# 1. They are secondary conditions that will execute depending on if the +# previous condition is true or false. + +# 2. Change integer variables + +# 3. Try more complex boolean expression. +if cars > people || trucks < cars + puts "What is the world coming to." +end + +# 4. See above. diff --git a/day_3/exercises/if_statements.rb b/day_3/exercises/if_statements.rb index a80b96840..b2bf04acb 100644 --- a/day_3/exercises/if_statements.rb +++ b/day_3/exercises/if_statements.rb @@ -3,7 +3,7 @@ # file by entering the following command in your terminal: # `ruby day_3/exercises/if_statements.rb` -# Example: Using the weather variable below, write code that decides +# Example: Using the weather variable below, write code that decides # what you should take with you based on the following conditions: # if it is sunny, print "sunscreen" # if it is rainy, print "umbrella" @@ -27,6 +27,20 @@ # Experiment with manipulating the value held in variable 'weather' # to print something other than 'coat' +weather = 'snowy' + +if weather == 'sunny' + p "sunscreen" +elsif weather == 'rainy' + p "umbrella" +elsif weather == 'snowy' + p "scarf" +elsif weather == 'icy' + p "yak traks" +else + p "good to go!" +end + ################## # Using the num_quarters variable defined below, determine @@ -35,21 +49,24 @@ # Right now, the program will print # out both "I have enough money for a gumball" and -# "I don't have enough money for a gumball". Write a +# "I don't have enough money for a gumball". Write a # conditional statement that prints only one or the other. # Experiment with manipulating the value held within num_quarters # to make sure both conditions can be achieved. -num_quarters = 0 +num_quarters = 3 -puts "I have enough money for a gumball" -puts "I don't have enough money for a gumball" +if num_quarters >= 2 + puts "I have enough money for a gumball" +elsif + puts "I don't have enough money for a gumball" +end ##################### # Using the variables defined below, write code that will tell you -# if you have the ingredients to make a pizza. A pizza requires +# if you have the ingredients to make a pizza. A pizza requires # at least two cups of flour and sauce. # You should be able to change the variables to achieve the following outputs: @@ -61,5 +78,13 @@ # Experiment with manipulating the value held within both variables # to make sure all above conditions output what you expect. -cups_of_flour = 1 +cups_of_flour = 3 has_sauce = true + +if cups_of_flour >= 2 && has_sauce == true + puts "I can make pizza" +elsif cups_of_flour <= 2 && has_sauce == false + puts "I cannot make pizza" +else + puts "I cannot make pizza" +end diff --git a/day_3/exercises/making_decisions.rb b/day_3/exercises/making_decisions.rb new file mode 100644 index 000000000..e1d8f1de0 --- /dev/null +++ b/day_3/exercises/making_decisions.rb @@ -0,0 +1,59 @@ +puts "You enter a dark room with two doors. Do you go through door #1 or door #2?" + +print "> " +door = $stdin.gets.chomp + +if door == "1" + puts "There's a giant bear here eating a cheese cake. What do you do?" + puts "1. Take the cake." + puts "2. Scream at the bear." + puts "3. Ask the bear if you can have some." + + print "> " + bear = $stdin.gets.chomp + + if bear == "1" + puts "The bear eats your face off. Good job!" + elsif bear == "2" + puts "The bear eats your legs off. Good job!" + elsif bear == "3" + puts "Bear states 'ight imma head out.'" + else + puts "Well, doing %s is probably better. Bear runs away." % bear + end + +elsif door == "2" + puts "You stare into the endless abyss at Cthulhu's retina." + puts "1. Blueberries." + puts "2. Yellow jacket clothespins." + puts "3. Understanding revolvers yelling melodies." + + print "> " + insanity = $stdin.gets.chomp + + if insanity == "1" || insanity == "2" + puts "Your body survives powered by a mind of jello. Good job!" + else + puts "The insanity rots your eyes into a pool of muck. Good job!" + end + +elsif door == "3" + puts "Woah, a third door.." + puts "1. Yell into the empty void." + puts "2. crab walk until you get tired." + + print "> " + mystery = $stdin.gets.chomp + + if mystery == "1" || mystery == "2" + puts "You win at life." + else + puts "You stumble around and fall on a knife and die. Good job!" + end +end + +# STUDY DRILLS + +# 1. Add to game. Please see above. + +# 2. See new_game file diff --git a/day_3/exercises/new_game.rb b/day_3/exercises/new_game.rb new file mode 100644 index 000000000..15c1f22d2 --- /dev/null +++ b/day_3/exercises/new_game.rb @@ -0,0 +1,40 @@ +puts "You're hungry but don't know which pie to eat. +Do you eat pie #1 or pie #2 ?" + +print "> " +pie = $stdin.gets.chomp + +if pie == "1" + puts "You ate old pie and get sick to your tummy. What do you do?" + puts "1. Run to the bathroom." + puts "2. Scream 'WHY!?'" + + print "> " + sick = $stdin.gets.chomp + + if sick == "1" + puts "Just before you make it to the toilet you puke...ewwwwwwaaaa!" + elsif sick == "2" + puts "You ask 'What do I do with my hands?'" + else + puts "Doing %s is your only escape. Life is good." % sick + end + +elsif pie == "2" + puts "Your scream appeases the chef and he gives you some pepto." + puts "1. Bark" + puts "2. tickle-me-elmo" + puts "3. Wrestle the undertaker." + + print "> " + scream = $stdin.gets.chomp + + if scream == "1" || scream == "2" + puts "No one can read chicken-scratch, just type it!" + else + puts "really?..." + end + +else + puts "BBQ stain on my white T-Shirt?" +end diff --git a/day_3/exercises/what_if.rb b/day_3/exercises/what_if.rb new file mode 100644 index 000000000..9165109ed --- /dev/null +++ b/day_3/exercises/what_if.rb @@ -0,0 +1,54 @@ +people = 20 +cats = 30 +dogs = 15 + + +if people < cats + puts "Too many cats! The world is doomed!" +end + +if people > cats + puts "Not my cats! The world is saved!" +end + +if people < dogs + puts "The world is drooled on!" +end + +if people > dogs + puts "The world is dry!" +end + + +dogs += 5 + +if people >= dogs + puts "People are greater than or queal to dogs." +end + +if people <= dogs + puts "People are less than or equal to dogs." +end + + +if people == dogs + puts "People are dogs." +end + +# STUDY DRILLS + +# 1. I think it makes the code actionable depending on if the +# condition is true or false. + +# 2. Readability for others, ruby only looks for linebreaks. +# It can help signal blocks of code though. + +# 3. Nothing happens, the code still works. It may make it more +# difficult to read. - asking about indentation not "end". + +# 4. Yes +if people != cats + puts "Cats are running rampant around here!" +end + +# 5. It could impact the results of the if statements. diff --git a/day_3/questions.md b/day_3/questions.md index db6170fa7..fa551f909 100644 --- a/day_3/questions.md +++ b/day_3/questions.md @@ -1,13 +1,33 @@ ## Day 3 Questions -1. What is a conditional statement? Give three examples. +1. **What is a conditional statement? Give three examples.** + - It's an expression that can be verified true or false. If true, then + the code you have inside that block can be executed. -1. Why might you want to use an if-statement? + - Examples + 1. if + 2. elsif + 3. else +1. **Why might you want to use an if-statement?** + - To verify if something is true or false. + - To show varied outcomes depending on input. -1. What is the Ruby syntax for an if statement? +1. **What is the Ruby syntax for an if statement?** + - if conditional (then) + code... + end -1. How do you add multiple conditions to an if statement? +1. **How do you add multiple conditions to an if statement?** + - by using more than one comparison operators such as &&(AND) -1. Provide an example of the Ruby syntax for an if/elsif/else statement: +1. **Provide an example of the Ruby syntax for an if/elsif/else statement:** + - if conditional (then) + code... + elsif conditional (then) + code... + else + code... + end -1. Other than an if-statement, can you think of any other ways we might want to use a conditional statement? +1. **Other than an if-statement, can you think of any other ways we might want to use a conditional statement?** + - Unless & Case statements diff --git a/day_4/exercises/intro_to_methods.rb b/day_4/exercises/intro_to_methods.rb new file mode 100644 index 000000000..be301e91c --- /dev/null +++ b/day_4/exercises/intro_to_methods.rb @@ -0,0 +1,58 @@ +# this one is like your scripts with ARGV +def print_two(*args) + arg1, arg2 = args + puts "arg1: #{arg1}, arg2: #{arg2}" +end + +# ok, that *args is actually pointless, we can just do this +def print_two_again(arg1, arg2) + puts "arg1: #{arg1}, arg2: #{arg2}" +end + +# this just takes one argument +def print_one(arg1) + puts "arg1: #{arg1}" +end + +# this one takes no arguments +def print_none() + puts "I got nothin'." +end + + +print_two("Zed","Shaw") +print_two_again("Zed","Shaw") +print_one("First!") +print_none() + +# STUDY DRILLS + +# FUNCTION/METHOD CHECKLIST : + +# 1. Did you start your function definition with def? + +# 2. Does your function name have only characters and _ (underscore) characters? + +# 3. Did you put an open parenthesis ( right after the function name? + +# 4. Did you put your arguments after the parenthesis ( separated by commas? + +# 5. Did you make each argument unique (meaning no duplicated names)? + +# 6. Did you put a close parenthesis ) after the arguments? + +# 7. Did you indent all lines of code you want in the function two spaces? + +# 8. Did you end your function with end lined up with the def above? + +# WHEN YOU RUN("USE" OR "CALL") A FUNCTION, CHECK THESE THINGS : + +# 1. Did you call/use/run this function by typing its name? + +# 2. Did you put the ( character after the name to run it? + +# 3. Did you put the values you want into the parenthesis separated by commas? + +# 4. Did you end the function call with a ) character? + +# 5. Functions that don't have parameters do not need the () after them, but would it be clearer if you wrote them anyway? diff --git a/day_4/exercises/methods.rb b/day_4/exercises/methods.rb index 6ed338e5d..782630101 100644 --- a/day_4/exercises/methods.rb +++ b/day_4/exercises/methods.rb @@ -12,16 +12,25 @@ def print_name # Write a method that takes a name as an argument and prints it: def print_name(name) - # YOUR CODE HERE + puts name end print_name("Albus Dumbledore") -# Write a method that takes in 2 numbers as arguments and prints +# Write a method that takes in 2 numbers as arguments and prints # their sum. Then call your method: -# YOUR CODE HERE +def numbers(a, b) + puts "#{a + b}" +end + +numbers(2, 3) -# Write a method that takes in two strings as arguments and prints -# a concatenation of those two strings. Example: The arguments could be -# (man, woman) and the end result might output: "When Harry Met Sally". +# Write a method that takes in two strings as arguments and prints +# a concatenation of those two strings. Example: The arguments could be +# (man, woman) and the end result might output: "When Harry Met Sally". # Then call your method: +def names(x, y) + puts "#{x} told #{y} a secret." +end + +names("John", "Jill") diff --git a/day_4/exercises/methods_and_return_values.rb b/day_4/exercises/methods_and_return_values.rb new file mode 100644 index 000000000..6d8ac06ab --- /dev/null +++ b/day_4/exercises/methods_and_return_values.rb @@ -0,0 +1,81 @@ +def add(a, b) + puts "ADDING #{a} + #{b}" + a + b +end + +def subtract(a, b) + puts "SUBTRACTING #{a} - #{b}" + a - b +end + +def multiply(a, b) + puts "MULTIPLYING #{a} * #{b}" + a * b +end + +def divide(a, b) + puts "DIVIDING #{a} / #{b}" + a / b +end + + +puts "Let's do some math with just functions!" + +age = add(25, 5) +height = subtract(80, 4) +weight = multiply(3, 2) +iq = divide(100, 2) + +puts "Age: #{age}, Height: #{height}, Weight: #{weight}, IQ: #{iq}" + + +# A puzzle for the extra credit, type it in anyway. +puts "Here is a puzzle." +### - add(age, subtract(height, multiply(weight, divide(iq, 2)))) +### - add(35, subtract(74, multiply(180, divide(50, 2)))) +what = add(age, subtract(height, multiply(weight, divide(iq, 2)))) + +puts "That becomes: #{what}. Can you do it by hand?" + +# STUDY DRILLS + +# 1. If you aren't really sure what return does, try writing a few of your own +# functions and have them return some values. You can return anything that +# you can put to the right of an =. + +## - Every method returns one object, even if the object is "nil(nothing)". +## Using "return" lets you choose which object is returned. Without it your +## method will return the last executed statement in the method body. + +# 2. At the end of the script is a puzzle. I'm taking the return value of one +# function and using it as the argument of another function. I'm doing this in +# a chain so that I'm kind of creating a formula using the functions. It looks +# really weird, but if you run the script, you can see the results. What you should +# do is try to figure out the normal formula that would recreate this same set of operations. +# +## - add(35, subtract(74, multiply(180, divide(25, 2)))) +## - 35 + -4426 = 74 - 4500 = 180 * 25 = 50 / 2 +## - 35 + -4426 = -4391 + +# 3. Once you have the formula worked out for the puzzle, get in there and see what +# happens when you modify the parts of the functions. Try to change it on purpose to +# make another value. + +## - please see above values changed + +# 4. Do the inverse. Write a simple formula and use the functions in the same way to calculate it. + +## - Formula : 2 + 5(6 - 3)(2 * 5) + 20 / 5 +## - add(age, subtract(height, multiply(weight, divide(iq, 2)))) +## - add(7, subtract(3, multiply(10, divide(4, 2)))) +## - add(7, subtract(3, multiply(10, 2)))) +## - add(7, subtract(3, 20)))) +## - add(7, -17)))) +## - add(7, -17)))) +## - what = -10 + +# 5. Remove the word return, and see if the script still works. You'll find that it +# does because Ruby implicitly returns whatever the last expression calculates. +# However, this isn't clear, so I want you to do it explicitly for my book. + +## - please see lack of "returns" above diff --git a/day_4/exercises/methods_and_variables.rb b/day_4/exercises/methods_and_variables.rb new file mode 100644 index 000000000..76f5c1514 --- /dev/null +++ b/day_4/exercises/methods_and_variables.rb @@ -0,0 +1,40 @@ +# defining the method name(with varibales), followed by puts commands in +# the method body. Use end to denote completion of method definition block. +def cheese_and_crackers(cheese_count, boxes_of_crackers) + puts "You have #{cheese_count} cheeses!" + puts "You have #{boxes_of_crackers} boxes of crackers!" + puts "Man that's enough for a party!" + puts "Get a blanket.\n" +end + +# print string and gave integer values to the variables being run through the +# method definition. +puts "We can just give the function numbers directly:" +cheese_and_crackers(20, 30) + +# print new string and gave new values and new variables. +puts "OR, we can use variables from our script:" +amount_of_cheese = 10 +amount_of_crackers = 50 +# stating method name with with new variable names. +cheese_and_crackers(amount_of_cheese, amount_of_crackers) + +# print new string and added math(new values) to method name. +puts "We can even do math inside too:" +cheese_and_crackers(10 + 20, 5 + 6) + +# print new string and added math(new values) to method name. +puts "And we can combine the two, variables and math:" +cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) + +# STUDY DRILLS + +# 1. Go back through the script and type a comment above each line explaining + +## - in English what it does. + +# 2. Start at the bottom and read each line backward, saying all the important characters. + +# 3. Write at least one more function of your own design, and run it 10 different ways. + +## - see my_method.rb file diff --git a/day_4/exercises/my_method.rb b/day_4/exercises/my_method.rb new file mode 100644 index 000000000..2209ef1eb --- /dev/null +++ b/day_4/exercises/my_method.rb @@ -0,0 +1,40 @@ +def people_and_cars(people_count, number_of_cars) + puts "You have #{people_count} people." + puts "You have #{number_of_cars} cars." + puts "That's enough for a race!" + puts "Grab a seat!\n" +end + +puts "What should we do with all of these people and cars?" +people_and_cars(50, 25) + +puts "Maybe we should try some demolition derby?" +people_count = 80 +number_of_cars = 20 + +people_and_cars(people_count, number_of_cars) + +puts "What happened to all of the people?" +people_and_cars(80 - 70, 2 * 20) + +puts "What happened to all of the cars?" +people_and_cars(2 * 40, 100 / 50) + + +puts "Seems like the stands are really starting to fill up?" +people_and_cars(people_count + 20, number_of_cars - 5) + +puts "I'm gonna grab some food. Do you want anything?" +people_and_cars(1 + 24 * 2, 50 / 2 - 5) + +puts "Doesn't it seem balanced today?" +people_and_cars(50, 50) + +puts "What is this, a junk yard?" +people_and_cars(25, 300) + +puts "Did they sell out?" +people_and_cars(150, 30) + +puts "You think they got enough for two races?" +people_and_cars(100, 100) diff --git a/day_4/exercises/say.rb b/day_4/exercises/say.rb new file mode 100644 index 000000000..925b2b802 --- /dev/null +++ b/day_4/exercises/say.rb @@ -0,0 +1,33 @@ +#puts "hello" +#puts "hi" +#puts "how are you" +#puts "I'm fine" + +#def say(words= 'hello') +# puts words + '.' +#end + +#say() +#say("hi") +#say("how are you") +#say("I'm fine") + +a = 5 + +def some_method + a = 3 +end + +puts a + +# Method invocation with a block + +[1, 2, 3].each do |num| + puts num +end + +# Method definition + +def print_num(num) + puts num +end diff --git a/day_4/questions.md b/day_4/questions.md index af17ab4da..0fea92e74 100644 --- a/day_4/questions.md +++ b/day_4/questions.md @@ -1,11 +1,35 @@ ## Day 4 Questions -1. In your own words, what is the purpose of a method? +1. **In your own words, what is the purpose of a method?** -1. Create a method named `hello` that will print `"Sam I am"`. + - To take some sort of action on classes, objects, variables etc. It's a + procedure that we can call, run, pass arguments through in order to a + accomplish some means. -1. Create a method named `hello_someone` that takes an argument of `name` and prints `"#{name} I am"`. +1. **Create a method named `hello` that will print `"Sam I am"`.** -1. How would you call or execute the method that you created above? + ``` + def hello(words) + puts words + end -1. What questions do you have about methods in Ruby? + hello("Sam I am.") + ``` + +1. **Create a method named `hello_someone` that takes an argument of `name` and prints `"#{name} I am"`.** + + ``` + def hello_someone(name) + puts name + end + + name = "#{name} I am" + ``` + +1. **How would you call or execute the method that you created above?** + + - I would re-type it or paste it into irb and press enter. + +1. **What questions do you have about methods in Ruby?** + + - None right now. diff --git a/day_5/exercises/ex39.rb b/day_5/exercises/ex39.rb new file mode 100644 index 000000000..a05bfcff2 --- /dev/null +++ b/day_5/exercises/ex39.rb @@ -0,0 +1,82 @@ +# create a mapping of state to abbreviation +states = { + 'Oregon' => 'OR', + 'Florida' => 'FL', + 'California' => 'CA', + 'New York' => 'NY', + 'Michigan' => 'MI' +} + +# create a basic set of states and some cities in them +cities = { + 'CA' => 'San Francisco', + 'MI' => 'Detroit', + 'FL' => 'Jacksonville' +} + +# add some more cities +cities['NY'] = 'New York' +cities['OR'] = 'Portland' + +# puts out some cities +puts '-' * 10 +puts "NY State has: #{cities['NY']}" +puts "OR State has: #{cities['OR']}" + +# puts some states +puts '-' * 10 +puts "Michigan's abbreviation is: #{states['Michigan']}" +puts "Florida's abbreviation is: #{states['Florida']}" + +# do it by using the state then cities dict +puts '-' * 10 +puts "Michigan has: #{cities[states['Michigan']]}" +puts "Florida has: #{cities[states['Florida']]}" + +# puts every state abbreviation +puts '-' * 10 +states.each do |state, abbrev| + puts "#{state} is abbreviated #{abbrev}" +end + +# puts every city in state +puts '-' * 10 +cities.each do |abbrev, city| + puts "#{abbrev} has the city #{city}" +end + +# now do both at the same time +puts '-' * 10 +states.each do |state, abbrev| + city = cities[abbrev] + puts "#{state} is abbreviated #{abbrev} and has city #{city}" +end + +puts '-' * 10 +# by default ruby says "nil" when something isn't in there +state = states['Texas'] + +if !state + puts "Sorry, no Texas." +end + +# default values using ||= with the nil result +city = cities['TX'] +city ||= 'Does Not Exist' +puts "The city for the state 'TX' is: #{city}" + +# STUDY DRILLS + +# 1. Do this same kind of mapping with cities and states/regions in your +# country or some other country. + +# - see study_drills39.rb file + +# 2. Find the Ruby documentation for hashes and try to do even more things to them. + +# - see study_drills39.rb file + +# 3. Find out what you can't do with hashes. A big one is that they do not have +# order, so try playing with that. + +# - see study_drills39.rb file diff --git a/day_5/exercises/hashes.rb b/day_5/exercises/hashes.rb index 99fcebb77..06b02d0c4 100644 --- a/day_5/exercises/hashes.rb +++ b/day_5/exercises/hashes.rb @@ -1,6 +1,6 @@ # In the below exercises, write code that achieves # the desired result. To check your work, run this -# file by entering the following command in your terminal: +# file by entering the following command in your terminal: # `ruby day_5/exercises/hashes.rb` # Example: Write code that prints a hash holding grocery store inventory: @@ -8,21 +8,23 @@ p foods # Write code that prints a hash holding zoo animal inventory: -zoo = #YOUR CODE HERE +zoo = {tigers: 10, apes: 20, eagles: 5} p zoo -# Write code that prints all of the 'keys' of the zoo variable +# Write code that prints all of the 'keys' of the zoo variable # you created above: -# YOUR CODE HERE +p zoo.keys -# Write code that prints all of the 'values' of the zoo variable +# Write code that prints all of the 'values' of the zoo variable # you created above: -# YOUR CODE HERE +p zoo.values -# Write code that prints the value of the first animal of the zoo variable +# Write code that prints the value of the first animal of the zoo variable # you created above: -# YOUR CODE HERE +p zoo.values[0] -# Write code that adds an animal to the zoo hash. +# Write code that adds an animal to the zoo hash. # Then, print the updated hash: -# YOUR CODE HERE +puts zoo +zoo.store(:lions, 7) +puts zoo diff --git a/day_5/exercises/study_drills39.rb b/day_5/exercises/study_drills39.rb new file mode 100644 index 000000000..9dfbdc768 --- /dev/null +++ b/day_5/exercises/study_drills39.rb @@ -0,0 +1,43 @@ +# STUDY DRILLS + +# 1. Do this same kind of mapping with cities and states/regions in your +# country or some other country. + +states = { + 'Colorado' => 'CO', + 'Ohio' => 'OH', + 'Texas' => "TX" +} + +cities = { + 'CO' => 'Denver', + 'OH' => 'Cincinnati', + 'TX' => 'Austin' +} + +puts '-' * 10 +puts "Ohio State has: #{cities['OH']}" +puts "Texas State has: #{cities['TX']}" + +puts '-' * 10 +states.each do |state, abbrev| + puts "#{state} is abbreviated #{abbrev}" +end + +puts '-' * 10 +states.each do |state, abbrev| + city = cities[abbrev] + puts "#{state} is abbreviated #{abbrev} and has city #{city}" +end + +# 2. Find the Ruby documentation for hashes and try to do even more things to them. + +puts '-' * 10 +produce = {apples: 5, oranges: 2, carrots: 8} +puts "There are #{produce[:apples]} apples in #{'CO'}." + +# 3. Find out what you can't do with hashes. A big one is that they do not have +# order, so try playing with that. +puts '-' * 10 +numbers = {one: 1, three: 3, two: 2, four: 4} +puts "#{numbers.sort}" # Result = [[:four, 4], [:one, 1], [:three, 3], [:two, 2]] diff --git a/day_5/questions.md b/day_5/questions.md index d059e12c6..d8c3c4153 100644 --- a/day_5/questions.md +++ b/day_5/questions.md @@ -1,13 +1,32 @@ ## Day 5 Questions -1. What is a Hash, and how is it different from an Array? +1. **What is a Hash, and how is it different from an Array?** -1. In the space below, create a Hash stored to a variable named `pet_store`. This hash should hold an inventory of items and the number of that item that you might find at a pet store. + - a collection of keys and their values similar to a dictionary. + - Arrays can be ordered and Hashes are not. -1. Given the following `states = {"CO" => "Colorado", "IA" => "Iowa", "OK" => "Oklahoma"}`, how would you access the value `"Iowa"`? +1. **In the space below, create a Hash stored to a variable named `pet_store`. This hash should hold an inventory of items and the number of that item that you might find at a pet store.** -1. With the same hash above, how would we get all the keys? How about all the values? + `pet_store = {snakes: 3, fish: 35, birds: 17}` -1. What is another example of when we might use a hash? In your example, why is a hash better than an array? +1. **Given the following `states = {"CO" => "Colorado", "IA" => "Iowa", "OK" => "Oklahoma"}`, how would you access the value `"Iowa"`?** -1. What questions do you still have about hashes? + `p states.values[1]` + +1. **With the same hash above, how would we get all the keys? How about all the values?** + + `p states.keys` and `p states.values` + +1. **What is another example of when we might use a hash? In your example, why is a hash better than an array?** + + ``` + person = {'OH' => 'Jim'} + cities = {'OH' => 'Cincinnati'} + + puts "#{person['OH']} lives in #{cities['OH']}." + ``` + - Hash works better since it has keys and values and doesn't need to be in order. + +1. **What questions do you still have about hashes?** + + - None right now. diff --git a/day_6/exercises/burrito.rb b/day_6/exercises/burrito.rb index 967f68b6c..f3e793a1a 100644 --- a/day_6/exercises/burrito.rb +++ b/day_6/exercises/burrito.rb @@ -1,4 +1,4 @@ -# Add the following methods to this burrito class and +# Add the following methods to this burrito class and # call the methods below the class: # 1. add_topping # 2. remove_topping @@ -13,7 +13,22 @@ def initialize(protein, base, toppings) end end + def add_topping(toppings) + @toppings.insert(2, 'onions') # add onions to toppings + end + + def remove_topping(toppings) + @toppings.delete_at(2) # removes "guacamole" from toppings array + end + + def change_protein + @protein.insert("Chicken") #inserts Chicken for protein + end + dinner = Burrito.new("Beans", "Rice", ["cheese", "salsa", "guacamole"]) -p dinner.protein -p dinner.base -p dinner.toppings +dinner.protein.delete!("Beans") # deletes OG protein, which is Beans +dinner.toppings.delete_at(2) # deletes guacamole +dinner.protein.insert(0, "Chicken") +p dinner.protein # prints protein +p dinner.base # prints base +p dinner.toppings.insert(2, 'onions') # prints onions where guacamole used to be diff --git a/day_6/exercises/dog.rb b/day_6/exercises/dog.rb index 03221314d..d5778778c 100644 --- a/day_6/exercises/dog.rb +++ b/day_6/exercises/dog.rb @@ -1,5 +1,5 @@ # In the dog class below, write a `play` method that makes -# the dog hungry. Call that method below the class, and +# the dog hungry. Call that method below the class, and # print the dog's hunger status. class Dog @@ -12,6 +12,11 @@ def initialize(breed, name, age) @hungry = true end + def play + @hungry = true + puts "#{@name} is hungry." + end + def bark p "woof!" end @@ -28,3 +33,4 @@ def eat p fido.hungry fido.eat p fido.hungry +puts fido.play diff --git a/day_6/exercises/person.rb b/day_6/exercises/person.rb index 2c26e9570..d2ac36781 100644 --- a/day_6/exercises/person.rb +++ b/day_6/exercises/person.rb @@ -1,5 +1,27 @@ -# Create a person class with at least 2 attributes and 2 behaviors. +# Create a person class with at least 2 attributes and 2 behaviors. # Call all person methods below the class and print results # to the terminal that show the methods in action. -# YOUR CODE HERE +class Person + attr_reader :name, :age + def initialize(name, age) + @name = name + @age = age + end + + def talk + "Hi I'm #{@name}" + end + + def number + "#{@name} is #{@age} years old." + end +end + +Joe = Person.new("Joe", 25) +puts Joe.talk +puts Joe.number +puts "---------------" +Bob = Person.new("Bob", 34) +puts Bob.talk +puts Bob.number diff --git a/day_6/questions.md b/day_6/questions.md index f58ca5f71..a4ea3a281 100644 --- a/day_6/questions.md +++ b/day_6/questions.md @@ -1,13 +1,49 @@ ## Day 6 Questions -1. In your own words, what is a Class? +1. **In your own words, what is a Class?** -1. What is an attribute of a Class? + - It's an outline or blueprint for creating objects. -1. What is behavior of a Class? +1. **What is an attribute of a Class?** -1. In the space below, create a Dog class with at least 2 attributes and 2 behaviors: + - It's a characteristic of the Class. -1. How do you create an instance of a class? +1. **What is behavior of a Class?** -1. What questions do you still have about classes in Ruby? + - It is basically the same as a Method. It's usually a verb that use or change an + attribute. + +1. **In the space below, create a Dog class with at least 2 attributes and 2 behaviors:** + + ``` + + class Dog + attr_reader :name, :height + + def initialize(name, height) + @name = name + @height = height + end + + def bark + '"roof!"' + end + + def jump + "#{@name} leaps into the air, which is pretty good for his height of #{@height}ft." + end + end + + Lucky = Dog.new("Lucky", 3) + puts Lucky.bark + puts Lucky.jump + + ``` + +1. **How do you create an instance of a class?** + + - By defining a Class and instantiating it by using the _.new_ method. + +1. **What questions do you still have about classes in Ruby?** + + - Currently none. diff --git a/day_7/10_speckled_frogs.rb b/day_7/10_speckled_frogs.rb new file mode 100644 index 000000000..b0cab0bc0 --- /dev/null +++ b/day_7/10_speckled_frogs.rb @@ -0,0 +1,68 @@ +# OG nursery rhyme + +puts '3 speckled frogs sat on a log' +puts 'eating some most delicious bugs.' +puts 'One jumped in the pool where its nice and cool,' +puts 'then there were 2 speckled frogs.' +puts '' +puts '2 speckled frogs sat on a log' +puts 'eating some most delicious bugs.' +puts 'One jumped in the pool where its nice and cool,' +puts 'then there was 1 speckled frog.'# removed the s at the end for every version that had one frog for this line. If there's only one it shouldn't be plural. +puts '' +puts '1 speckled frog sat on a log' +puts 'eating some most delicious bugs.' +puts 'One jumped in the pool where its nice and cool,' +puts 'then there were no more speckled frogs!' +puts '-----------------------------------------------' +# nursery ryhme for 10 frogs +puts '10 speckled frogs sat on a log' +puts 'eating some most delicious bugs.' +puts 'One jumped in the pool where its nice and cool,' +puts 'then there were 9 speckled frogs.' +puts '' +puts '9 speckled frogs sat on a log' +puts 'eating some most delicious bugs.' +puts 'One jumped in the pool where its nice and cool,' +puts 'then there was 8 speckled frogs.' +puts '' +puts '8 speckled frog sat on a log' +puts 'eating some most delicious bugs.' +puts 'One jumped in the pool where its nice and cool,' +puts 'then there was 7 speckled frogs.' +puts '-----------------------------------------------' +# word version +puts 'Three speckled frogs sat on a log' +puts 'eating some most delicious bugs.' +puts 'One jumped in the pool where its nice and cool,' +puts 'then there were two speckled frogs.' +puts '' +puts 'Two speckled frogs sat on a log' +puts 'eating some most delicious bugs.' +puts 'One jumped in the pool where its nice and cool,' +puts 'then there was one speckled frog.' +puts '' +puts 'One speckled frog sat on a log' +puts 'eating some most delicious bugs.' +puts 'One jumped in the pool where its nice and cool,' +puts 'then there were no more speckled frogs!' +puts '-----------------------------------------------' +# any number of frogs +x = 3 +y = 2 +z = 1 +puts "#{x} speckled frogs sat on a log" +puts 'eating some most delicious bugs.' +puts 'One jumped in the pool where its nice and cool,' +puts "then there were #{y} speckled frogs." +puts '' +puts "#{y} speckled frogs sat on a log" +puts 'eating some most delicious bugs.' +puts 'One jumped in the pool where its nice and cool,' +puts "then there was #{z} speckled frog." +puts '' +puts "#{z} speckled frog sat on a log" +puts 'eating some most delicious bugs.' +puts 'One jumped in the pool where its nice and cool,' +puts 'then there were no more speckled frogs!' +puts '-----------------------------------------------' diff --git a/day_7/caesar_cipher.rb b/day_7/caesar_cipher.rb new file mode 100644 index 000000000..084b04b13 --- /dev/null +++ b/day_7/caesar_cipher.rb @@ -0,0 +1,13 @@ +# Caesar Cipher + +class CaesarCipher + + def encrypt(string, key) + string_to_ascii_array = string.chars.map {|char| char.ord} + shifted = string_to_ascii_array.map {|char| char + key} + shifted.map {|char| char.chr }.join(', ') + end +end + +cipher = CaesarCipher.new +puts cipher.encrypt("Hellow World", 5) diff --git a/day_7/fizzbuzz.rb b/day_7/fizzbuzz.rb new file mode 100644 index 000000000..1d670f6a8 --- /dev/null +++ b/day_7/fizzbuzz.rb @@ -0,0 +1,18 @@ +def FizzBuzz (x,y) + while x <= y + if x % 3 == 0 && x % 5 == 0 + print "FizzBuzz," + elsif x % 5 == 0 + print "Buzz," + elsif x % 3 == 0 + print "Fizz," + else + print "#{x}," + end + x += 1 + end +end + + + +FizzBuzz(1,100) diff --git a/day_7/high_level.md b/day_7/high_level.md new file mode 100644 index 000000000..6706a39a0 --- /dev/null +++ b/day_7/high_level.md @@ -0,0 +1,24 @@ +# High Level Notes - Cesar Cipher + +# **NOTES :** + +1. We are going to take advantage of the fact that our letters are + represented by numbers via **ASCII codes** instead of using A..Z to wrap + around when shifting. + +2. First, I defined a method called encrypt for my CaesarCipher class. Then I converted + the string to an array of characters using `.chars`. Secondly, you can use `.map` + to iterate through the array and find the ASCII characters using `.ord`. I now needed to + shift. To do this, I used `.map` to iterate through the ASCII character codes + and then added our "shift value", which I called "key". Next, using `.join` + I was able to convert and join our new characters in the shifted array into + a new string of characters correlated to ASCII code. I got close to the end by defining + and instantiating a new object called "cipher" using the `.new` method. Lastly, + I printed the encrypted message and designated that message with they argument + and key(or value to be shifted). + +___________________________________________________________________________________________ +# **SOLUTION FOR CIPHER** + + Please see the following file in my _Day_7_ folder. + - caesar_cipher.rb