From 1a358f3b4bef5b2f42e209357adf62e1708e4b6b Mon Sep 17 00:00:00 2001 From: Wyatt Wicks Date: Mon, 21 Dec 2020 14:04:07 -0700 Subject: [PATCH 01/12] Add practice directories and files --- day_0/_secret_library/Gemfile | 0 day_0/_secret_library/README.md | 0 day_0/_secret_library/Rakefile | 0 day_0/_secret_library/_lib/library_system.rb | 0 day_0/_secret_library/_lib/patron.rb | 0 day_0/_secret_library/_lib/secret_book.rb | 0 day_0/_secret_library/_lib/secret_librarian.rb | 0 day_0/_secret_library/_lib/secret_library.rb | 0 day_0/_secret_library/_test/library_system_test.rb | 0 day_0/_secret_library/_test/patron_test.rb | 0 day_0/_secret_library/_test/secret_book_test.rb | 0 day_0/_secret_library/_test/secret_librarian_test.rb | 0 day_0/_secret_library/_test/secret_library_test.rb | 0 13 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 day_0/_secret_library/Gemfile create mode 100644 day_0/_secret_library/README.md create mode 100644 day_0/_secret_library/Rakefile create mode 100644 day_0/_secret_library/_lib/library_system.rb create mode 100644 day_0/_secret_library/_lib/patron.rb create mode 100644 day_0/_secret_library/_lib/secret_book.rb create mode 100644 day_0/_secret_library/_lib/secret_librarian.rb create mode 100644 day_0/_secret_library/_lib/secret_library.rb create mode 100644 day_0/_secret_library/_test/library_system_test.rb create mode 100644 day_0/_secret_library/_test/patron_test.rb create mode 100644 day_0/_secret_library/_test/secret_book_test.rb create mode 100644 day_0/_secret_library/_test/secret_librarian_test.rb create mode 100644 day_0/_secret_library/_test/secret_library_test.rb diff --git a/day_0/_secret_library/Gemfile b/day_0/_secret_library/Gemfile new file mode 100644 index 000000000..e69de29bb diff --git a/day_0/_secret_library/README.md b/day_0/_secret_library/README.md new file mode 100644 index 000000000..e69de29bb diff --git a/day_0/_secret_library/Rakefile b/day_0/_secret_library/Rakefile new file mode 100644 index 000000000..e69de29bb diff --git a/day_0/_secret_library/_lib/library_system.rb b/day_0/_secret_library/_lib/library_system.rb new file mode 100644 index 000000000..e69de29bb diff --git a/day_0/_secret_library/_lib/patron.rb b/day_0/_secret_library/_lib/patron.rb new file mode 100644 index 000000000..e69de29bb diff --git a/day_0/_secret_library/_lib/secret_book.rb b/day_0/_secret_library/_lib/secret_book.rb new file mode 100644 index 000000000..e69de29bb diff --git a/day_0/_secret_library/_lib/secret_librarian.rb b/day_0/_secret_library/_lib/secret_librarian.rb new file mode 100644 index 000000000..e69de29bb diff --git a/day_0/_secret_library/_lib/secret_library.rb b/day_0/_secret_library/_lib/secret_library.rb new file mode 100644 index 000000000..e69de29bb diff --git a/day_0/_secret_library/_test/library_system_test.rb b/day_0/_secret_library/_test/library_system_test.rb new file mode 100644 index 000000000..e69de29bb diff --git a/day_0/_secret_library/_test/patron_test.rb b/day_0/_secret_library/_test/patron_test.rb new file mode 100644 index 000000000..e69de29bb diff --git a/day_0/_secret_library/_test/secret_book_test.rb b/day_0/_secret_library/_test/secret_book_test.rb new file mode 100644 index 000000000..e69de29bb diff --git a/day_0/_secret_library/_test/secret_librarian_test.rb b/day_0/_secret_library/_test/secret_librarian_test.rb new file mode 100644 index 000000000..e69de29bb diff --git a/day_0/_secret_library/_test/secret_library_test.rb b/day_0/_secret_library/_test/secret_library_test.rb new file mode 100644 index 000000000..e69de29bb From 05a8ecaee1cbc80727b8b6ba8af938c696ae0910 Mon Sep 17 00:00:00 2001 From: Wyatt Wicks Date: Mon, 21 Dec 2020 16:21:07 -0700 Subject: [PATCH 02/12] Add day 1 Ruby the hard way exercises --- day_1/ex1.rb | 8 ++++++++ day_1/ex2.rb | 9 +++++++++ day_1/ex3-5.rb | 2 ++ day_1/ex3.rb | 30 ++++++++++++++++++++++++++++++ day_1/ex4.rb | 33 +++++++++++++++++++++++++++++++++ day_1/ex5.rb | 20 ++++++++++++++++++++ day_1/ex6.rb | 36 ++++++++++++++++++++++++++++++++++++ day_1/ex7.rb | 8 ++++++++ 8 files changed, 146 insertions(+) create mode 100644 day_1/ex1.rb create mode 100644 day_1/ex2.rb create mode 100644 day_1/ex3-5.rb create mode 100644 day_1/ex3.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 diff --git a/day_1/ex1.rb b/day_1/ex1.rb new file mode 100644 index 000000000..49bd8a1e1 --- /dev/null +++ b/day_1/ex1.rb @@ -0,0 +1,8 @@ + "Hello World!" +"hello Again" + "I like typing this." + "this is fun." + "yay! Printing." +#puts "I'd much rather you 'not'." + 'I "said" do not touch this.' + "another line" diff --git a/day_1/ex2.rb b/day_1/ex2.rb new file mode 100644 index 000000000..d192d9583 --- /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-5.rb b/day_1/ex3-5.rb new file mode 100644 index 000000000..da0d936bc --- /dev/null +++ b/day_1/ex3-5.rb @@ -0,0 +1,2 @@ +puts "How long will my next break be if I need to let the dogs out for 5 minutes, and drink coffee for 2 minutes" +puts 5 + 2 diff --git a/day_1/ex3.rb b/day_1/ex3.rb new file mode 100644 index 000000000..ed7a4b543 --- /dev/null +++ b/day_1/ex3.rb @@ -0,0 +1,30 @@ +# This will print the following phrase +puts "I will now count my chickens:" + +#This will print the following phrases with the answers to the math problems inbetween the brackets +puts "Hens #{25.0 + 30.0 / 6.0}" +puts "Roosters #{100.0 - 25.0 * 3.0 % 4.0}" + +#This will print the following phrase +puts "Now I will count the eggs:" + +#This will print the answer to the math problem below +puts 3.0 + 2.0 + 1.0 - 5.0 + 4.0 % 2.0 - 1.0 / 4.0 + 6.0 + +#This will print the following phrase without doing the math problem as the math is not in brackets +puts "Is it true that 3 + 2 < 5 - 7?" + +#This will print if the following math problem is true or false +puts 3.0 + 2.0 < 5.0 - 7.0 + +#This will print the following phrases with the answers to the math problems in brackets +puts "What is 3 + 2? #{3.0 + 2.0}" +puts "What is 5 - 7? #{5.0 - 7.0}" + +#This will print the following phrases +puts "Oh, that's why it's false." +puts "How about some more." +# This will print the following phrases including the answers to the the true/false math problems below. They will say true or false. +puts "Is it greater? #{5.0 > -2.0}" +puts "Is greater or equal? #{5.0 >= -2.0}" +puts "Is it less or equal? #{5.0 <= -2.0}" diff --git a/day_1/ex4.rb b/day_1/ex4.rb new file mode 100644 index 000000000..5d8373958 --- /dev/null +++ b/day_1/ex4.rb @@ -0,0 +1,33 @@ +#This assigns the variable cars the value of 100 +cars = 100 + +#This assigns the variable space_in_a_car the value of 4.0 +space_in_a_car = 4.0 + +#This assigns the variable drivers the value of 30 +drivers = 30 + +#This assigns the variable passengers the value of 90 +passengers = 90 + +#This assigns the variable cars_not_driven the value of the variable cars minus the value of the variable drivers +cars_not_driven = cars - drivers + +#This assigns the variable cars_driven the same value as the variable drivers +cars_driven = drivers + +#This assigns the variable carpool_capacity the value of the variable cars_driven multiplied by the variable space_in_a_car +carpool_capacity = cars_driven * space_in_a_car + +#This assigns the variable average_passengers_per_car the value of the variable passengers divided by the variable 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." + +#Answer to study drill question 1: If you do not use a float number, the answer will be a whole number rounded to the nearest digit. diff --git a/day_1/ex5.rb b/day_1/ex5.rb new file mode 100644 index 000000000..ac0418948 --- /dev/null +++ b/day_1/ex5.rb @@ -0,0 +1,20 @@ +name = 'Zed A. Shaw' +age = 35 # not a lie in 2009 +height = 74 # inches +weight = 180 # lbs +eyes = 'Blue' +teeth = 'White' +hair = 'Brown' + +height_in_cm = height * 2.54 +weight_in_kg = weight * 0.453592 + +puts "Let's talk about #{name}" +puts "He's #{height_in_cm} centimeters tall." +puts "He's #{weight_in_kg} kilograms 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." + + # this line is tricky, try to get it exactly right + puts "If I add #{age}, #{height}, and #{weight} I get #{age + height + weight}." diff --git a/day_1/ex6.rb b/day_1/ex6.rb new file mode 100644 index 000000000..dec0c8867 --- /dev/null +++ b/day_1/ex6.rb @@ -0,0 +1,36 @@ +# variable assignment for types_of_people +types_of_people = 10 + +# variable assignment for x where value is a string that includes a variable as well +x = "There are #{types_of_people} types of people." + +# assign values for 2 variables +binary = "binary" +do_not = "don't" + +# assigns a value that includes a string and 2 other variables to y +y = "Those who know #{binary} and those who #{do_not}." + +#These commands print the value of the variables x and y +puts x +puts y + +#These commands print strings along with the values of the variables assigned to x and y +puts "I said: #{x}." +puts "I also said: '#{y}'." + +# this assigns a boolean value to the variable 'hilarious' +hilarious = false + +# this assigns a value to joke_evaluation that includes a string as well as the value of 'hilarious' +joke_evaluation = "Isn't that joke so funny?! #{hilarious}" + +# this prints the value of joke_evaluation +puts joke_evaluation + +# these commands assign values to w and e and then prints the value of them added together. +w = "This is the left side of..." +e = "a string with a right side." +puts w + e + +#Study Drills answers: 3. there are 4 places where a string is inside of a string, the rest are not strings as the variables do not have quotation marks. 3. you are adding two strings together, making them longer. 4. Single quotes only work where the thing will not be in double quotes as that would confuse the terminal into thinking they are seperate strings. diff --git a/day_1/ex7.rb b/day_1/ex7.rb new file mode 100644 index 000000000..4bd3b1902 --- /dev/null +++ b/day_1/ex7.rb @@ -0,0 +1,8 @@ +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." From 37739531854055a9665da2603e464ee773addc52 Mon Sep 17 00:00:00 2001 From: Wyatt Wicks Date: Tue, 22 Dec 2020 15:18:39 -0700 Subject: [PATCH 03/12] Add exercises directory and questions.md answers --- 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 | 16 ++++++++++------ 6 files changed, 27 insertions(+), 20 deletions(-) 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..c67fdbe5d 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 end # Write code that prints the phrase 'She sells seashells down by the seashore' # ten times: -# YOUR CODE HERE +10.times do + 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..a6d40d253 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 # 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 +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..1720e06eb 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 +hogwarts = "Harry Potter must not return to Hogwarts!" +p hogwarts # Write code that adds 2 to the `students` variable and # prints the result: students = 22 -# YOUR CODE HERE +students = 22 + 2 p students # Write code that subracts 2 from the `students` variable and # prints the result: -# YOUR CODE HERE +students = 22 - 2 p students diff --git a/day_1/questions.md b/day_1/questions.md index 73700e323..ada0e0cb9 100644 --- a/day_1/questions.md +++ b/day_1/questions.md @@ -1,17 +1,21 @@ ## Day 1 Questions 1. How would you print the string `"Hello World!"` to the terminal? + p "Hello World!" 1. What character is used to indicate comments in a ruby file? - + # pound sign 1. Explain the difference between an integer and a float? - +an integer is a whole number, a float has decimals i.e. not 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? - +p animal 1. What is interpolation? Use interpolation to print a sentence using the variable `animal`. - + It is a method of printing a string with variables inside it. + p "My favorite animal is a #{animal}" 1. What method is used to get input from a user? - + the 'gets' command. 1. Name and describe two common string methods: + "string".size will show how many characters are in the string. + "string".strip will remove all extra space from the string. From 55a91369795847ec73b44eb4371ad82f8a3e6cf6 Mon Sep 17 00:00:00 2001 From: Wyatt Wicks Date: Tue, 29 Dec 2020 15:03:29 -0700 Subject: [PATCH 04/12] Commit compleated iteration and each excercises --- .../exercises/iteration_and_each_exercises.rb | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 day_2/exercises/iteration_and_each_exercises.rb 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..43341f7ea --- /dev/null +++ b/day_2/exercises/iteration_and_each_exercises.rb @@ -0,0 +1,69 @@ +#each method to print doubles of each number +numbers = [1, 2, 3, 4] +numbers.each do |number| + puts number * 2 +end + +#each method to print triples of each number +numbers = [1, 2, 3, 4] +numbers.each do |number| + puts number * 3 +end + +#each method to print even numbers +numbers = [1, 2, 3, 4] +numbers.each { |number| puts number if number.even?} + +#each method to print odd numbers +numbers = [1, 2, 3, 4] +numbers.each { |number| puts number if number.odd?} + +#each method to print each number * 2 (same as printing doubles) +numbers = [1, 2, 3, 4] +numbers.each do |number| + puts number * 2 +end + +#each method to print full names +names = ["Alice Smith", "Bob Evans", "Roy Rogers"] +names.each do |name| + puts name +end + +#each method to print only the first name +full_names = ["Alice Smith", "Bob Evans", "Roy Rogers"] +full_names.each do |name| + puts name.split.first +end + +#each method to print only the last name +full_names = ["Alice Smith", "Bob Evans", "Roy Rogers"] +full_names.each do |name| + puts name.split.last +end + +#each method to print only the initials +full_names = ["Alice Smith", "Bob Evans", "Roy Rogers"] +full_names.each do |name| + full_names = name.split + firstinitial = full_names.first[0,1] + lastinitial = full_names.last[0,1] + puts "#{firstinitial}. #{lastinitial}." +end + +#each method to print number of characters in last names +full_names = ["Alice Smith", "Bob Evans", "Roy Rogers"] +full_names.each do |name| + full_names = name.split.last + characters = full_names.size + puts "#{full_names} #{characters}" +end + +#each method to create an integer of all charactrs in all names +full_names = ["Alice Smith", "Bob Evans", "Roy Rogers"] +full_names.each do |name| + full_names.join.size.to_i + + + puts full_names +end From fab281c6a91abb8404636320176700b534753a87 Mon Sep 17 00:00:00 2001 From: Wyatt Wicks Date: Tue, 29 Dec 2020 15:04:48 -0700 Subject: [PATCH 05/12] Commit array methods excercise --- day_2/array_method.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 day_2/array_method.md diff --git a/day_2/array_method.md b/day_2/array_method.md new file mode 100644 index 000000000..a7db54ae5 --- /dev/null +++ b/day_2/array_method.md @@ -0,0 +1,8 @@ +.sort This will sort the items in your array alphabetically if they are strings, or ascending value order if they are numbers. +.each This will select all the elements in the array that fit whatever parameters you want and changes them based on the parameters you type. +.join will combine elements in an array in to a string. +.index will show the position of each thing in the array. +.collect will will create a new array for the values returned in the paramters you choose. +.first will return the first thing in the array. +.last will collect the last thing in the array. +.shuffle will give a new array with all of the elements shuffled to new positions. From e41c9db6f361ac35f53ec02abb0a0b9ebdf0f60f Mon Sep 17 00:00:00 2001 From: Wyatt Wicks Date: Tue, 29 Dec 2020 15:59:50 -0700 Subject: [PATCH 06/12] Add Day 2 Work --- day_2/exercises/arrays.rb | 17 ++++++++++------- day_2/exercises/boolean_practice.md | 28 ++++++++++++++++++++++++++++ day_2/exercises/iteration.rb | 19 +++++++++++++------ day_2/questions.md | 23 +++++++++++++++++------ 4 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 day_2/exercises/boolean_practice.md diff --git a/day_2/exercises/arrays.rb b/day_2/exercises/arrays.rb index f572a5ae6..891e56871 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 = ["Colorado", "Utah", "Wyoming"] p states # Write code that stores an array of foods in a variable, # then prints that array: -# YOUR CODE HERE +foods = ["grape", "mushroom", "beef"] +p foods # Example: Write code that prints the number of elements # in your above array of animals: @@ -23,18 +24,20 @@ # 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[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 +animals.insert(3, "lion") +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..51a7978bf --- /dev/null +++ b/day_2/exercises/boolean_practice.md @@ -0,0 +1,28 @@ +true && true is true +false && true is false +1 == 1 && 2 == 1 is false +"test" == "test" is true +1 == 1 || 2 != 1 is true +true && 1 == 1 is true +false && 0 !=0 is false +true || 1 == 1 is true +"test" == "testing" is false +1 != 0 && 2 == 1 is false +"test" != "testing" is true +"test" == 1 is false +!(true && false) is true +!(1 == 1 && 0 != 1) is false +!(10 == 1 || 1000 == 1000) is false +!(1 != 10 || 3 == 4) is false +!("testing" == "testing" && "Zed" == "Cool Guy") is true +1 == 1 && (!("testing" == 1|| 1 == 0)) is true +"chunky" == "bacon" && (!(3 == 4 || 3 == 3)) is false +3 == 3 && (!("testing" == "testing" || "Ruby" == "Fun")) is false + +&& (and) +|| (or) +! (not) +!= (not equal) +== (equal) +>= (greater-than-equal) +<= (less-than-equal) diff --git a/day_2/exercises/iteration.rb b/day_2/exercises/iteration.rb index a801cb4fc..5fefe6b41 100644 --- a/day_2/exercises/iteration.rb +++ b/day_2/exercises/iteration.rb @@ -15,14 +15,21 @@ # "The is awesome!" for each animal: animals.each do |animal| - # YOUR CODE HERE + p "the #{animal} is awesome!" 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 = ["sugar", "watermelon", "eggs"] +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 +# 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| + p 2 * number +end diff --git a/day_2/questions.md b/day_2/questions.md index a179f0b04..058d82d4f 100644 --- a/day_2/questions.md +++ b/day_2/questions.md @@ -1,17 +1,28 @@ ## Day 2 Questions 1. Create an array containing the following strings: `"zebra", "giraffe", "elephant"`. - + ["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[1] 1. How would you add `"lion"` to the `animals` array? - +animals.push("lion") 1. Name and describe two additional array methods: - +.pop removes last element from array. +.first shows the first element in the array. 1. What are the boolean values in Ruby? +true +false +!() means not true +|| means OR +&& means AND +!(||) means not OR +!(&&) means not AND 1. In Ruby, how would you evaluate if `2` is equal to `25`? What is the result of this evaluation? - +2 == 25 +not true 1. In Ruby, how would you evaluate if `25` is greater than `2`? What is the result of this evaluation? +25 > 2 +true From 937952f55c907d47af7da5ecb55950debe7cf831 Mon Sep 17 00:00:00 2001 From: Wyatt Wicks Date: Wed, 30 Dec 2020 12:32:41 -0700 Subject: [PATCH 07/12] Add day_3 completed answers --- .../exercises/else_and_if_study_questions.md | 40 ++++++++ day_3/exercises/exercise_31.rb | 94 +++++++++++++++++++ day_3/exercises/if_statements.rb | 29 ++++-- day_3/exercises/new_game.rb | 31 ++++++ day_3/exercises/what_if_study_questions.md | 5 + day_3/questions.md | 17 ++++ 6 files changed, 210 insertions(+), 6 deletions(-) create mode 100644 day_3/exercises/else_and_if_study_questions.md create mode 100644 day_3/exercises/exercise_31.rb create mode 100644 day_3/exercises/new_game.rb create mode 100644 day_3/exercises/what_if_study_questions.md diff --git a/day_3/exercises/else_and_if_study_questions.md b/day_3/exercises/else_and_if_study_questions.md new file mode 100644 index 000000000..157618e6a --- /dev/null +++ b/day_3/exercises/else_and_if_study_questions.md @@ -0,0 +1,40 @@ +elsif will make the code follow that scenario if the initial if statement does not work. Else works simmilarly but if none of the if or elseif statements happen then the computer will use the else statement. +By changing the initial values you are again changing whether each if statement is true, so the computer will display different statements if the initial if-statement is not true any more. + +the lines below this give values to variables. +people = 30 +cars = 40 +trucks = 15 + +The first line asks if the value of the variable cars is greater than the value of the variable people. +if cars > people +If the above statement was true, then the program would print the statement after puts. If it is not true, then the computer tests if the next statement is true and does not print the puts statement. + puts "We should take the cars." +If cars is less than people, the computer will print the puts line. +elsif cars < people + puts "We should not take the cars." +If neither of the first 2 statements were true, like if cars = people, then the else puts statement is printed. +else + puts "We can't decide." +This ends the block. +end + +This line asks the same if statement as the above code, but using the variable 'trucks' and 'cars' instead of 'cars' and 'people'. +if trucks > cars +If the trucks value is greater than the cars value, the below statement will be printed. + puts "That's too many trucks." +If the first statement was not true, then the below statement would be printed. +elsif trucks < cars + puts "Maybe we could take the trucks." +if neither of the above statements are true, then the statement below else would be printed. +else + puts "We still can't decide." +The end command ends the block telling the computer what to do for those variables when tested like that. +end +The below if-statement asks if the value of 'people' is greater than the value of 'trucks'. If this is true, the puts statement will print. +if people > trucks + puts "Alright, let's just take the trucks." +If the above statement was not true or could not be decided, then the below code will be used and it will print the puts command below. +else + puts "Fine, let's stay home then." +end diff --git a/day_3/exercises/exercise_31.rb b/day_3/exercises/exercise_31.rb new file mode 100644 index 000000000..b76f09719 --- /dev/null +++ b/day_3/exercises/exercise_31.rb @@ -0,0 +1,94 @@ +puts "You enter a dark room with two 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 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 "you see 2 portals, green and yellow in color, do you take portal #1 or portal #2?" + + print "> " + portal = $stdin.gets.chomp + + if portal == "1" + puts "the portal leads you to a wizard who asks you if you want a sword or a tiger." + + print "> " + object = $stdin.gets.chomp + if object == "sword" + puts "you grab the sword and the wizard takes you to a dragon's lair." + puts "The wizard tells you to slay the dragon, do you accept? Y/N" + print "> " + decision = $stdin.gets.chomp + if decision == "Y" + puts "you try to slay the dragon, but the sword breaks and you die." + + elsif decision == "N" + puts "The wizard kills you for being cowardly" + + else + puts "The dragon burns you alive while you decide what to do." + end + + elsif object == "tiger" + puts "The tiger is feral and eats you." + + else + puts "The wizard gets bored waiting for your decision and turns you into a frog." + end + + elsif portal == "2" + puts "The portal takes you to the realm of the mushrooms, where you see a blue path and a red path." + puts "Which path do you take?" + print "> " + path = $stdin.gets.chomp + if path == "blue" + puts "the path leads you to a group of hostile gnomes that kill you for lookin' at 'em funny." + + elsif path == "red" + puts "The path takes you to a lonely princess who you spend the rest of your life with, yay..." + + else + puts "While you wait to decide a centaur comes and kills you." + end + + + else + puts "You take too long to decide and another portal drops below you taking you away to the inescapable realm." + end + +else + puts "You can't decide which door to take and die waiting" + + +end diff --git a/day_3/exercises/if_statements.rb b/day_3/exercises/if_statements.rb index a80b96840..8fdf7691a 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" @@ -17,7 +17,7 @@ elsif weather == 'rainy' p "umbrella" elsif weather == 'snowy' - p "coat" + p "stay inside" elsif weather == 'icy' p "yak traks" else @@ -35,21 +35,25 @@ # 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 +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 +65,18 @@ # 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 == 1 and has_sauce == true + puts "I cannot make pizza" + +elsif cups_of_flour >= 2 and has_sauce == true + puts "I can make a pizza" + +elsif has_sauce == false + puts "I cannot make a pizza" + +else + puts "I can make a pizza" +end diff --git a/day_3/exercises/new_game.rb b/day_3/exercises/new_game.rb new file mode 100644 index 000000000..93579ecf2 --- /dev/null +++ b/day_3/exercises/new_game.rb @@ -0,0 +1,31 @@ +puts "What's your favorite color?" +print "> " +color = $stdin.gets.chomp + +if color == "red" + puts "That's a nice color." + +elsif color == "blue" + puts "Are you sure?" + print "> " + decision = $stdin.gets.chomp + + if decision == "yes" + puts "Okay good that's my favorite color too." + + elsif decision == "no" + puts "Yeah that's what I thought." + + else + puts "???" + end + +elsif color == "yellow" + puts "That's an okay color." + +elsif color == "purple" + puts "Purple is a good color." + +else + puts "That color sucks." +end diff --git a/day_3/exercises/what_if_study_questions.md b/day_3/exercises/what_if_study_questions.md new file mode 100644 index 000000000..ec6dfc4b9 --- /dev/null +++ b/day_3/exercises/what_if_study_questions.md @@ -0,0 +1,5 @@ +If statements will test if the code under it passes or fails a set of parameters and if the code does, it will do the next command. +The code under the if needs to be indented because it is part of a block and the indent says "look at the stuff below that's indented for the parameters" +If the code isn't indented, the computer will take that code as a regular command. +Yes, and it will test both of the parameters and only do the thing if you specify it is true or false. EX: && statements will test if the statement is true or false and then do whatever you based on if you want the answer to be true or false. +If you change the initial values then the statements will change from true or false and what is printed will depend on if those statements are still true with different values. diff --git a/day_3/questions.md b/day_3/questions.md index db6170fa7..9edccf746 100644 --- a/day_3/questions.md +++ b/day_3/questions.md @@ -1,13 +1,30 @@ ## Day 3 Questions 1. What is a conditional statement? Give three examples. +A conditional statement is a statement that asks if a given value evaluates to true or false based on conditions. + +Ex1: If 6 > 0, then x will happen. (since this is true, x will happen) +EX2: If 6 == 4, then x will happen. (since 6 does not equal 4, x will not happen) +EX3: If 6 != 4, then x will happen. (since 6 is not equal to 4, x will happen) 1. Why might you want to use an if-statement? +You might want to use an if statement to make your program do something if a set of values is true or false, this way you can have values for things that will do something specific if the statement is true or false. 1. What is the Ruby syntax for an if statement? +The syntax is to use 'if', followed by a boolean statement, then indented underneath is what you want to happen if the statement matches what the boolean statement shows. this can be followed by elsif or else on the next line, indented to the same space the original if statement is. You must end the statement with 'end' indented to the same space as the original 'if'. 1. How do you add multiple conditions to an if statement? +To add multiple conditions you can use 'elsif' or 'else', to add more than one condition that needs to be met if you have multiple values, then you can use 'and' or 'or' to add more conditions. 1. Provide an example of the Ruby syntax for an if/elsif/else statement: + x = 4 + if x == 4 + puts "That is correct" + elsif x < 4 + puts "x is less than 4" + else + puts "X is unknown" + end 1. Other than an if-statement, can you think of any other ways we might want to use a conditional statement? + You might want to use a conditional statement to test if a long math problem is what you would expect it to be given certain values. From 2dea3615e0b6c946f0f1a73084abfb982c155e92 Mon Sep 17 00:00:00 2001 From: Wyatt Wicks Date: Sat, 2 Jan 2021 16:56:21 -0700 Subject: [PATCH 08/12] Add Day 4 work --- day_4/exercises/ex21.rb | 46 +++++++++++++++++++ day_4/exercises/lrthw_ex19.rb | 66 +++++++++++++++++++++++++++ day_4/exercises/lrthw_excercise_18.rb | 26 +++++++++++ day_4/exercises/methods.rb | 26 ++++++++--- day_4/exercises/mutate.rb | 22 +++++++++ day_4/exercises/return.rb | 6 +++ day_4/exercises/say.rb | 13 ++++++ day_4/questions.md | 11 ++++- 8 files changed, 209 insertions(+), 7 deletions(-) create mode 100644 day_4/exercises/ex21.rb create mode 100644 day_4/exercises/lrthw_ex19.rb create mode 100644 day_4/exercises/lrthw_excercise_18.rb create mode 100644 day_4/exercises/mutate.rb create mode 100644 day_4/exercises/return.rb create mode 100644 day_4/exercises/say.rb diff --git a/day_4/exercises/ex21.rb b/day_4/exercises/ex21.rb new file mode 100644 index 000000000..527511ac4 --- /dev/null +++ b/day_4/exercises/ex21.rb @@ -0,0 +1,46 @@ +def add(a, b) + puts "ADDING #{a} + #{b}" + 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}" + + +# A puzzle for the extra credit, type it in anyway. +puts "Here is a puzzle." + +what = add(age, subtract(height, multiply(add(weight, 3), divide(iq, 2)))) + +puts "That becomes: #{what}. Can you do it by hand?" + +answer = (43 + 6 + 2) * 4 + +def form(a,b,c,d) + def first_part(a,b,c) + return a + b + c + end + return first_part * d +end diff --git a/day_4/exercises/lrthw_ex19.rb b/day_4/exercises/lrthw_ex19.rb new file mode 100644 index 000000000..638dce462 --- /dev/null +++ b/day_4/exercises/lrthw_ex19.rb @@ -0,0 +1,66 @@ +#The line below creates a function that has 2 arguments. +def cheese_and_crackers(cheese_count, boxes_of_crackers) + #The line below gives a puts command that will print the line as well as the number we put in for cheese_count when we call the function. + puts "You have #{cheese_count} cheeses!" + #The line below does the same as the line above, but with boxes_of_crackers. + puts "You have #{boxes_of_crackers} boxes of crackers!" + #The line below will be printed when the function is called. + puts "Man that's enough for a party!" + #The line below will be printed when the function is called, and will end on a new line. + puts "Get a blanket. \n" + #The line below ends the function. +end + +#The line below tells ruby to print the line. +puts "We can just give the function numbers directly:" +#The line below calls the function we definied above, with the numbers 20 and 30 for the argument values. +cheese_and_crackers(20, 30) + +#The line below tells ruby to print the line. +puts "OR, we can use variables from our script:" +#The lines below create variables with values of 10 and 50. +amount_of_cheese = 10 +amount_of_crackers = 50 +#The line below calls the function using the variables we defined above as argument values. +cheese_and_crackers(amount_of_cheese, amount_of_crackers) + +#The line below prints the line. +puts "We can even do math inside too:" +#The line below calls the function using the math inside as values for the arguments of the function. +cheese_and_crackers(10 + 20, 5 + 6) + +#The line below prints the line. +puts "And we can combine the two, variables and math:" +#The line below calls the function using the variables we identifed above plus the numbers below. +cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) + +def cooking(tomato, lettuce, cheese) + if cheese == 0 + puts "We can not make a cheeseburger today." + else + puts "Ok we can make you a burger" + puts " We have #{cheese} cheese, #{tomato} tomato slices, and #{lettuce} lettuce." + end +end + +cooking(6, 4, 0) + +cooking(6, 4, 3) + +tomato_slice = 4 +cooking(tomato_slice, 4, 3) + +cheese_slice = 6 +cooking(cheese_slice - 6, 3, tomato_slice) + +cooking(tomato_slice, 3, cheese_slice - 6) + +cooking(tomato_slice - cheese_slice, 3, cheese_slice) + +cooking(6 * 4, 3 + 2, 1 + 1) + +cooking(0, 0, 4+3) + +cooking(15 - 5 * (2+4), 5, 8) + +cooking(tomato_slice * cheese_slice, 6, 3) diff --git a/day_4/exercises/lrthw_excercise_18.rb b/day_4/exercises/lrthw_excercise_18.rb new file mode 100644 index 000000000..d14552c2d --- /dev/null +++ b/day_4/exercises/lrthw_excercise_18.rb @@ -0,0 +1,26 @@ +# 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() diff --git a/day_4/exercises/methods.rb b/day_4/exercises/methods.rb index 6ed338e5d..8e8130dd3 100644 --- a/day_4/exercises/methods.rb +++ b/day_4/exercises/methods.rb @@ -12,16 +12,30 @@ 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("wyatt") + 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 new(a,b) + answer = a + b + p "The sum of #{a} and #{b} = #{answer}" +end + +new(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 greeting(person_1, person_2) + p "#{person_1} said the first man, #{person_2} said the second man." +end + +greeting("hello", "hi") diff --git a/day_4/exercises/mutate.rb b/day_4/exercises/mutate.rb new file mode 100644 index 000000000..baaee30c2 --- /dev/null +++ b/day_4/exercises/mutate.rb @@ -0,0 +1,22 @@ +a = [1, 2, 3] + +# Example of a method definition that modifies its argument permanently +def mutate(array) + array.pop +end + +puts "Before mutate method: #{a}" +mutate(a) +puts "After mutate method: #{a}" + + +a = [1, 2, 3] + +# Example of a method definition that does not mutate the caller +def no_mutate(array) + array.last +end + +p "Before no_mutate method: #{a}" +no_mutate(a) +p "After no_mutate method: #{a}" diff --git a/day_4/exercises/return.rb b/day_4/exercises/return.rb new file mode 100644 index 000000000..c83d02acf --- /dev/null +++ b/day_4/exercises/return.rb @@ -0,0 +1,6 @@ +def add_three(number) + return number + 3 +end + +returned_value = add_three(4) +puts returned_value diff --git a/day_4/exercises/say.rb b/day_4/exercises/say.rb new file mode 100644 index 000000000..346df9ff4 --- /dev/null +++ b/day_4/exercises/say.rb @@ -0,0 +1,13 @@ +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") diff --git a/day_4/questions.md b/day_4/questions.md index af17ab4da..ea6cece80 100644 --- a/day_4/questions.md +++ b/day_4/questions.md @@ -1,11 +1,20 @@ ## Day 4 Questions 1. In your own words, what is the purpose of a method? + a method is used to execute code many times so you don't have to type out a bunch of things a bunch of times, instead you can just call the method with whatever values and it will run the method with those values so you aren't constantly making new variables or formulas. 1. Create a method named `hello` that will print `"Sam I am"`. +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"`. - +def hello_someone(name) + p '"#{name} I Am"' +end 1. How would you call or execute the method that you created above? +hello_someone(whatever) 1. What questions do you have about methods in Ruby? +I'm still a little confused about methods being inside out, so what's put last in the method will be put on the stack first? +can you alter a method at a later point using other arguments or commands? From 6130e5e395f1b27dfdcbf671b70078380c616000 Mon Sep 17 00:00:00 2001 From: Wyatt Wicks Date: Sun, 3 Jan 2021 17:19:00 -0700 Subject: [PATCH 09/12] Add Day 5 Work --- day_5/exercises/ex39.rb | 67 +++++++++++++++++++++++++++++ day_5/exercises/ex39_studydrills.rb | 47 ++++++++++++++++++++ day_5/exercises/hashes.rb | 21 ++++----- day_5/questions.md | 11 ++++- 4 files changed, 134 insertions(+), 12 deletions(-) create mode 100644 day_5/exercises/ex39.rb create mode 100644 day_5/exercises/ex39_studydrills.rb diff --git a/day_5/exercises/ex39.rb b/day_5/exercises/ex39.rb new file mode 100644 index 000000000..ec9c58350 --- /dev/null +++ b/day_5/exercises/ex39.rb @@ -0,0 +1,67 @@ +# 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 states 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}" diff --git a/day_5/exercises/ex39_studydrills.rb b/day_5/exercises/ex39_studydrills.rb new file mode 100644 index 000000000..6990137bd --- /dev/null +++ b/day_5/exercises/ex39_studydrills.rb @@ -0,0 +1,47 @@ +# new hash for testing hashes +alphabet = { + 'a' => 'apple', + 'b' => 'banana', + 'c' => 'car', + 'd' => 'dog', + 'e' => 'elephant', + 'f' => 'farm', + 'g' => 'grape', + 'h' => 'horse', + 'i' => 'indigo', + 'j' => 'jupiter' +} + +# setting a default value if a key is called that doesn't exist +alphabet.default = 'no letter found' + +puts alphabet['y'] + +# method for getting a hash value in upper case +puts alphabet['a'].upcase! + +# another method for fetching a value +puts alphabet.fetch("a") + +# testing if the hash has a key +puts alphabet.has_key?("c") + +# testing if the hash has a value +puts alphabet.has_value?('grape') + +# testing creating a list of keys from the hash +puts alphabet.keys + +# testing replacing a hash for a new hash +alphabet.replace('h' => 'highway', 'c' => 'casserole', 'g' => 'great') + +puts alphabet + +# testing store method to add new key/value pair +alphabet.store("d", "dingo") + +puts alphabet + +# testing values of a hash in order + +puts alphabet[2] diff --git a/day_5/exercises/hashes.rb b/day_5/exercises/hashes.rb index 99fcebb77..58a8a89b6 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,22 @@ p foods # Write code that prints a hash holding zoo animal inventory: -zoo = #YOUR CODE HERE +zoo = {tigers: 6, bears: 2, monkeys: 12, dragons: 1} 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[:tigers] -# 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[:dinosaurs] = 2 +p zoo diff --git a/day_5/questions.md b/day_5/questions.md index d059e12c6..36a1e9549 100644 --- a/day_5/questions.md +++ b/day_5/questions.md @@ -1,13 +1,20 @@ ## Day 5 Questions 1. What is a Hash, and how is it different from an Array? - + A hash is a stored map of keys with associated values, it is different from an array in that the key/value pairs are not in any order. 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. +pet_store = {'leashes' => 23, 'cat food' => 30, 'heat lamps' => 4} 1. Given the following `states = {"CO" => "Colorado", "IA" => "Iowa", "OK" => "Oklahoma"}`, how would you access the value `"Iowa"`? +One way would be to use states['IA'] 1. With the same hash above, how would we get all the keys? How about all the values? - + states.keys and 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? +We could use a hash to store books with their authors as a key. A hash would work better than an array because it doesn't matter if a book you are looking for is the first key/value pair. 1. What questions do you still have about hashes? +How can you utilize hashes in a method? +How many key/value pairs can a hash hold? +How do you know right away if a hash or array will be better to store your data in? +Can you convert a hash to an array if you realize an array would work better? From cf3068ca9834017e28f70cff1e52b71324ec8777 Mon Sep 17 00:00:00 2001 From: Wyatt Wicks Date: Mon, 4 Jan 2021 11:14:27 -0700 Subject: [PATCH 10/12] Add Day 6 Work --- day_6/exercises/burrito.rb | 21 +++++++++++++++++- day_6/exercises/classtest1.rb | 42 +++++++++++++++++++++++++++++++++++ day_6/exercises/dog.rb | 9 +++++++- day_6/exercises/objectex1.rb | 17 ++++++++++++++ day_6/exercises/person.rb | 34 ++++++++++++++++++++++++++-- day_6/exercises/student.rb | 15 +++++++++++++ day_6/questions.md | 20 +++++++++++++---- 7 files changed, 150 insertions(+), 8 deletions(-) create mode 100644 day_6/exercises/classtest1.rb create mode 100644 day_6/exercises/objectex1.rb create mode 100644 day_6/exercises/student.rb diff --git a/day_6/exercises/burrito.rb b/day_6/exercises/burrito.rb index 967f68b6c..8ba1b215e 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,28 @@ def initialize(protein, base, toppings) @base = base @toppings = toppings end + + def add_topping(topping) + @toppings.append(topping) + puts "You added #{topping} to your burrito!" + end + + def remove_topping(topping) + @toppings.delete(topping) + puts "You removed #{topping} from your burrito!" + end + + def change_protein(protein) + @protein = protein + puts "You changed your protein to #{protein}!" + end end dinner = Burrito.new("Beans", "Rice", ["cheese", "salsa", "guacamole"]) p dinner.protein p dinner.base p dinner.toppings +p dinner.add_topping("queso") +p dinner.toppings +p dinner.remove_topping("cheese") +p dinner.change_protein("steak") diff --git a/day_6/exercises/classtest1.rb b/day_6/exercises/classtest1.rb new file mode 100644 index 000000000..b6015c3d7 --- /dev/null +++ b/day_6/exercises/classtest1.rb @@ -0,0 +1,42 @@ +# Creating a class and playing with it +class MyCar +attr_accessor :color +attr_reader :year + def initialize(year, color, model) + @year = year + @color = color + @model = model + @current_speed = 0 + end + def speed_up(number) + @current_speed += number + puts "you push the gas pedal and accelerate #{number} mph." + end + + def brake(number) + @current_speed += (number) + puts "you push the brake and decelerate #{number} mph." + end + + def current_speed + puts "you are going #{@current_speed} mph." + end + + def shut_down + @current_speed = 0 + puts "stop the car!" + end + + def spray_paint(color) + self.color = color + puts "You changed the color to #{color}" + end + +end + +new_car = MyCar.new('2019', 'white', 'kia') +new_car.speed_up(20) +new_car.brake(5) +new_car.color = 'black' +puts new_car.color +new_car.spray_paint('blue') diff --git a/day_6/exercises/dog.rb b/day_6/exercises/dog.rb index 03221314d..d640cffae 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,11 @@ def bark def eat @hungry = false end + + def play + @hungry = true + end + end fido = Dog.new("Bernese", "Fido", 4) @@ -28,3 +33,5 @@ def eat p fido.hungry fido.eat p fido.hungry +fido.play +p fido.hungry diff --git a/day_6/exercises/objectex1.rb b/day_6/exercises/objectex1.rb new file mode 100644 index 000000000..b3399658e --- /dev/null +++ b/day_6/exercises/objectex1.rb @@ -0,0 +1,17 @@ +# Creating a class in Ruby +class AnimalNames +end + +fred = AnimalNames.new + +# Answer to 2. +# a module is something like a collection of behaviors/actions that can be used by various classes. + +module SayName +end + +class AnimalNames + include SayName +end + +fred = AnimalNames.new diff --git a/day_6/exercises/person.rb b/day_6/exercises/person.rb index 2c26e9570..f0ba7b5a3 100644 --- a/day_6/exercises/person.rb +++ b/day_6/exercises/person.rb @@ -1,5 +1,35 @@ -# 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_accessor :name, :height, :weight, :age + + def initialize(height, weight, age) + @height = height + @weight = weight + @age = age + end + + def grow_older + age = @age + 1 + p "You are #{age} years old!" + end + + def eat + weight = @weight + 5 + p "You are #{weight} lbs fat!" + end + + def info + p "You are #{height} inches tall, #{weight} lbs heavy, and #{age} old!" + end + + + +end + +wyatt = Person.new(6, 150, 27) +wyatt.grow_older +wyatt.eat +wyatt.info diff --git a/day_6/exercises/student.rb b/day_6/exercises/student.rb new file mode 100644 index 000000000..dbeac3018 --- /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(number) + puts "favorite number is #{number}" + end +end +mike = Student.new() +mike.first_name = 'Mike' +mike.introduction('Sara') +mike.favorite_number(6) diff --git a/day_6/questions.md b/day_6/questions.md index f58ca5f71..79f2e7056 100644 --- a/day_6/questions.md +++ b/day_6/questions.md @@ -1,13 +1,25 @@ ## Day 6 Questions 1. In your own words, what is a Class? - + A class is a set of attributes or behaviors that are the same amongst all instances of that class. 1. What is an attribute of a Class? - +An attribute is a specific value for any instance in that class. 1. What is behavior of a Class? - +A behavior is something you can do to the instances of a class to modify them or have them do something. 1. In the space below, create a Dog class with at least 2 attributes and 2 behaviors: +class Dog + attr_accessor :breed, :name, :size -1. How do you create an instance of a class? + def jump + p 'the dog jumps!' + end + + def bark + p 'bark!' + end +end +1. How do you create an instance of a class? + instance_name = ClassName.new 1. What questions do you still have about classes in Ruby? + How can I use other things I've learned about so far (boolean logic, hashes, etc.) with classes? From fc04a4f0ed805b9f241d74a9c555c9ea69224604 Mon Sep 17 00:00:00 2001 From: Wyatt Wicks Date: Mon, 4 Jan 2021 15:35:46 -0700 Subject: [PATCH 11/12] Add FizzBuzz and 10 Speckled Frogs answers --- day_7/10_speckled_frogs.rb | 49 ++++++++++++++++++++++++++++++++++++++ day_7/fizzbuzz.rb | 27 +++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 day_7/10_speckled_frogs.rb create mode 100644 day_7/fizzbuzz.rb diff --git a/day_7/10_speckled_frogs.rb b/day_7/10_speckled_frogs.rb new file mode 100644 index 000000000..76878e5d6 --- /dev/null +++ b/day_7/10_speckled_frogs.rb @@ -0,0 +1,49 @@ +## 10 Speckled Frogs + +# Create a file named `10_speckled_frogs.rb` and within that file, write several a program that will print the following nursery rhyme: + +# 3 speckled frogs sat on a log +# eating some most delicious bugs. +# One jumped in the pool where its nice and cool, +# then there were 2 speckled frogs. + +# 2 speckled frogs sat on a log +# eating some most delicious bugs. +# One jumped in the pool where its nice and cool, +# then there was 1 speckled frogs. + +# 1 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! + +### Required +# Make your program print the rhyme above for *10* frogs, with attention to where language changes + +number_frogs = (1..10).to_a +number_frogs.sort! {|x, y| y <=> x} +number_frogs.each do |n| + number = n + new_number = n - 1 + if new_number == 0 + p "1 speckled frog sat on a log" + \ + else + p "#{number} speckled frogs sat on a log" + \ + end + + p "eating some most delicious bugs." + \ + p "One jumped in the pool where its nice and cool," + \ + + if new_number == 0 + p "then there were no more speckled frogs!" + elsif new_number == 1 + p "then there was one speckled frog." + else + p "then there were #{new_number} speckled frogs." + end + +end diff --git a/day_7/fizzbuzz.rb b/day_7/fizzbuzz.rb new file mode 100644 index 000000000..55ddc7eed --- /dev/null +++ b/day_7/fizzbuzz.rb @@ -0,0 +1,27 @@ +# Create a file named fizzbuzz.rb and within that file, write a program that prints something for each number from 1 to 100 with the following rules: + +# For any number that is a multiple of 3, print 'Fizz' +# For any number that is a multiple of 5, print 'Buzz' +# For any number that is a multiple of both 3 and 5, print 'FizzBuzz' +# For all other numbers, print the number. + +fizzbuzz = (1..100).to_a + +fizzbuzz.each do |n| + if (n % 3) == 0 && (n % 5) != 0 + p 'Fizz' + + elsif (n % 5) == 0 && (n % 3) != 0 + p 'Buzz' + + elsif (n % 5) == 0 && (n % 3) == 0 + p 'FizzBuzz' + + else + p "#{n}" + end +end + +#Bonus: Can you make the program work for any range of numbers? + +#the above program should work if you change the 1..100).to_a in line 8 to any range From fba24893f61ec7e575a308f7d85e71abd6908bbc Mon Sep 17 00:00:00 2001 From: Wyatt Wicks Date: Tue, 5 Jan 2021 12:55:15 -0700 Subject: [PATCH 12/12] Add Day 7 Work --- day_7/caesar_cipher.rb | 12 ++++++++++++ day_7/high_level.md | 12 ++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 day_7/caesar_cipher.rb create mode 100644 day_7/high_level.md diff --git a/day_7/caesar_cipher.rb b/day_7/caesar_cipher.rb new file mode 100644 index 000000000..c4957a38d --- /dev/null +++ b/day_7/caesar_cipher.rb @@ -0,0 +1,12 @@ +# Creating a program to shift letters in a message by a specified shift amount and return the new phrase. + +#build a method to encrypt a message by a certain number of shifts. +def encode(message, shift) +# we want to include an array of all of the letters in the alphabet as a reference + alphabet = ('a'..'z').to_a +# now we use the .chars command to split the message into individual characters, then use the .map command to show how we want our chracters shifted. + message.chars.map {|char| alphabet.include?(char.downcase) ? + alphabet[alphabet.find_index(char.downcase) + shift - alphabet.size] : char}.join +end + +p encode("Hello World", 5) diff --git a/day_7/high_level.md b/day_7/high_level.md new file mode 100644 index 000000000..ee2deb429 --- /dev/null +++ b/day_7/high_level.md @@ -0,0 +1,12 @@ +Caesar Cipher Notes +1. The first thing we need to do is create a method that will encompass the rest of our program so that when we type the method and put the message and how much we want to shift it by, it will work with any message or shift number. +we want to define our method and give it the two values we want to enter, the message and the number to shift by. +2. After that we want to identify the entire alphabet as a string of lowercase letters for reference, so that our program knows the starting point of every letter. +3. Now comes the actual code. We use message.chars to separate whatever message we type in to a new array of individual characters. then we use the .map command to map out this array of characters in a new way. +4. the code after the .map command is telling the computer that for each character in our array, we first check if the character is in the alphabet array that we created earlier. This eliminates spaces, symbols, and numbers so the end result only has the characters shifted and not the other spaces and punction marks. using the .downcase command will make it so that even if the letters in the message are capitalized, they will still be able to be found in our alphabet array as those letters are all lower case. +5. then we use alphabet.find_index(char.downcase) to find the numbered position of each character in the alphabet array. +6. we then add shift to the index number to each character. shift has been defined as the number you want the letters to move, so by adding that number to each character, the new character is the value of the old character's index plus the amount to shift. +7. I then subtracted the size of the alphabet (26) to account for any character that has been shifted higher than the amount of characters in the alphabet. This also wraps the characters around the alphabet again because there is no value in alphabet for anything higher than 26. +8. I then close out my maps bracket with :char so the program knows to do the above for every character in the message. I then use the .join command to take those new characters and rejoin them in to words again. +9. Finally, I end my method. +10. Now, when I print my method with a message and a number to shift by, ruby will take that message, separate the letters, move them by the amount i want to shift by, return those new letters, and rejoin then into one string.