diff --git a/day_1/ex1.rb b/day_1/ex1.rb new file mode 100644 index 000000000..bccdb09b3 --- /dev/null +++ b/day_1/ex1.rb @@ -0,0 +1,8 @@ + "Hellow World!" + "Hello Again" + "I like typing this." + "This is fun." + "Yay! Printing." + "I'd much rather you 'not'." + # puts 'I "said" do not touch this.' +puts 'I wrote another line!!' diff --git a/day_1/ex2.rb b/day_1/ex2.rb new file mode 100644 index 000000000..dffb2b27c --- /dev/null +++ b/day_1/ex2.rb @@ -0,0 +1,9 @@ +# A comment, this is so you can read your program later. +# Anything after the # is ignored by ruby. + +puts "I could have code like this." # and the comment after is ignored + +# You can also use a comment to "disable" or comment out a piece of code: +# puts "This won't run." + +puts "This will run." diff --git a/day_1/ex3.1.rb b/day_1/ex3.1.rb new file mode 100644 index 000000000..8dbfa595e --- /dev/null +++ b/day_1/ex3.1.rb @@ -0,0 +1,3 @@ +puts "This file is going to help me figure out my bills" +x = 710+210+80+80 +puts "#{x} isn't event that much" diff --git a/day_1/ex3.rb b/day_1/ex3.rb new file mode 100644 index 000000000..a23b76fe4 --- /dev/null +++ b/day_1/ex3.rb @@ -0,0 +1,24 @@ +puts "I will now count my chickens:" +puts "Hens #{25.0 + 30.0 / 6.0}" +#the curly brackets do the math in the string +puts "Roosters #{100.0 - 25.0 * 3.0 % 4.0}" +#the curly brackets do the math in the string +puts "Now I will count the eggs:" +puts 3.0+20+1.0-5.0+4.0%2.0-1.0/4.0+6.0 +# no need for curly brackets because it is not in the string and the equation is alone on a line +puts "Is it true that 3 + 2 < 5 - 7?" +#the question is asked +puts 3+2<5-7 +# the equation is given and solved. It has been determined to be false +puts "What is 3 + 2? #{3 + 2}" +# the question is asked and the actual work is done in the same spot in the curly brackets +puts "What is 5 - 7 #{5-7}" +# the question is asked and the actual work is done in the same spot in the curly brackets +puts "Oh, that's why it's false." +puts "How about some more." + +puts "Is it greater? #{5 > -2}" +# the question is asked and the actual work is done in the same spot in the curly brackets +puts "Is it greater or equal? #{5 >= -2}" +# the question is asked and the actual work is done in the same spot in the curly brackets +puts "Is it less or equal? #{5 <= -2}" diff --git a/day_1/ex4.rb b/day_1/ex4.rb new file mode 100644 index 000000000..fafecaa0c --- /dev/null +++ b/day_1/ex4.rb @@ -0,0 +1,17 @@ +cars = 100 +space_in_a_car = 4.0 +drivers = 30 +passengers = 90 +cars_not_driven = cars - drivers +cars_driven = drivers +carpool_capacity = cars_driven * space_in_a_car +average_passengers_per_car = passengers / cars_driven + +puts "There are #{cars} cars available." +puts "There are only #{drivers} drivers available." +puts "There are only #{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." + +# When I deleted line 7 and ran it, the same error code popped up. Zed must have forgotten to define what carpool capcity was. diff --git a/day_1/ex5.rb b/day_1/ex5.rb new file mode 100644 index 000000000..0af38569d --- /dev/null +++ b/day_1/ex5.rb @@ -0,0 +1,22 @@ +name = 'Zed A. Shaw' +age = 35 # not a lie in 2009 +height =74 # inches +height_in_centimeters = height * 2.54 +weight = 180 # lbs +weight_in_kilos = height * 0.453592 +eyes = 'Blue' +teeth = 'White' +hair = "Brown" + +puts "Let's talk about #{name}" +puts "He's #{height} tall." +puts "That means #{height_in_centimeters} in centimeters." +puts "He's #{weight} pounds heavy." +puts "Acutally that's not too heavy." +puts "Especially in kilos like this #{weight_in_kilos}" +puts "He's got #{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..6403c6023 --- /dev/null +++ b/day_1/ex6.rb @@ -0,0 +1,33 @@ +# defining types of people as 10 +types_of_people = 10 +# defining x as the string +x = "There are #{types_of_people} types of people." +# defining binary +binary = "binary" +# defining do not as don't +do_not = "don't" +#defining y as the string +y = "Those who know #{binary} and those who #{do_not}." +# these two are telling ruby to export x and y values +puts x +puts y + +# these two are asking for the two strings to be written including the values which are other strings +puts "I said: #{x}." +puts "I also said:'#{y}'." + +# this defines hilarious +hilarious = false + +# this defines joke_evaluation which includes the previously defined variable +joke_evaluation = "Ins't that joke so funny?! #{hilarious}" + +# this is asking ruby to export joke_evaluation +puts joke_evaluation + +# these two are defining variables w and each +w = 'This is the left side of...' +e = 'a string with a right side.' + +# this is asking ruby to export w and e values next to eachother +puts w + e diff --git a/day_1/ex7.rb b/day_1/ex7.rb new file mode 100644 index 000000000..f4aedec90 --- /dev/null +++ b/day_1/ex7.rb @@ -0,0 +1,17 @@ +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, youre #{age} old, #{height} tall and #{weight} heavy." + +#chomp stops the line break/new line from being created after the gets adds the answer gotten to the strings + +print "What is your favorite animal?" +animal = gets.chomp +print "What would happen if you never saw that animal again?" +reaction = gets.chomp + +puts "So #{animal} is really important to you. I'd probably #{reaction} too." 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..d4bc13867 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..eacf3d3fb 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 54 % 10 diff --git a/day_1/exercises/strings.rb b/day_1/exercises/strings.rb index f2f903ffc..414286dfb 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.... we both know you mean soda...." diff --git a/day_1/exercises/variables.rb b/day_1/exercises/variables.rb index a1e45bb26..5425c3347 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,21 @@ # 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 +warning_message = "Harry Potter must not return to Hogwarts!" +p warning_message # Write code that adds 2 to the `students` variable and # prints the result: students = 22 -# YOUR CODE HERE -p students +student = students + 2 +p student # Write code that subracts 2 from the `students` variable and # prints the result: -# YOUR CODE HERE -p students +students2 = 22 -2 + +p students2 diff --git a/day_1/questions.md b/day_1/questions.md index 73700e323..1b93ee61b 100644 --- a/day_1/questions.md +++ b/day_1/questions.md @@ -1,17 +1,20 @@ ## Day 1 Questions 1. How would you print the string `"Hello World!"` to the terminal? + puts "Hello World!" 1. What character is used to indicate comments in a ruby file? - + # 1. Explain the difference between an integer and a float? - + Integers are whole numbers and float have a decimal place 1. In the space below, create a variable `animal` that holds the string `"zebra"` - + animal = "zebra" 1. How would you print the string `"zebra"` using the variable that you created above? - + puts "animal" 1. What is interpolation? Use interpolation to print a sentence using the variable `animal`. - + "What a cute #{animal} you have over there!" 1. What method is used to get input from a user? - + gets 1. Name and describe two common string methods: + .length finds out how many characters there are in a strings + .chomp stops the line break from occurring after input from user. diff --git a/day_2/array_methods.md b/day_2/array_methods.md new file mode 100644 index 000000000..cbea088bd --- /dev/null +++ b/day_2/array_methods.md @@ -0,0 +1 @@ +each method: each allows us to do the same thing to each part/element of an array. For example if we had a bunch of elements in an array and we wanted to loop back over and over and do the same thing to each individual element we would use each. diff --git a/day_2/exercises/arrays.rb b/day_2/exercises/arrays.rb index f572a5ae6..d412230b9 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 = ["Alabama", "Arkanas", "Alaska"] p states # Write code that stores an array of foods in a variable, # then prints that array: -# YOUR CODE HERE +foods = ["Salami", "Pepperoni", "Bratwurst"] +p foods # Example: Write code that prints the number of elements # in your above array of animals: @@ -23,18 +24,19 @@ # 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.first # 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.append("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.delete ("Bratwurst") + p foods diff --git a/day_2/exercises/iteration.rb b/day_2/exercises/iteration.rb index a801cb4fc..e48f51890 100644 --- a/day_2/exercises/iteration.rb +++ b/day_2/exercises/iteration.rb @@ -15,14 +15,22 @@ # "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 = ["bok choy", "soy", "tomato", "banana"] +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 = [5,6,7,8,9] +numbers.each do |number| + 2.times do + p number + end +end diff --git a/day_2/experiment.rb b/day_2/experiment.rb new file mode 100644 index 000000000..42f37ab14 --- /dev/null +++ b/day_2/experiment.rb @@ -0,0 +1,69 @@ +numbers = [1,2,3,4] + +numbers.each do |number| + 2.times do + p number + end +end + +numbers.each do |number| + 3.times do + p number + end + end + +numbers.each do |number| + p number * 2 +end + +numbers.each do |number| + p number * 3 +end + +numbers.each do |number| + if number.even? + p number + end +end + +numbers.each do |number| + if number.odd? + p number + end +end + +names = ["Alice Smith", "Bob Evans", "Roy Rogers"] + +names.each do |name| + p name +end + +names.each do |name| + p name.split.first +end + +names.each do |name| + p name.chr +end + +names.each do |name| + last_name = name.split.last + p "#{last_name} #{last_name.length}" +end + +total_number_of_letters = 0 +names.each do |name| + total_number_of_letters += name.length +end +p total_number_of_letters +#Given an array of first and last names, e.g. , how would you print out the full names line by line? +#How would you print out only the first name? +#How would you print out only the last name? +#How could you print out only the initials? +#How can you print out the last name and how many characters are in it? +#How can you create an integer which represents the total number of characters in all the names? + + +animals = ["zebra", "giraffe", "elephant"] + +p animals[1] diff --git a/day_2/questions.md b/day_2/questions.md index a179f0b04..7f4b4abf5 100644 --- a/day_2/questions.md +++ b/day_2/questions.md @@ -1,17 +1,20 @@ ## 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"`? - +p animals[1] 1. How would you add `"lion"` to the `animals` array? - +animals.append("lion") +p animals 1. Name and describe two additional array methods: - +.length gives the number of elements in an array. +.last give the last element in an array 1. What are the boolean values in Ruby? - +True and False 1. In Ruby, how would you evaluate if `2` is equal to `25`? What is the result of this evaluation? - +2 == 25 false 1. In Ruby, how would you evaluate if `25` is greater than `2`? What is the result of this evaluation? +25 > 2 true diff --git a/day_3/exercises/ex29.rb b/day_3/exercises/ex29.rb new file mode 100644 index 000000000..51d9d5f72 --- /dev/null +++ b/day_3/exercises/ex29.rb @@ -0,0 +1,29 @@ +people = 20 +cats = 30 +dogs = 15 + +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 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 diff --git a/day_3/exercises/ex30.rb b/day_3/exercises/ex30.rb new file mode 100644 index 000000000..a74fec610 --- /dev/null +++ b/day_3/exercises/ex30.rb @@ -0,0 +1,30 @@ +people = 30 +cars = 40 +trucks = 15 + +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 + +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 + +if people > trucks + puts "Alright, let's just take the trucks." +else + puts "Fine, let's stay home then." +end + +if cars > people || trucks < cars + puts "this is correct" + +end diff --git a/day_3/exercises/ex31.rb b/day_3/exercises/ex31.rb new file mode 100644 index 000000000..5dfc08ed8 --- /dev/null +++ b/day_3/exercises/ex31.rb @@ -0,0 +1,38 @@ +puts "You enter a dark room with two doors. Do you go through door #1 or #2?" + +print "> " +door = $stdin.gets.chomp + +if door == "1" + puts "There's a giant bear here eating a cheese cake. What do you do?" + puts "1. Take the cake." + puts "2. Scream at the bear." + + 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 th eendless 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 you eyes into a pool of muck. Good job!" + 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..fd51cdd45 100644 --- a/day_3/exercises/if_statements.rb +++ b/day_3/exercises/if_statements.rb @@ -3,30 +3,30 @@ # 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' + weather = 'icy' + - if weather == 'sunny' - p "sunscreen" - elsif weather == 'rainy' - p "umbrella" - elsif weather == 'snowy' - p "coat" - elsif weather == 'icy' - p "yak traks" - else - p "good to go!" - end # Experiment with manipulating the value held in variable 'weather' # to print something other than 'coat' - +if weather == 'snowy' + p "boots" +elsif weather == 'rainy' + p "umbrella" +elsif weather == 'sunny' + p sunglasses +elsif weather == 'icy' + p "yak traks" +else + p "good to go!" +end ################## # Using the num_quarters variable defined below, determine @@ -35,21 +35,26 @@ # Right now, the program will print # out both "I have enough money for a gumball" and -# "I don't have enough money for a gumball". Write a +# "I don't have enough money for a gumball". Write a # conditional statement that prints only one or the other. # Experiment with manipulating the value held within num_quarters # to make sure both conditions can be achieved. -num_quarters = 0 +num_quarters = 3 -puts "I have enough money for a gumball" -puts "I don't have enough money for a gumball" +if num_quarters == 2 + puts "I have enough money for a gumball" +elsif num_quarters > 2 + puts "I have more than enough for a gumball" +elsif num_quarters < 2 + 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 +66,12 @@ # Experiment with manipulating the value held within both variables # to make sure all above conditions output what you expect. -cups_of_flour = 1 -has_sauce = true +cups_of_flour = 3 +has_sauce = false + +if cups_of_flour >= 2 and has_sauce == true + p "I can make pizza" +elsif cups_of_flour < 2 or has_sauce == false + p "I cannot make pizza" +end +#elsif cups_of_flour diff --git a/day_3/questions.md b/day_3/questions.md index db6170fa7..bd70f495c 100644 --- a/day_3/questions.md +++ b/day_3/questions.md @@ -1,13 +1,28 @@ ## Day 3 Questions 1. What is a conditional statement? Give three examples. +A conditional statement is if this then that. Examples of this are: +If x is greater than y then x is pink +If x is less than y then x is green +If it is cloudy outside there will be meatballs. 1. Why might you want to use an if-statement? +To give folks an option and a more specified response to their input. -1. What is the Ruby syntax for an if statement? +1. What is the Ruby syntax for an if statement? after the variables are defined +if *variable* **operator** *variable assignment* +puts "desired statement" +end 1. How do you add multiple conditions to an if statement? + adding "and" 1. Provide an example of the Ruby syntax for an if/elsif/else statement: +if homework == finished +puts "alex can relax" +elsif homework == unfinished +puts "alex is still in panic town" +end 1. Other than an if-statement, can you think of any other ways we might want to use a conditional statement? +When this happens this happens maybe? diff --git a/day_4/exercises/ex18.rb b/day_4/exercises/ex18.rb new file mode 100644 index 000000000..4ca11186e --- /dev/null +++ b/day_4/exercises/ex18.rb @@ -0,0 +1,21 @@ +def print_two (*args) + arg1, arg2 = args + puts "arg1: #{arg1}, arg2 #{arg2}" +end + +def print_two_again (arg1, arg2) + puts "arg1: #{arg1}, arg2: #{arg2}" +end + +def print_one (arg1) + puts "arg1: #{arg1}" +end + +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/ex19.rb b/day_4/exercises/ex19.rb new file mode 100644 index 000000000..26b6cd6e7 --- /dev/null +++ b/day_4/exercises/ex19.rb @@ -0,0 +1,21 @@ +def cheese_and_crackers(cheese_count, boxes_of_crackers) + puts "You have #{cheese_count} cheeses!" + puts "You have #{boxes_of_crackers} boxes of crackers!" + puts "Man that's not enough for a party!" + puts "Get a blanket.\n" +end + +puts "We can just give the function numbers directly:" +cheese_and_crackers(20,80) + +puts "OR, we can use variables from our script:" +amount_of_cheese = 10 +amount_of_crackers = 50 + +cheese_and_crackers(amount_of_cheese, amount_of_crackers) + +puts "We can even do math inside too:" +cheese_and_crackers(10+2, 5+6) + +puts "And we can combine the two, variables and math:" +cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) diff --git a/day_4/exercises/ex21.rb b/day_4/exercises/ex21.rb new file mode 100644 index 000000000..7e868c096 --- /dev/null +++ b/day_4/exercises/ex21.rb @@ -0,0 +1,34 @@ +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/launch_school.rb b/day_4/exercises/launch_school.rb new file mode 100644 index 000000000..1903e25ec --- /dev/null +++ b/day_4/exercises/launch_school.rb @@ -0,0 +1,32 @@ +puts "hello" +puts "hi" +puts "how are you" +puts "I'm fine" + +def say(words='hello') + puts words + '.' +end + +say() +say("hi") +say("how are you") +say("I'm fine") + +a = 5 + +def some_method + a = 3 +end + +puts a + +# Method invocation with a block +[1, 2, 3].each do |num| + puts num +end + +# Method definition + +def print_num(num) + puts num +end diff --git a/day_4/exercises/methods.rb b/day_4/exercises/methods.rb index 6ed338e5d..0296335d9 100644 --- a/day_4/exercises/methods.rb +++ b/day_4/exercises/methods.rb @@ -12,16 +12,25 @@ def print_name # Write a method that takes a name as an argument and prints it: def print_name(name) - # YOUR CODE HERE + 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 print_sports_card_collection(baseball_cards, basketball_cards) + p baseball_cards + basketball_cards +end + +print_sports_card_collection(30,20) -# 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 print_harry_sally_method(name_1, name_2) + p "Hello world, #{name_1} and #{name_2} at your service." +end + +print_harry_sally_method("Alex", "Margot") diff --git a/day_4/questions.md b/day_4/questions.md index af17ab4da..6f7671fe9 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? - + The purpose of a method is to have the code do something multiple times to different things without having to retype what you want it to do each time. 1. Create a method named `hello` that will print `"Sam I am"`. + def hello + p "Sam I am" + end + hello <-- that would call the method 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 ("Sam") 1. What questions do you have about methods in Ruby? +What is the difference between a parameter and an argument? diff --git a/day_5/exercises/ex39.rb b/day_5/exercises/ex39.rb new file mode 100644 index 000000000..2bc353c5d --- /dev/null +++ b/day_5/exercises/ex39.rb @@ -0,0 +1,56 @@ +states = { + 'Oregon' => 'OR', + 'Florida'=> 'FL', + 'California' => 'CA', + 'New York' => 'NY', + 'Michigan' => 'MI' +} + +cities ={ + 'CA' => 'San Fransisco', + '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 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..d510e1c36 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,25 @@ p foods # Write code that prints a hash holding zoo animal inventory: -zoo = #YOUR CODE HERE +zoo = {lions: 14, tigers: 2, bears: 99} p zoo -# Write code that prints all of the 'keys' of the zoo variable +# Write code that prints all of the 'keys' of the zoo variable # you created above: -# YOUR CODE HERE +p zoo.keys -# Write code that prints all of the 'values' of the zoo variable +# Write code that prints all of the 'values' of the zoo variable # you created above: # YOUR CODE HERE +p zoo.values -# Write code that prints the value of the first animal of the zoo variable +# Write code that prints the value of the first animal of the zoo variable # you created above: -# YOUR CODE HERE +p zoo.values.first -# 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.store(:oh_my,7) + +p zoo diff --git a/day_5/questions.md b/day_5/questions.md index d059e12c6..066d2c177 100644 --- a/day_5/questions.md +++ b/day_5/questions.md @@ -1,13 +1,17 @@ ## Day 5 Questions 1. What is a Hash, and how is it different from an Array? - + A hash is a collection of keys and values that are unordered. An array is an ordered list that has an integer index. 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 = {bird_seed:4, dog_bed:5, cat_tower:6} + p pet_store 1. Given the following `states = {"CO" => "Colorado", "IA" => "Iowa", "OK" => "Oklahoma"}`, how would you access the value `"Iowa"`? - + p states[IA] 1. With the same hash above, how would we get all the keys? How about all the values? - + p states.keys p states.values 1. What is another example of when we might use a hash? In your example, why is a hash better than an array? - + If we had a list of dance moves in a dance ie: dance_moves = {'plie'=>'bend the knees','chaine'=>'chain turn','sote'=>'little jump in first position'} + p dance_moves + A hash is better than an array because it allows the freedom to look for values based on the key ie dance moves in this example rather than where they exist in the list. This would be great for a how to program for this little dance. 1. What questions do you still have about hashes? + I would just like an ELI5 chat about hashes because I *think* I get it but I want to make sure I get it. Mostly because I am not sure if my answer to the preceding question was correct. diff --git a/day_6/exercises/burrito.rb b/day_6/exercises/burrito.rb index 967f68b6c..bba014eb3 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 @@ -6,14 +6,33 @@ class Burrito attr_reader :protein, :base, :toppings + #attr_accessor :protein, :base, :toppings def initialize(protein, base, toppings) @protein = protein @base = base @toppings = toppings end -end + def add_topping(new_topping) + @toppings.push(new_topping) + end -dinner = Burrito.new("Beans", "Rice", ["cheese", "salsa", "guacamole"]) -p dinner.protein -p dinner.base -p dinner.toppings + def remove_topping(topping) + @toppings.delete(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("peppers") + p dinner.toppings + dinner.remove_topping("cheese") + p dinner.toppings + dinner.change_protein("tofu") + p dinner.protein diff --git a/day_6/exercises/dog.rb b/day_6/exercises/dog.rb index 03221314d..c904f3b82 100644 --- a/day_6/exercises/dog.rb +++ b/day_6/exercises/dog.rb @@ -1,5 +1,5 @@ # In the dog class below, write a `play` method that makes -# the dog hungry. Call that method below the class, and +# the dog hungry. Call that method below the class, and # print the dog's hunger status. class Dog @@ -12,19 +12,48 @@ def initialize(breed, name, age) @hungry = true end - def bark - p "woof!" + def bark + p "woof!" + end + + def eat + @hungry = false + end + + def play + @hungry = true + end end - def eat - @hungry = false + fido = Dog.new("Bernese", "Fido", 4) + p fido.breed + p fido.name + p fido.age + p fido.hungry + fido.eat + p fido.hungry + fido.play + p fido.hungry + + +class Doggo + attr_reader :face_shape, :name + def initialize(face_shape, name) + @face_shape = face_shape + @name = name end + + def sing + p "bark bark bark" + end + + def tail_wag + p "Look at #{name} wagging their tail!" + end end -fido = Dog.new("Bernese", "Fido", 4) -p fido.breed -p fido.name -p fido.age -p fido.hungry -fido.eat -p fido.hungry +wanda = Doggo.new("smush face", "wanda") +p wanda.face_shape +p wanda.sing +p wanda.tail_wag +p wanda.name diff --git a/day_6/exercises/person.rb b/day_6/exercises/person.rb index 2c26e9570..a9f8e995b 100644 --- a/day_6/exercises/person.rb +++ b/day_6/exercises/person.rb @@ -1,5 +1,32 @@ -# 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 +# YOUR CODE + +class BossBitch + attr_accessor :name,:hobby, :relaxed + def initialize(name, hobby) + @name = name + @hobby = hobby + @relaxed = false + end + + def knitting + @relaxed = true + end + def walk + p "Alex is breaking a sweat and it's great" + end + +end + + +hana = BossBitch.new("hana","rollerblading") + +p hana.name +p hana.hobby +p hana.relaxed +p hana.walk +hana.knitting +p hana.relaxed diff --git a/day_6/questions.md b/day_6/questions.md index f58ca5f71..5752021d7 100644 --- a/day_6/questions.md +++ b/day_6/questions.md @@ -1,13 +1,29 @@ -## Day 6 Questions +The The Day 6 Questions 1. In your own words, what is a Class? - +A class is the category of a thing where every one of the things in that category have the same kinds of things. It's the mold that item/object/instance is shaped with. 1. What is an attribute of a Class? - +An attribute of a class is a piece of the mold that every single instance of a class has. 1. What is behavior of a Class? - +A behavior, similar to an attribute, is a piece of the mold that every instance of a class has. While an attribute describes, the behaviors are all things that a class can do or be done to it. 1. In the space below, create a Dog class with at least 2 attributes and 2 behaviors: +``class Doggo + attr_reader :face_shape, :name + def initialize(face_shape, name) + @face_shape = face_shape + @name = name + end -1. How do you create an instance of a class? + def sing + p "bark bark bark" + end + def tail_wag + p "Look at #{name} wagging their tail!" + end +end +`` +1. How do you create an instance of a class? +use the .new method 1. What questions do you still have about classes in Ruby? +I'm actually pretty confused still. I am going to try digging into the practice exercises. Also I couldn't figure out how to get it to stop printing twice. diff --git a/day_7/10_speckled_frogs.rb b/day_7/10_speckled_frogs.rb new file mode 100644 index 000000000..45db2f3fd --- /dev/null +++ b/day_7/10_speckled_frogs.rb @@ -0,0 +1,25 @@ +frogs = 10 + + +def counter(number) + + while number > 0 + + print "#{number} speckled frogs sat on a log + eating some most delicious bugs. + One jumped in the pool where its nice and cool, + then there" + number -= 1 + + if number == 1 + puts " was #{number} speckled frogs." + else + puts " were #{number} speckled frogs." + end + end + if number = 0 + print 'we are done here' + end +end + +p counter(frogs) diff --git a/day_7/ceasar_cipher.rb b/day_7/ceasar_cipher.rb new file mode 100644 index 000000000..6e1a3ed38 --- /dev/null +++ b/day_7/ceasar_cipher.rb @@ -0,0 +1,21 @@ +puts "Enter phrase to be encoded:" +@phrase = gets.chomp + +puts "Enter the number you would like to shift:" +@shift = gets.chomp.to_i + +def encode + @phrase.chars.map do |letter| + shifted_letter = letter.ord + @shift + shifted_letter.chr.upcase + end.join +end + +def decode + @phrase.chars.map do |letter| + unshifted_letter = letter.ord - @shift + unshifted_letter.chr + end.join.capitalize +end + +puts encode diff --git a/day_7/checkerboard.rb b/day_7/checkerboard.rb new file mode 100644 index 000000000..ea642d871 --- /dev/null +++ b/day_7/checkerboard.rb @@ -0,0 +1,46 @@ +puts "Type in your checkerboard height and width please" +print ">" +dimensions = gets.chomp.to_i + +def print_even_rows(column_counter) + while column_counter >= 1 + if column_counter == 1 + puts ' ' + elsif column_counter % 2 == 0 + print 'x' + elsif column_counter % 2 != 0 + print ' ' + end + column_counter -= 1 + end + +end + +def print_odd_rows(column_counter) + while column_counter >= 1 + if column_counter == 1 + puts 'x' + elsif column_counter % 2 == 0 + print ' ' + elsif column_counter % 2 != 0 + print 'x' + end + column_counter -= 1 + end +end + + +def track_rows(row_counter) + column_counter = row_counter + while row_counter >= 1 + if row_counter % 2 == 0 + print_even_rows(column_counter) + row_counter -= 1 + elsif row_counter % 2 != 0 + print_odd_rows(column_counter) + row_counter -= 1 + end + end +end + +track_rows(dimensions) diff --git a/day_7/fizzbuzz.rb b/day_7/fizzbuzz.rb new file mode 100644 index 000000000..e1fc2a7d9 --- /dev/null +++ b/day_7/fizzbuzz.rb @@ -0,0 +1,18 @@ + +#* 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. + + +(1..100).each do |n| + if n % 3 == 0 && n % 5 == 0 + puts 'FizzBuzz' + elsif n % 3 == 0 + puts 'Fizz' + elsif n % 5 == 0 + puts 'Buzz' + else + puts n + end +end diff --git a/day_7/high_level.md b/day_7/high_level.md new file mode 100644 index 000000000..e69de29bb