From 059d4a181261dd1adc1696ee80fe88255c1c2796 Mon Sep 17 00:00:00 2001 From: Logan Anderson Date: Fri, 1 Jan 2021 16:22:27 -0700 Subject: [PATCH 01/11] Add day 1 --- day_1/README.md | 16 +++++------ day_1/ex1.rb | 10 +++++++ day_1/ex2.rb | 11 ++++++++ day_1/ex3.rb | 48 ++++++++++++++++++++++++++++++++ day_1/ex3_drill.rb | 4 +++ day_1/ex4.rb | 27 ++++++++++++++++++ day_1/ex5.rb | 19 +++++++++++++ day_1/ex6.rb | 41 +++++++++++++++++++++++++++ day_1/ex7.rb | 18 ++++++++++++ day_1/ex7_drill.rb | 24 ++++++++++++++++ day_1/exercises/interpolation.rb | 4 +-- day_1/exercises/loops.rb | 6 ++-- day_1/exercises/numbers.rb | 6 ++-- day_1/exercises/strings.rb | 4 +-- day_1/exercises/variables.rb | 11 ++++---- day_1/questions.md | 46 ++++++++++++++++++++---------- 16 files changed, 259 insertions(+), 36 deletions(-) create mode 100644 day_1/ex1.rb create mode 100644 day_1/ex2.rb create mode 100644 day_1/ex3.rb create mode 100644 day_1/ex3_drill.rb create mode 100644 day_1/ex4.rb create mode 100644 day_1/ex5.rb create mode 100644 day_1/ex6.rb create mode 100644 day_1/ex7.rb create mode 100644 day_1/ex7_drill.rb diff --git a/day_1/README.md b/day_1/README.md index 034ae11da..0f4090e5a 100644 --- a/day_1/README.md +++ b/day_1/README.md @@ -48,21 +48,21 @@ This will open the day_1 directory in Atom. You should be able to see the direct 1. Check off the items below as you complete the steps you just read for each lesson. ***Remember to create a file containing your work for each lesson!*** - - [ ] [A Good First Program](https://learnrubythehardway.org/book/ex1.html) + - [x] [A Good First Program](https://learnrubythehardway.org/book/ex1.html) - - [ ] [Comments in Code](https://learnrubythehardway.org/book/ex2.html) + - [x] [Comments in Code](https://learnrubythehardway.org/book/ex2.html) - - [ ] [Numbers and Math](https://learnrubythehardway.org/book/ex3.html) + - [x] [Numbers and Math](https://learnrubythehardway.org/book/ex3.html) - - [ ] [Variables and Names](https://learnrubythehardway.org/book/ex4.html) + - [x] [Variables and Names](https://learnrubythehardway.org/book/ex4.html) - - [ ] [Strings](https://learnrubythehardway.org/book/ex5.html) + - [x] [Strings](https://learnrubythehardway.org/book/ex5.html) - - [ ] [More Strings](https://learnrubythehardway.org/book/ex6.html) + - [x] [More Strings](https://learnrubythehardway.org/book/ex6.html) - - [ ] [Asking for Input](https://learnrubythehardway.org/book/ex11.html) + - [x] [Asking for Input](https://learnrubythehardway.org/book/ex11.html) - - [ ] Have you created 7 `ex.rb` files with your code in them? + - [x] Have you created 7 `ex.rb` files with your code in them? 1. Work through the [Strings](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#3.-strings) and [Numbers](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#5.-numbers) sections from Ruby in 100 Minutes. For each of these sections, open an `irb` session by typing `irb` into your terminal and type in the code snippets provided. diff --git a/day_1/ex1.rb b/day_1/ex1.rb new file mode 100644 index 000000000..b08260ae1 --- /dev/null +++ b/day_1/ex1.rb @@ -0,0 +1,10 @@ +#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 'Study drill extra line!' + +# The pound symbol allows you to make comments on your code diff --git a/day_1/ex2.rb b/day_1/ex2.rb new file mode 100644 index 000000000..9ef9060b2 --- /dev/null +++ b/day_1/ex2.rb @@ -0,0 +1,11 @@ +# 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." + +# I was correct on what the "#" character does. diff --git a/day_1/ex3.rb b/day_1/ex3.rb new file mode 100644 index 000000000..70700feaa --- /dev/null +++ b/day_1/ex3.rb @@ -0,0 +1,48 @@ +# prints string +puts "I will now count my chickens:" + +# prints string with mathematical interpolation +puts "Hens #{25.0 + 30.0 / 6.0}" +# prints string with mathematical interpolation +puts "Roosters #{100.0 - 25.0 * 3.0 % 4.0}" + +# prints string +puts "Now I will count the eggs:" + +# prints solution to math problem +puts 3.0 + 2.0 + 1.0 - 5.0 + 4.0 % 2.0 - 1.0 / 4.0 + 6.0 + +# prints string +puts "Is it true that 3 + 2 < 5 - 7?" + +# calculates value of each side of the inequality, compares, returns true or false +puts 3.0 + 2.0 < 5.0 - 7.0 + +# prints string with mathematical interpolation +puts "What is 3 + 2? #{3.0 + 2.0}" +# prints string with mathematical interpolation +puts "What is 5 - 7? #{5.0 - 7.0}" + +# prints string +puts "Oh, that's why it's false." + +# prints string +puts "How about some more." + +# prints string with interpolation that compares the values and returns true or false +puts "Is it greater? #{5.0 > -2.0}" +# prints string with interpolation that compares the values and returns true or false +puts "Is it greater or equal? #{5.0 >= -2.0}" +# prints string with interpolation that compares the values and returns true or false +puts "Is it less or equal? #{5.0 <= -2.0}" + +# + plus does addition +# - minus does subtraction +# / slash does division +# * asterisk does multiplication +# % percent returns the remainder of the left number divided by the right number +# < less-than compares to see if left side is mathematically less than right side +# > greater-than compares to see if left side is mathematically greater than right side +# <= less-than-equal compares to see if left side is mathematically less than or equal to right side +# >= greater-than-equal compares to see if left side is mathematically greater than or equal to left side +# <, >, <=, >= all return true or false value diff --git a/day_1/ex3_drill.rb b/day_1/ex3_drill.rb new file mode 100644 index 000000000..f14aa314d --- /dev/null +++ b/day_1/ex3_drill.rb @@ -0,0 +1,4 @@ +puts "Amount of money in my wallet: #{21.00 + 7.00}" +puts "Cost of meal at restaurant:#{12.99 + 2.60}" + +puts "I have enough money to buy the meal: #{21.00 + 7.00 >= 12.99 + 2.60}" diff --git a/day_1/ex4.rb b/day_1/ex4.rb new file mode 100644 index 000000000..32b67455f --- /dev/null +++ b/day_1/ex4.rb @@ -0,0 +1,27 @@ +# There are 100 cars +cars = 100 +# You can fit 4 passengers in a car +space_in_a_car = 4.0 +# There are only 30 drivers available for the 100 cars +drivers = 30 +# They need to transport 90 people +passengers = 90 +# One driver per car means that subtracting drivers from cars returns the number of empty cars +cars_not_driven = cars - drivers +# cars_driven can only be as large as the number of drivers available to drive them +cars_driven = drivers +# The total number of seats available is determined by multiplying the number of cars available by the number of seats available per car +carpool_capacity = cars_driven * space_in_a_car +# To determine how to divide passangers among the cars you just need to divide the number of passengers by the number of cars driven +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." + +# 1. The error that the author received was stating that the variable carpool_capacity was not properly assigned a value +# 2. I don't believe that it is necessary for space_in_a_car to be a float because you can't have a partial seat as you can't have a partial person to fill it diff --git a/day_1/ex5.rb b/day_1/ex5.rb new file mode 100644 index 000000000..a9a1c095c --- /dev/null +++ b/day_1/ex5.rb @@ -0,0 +1,19 @@ +name = 'Zed A. Shaw' +age = 35 +height = 74 # inches +weight = 180 # lbs +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 "If I add #{age}, #{height}, and #{weight} I get #{age + height + weight}." + +puts "Being #{height} inches tall means that #{name} is #{height * 2.54} centimeters tall." +puts "And weighing #{weight} pounds means that #{name} weighs #{weight * 0.45} kilograms." diff --git a/day_1/ex6.rb b/day_1/ex6.rb new file mode 100644 index 000000000..13db2ffbd --- /dev/null +++ b/day_1/ex6.rb @@ -0,0 +1,41 @@ +# assigns the variable to the value of 10 +types_of_people = 10 +# makes a string variable that interpolates the previous variable +x = "There are #{types_of_people} types of people." +# a new string variable +binary = "binary" +# a new string variable +do_not = "don't" +# makes a string variable that interpolates the previous 2 variables +y = "Those who know #{binary} and those who #{do_not}." + +# prints variable x with interpolation +puts x +# prints variable y with interpolation +puts y + +# prints string that interpolates variable x +puts "I said #{x}." +# prints string that interpolates variable y +puts "I also said: '#{y}'." + +# creates variable with boolean value false +hilarious = false +# makes a string variable that interpolates the previous variable +joke_evaluation = "Isn't that joke so funny?! #{hilarious}" + +# prints variable joke_evaluation that interpolates variable hilarious +puts joke_evaluation + +# a new string variable +w = "This is the left side of ..." +# a new string variable +e = "a string with a right side." + +# prints both listed variables from left to right +puts w + e + +# 2. lines: 10, 10, 18, 20 +# 3. I believe there are only 4 places because line 4 interpolates an integer and line 25 interpolates a boolean +# 4. I am guessing that adding the two strings using a + makes a longer string because using that math function tells ruby to combine the two strings into 1 +# 5. changing the strings to use single instead of double quotes breaks the program, I think that is because string will only interpolate with double quotes and using the single quotes comments out everything after the pound in the string diff --git a/day_1/ex7.rb b/day_1/ex7.rb new file mode 100644 index 000000000..29c4d4c1c --- /dev/null +++ b/day_1/ex7.rb @@ -0,0 +1,18 @@ +print "How old are you? " +age = gets.chomp +print "How tall are you " +height = gets.chomp +print "How much do you weigh? " +weight = gets.chomp + +puts "So, you're #{age} old, #{height} tall and #{weight} heavy." + +# 1. The gets portion creates a new line that makes a new value of user input while the .chomp portion removes the line break +# 2. Another way I can think of to use .chomp would be to do the same thing we did in an earlier exercise that did puts x + y + +x = "This is the left part of a string ..." +y = "that has a right part." + +print x.chomp +print y + diff --git a/day_1/ex7_drill.rb b/day_1/ex7_drill.rb new file mode 100644 index 000000000..fcb441dc6 --- /dev/null +++ b/day_1/ex7_drill.rb @@ -0,0 +1,24 @@ +print "Do you know Jimmy? " +friend = gets.chomp.downcase + +if friend == "yes" + print "Does he live on Main Street? " + home = gets.chomp.downcase + + if home == "yes" + print "Awesome, I'll head there now!" + else + print "Okay I'll figure it out then." + end + +else + print "Do you know his roommate Bob? " + friend2 = gets.chomp.downcase + + if friend2 == "yes" + print "If you could give me his phone number that would be great." + else + print "Oh, nevermind then I'll ask Greg." + end + +end diff --git a/day_1/exercises/interpolation.rb b/day_1/exercises/interpolation.rb index c7f4f47df..a92d9b3f3 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}" # YOUR CODE HERE # 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}!" # YOUR CODE HERE diff --git a/day_1/exercises/loops.rb b/day_1/exercises/loops.rb index 90dc15ab1..dd2da4238 100644 --- a/day_1/exercises/loops.rb +++ b/day_1/exercises/loops.rb @@ -10,9 +10,11 @@ # Write code that prints the sum of 2 plus 2 seven times: 7.times do - # YOUR CODE HERE + p 2 + 2 # YOUR CODE HERE end # Write code that prints the phrase 'She sells seashells down by the seashore' # ten times: -# YOUR CODE HERE +10.times do # YOUR CODE HERE + p "She sells seashells down by the seashore" +end diff --git a/day_1/exercises/numbers.rb b/day_1/exercises/numbers.rb index 9a5468a31..4b6d0219e 100644 --- a/day_1/exercises/numbers.rb +++ b/day_1/exercises/numbers.rb @@ -7,10 +7,10 @@ p 2 + 2 # Write code that prints the result of 7 subtracted from 83: -p #YOUR CODE HERE +p 83 - 7 #YOUR CODE HERE # Write code that prints the result of 6 multiplied by 53: -# YOUR CODE HERE +p 6 * 53 # YOUR CODE HERE # Write code that prints the result of the modulo of 10 into 54: -# YOUR CODE HERE +p 54 % 10 # YOUR CODE HERE diff --git a/day_1/exercises/strings.rb b/day_1/exercises/strings.rb index f2f903ffc..7c5c83acb 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!" #YOUR CODE HERE # 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..." # YOUR CODE HERE diff --git a/day_1/exercises/variables.rb b/day_1/exercises/variables.rb index a1e45bb26..9f9125ebe 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 # YOUR CODE HERE # 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 +safety = "Harry Potter must not return to Hogwarts!" # YOUR CODE HERE +p safety # Write code that adds 2 to the `students` variable and # prints the result: students = 22 -# YOUR CODE HERE +students = students + 2 # YOUR CODE HERE p students # Write code that subracts 2 from the `students` variable and # prints the result: -# YOUR CODE HERE +students = students - 2 # YOUR CODE HERE p students diff --git a/day_1/questions.md b/day_1/questions.md index 73700e323..72589b362 100644 --- a/day_1/questions.md +++ b/day_1/questions.md @@ -1,17 +1,35 @@ ## Day 1 Questions 1. How would you print the string `"Hello World!"` to the terminal? - -1. What character is used to indicate comments in a ruby file? - -1. Explain the difference between an integer and a float? - -1. In the space below, create a variable `animal` that holds the string `"zebra"` - -1. How would you print the string `"zebra"` using the variable that you created above? - -1. What is interpolation? Use interpolation to print a sentence using the variable `animal`. - -1. What method is used to get input from a user? - -1. Name and describe two common string methods: +```ruby +p "Hello World!" +``` + +2. What character is used to indicate comments in a ruby file? + - The pound symbol (#) + +3. Explain the difference between an integer and a float? + - A float uses decimals + +4. In the space below, create a variable `animal` that holds the string `"zebra"` +```ruby +animal = "zebra" +``` + +5. How would you print the string `"zebra"` using the variable that you created above? +```ruby +p animal +``` + +6. What is interpolation? Use interpolation to print a sentence using the variable `animal`. + - Interpolation is a way to process code inside of a string +```ruby +animal = "zebra" +p "A #{animal} is often recognized for having black and white stripes." +``` + +7. What method is used to get input from a user? + - Input can be gotten using "gets" or if you want it in the same line you would use "gets.chomp" + +8. Name and describe two common string methods: + - One method is the ".split" method which breaks up string based on a specified criteria and returns an array. Another method is the ".sub" method which allows one to find and replace specific things within a string. From f36e08f58a217b719c00c7b4d628f2efe24e0f53 Mon Sep 17 00:00:00 2001 From: Logan Anderson Date: Sat, 2 Jan 2021 19:51:28 -0700 Subject: [PATCH 02/11] Add Day 2 Work --- day_2/README.md | 6 +-- day_2/array_methods.md | 17 +++++++ day_2/exercises/arrays.rb | 16 +++--- day_2/exercises/boolean_practice.md | 70 +++++++++++++++++++++++++++ day_2/exercises/iteration.rb | 18 +++++-- day_2/exercises/iteration_and_each.rb | 63 ++++++++++++++++++++++++ day_2/questions.md | 22 +++++++++ 7 files changed, 199 insertions(+), 13 deletions(-) create mode 100644 day_2/array_methods.md create mode 100644 day_2/exercises/boolean_practice.md create mode 100644 day_2/exercises/iteration_and_each.rb diff --git a/day_2/README.md b/day_2/README.md index 0c8c1571c..f8df9d04a 100644 --- a/day_2/README.md +++ b/day_2/README.md @@ -13,11 +13,11 @@ Using your terminal, open your local copy of the forked repository you created d 1. Work through the [Arrays](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#7.-arrays) section of Ruby in 100 Minutes. As you work through this section, research each of the methods mentioned by looking through the [Ruby docs for Arrays](https://ruby-doc.org/core-2.4.1/Array.html). Documentation like this might look intimidating, but diving in and practicing now will build your comfort level. Create a file in your day_2 directory called `array_methods.md` and describe what each method does in your own words. 1. Work through the following lessons. Any files that you create while working can be kept in today's `exercises` directory. - - [ ] Turing's [Iteration and Each](http://backend.turing.io/module1/lessons/iteration_and_each) lesson. + - [x] Turing's [Iteration and Each](http://backend.turing.io/module1/lessons/iteration_and_each) lesson. - - [ ] [Booleans](https://learnrubythehardway.org/book/ex27.html) from Learn Ruby the Hard Way. + - [x] [Booleans](https://learnrubythehardway.org/book/ex27.html) from Learn Ruby the Hard Way. - - [ ] [Boolean Practice](https://learnrubythehardway.org/book/ex28.html) from Learn Ruby the Hard Way. + - [x] [Boolean Practice](https://learnrubythehardway.org/book/ex28.html) from Learn Ruby the Hard Way. 1. Work through the exercise files in the day_2/exercises directory. Complete them in this order: 1. arrays diff --git a/day_2/array_methods.md b/day_2/array_methods.md new file mode 100644 index 000000000..91ea0d252 --- /dev/null +++ b/day_2/array_methods.md @@ -0,0 +1,17 @@ +1. ".last": A method that allows the user to return the last element in an array. + +2. ".sort": A method that returns a new array by sorting the array it is called on. The default method of sorting is alphabetical, but by using a code block the user can cause the method to sort the array based on a determined criteria. + +3. ".each": A method that allows the user to iterate through each item of an array. The user writes a code block to determine what they want done to each item in the array, and then uses this method to make it happen. + +4. ".join": A method that converts all elements in an array into string and combines them. The user may denote a separator to place between the elements of the array when they are put together. + +5. ".index": Each element in an array is assigned a numerical value, counting upwards from 0, known as its index. Using this method and specifying an element of the array (known as the argument) will return the index value of that element. The user can also write a code block which will return the index of the first object of the array which causes the block to return a value of 'true'. If there is no match it will return 'nil'. + +6. ".include?": This method is paired with an argument that specifies an element that the user is searching for in an array. If the object exists in the array it will return the value 'true' otherwise it will return 'false'. + +7. ".collect": Another method of iterating through an array. This method is paired with a code block and applies the block to each element in the array, returning a new array with the results of the iteration. + +8. ".first": Similar to the ".last" method, this method allows the user to return the first element in the array. + +9. ".shuffle": A method that returns a new array with the same elements as the array it was called on, but shuffled into a different order. diff --git a/day_2/exercises/arrays.rb b/day_2/exercises/arrays.rb index f572a5ae6..0398c4d0c 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 = ["Florida", "Colorado", "Pennsylvania"] #YOUR CODE HERE p states # Write code that stores an array of foods in a variable, # then prints that array: -# YOUR CODE HERE +foods = ["apples", "bananas", "oranges"] # YOUR CODE HERE +p foods # Example: Write code that prints the number of elements # in your above array of animals: @@ -23,18 +24,21 @@ # Write code that prints the number of elements # in your above array of foods: -# YOUR CODE HERE +p foods.count # YOUR CODE HERE # Write code that prints "Zebra" from your animals array: -# YOUR CODE HERE +p animals[0]# YOUR CODE HERE # Write code that prints the last item of your foods array: -# YOUR CODE HERE +p foods.last# YOUR CODE HERE # Write code that adds "lion" to your animals array # and prints the result (Hint- use a method): -# YOUR CODE HERE +animals << "Lion" # YOUR CODE HERE +p animals # Write code that removes the last element from your foods array # and prints the result (Hint- use a method): # YOUR CODE HERE +foods.pop +p foods diff --git a/day_2/exercises/boolean_practice.md b/day_2/exercises/boolean_practice.md new file mode 100644 index 000000000..16d8c1668 --- /dev/null +++ b/day_2/exercises/boolean_practice.md @@ -0,0 +1,70 @@ +## Exercise +1. true && true + - true + +2. false && true + - false + +3. 1 == 1 && 2 == 1 + - false + +4. "test" == "test" + - true + +5. 1 == 1 || 2 != 1 + - true + +6. true && 1 == 1 + - true + +7. false && 0 != 0 + - false + +8. true || 1 == 1 + - true + +9. "test" == "testing" + - false + +10. 1 != 0 && 2 == 1 + - false + +11. "test" != "testing" + - true + +12. "test" == 1 + - false + +13. !(true && false) + - true + +14. !(1 == 1 && 0 != 1) + - false + +15. !(10 == 1 || 1000 == 1000) + - false + +16. !(1 != 10 || 3 == 4) + - false + +17. !("testing" == "testing" && "Zed" == "Cool Guy") + - true + +18. 1 == 1 && (!("testing" == 1 || 1 == 0)) + - true + +19. "chunky" == "bacon" && (!(3 == 4 || 3 == 3)) + - false + +20. 3 == 3 && (!("testing" == "testing" || "Ruby" == "Fun")) + - false + +## Study Drills +1. There are a lot of operators in Ruby similar to != and ==. Try to find as many "equality operators" as you can. They should be like < or <=. Write out the names of each of these equality operators. For example, I call != "not equal." + - == equal + - != not equal + - > greater than + - >= greater than or equal to + - < less than + - <= less than or equal to + - <=> combined comparison operator (if larger value is on left returns 1, on right -1, if equal 0) diff --git a/day_2/exercises/iteration.rb b/day_2/exercises/iteration.rb index a801cb4fc..31e60b01d 100644 --- a/day_2/exercises/iteration.rb +++ b/day_2/exercises/iteration.rb @@ -15,14 +15,24 @@ # "The is awesome!" for each animal: animals.each do |animal| - # YOUR CODE HERE + p "#{animal} is awesome!" # 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 = ["apples", "bananas", "oranges"] -# Write code that stores an array of numbers in a variable, -# then iterates over that array to print doubles of each number: +foods.each do |food| + p "Add #{food} to shopping list" +end + +# 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 +numbers = [0 , 1 , 2 , 3 , 4 , 5] + +numbers.each do |x| + p "#{x},#{x} " +end diff --git a/day_2/exercises/iteration_and_each.rb b/day_2/exercises/iteration_and_each.rb new file mode 100644 index 000000000..f49d357c6 --- /dev/null +++ b/day_2/exercises/iteration_and_each.rb @@ -0,0 +1,63 @@ +num_arr = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10] + +# If you had an array of numbers, e.g. [1,2,3,4], how do you print out the doubles of each number? Triples? +num_arr.each do |double| + print "#{double},#{double} " +end + +num_arr.each do |triple| + print "#{triple},#{triple},#{triple} " +end + +# If you had the same array, how would you only print out the even numbers? What about the odd numbers? +num_arr.each do |even| + if even % 2 == 0 + puts even + end +end + +num_arr.each do |odd| + if odd % 2 == 1 + puts odd + end +end + +# How could you create a new array which contains each number multipled by 2? +num_arr.each do |multipled| + print multipled * 2 +end + + +names = ["Joe Shmoe" , "John Doe", "Jane Doe"] + +# Given an array of first and last names, e.g. [“Alice Smith”, “Bob Evans”, “Roy Rogers”], how would you print out the full names line by line? +names.each do |name| + puts name +end + +# How would you print out only the first name? +names.each do |name| + puts name.split.first +end + +# How would you print out only the last name? +names.each do |name| + puts name.split.last +end + +# How could you print out only the initials? +names.each do |name| + puts "#{name.split.first[0 , 1]}.#{name.split.last[0 , 1]}." +end + +# How can you print out the last name and how many characters are in it? +names.each do |name| + puts "#{name.split.last} (#{name.split.last.length})." +end + +# How can you create an integer which represents the total number of characters in all the names? +names.each do |name| + name.gsub!(" ", "") +end + +print names.join.length diff --git a/day_2/questions.md b/day_2/questions.md index a179f0b04..a2df52adf 100644 --- a/day_2/questions.md +++ b/day_2/questions.md @@ -1,17 +1,39 @@ ## Day 2 Questions 1. Create an array containing the following strings: `"zebra", "giraffe", "elephant"`. +```ruby +["zebra", "giraffe", "elephant"] +``` 1. Save the array you created above to a variable `animals`. +```ruby +animals = ["zebra", "giraffe", "elephant"] +``` 1. Using the array `animals`, how would you access `"giraffe"`? +```ruby +p animals[1] +``` 1. How would you add `"lion"` to the `animals` array? +```ruby +animals << "lion" +``` 1. Name and describe two additional array methods: + - One array method is the `.index` method which will return the index value assigned to the object in the array specified in the argument. Another method is the `.shuffle` method which returns a new array with the objects of the array it was called on shuffled into a different order. 1. What are the boolean values in Ruby? + - The boolean values in Ruby are `true` and `false`. 1. In Ruby, how would you evaluate if `2` is equal to `25`? What is the result of this evaluation? +```ruby +2 == 25 +false +``` 1. In Ruby, how would you evaluate if `25` is greater than `2`? What is the result of this evaluation? +```ruby +25 > 2 +true +``` From 09741ff9ddae2c54f4e34677f973d1b2139cb99f Mon Sep 17 00:00:00 2001 From: loganjacob76 <75458154+loganjacob76@users.noreply.github.com> Date: Sat, 2 Jan 2021 19:54:19 -0700 Subject: [PATCH 03/11] Update boolean_practice.md --- day_2/exercises/boolean_practice.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/day_2/exercises/boolean_practice.md b/day_2/exercises/boolean_practice.md index 16d8c1668..bdcedeae9 100644 --- a/day_2/exercises/boolean_practice.md +++ b/day_2/exercises/boolean_practice.md @@ -61,10 +61,10 @@ ## Study Drills 1. There are a lot of operators in Ruby similar to != and ==. Try to find as many "equality operators" as you can. They should be like < or <=. Write out the names of each of these equality operators. For example, I call != "not equal." - - == equal - - != not equal - - > greater than - - >= greater than or equal to - - < less than - - <= less than or equal to - - <=> combined comparison operator (if larger value is on left returns 1, on right -1, if equal 0) + - `== equal` + - `!= not equal` + - `> greater than` + - `>= greater than or equal to` + - `< less than` + - `<= less than or equal to` + - `<=> combined comparison operator (if larger value is on left returns 1, on right -1, if equal 0)` From 94873ef3cb1d44b1da9da4ca1a0ebec9bbeea8fc Mon Sep 17 00:00:00 2001 From: loganjacob76 <75458154+loganjacob76@users.noreply.github.com> Date: Sat, 2 Jan 2021 19:57:04 -0700 Subject: [PATCH 04/11] Update array_methods.md --- day_2/array_methods.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/day_2/array_methods.md b/day_2/array_methods.md index 91ea0d252..cf1fabbda 100644 --- a/day_2/array_methods.md +++ b/day_2/array_methods.md @@ -1,17 +1,17 @@ -1. ".last": A method that allows the user to return the last element in an array. +1. `.last`: A method that allows the user to return the last element in an array. -2. ".sort": A method that returns a new array by sorting the array it is called on. The default method of sorting is alphabetical, but by using a code block the user can cause the method to sort the array based on a determined criteria. +2. `.sort`: A method that returns a new array by sorting the array it is called on. The default method of sorting is alphabetical, but by using a code block the user can cause the method to sort the array based on a determined criteria. -3. ".each": A method that allows the user to iterate through each item of an array. The user writes a code block to determine what they want done to each item in the array, and then uses this method to make it happen. +3. `.each`: A method that allows the user to iterate through each item of an array. The user writes a code block to determine what they want done to each item in the array, and then uses this method to make it happen. -4. ".join": A method that converts all elements in an array into string and combines them. The user may denote a separator to place between the elements of the array when they are put together. +4. `.join`: A method that converts all elements in an array into string and combines them. The user may denote a separator to place between the elements of the array when they are put together. -5. ".index": Each element in an array is assigned a numerical value, counting upwards from 0, known as its index. Using this method and specifying an element of the array (known as the argument) will return the index value of that element. The user can also write a code block which will return the index of the first object of the array which causes the block to return a value of 'true'. If there is no match it will return 'nil'. +5. `.index`: Each element in an array is assigned a numerical value, counting upwards from 0, known as its index. Using this method and specifying an element of the array (known as the argument) will return the index value of that element. The user can also write a code block which will return the index of the first object of the array which causes the block to return a value of 'true'. If there is no match it will return 'nil'. -6. ".include?": This method is paired with an argument that specifies an element that the user is searching for in an array. If the object exists in the array it will return the value 'true' otherwise it will return 'false'. +6. `.include?`: This method is paired with an argument that specifies an element that the user is searching for in an array. If the object exists in the array it will return the value 'true' otherwise it will return 'false'. -7. ".collect": Another method of iterating through an array. This method is paired with a code block and applies the block to each element in the array, returning a new array with the results of the iteration. +7. `.collect`: Another method of iterating through an array. This method is paired with a code block and applies the block to each element in the array, returning a new array with the results of the iteration. -8. ".first": Similar to the ".last" method, this method allows the user to return the first element in the array. +8. `.first`: Similar to the ".last" method, this method allows the user to return the first element in the array. -9. ".shuffle": A method that returns a new array with the same elements as the array it was called on, but shuffled into a different order. +9. `.shuffle`: A method that returns a new array with the same elements as the array it was called on, but shuffled into a different order. From c7b546de58f41883b240693b368f8362a23384ac Mon Sep 17 00:00:00 2001 From: Logan Anderson Date: Sun, 3 Jan 2021 20:33:09 -0700 Subject: [PATCH 05/11] Add Day 3 Work --- day_3/exercises/ex1.rb | 45 ++++++++++++++++++++ day_3/exercises/ex2.rb | 42 +++++++++++++++++++ day_3/exercises/ex3.rb | 70 ++++++++++++++++++++++++++++++++ day_3/exercises/if_statements.rb | 31 ++++++++++---- day_3/questions.md | 31 +++++++++++--- 5 files changed, 206 insertions(+), 13 deletions(-) create mode 100644 day_3/exercises/ex1.rb create mode 100644 day_3/exercises/ex2.rb create mode 100644 day_3/exercises/ex3.rb diff --git a/day_3/exercises/ex1.rb b/day_3/exercises/ex1.rb new file mode 100644 index 000000000..4fe7636b1 --- /dev/null +++ b/day_3/exercises/ex1.rb @@ -0,0 +1,45 @@ +people = 200 +cats = 3 +dogs = 25 + + +if people < cats + puts "Too many cats! The world is doomed!" +end + +if people > cats + puts "Not many 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 equal to dogs." +end + +if people <= dogs + puts "People are less than or equal to dogs." +end + + +if people == dogs + puts "People are dogs." +end + +# 1. I believe that the "if" will only run the code beneath it when specific criteria is met +# 2. I believe that the code under the "if" is indented for to make the code easier to read visually +# 3. The program will still have the same output if it isn't indented +# 4 +if cats != dogs + puts "The world is out of balance!" +end +# 5. Depending on how you change the initial values, different "if" statements may or may not meet the conditions to run their code diff --git a/day_3/exercises/ex2.rb b/day_3/exercises/ex2.rb new file mode 100644 index 000000000..69f9bde91 --- /dev/null +++ b/day_3/exercises/ex2.rb @@ -0,0 +1,42 @@ +people = 45 +cars = 25 +trucks = 60 + +# a. +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 + +# b. +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 + +# c. +if people > trucks + puts "Alright, let's just take the trucks." +else + puts "Fine, let's stay home then." +end + +# 1. I believe that elsif and else create other branches with boolean expressions. "Elsif" specifies requirements the same way that "if" does. +# "Else" basically returns true with anything that doesn't meet the requirements for "if" or "elsif". +# 2. a. "elsif" runs b. "if" runs c. "else" runs +# 3 & 4. +# If people are less than trucks AND NOT(cars are greater than trucks) +if people < trucks && !(cars > trucks) + # Prints string if boolean returns true + puts "Well we seem to have enough trucks." +# Should the "if" boolean return false, this will be evaluated +elsif cars > trucks || people > trucks + # If it returns true this string will be printed + puts "I'm thinking we definitely shouldn't take the trucks." +end diff --git a/day_3/exercises/ex3.rb b/day_3/exercises/ex3.rb new file mode 100644 index 000000000..4b30e9011 --- /dev/null +++ b/day_3/exercises/ex3.rb @@ -0,0 +1,70 @@ +puts "You enter a dark room with multiple doors. Do you go through door #1, door #2, or door #3?" + +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." + + 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!" + else + puts "Well, doing %s is probably better. Bear runs away." % bear + end + +elsif door == "2" + puts "You stare into the endless abyss at Cthulhus'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 "You are locked in a room quickly filling with water." + puts "1. Hold your breath and look around for a way to drain the water." + puts "2. Look around the top of the room for an air vent or some other way out of the room." + puts "3. Search the room for a key to the door on the opposite side of the room." + + print "> " + swim = $stdin.gets.chomp + + if swim == "1" + puts "The room gets drained from the outside...after you die. Good job!" + elsif swim == "2" + puts "There are air vents in multiple spots on the roof! Too bad they're squirrel sized. Good job!" + elsif swim == "3" + puts "You spot two keys, but only have time to grab and try one. Do you try key #1 or key #2." + + print "> " + key = $stdin.gets.chomp + + if key == "1" + puts "Shame, wrong key. Fishes for you I guess. Good job!" + elsif key == "2" + puts "The key works! Unfortunately on the other side of the door was a hungry shark. Good job!" + else + puts "Doing %s wasn't a great idea, it caused you to drown. Good job!" % key + end + + else + puts "Doing %s backfired, the room just got electrified. Good job!" % swim + end + +else + puts "You stumble around and fall on a knife and die. Good job!" +end diff --git a/day_3/exercises/if_statements.rb b/day_3/exercises/if_statements.rb index a80b96840..3a132fce8 100644 --- a/day_3/exercises/if_statements.rb +++ b/day_3/exercises/if_statements.rb @@ -3,14 +3,17 @@ # 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" # if it is snowy, print "coat" # if it is icy, print "yak traks" - weather = 'snowy' +puts "How is the weather today?" + +print "> " + weather = $stdin.gets.chomp if weather == 'sunny' p "sunscreen" @@ -35,21 +38,27 @@ # 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 +puts "How many quarters do I have?" + +print "> " +num_quarters = $stdin.gets.chomp.to_i -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" +else + 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 +70,11 @@ # 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 has_sauce == true && cups_of_flour >= 2 + p "I can make pizza." +else + p "I cannot make pizza." +end diff --git a/day_3/questions.md b/day_3/questions.md index db6170fa7..afa3808cb 100644 --- a/day_3/questions.md +++ b/day_3/questions.md @@ -1,13 +1,34 @@ ## Day 3 Questions 1. What is a conditional statement? Give three examples. + - A conditional statement is a statement that compares the value of a variable against its argument to return a boolean value. Should the value return as true, ruby will know to run the code block associated with the statement. One example is the `if` statement which is always the first evaluated. Another is the `elsif` statement which is optional, but can be used repeatedly and does the same thing as the `if` statement, but is evaluated after the `if` statement. A third example is the `else` statement which is the final one evaluated. It is also optional, but can only be used one time because it always returns true as long as neither the `if` nor any of the `elsif`s return true. -1. Why might you want to use an if-statement? +2. Why might you want to use an if-statement? + - An `if` statement allows you to write more intelligent code that can react any number of ways depending on the variable or user input being compared. -1. What is the Ruby syntax for an if statement? +3. What is the Ruby syntax for an if statement? + - In ruby the `if`, `elsif`, `else`, and `end` are all on the same plane, but the blocks inside them are indented twice. For `if` statements nested inside other `if` statements they begin on the same plane as the blocks and their own blocks are further indented twice. -1. How do you add multiple conditions to an if statement? +4. How do you add multiple conditions to an if statement? + - multiple conditions are added by using `and (&&)` and `or (||)` operators. The `&&` operator requires both sides of the operator to be true in order to return true. On the other hand, the `||` operator only requires one side to be true. -1. Provide an example of the Ruby syntax for an if/elsif/else statement: +5. Provide an example of the Ruby syntax for an if/elsif/else statement: +```ruby +p "Pick a number 1 - 3." -1. Other than an if-statement, can you think of any other ways we might want to use a conditional statement? +print "> " +num = $stdin.gets.chomp.to_i + +if num == 1 + p "You chose 1." +elsif num == 2 + p "You chose 2." +elsif num == 3 + p "You chose 3." +else + p "You didn't follow instructions." +end +``` + +6. Other than an if-statement, can you think of any other ways we might want to use a conditional statement? + - Maybe for a test or something you might want to compare the input answer to the correct answer. From baa7534298e0662784d9a1a5137b9cc3ace9a930 Mon Sep 17 00:00:00 2001 From: Logan Anderson Date: Sun, 3 Jan 2021 20:36:00 -0700 Subject: [PATCH 06/11] Day 3 README --- day_3/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/day_3/README.md b/day_3/README.md index d4534e0e1..5ce4795a3 100644 --- a/day_3/README.md +++ b/day_3/README.md @@ -12,13 +12,13 @@ Using your terminal, open your local copy of the forked repository you created d 1. Work through the following lessons. Any files that you create while working can be kept in today's `exercises` directory. - - [ ] [What If?](https://learnrubythehardway.org/book/ex29.html) from Learn Ruby the Hard Way. + - [x] [What If?](https://learnrubythehardway.org/book/ex29.html) from Learn Ruby the Hard Way. - - [ ] [Else and If](https://learnrubythehardway.org/book/ex30.html) from Learn Ruby the Hard Way. + - [x] [Else and If](https://learnrubythehardway.org/book/ex30.html) from Learn Ruby the Hard Way. - - [ ] [Making Decisions](https://learnrubythehardway.org/book/ex31.html) from Learn Ruby the Hard Way. + - [x] [Making Decisions](https://learnrubythehardway.org/book/ex31.html) from Learn Ruby the Hard Way. - - [ ] [Conditionals](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#9.-conditionals) from Ruby in 100 Minutes. + - [x] [Conditionals](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#9.-conditionals) from Ruby in 100 Minutes. 1. Work through the exercise files in the day_3/exercises directory. From 074090646bb46165ac90f015d61de26905d2b4c5 Mon Sep 17 00:00:00 2001 From: Logan Anderson Date: Tue, 5 Jan 2021 18:23:02 -0700 Subject: [PATCH 07/11] Add Day 4 Work --- day_4/exercises/ex2.rb | 26 ++++++++++++++++++++++++++ day_4/exercises/ex3.rb | 36 ++++++++++++++++++++++++++++++++++++ day_4/exercises/ex4.rb | 36 ++++++++++++++++++++++++++++++++++++ day_4/exercises/methods.rb | 20 +++++++++++++++----- day_4/exercises/say.rb | 8 ++++++++ day_4/questions.md | 23 +++++++++++++++++++---- 6 files changed, 140 insertions(+), 9 deletions(-) create mode 100644 day_4/exercises/ex2.rb create mode 100644 day_4/exercises/ex3.rb create mode 100644 day_4/exercises/ex4.rb create mode 100644 day_4/exercises/say.rb diff --git a/day_4/exercises/ex2.rb b/day_4/exercises/ex2.rb new file mode 100644 index 000000000..dca7c8fd0 --- /dev/null +++ b/day_4/exercises/ex2.rb @@ -0,0 +1,26 @@ +# Like scripts with ARGV +def print_two(*args) + arg1, arg2 = args + puts "arg1: #{arg1}, arg2: #{arg2}" +end + +# Instead of "*args" +def print_two_again(arg1, arg2) + puts "arg1: #{arg1}, arg2: #{arg2}" +end + +# 1 argument +def print_one(arg1) + puts "arg1: #{arg1}" +end + +# No arguments +def print_none() + puts "I got nothin'." +end + + +print_two("Zed","Shaw") +print_two_again("Zed","Shaw") +print_one("First") +print_none() diff --git a/day_4/exercises/ex3.rb b/day_4/exercises/ex3.rb new file mode 100644 index 000000000..a3681d4bb --- /dev/null +++ b/day_4/exercises/ex3.rb @@ -0,0 +1,36 @@ +# Creates a new method and sets the arguments that will be passed through to it +def cheese_and_crackers(cheese_count, boxes_of_crackers) + # Prints string with interpolation of first argument + puts "You have #{cheese_count} cheeses!" + # Prints string with interpolation of second argument + puts "You have #{boxes_of_crackers} boxes of crackers!" + # Prints string + puts "Man that's enough for a party!" + # Prints string with a new line symbol + puts "Get a blanket.\n" +# Notes that everything after this point is no longer part of the method +end + +# Prints string +puts "We can just give the function numbers directly:" +# Calls the method and feeds the integers as the arguments +cheese_and_crackers(20, 30) + +# Prints string +puts "OR, we can use variables from our script:" +# Assigns integer value to variable 1 +amount_of_cheese = 10 +# Assigns integer value to variable 2 +amount_of_crackers = 50 +# Calls the method feeding variables 1 and 2 as the arguments +cheese_and_crackers(amount_of_cheese, amount_of_crackers) + +# Prints string +puts "We can even do math inside too:" +# Calls the method with math as the arguments where the output is the solution to the math problem +cheese_and_crackers(10 + 20, 5 + 6) + +# Prints string +puts "And we can combine the two, variables and math:" +# Calls the method with variables 1 and 2 used with math as the arguments where the output is the solution of the value of the variables and the integers +cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) diff --git a/day_4/exercises/ex4.rb b/day_4/exercises/ex4.rb new file mode 100644 index 000000000..6c1d4be8a --- /dev/null +++ b/day_4/exercises/ex4.rb @@ -0,0 +1,36 @@ +def add(a, b) + puts "ADDING #{a} + #{b}" + return a + b +end + +def subtract(a, b) + puts "SUBTRACTING #{a} - #{b}" + return a - b +end + +def multiply(a, b) + puts "MULTIPLYING #{a} * #{b}" + return a * b +end + +def divide(a, b) + puts "DIVIDING #{a} / #{b}" + return a / b +end + + +puts "Let's do some math with just functions!" + +age = add(30, 5) +height = subtract(78, 4) +weight = multiply(90, 2) +iq = divide(100, 2) + +puts "Age: #{age}, Height: #{height}, Weight: #{weight}, IQ: #{iq}" + + +puts "Here is a puzzle" + +what = add(age, subtract(height, multiply(weight, divide(iq, 2)))) + +puts "That becomes: #{what}. Can you do it by hand?" diff --git a/day_4/exercises/methods.rb b/day_4/exercises/methods.rb index 6ed338e5d..8eb6064c0 100644 --- a/day_4/exercises/methods.rb +++ b/day_4/exercises/methods.rb @@ -12,16 +12,26 @@ def print_name # Write a method that takes a name as an argument and prints it: def print_name(name) - # YOUR CODE HERE + p 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 add(a , b) + p a + b +end + +add(5 , 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 full_name(first , last) + p "My first name is #{first} and my last name is #{last}." +end + +full_name("John", "Doe") diff --git a/day_4/exercises/say.rb b/day_4/exercises/say.rb new file mode 100644 index 000000000..c9c80fca6 --- /dev/null +++ b/day_4/exercises/say.rb @@ -0,0 +1,8 @@ +def say(words='hello') + puts words + '.' +end + +say() +say("hi") +say("how are you") +say("I'm fine") diff --git a/day_4/questions.md b/day_4/questions.md index af17ab4da..7b21b84b9 100644 --- a/day_4/questions.md +++ b/day_4/questions.md @@ -1,11 +1,26 @@ ## Day 4 Questions 1. In your own words, what is the purpose of a method? + - A method is a shortcut for when the programmer needs to do the same thing over and over again, but to for different values. It is especially useful because If you realize that you messed something up or left something out you only need to change it one time inside of the method instead of any number of times and risk missing an instance of it. -1. Create a method named `hello` that will print `"Sam I am"`. +2. Create a method named `hello` that will print `"Sam I am"`. +```ruby +def hello() + p "Sam I am" +end +``` -1. Create a method named `hello_someone` that takes an argument of `name` and prints `"#{name} I am"`. +3. Create a method named `hello_someone` that takes an argument of `name` and prints `"#{name} I am"`. +```ruby +def hello_someone(name) + p "#{name} I am" +end +``` -1. How would you call or execute the method that you created above? +4. How would you call or execute the method that you created above? +```ruby +hello_someone(Sam) +``` -1. What questions do you have about methods in Ruby? +5. What questions do you have about methods in Ruby? + - I might be confused, but with pre-defined methods like `length` you can call them by using a period like `.length`, however I could not seem to get that to work for the methods that I created. Is there a way to do that or am I just mixing things up? From 29bdfb689b46f0ea258e22b38b23b0d425965890 Mon Sep 17 00:00:00 2001 From: Logan Anderson Date: Wed, 6 Jan 2021 12:47:23 -0700 Subject: [PATCH 08/11] Add Day 5 Work --- day_5/exercises/ex1.rb | 55 +++++++++++++++++++++++++++++++++++++++ day_5/exercises/hashes.rb | 17 +++++++----- day_5/questions.md | 27 +++++++++++++++---- 3 files changed, 88 insertions(+), 11 deletions(-) create mode 100644 day_5/exercises/ex1.rb diff --git a/day_5/exercises/ex1.rb b/day_5/exercises/ex1.rb new file mode 100644 index 000000000..f89b7b25e --- /dev/null +++ b/day_5/exercises/ex1.rb @@ -0,0 +1,55 @@ +states = { + 'Oregon' => 'OR', + 'Florida' => 'FL', + 'California' => 'CA', + 'New York' => 'NY', + 'Michigan' => 'MI' +} + +cities = { + 'CA' => 'San Francisco', + 'MI' => 'Detroit', + 'FL' => 'Jacksonville' +} + +cities['NY'] = 'New York' +cities['OR'] = 'Portland' + +puts '-' * 10 +puts "NY State has: #{cities['NY']}" +puts "OR State has: #{cities['OR']}" + +puts '-' * 10 +puts "Michigan's abbreviation is: #{states['Michigan']}" +puts "Florida's abbreviation is: #{states['Florida']}" + +puts '-' * 10 +puts "Michigan has: #{cities[states['Michigan']]}" +puts "Florida has: #{cities[states['Florida']]}" + +puts '-' * 10 +states.each do |state, abbrev| + puts "#{state} is abbreviated #{abbrev}" +end + +puts '-' * 10 +cities.each do |abbrev, city| + puts "#{abbrev} has the city #{city}" +end + +puts '-' * 10 +states.each do |state, abbrev| + city = cities[abbrev] + puts "#{state} is abbreviated #{abbrev} and has the city #{city}" +end + +puts '-' * 10 +state = states['Texas'] + +if !state + puts "Sorry, no Texas." +end + +city = cities['TX'] +city ||= 'Does Not Exist' +puts "The city for the state 'TX' is: #{city}" diff --git a/day_5/exercises/hashes.rb b/day_5/exercises/hashes.rb index 99fcebb77..52c9c3393 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,26 @@ p foods # Write code that prints a hash holding zoo animal inventory: -zoo = #YOUR CODE HERE +zoo = {lions: 3, tigers: 6, penguins: 12} #YOUR CODE HERE 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 +puts 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 +puts 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 +zoo[:lions] -# 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 +zoo[:elephants] = 2 +p zoo diff --git a/day_5/questions.md b/day_5/questions.md index d059e12c6..f44988e49 100644 --- a/day_5/questions.md +++ b/day_5/questions.md @@ -1,13 +1,30 @@ ## Day 5 Questions 1. What is a Hash, and how is it different from an Array? + - A hash is another way to store data similar to an array. A hash uses `keys` which are assigned `values` and these keys are called upon in order to return the assigned value. A hash is different from an array because an array is ordered and each object is assigned an index value which can be called to return the object. Hashes are unordered and can only return values when the specific and unique key is called. -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. +2. 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. +```ruby +pet_store = { + chew_toys: 15, + food_bags: 30, + treat_bags: 55 +} +``` -1. Given the following `states = {"CO" => "Colorado", "IA" => "Iowa", "OK" => "Oklahoma"}`, how would you access the value `"Iowa"`? +3. Given the following `states = {"CO" => "Colorado", "IA" => "Iowa", "OK" => "Oklahoma"}`, how would you access the value `"Iowa"`? +```ruby +states["IA"] +``` -1. With the same hash above, how would we get all the keys? How about all the values? +4. With the same hash above, how would we get all the keys? How about all the values? +```ruby +keys = states.keys +values = 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? +5. What is another example of when we might use a hash? In your example, why is a hash better than an array? + - Another time to use a hash might be like for a catalog, where entering the item name will return its price. A hash can be superior to an array especially if you are storing large amounts of data in them, because with many objects in an array it might be difficult to figure out the index number every time you want to call an object whereas with hashes it is much simpler to call the key. -1. What questions do you still have about hashes? +6. What questions do you still have about hashes? + - I was wondering about the syntax that is generally used with hashes, are they standardly made with colons or rockets? From 4b35228a8547bb39496ee78c04f27d9f96a81b88 Mon Sep 17 00:00:00 2001 From: Logan Anderson Date: Wed, 6 Jan 2021 16:05:39 -0700 Subject: [PATCH 09/11] Add Day 6 Work --- day_6/exercises/burrito.rb | 20 +++++++++++++++++++- day_6/exercises/dog.rb | 8 +++++++- day_6/exercises/person.rb | 23 ++++++++++++++++++++++- day_6/exercises/student.rb | 15 +++++++++++++++ day_6/questions.md | 36 +++++++++++++++++++++++++++++++----- 5 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 day_6/exercises/student.rb diff --git a/day_6/exercises/burrito.rb b/day_6/exercises/burrito.rb index 967f68b6c..2b2cf9dd1 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 @@ -11,9 +11,27 @@ def initialize(protein, base, toppings) @base = base @toppings = toppings end + + def add_topping(new_topping) + toppings << new_topping + end + + def remove_topping(bad_topping) + toppings.delete(bad_topping) + end + + def change_protein(new_protein) + @protein = new_protein + end end dinner = Burrito.new("Beans", "Rice", ["cheese", "salsa", "guacamole"]) p dinner.protein p dinner.base p dinner.toppings +dinner.add_topping("Corn") +p dinner.toppings +dinner.remove_topping("salsa") +p dinner.toppings +dinner.change_protein("Chicken") +p dinner.protein diff --git a/day_6/exercises/dog.rb b/day_6/exercises/dog.rb index 03221314d..0b3a9369a 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 @@ -19,6 +19,10 @@ def bark def eat @hungry = false end + + def play + @hungry = true + end end fido = Dog.new("Bernese", "Fido", 4) @@ -28,3 +32,5 @@ def eat p fido.hungry fido.eat p fido.hungry +fido.play +p fido.hungry diff --git a/day_6/exercises/person.rb b/day_6/exercises/person.rb index 2c26e9570..97b4eb519 100644 --- a/day_6/exercises/person.rb +++ b/day_6/exercises/person.rb @@ -1,5 +1,26 @@ -# 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 :first_name, :hobby + + def initialize(first_name, hobby) + @first_name = first_name + @hobby = hobby + end + + def introduction + "Hello everybody, my name is #{first_name}!" + end + + def conversation + "I would have to say that my favorite activity is #{hobby}!" + end +end + +john = Person.new("John", "hiking") +p john.introduction +p john.conversation +p john.introduction + " " + john.conversation diff --git a/day_6/exercises/student.rb b/day_6/exercises/student.rb new file mode 100644 index 000000000..30ae432b2 --- /dev/null +++ b/day_6/exercises/student.rb @@ -0,0 +1,15 @@ +class Student + attr_accessor :first_name, :last_name, :primary_phone_number + + def introduction(target) + puts "Hi #{target}, I'm #{first_name}!" + end + + def favorite_number + 7 + end +end + +frank = Student.new +frank.first_name = "Frank" +puts "Frank's favorite number is #{frank.favorite_number}." diff --git a/day_6/questions.md b/day_6/questions.md index f58ca5f71..292254d02 100644 --- a/day_6/questions.md +++ b/day_6/questions.md @@ -1,13 +1,39 @@ ## Day 6 Questions 1. In your own words, what is a Class? + - A class defines a type of object in programming. There can be countless iterations of that class, but all of them will share the specific defining characteristics of that class. For example, a `People` class would contain all the people in the world, who vary in all sorts of ways. However, those people would all still share the same basic attributes of that class that makes people human. -1. What is an attribute of a Class? +2. What is an attribute of a Class? + - An attribute of a class is a characteristic that all instances of that class share. For example in the `People` class some attributes may include things like `name`, `hair_color`, `height`, etc. -1. What is behavior of a Class? +3. What is behavior of a Class? + - A behavior of a class is a common action that all instances of a class perform. For example in the `People` class some behaviors might include things like `eat`, `sleep`, `communicate`, etc. -1. In the space below, create a Dog class with at least 2 attributes and 2 behaviors: +4. In the space below, create a Dog class with at least 2 attributes and 2 behaviors: +```ruby +class Dog + attr_reader :fur_luster, :tired -1. How do you create an instance of a class? +def bath + fur_luster = shiny +end -1. What questions do you still have about classes in Ruby? + def play + tired = true + end +end +``` + +5. How do you create an instance of a class? +```ruby +# Create the class +class Person + # Give the class attributes and behaviors here +end + +# Create a new instance +john = Person.new +``` + +6. What questions do you still have about classes in Ruby? + - I was confused a bit about the variables in a class. Specifically about the variables that began with an `@`. What does that symbol do? Why and when is it supposed to be used? From 69e97c237abe12750429bf2c1509956fd9a23925 Mon Sep 17 00:00:00 2001 From: Logan Anderson Date: Wed, 6 Jan 2021 22:20:55 -0700 Subject: [PATCH 10/11] Add Day 7 Work --- day_7/10_speckled_frogs.rb | 11 ++++++++ day_7/10_speckled_frogs_ext1.rb | 24 +++++++++++++++++ day_7/10_speckled_frogs_ext2.rb | 15 +++++++++++ day_7/checker_board.rb | 17 ++++++++++++ day_7/fizzbuzz.rb | 46 +++++++++++++++++++++++++++++++++ day_7/high_level.md | 19 ++++++++++++++ 6 files changed, 132 insertions(+) create mode 100644 day_7/10_speckled_frogs.rb create mode 100644 day_7/10_speckled_frogs_ext1.rb create mode 100644 day_7/10_speckled_frogs_ext2.rb create mode 100644 day_7/checker_board.rb create mode 100644 day_7/fizzbuzz.rb create mode 100644 day_7/high_level.md diff --git a/day_7/10_speckled_frogs.rb b/day_7/10_speckled_frogs.rb new file mode 100644 index 000000000..419180f9a --- /dev/null +++ b/day_7/10_speckled_frogs.rb @@ -0,0 +1,11 @@ +# Required +10.downto(1).each do |frog| + + if frog == 1 + puts "#{frog} speckled frog sat on a log eating some most delicious bugs. One jumped in the pool where its nice and cool, then there were no more speckled frogs!" + elsif frog == 2 + puts "#{frog} speckled frogs sat on a log eating some most delicious bugs. One jumped in the pool where its nice and cool, then there was #{frog - 1} speckled frog." + else + puts "#{frog} speckled frogs sat on a log eating some most delicious bugs. One jumped in the pool where its nice and cool, then there were #{frog - 1} speckled frogs." + end +end diff --git a/day_7/10_speckled_frogs_ext1.rb b/day_7/10_speckled_frogs_ext1.rb new file mode 100644 index 000000000..266cb103b --- /dev/null +++ b/day_7/10_speckled_frogs_ext1.rb @@ -0,0 +1,24 @@ +# Extension 1 +num = { + 10 => "ten", + 9 => "nine", + 8 => "eight", + 7 => "seven", + 6 => "six", + 5 => "five", + 4 => "four", + 3 => "three", + 2 => "two", + 1 => "one" +} + +num.each do |x , frog| + + if frog == "one" + puts "#{frog.capitalize} speckled frog sat on a log eating some most delicious bugs. One jumped in the pool where its nice and cool, then there were no more speckled frogs!" + elsif frog == "two" + puts "#{frog.capitalize} speckled frogs sat on a log eating some most delicious bugs. One jumped in the pool where its nice and cool, then there was #{num[x - 1]} speckled frog." + else + puts "#{frog.capitalize} speckled frogs sat on a log eating some most delicious bugs. One jumped in the pool where its nice and cool, then there were #{num[x - 1]} speckled frogs." + end +end diff --git a/day_7/10_speckled_frogs_ext2.rb b/day_7/10_speckled_frogs_ext2.rb new file mode 100644 index 000000000..ea127d747 --- /dev/null +++ b/day_7/10_speckled_frogs_ext2.rb @@ -0,0 +1,15 @@ +# Extension 2 +puts "What is the max number of frogs you would like to have?" +print "> " +max_num = gets.to_i + +max_num.downto(1).each do |frog| + + if frog == 1 + puts "#{frog} speckled frog sat on a log eating some most delicious bugs. One jumped in the pool where its nice and cool, then there were no more speckled frogs!" + elsif frog == 2 + puts "#{frog} speckled frogs sat on a log eating some most delicious bugs. One jumped in the pool where its nice and cool, then there was #{frog - 1} speckled frog." + else + puts "#{frog} speckled frogs sat on a log eating some most delicious bugs. One jumped in the pool where its nice and cool, then there were #{frog - 1} speckled frogs." + end +end diff --git a/day_7/checker_board.rb b/day_7/checker_board.rb new file mode 100644 index 000000000..fbc3e0dd9 --- /dev/null +++ b/day_7/checker_board.rb @@ -0,0 +1,17 @@ +class Board + attr_reader :size + + def initialize(size) + @size = (size / 2) + end + + def pattern + size.times { + puts "X " * size + puts " X" * size + } + end +end + +example = Board.new(8) +example.pattern diff --git a/day_7/fizzbuzz.rb b/day_7/fizzbuzz.rb new file mode 100644 index 000000000..2af232f88 --- /dev/null +++ b/day_7/fizzbuzz.rb @@ -0,0 +1,46 @@ +(1..100).each do |number| + + if number == 100 + puts "Buzz " + elsif number % 3 == 0 && number % 5 == 0 + print "FizzBuzz, " + elsif number % 3 == 0 + print "Fizz, " + elsif number % 5 == 0 + print "Buzz, " + else + print "#{number}, " + end +end + + + +# Bonus +puts "What number would you like to start your range at?" +print "> " +beginning_num = gets.chomp.to_i + +puts "What number would you like to end your range at?" +print "> " +ending_num = gets.chomp.to_i + +(beginning_num..ending_num).each do |number| + + if number % 3 == 0 && number % 5 == 0 && number == ending_num + print "FizzBuzz " + elsif number % 3 == 0 && number == ending_num + print "Fizz " + elsif number % 5 == 0 && number == ending_num + print "Buzz " + elsif number == ending_num + print "#{number} " + elsif number % 3 == 0 && number % 5 == 0 && !(number == ending_num) + print "FizzBuzz, " + elsif number % 3 == 0 && !(number == ending_num) + print "Fizz, " + elsif number % 5 == 0 && !(number == ending_num) + print "Buzz, " + else + print "#{number}, " + end +end diff --git a/day_7/high_level.md b/day_7/high_level.md new file mode 100644 index 000000000..f80b7e172 --- /dev/null +++ b/day_7/high_level.md @@ -0,0 +1,19 @@ +### Checker Board + +- The first step to creating a checker board is to create a new class for that board. + +- The class will need an attribute reader and also needs to be initialized to be fed the size of the board as the attribute. + +- In the initialize method of the class make sure the size attribute is saved to be half of itself (to be explained soon). + +- Now the class needs a method that lets it know to print the Xs and blank spaces as well as how many times they should be printed. + +- Inside the method should be a loop that repeats the number of times of the size attribute (which will actually be size / 2). + +- Inside this loop will be two lines of code, one that `puts` an X then a blank space and multiplies that string by the size attribute, and one that `puts` a blank space then an X and multiplies that string by the size attribute. It is important to switch the blank space and the X because this creates the checkerboard effect, otherwise there would just be a column of Xs and a column of blank spaces. + +- This explains why the size attribute is divided by two. Each X and blank space counts as one space on the board, so each string represents two spaces. Therefore, you need to multiply the string by half the number of spaces you want to get the correct number of spaces. This works well because our `puts` commands mean we have two lines print out every time the method loops. So for that reason you would also need to multiply the height by half the desired number of spaces to get the correct height. + +- The next step is to create a new board by creating a variable that is assigned `Class.new` with the size fed in via parentheses. Then call the method on the variable and up pops your new board! + +`For an example check out the checker_board.rb file!` From fe42de95d83438b2d00cbbc0ffca720ea3918dee Mon Sep 17 00:00:00 2001 From: Logan Anderson Date: Wed, 6 Jan 2021 22:27:10 -0700 Subject: [PATCH 11/11] Move work to directory --- day_7/{ => my_work}/10_speckled_frogs.rb | 0 day_7/{ => my_work}/10_speckled_frogs_ext1.rb | 0 day_7/{ => my_work}/10_speckled_frogs_ext2.rb | 0 day_7/{ => my_work}/checker_board.rb | 0 day_7/{ => my_work}/fizzbuzz.rb | 0 day_7/{ => my_work}/high_level.md | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename day_7/{ => my_work}/10_speckled_frogs.rb (100%) rename day_7/{ => my_work}/10_speckled_frogs_ext1.rb (100%) rename day_7/{ => my_work}/10_speckled_frogs_ext2.rb (100%) rename day_7/{ => my_work}/checker_board.rb (100%) rename day_7/{ => my_work}/fizzbuzz.rb (100%) rename day_7/{ => my_work}/high_level.md (100%) diff --git a/day_7/10_speckled_frogs.rb b/day_7/my_work/10_speckled_frogs.rb similarity index 100% rename from day_7/10_speckled_frogs.rb rename to day_7/my_work/10_speckled_frogs.rb diff --git a/day_7/10_speckled_frogs_ext1.rb b/day_7/my_work/10_speckled_frogs_ext1.rb similarity index 100% rename from day_7/10_speckled_frogs_ext1.rb rename to day_7/my_work/10_speckled_frogs_ext1.rb diff --git a/day_7/10_speckled_frogs_ext2.rb b/day_7/my_work/10_speckled_frogs_ext2.rb similarity index 100% rename from day_7/10_speckled_frogs_ext2.rb rename to day_7/my_work/10_speckled_frogs_ext2.rb diff --git a/day_7/checker_board.rb b/day_7/my_work/checker_board.rb similarity index 100% rename from day_7/checker_board.rb rename to day_7/my_work/checker_board.rb diff --git a/day_7/fizzbuzz.rb b/day_7/my_work/fizzbuzz.rb similarity index 100% rename from day_7/fizzbuzz.rb rename to day_7/my_work/fizzbuzz.rb diff --git a/day_7/high_level.md b/day_7/my_work/high_level.md similarity index 100% rename from day_7/high_level.md rename to day_7/my_work/high_level.md