diff --git a/day_1/README.md b/day_1/README.md index 034ae11da..793185350 100644 --- a/day_1/README.md +++ b/day_1/README.md @@ -48,21 +48,21 @@ This will open the day_1 directory in Atom. You should be able to see the direct 1. Check off the items below as you complete the steps you just read for each lesson. ***Remember to create a file containing your work for each lesson!*** - - [ ] [A Good First Program](https://learnrubythehardway.org/book/ex1.html) + - [X] [A Good First Program](https://learnrubythehardway.org/book/ex1.html) - - [ ] [Comments in Code](https://learnrubythehardway.org/book/ex2.html) + - [X] [Comments in Code](https://learnrubythehardway.org/book/ex2.html) - - [ ] [Numbers and Math](https://learnrubythehardway.org/book/ex3.html) + - [X] [Numbers and Math](https://learnrubythehardway.org/book/ex3.html) - - [ ] [Variables and Names](https://learnrubythehardway.org/book/ex4.html) + - [X] [Variables and Names](https://learnrubythehardway.org/book/ex4.html) - - [ ] [Strings](https://learnrubythehardway.org/book/ex5.html) + - [X] [Strings](https://learnrubythehardway.org/book/ex5.html) - - [ ] [More Strings](https://learnrubythehardway.org/book/ex6.html) + - [X] [More Strings](https://learnrubythehardway.org/book/ex6.html) - - [ ] [Asking for Input](https://learnrubythehardway.org/book/ex11.html) + - [X] [Asking for Input](https://learnrubythehardway.org/book/ex11.html) - - [ ] Have you created 7 `ex.rb` files with your code in them? + - [X] Have you created 7 `ex.rb` files with your code in them? 1. Work through the [Strings](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#3.-strings) and [Numbers](http://tutorials.jumpstartlab.com/projects/ruby_in_100_minutes.html#5.-numbers) sections from Ruby in 100 Minutes. For each of these sections, open an `irb` session by typing `irb` into your terminal and type in the code snippets provided. diff --git a/day_1/ex1.rb b/day_1/ex1.rb new file mode 100644 index 000000000..d626d9a87 --- /dev/null +++ b/day_1/ex1.rb @@ -0,0 +1,8 @@ +puts "Hello World!" +puts "Hello Again" +puts "I like typing this." +puts "This is fun." +puts "Yay! Printing." +puts "I'd much rather you 'not'." +puts 'I "said" do not touch this.' +#mod for new commit diff --git a/day_1/ex2.rb b/day_1/ex2.rb new file mode 100644 index 000000000..a98650c9e --- /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." + +# 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.rb b/day_1/ex3.rb new file mode 100644 index 000000000..1327ca1cf --- /dev/null +++ b/day_1/ex3.rb @@ -0,0 +1,22 @@ +puts "I will now count my chickens:" + +puts "Hens #{25.0 + 30 /6}" +puts "Rooster #{100.0 - 25 * 3 % 4}" + +puts "Now I will count the eggs:" + +puts 3.0 + 2 + 1 - 5 + 4 % 2 - 1 /4 + 6 +puts "Is it true that 3 + 2 < 5 - 7?" + +puts 3 + 2 < 5 - 7 + +puts "What is 3 + 2? #{3.0 + 2}" +puts "What is 5 - 7? #{5.0 - 7}" + +puts "Oh, that's why it's false." + +puts "How about some more." + +puts "Is it greater? #{5 > -2}" +puts "Is it greater or equal? #{5 >= -2}" +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..b04f09e4f --- /dev/null +++ b/day_1/ex4.rb @@ -0,0 +1,21 @@ +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 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." + +#Study Drills ?: The error says that on line 14 in the code, the variables +#'carpool_capacity' was not defined before its use. +#1. '.0' making the 4 a float is not necessary bc there will not be a decimal +# or tenth of a person. diff --git a/day_1/ex5.rb b/day_1/ex5.rb new file mode 100644 index 000000000..8e9a72e51 --- /dev/null +++ b/day_1/ex5.rb @@ -0,0 +1,19 @@ +name = 'Zed A. Shaw' +age = 35 #not a lie in 2009 +height = 74 # inches +ht_cm = height * 2.54 # centimeters +weight = 180 # lbs +wt_kg = weight * 0.45 # kilograms +eyes = 'Blue' +teeth = 'White' +hair = 'Brown' + +puts "Let's talk about #{name}." +puts "He's #{height} inches tall, or #{ht_cm} centimeters." +puts "He's #{weight} pounds or #{wt_kg} kg 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..2fc35ecbf --- /dev/null +++ b/day_1/ex6.rb @@ -0,0 +1,37 @@ +# Define first variable +types_of_people = 10 +# Defines string wtih first variable inserted +x = "There are #{types_of_people} types of people." + +# Defines a third variable +binary = "binary" +# Defines a fourth variable +do_not = "don't" +# Defines string wtih two variables inserted +y = "Those who know #{binary} and those who #{do_not}." + +# Prints first string +puts x +# Prints second string +puts y + +# Prints first string insterted into a string +puts "I said: #{x}." +# Prints second string insterted into a string +puts "I also said: #{y}." + +# Define a boolean variable 'hilarious' +hilarious = false +# Defines another string, that has the boolean variable insterted +joke_evaluation = "Isn't that joke so funny?! #{hilarious}" + +# Prints the previous string +puts joke_evaluation + +# Defines w string +w = "This is the left side of..." +# Defines e string +e = 'a string with a right side.' + +# Prints w and e string in sequence +puts w + e diff --git a/day_1/ex7.rb b/day_1/ex7.rb new file mode 100644 index 000000000..004c6ae3c --- /dev/null +++ b/day_1/ex7.rb @@ -0,0 +1,10 @@ +print "How old are you? " +age = gets.to_i +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." +ten_years = age + 10 +p "You'll be #{ten_years} in a decade, wow." diff --git a/day_1/exercise7.rb b/day_1/exercise7.rb new file mode 100644 index 000000000..f40ba601d --- /dev/null +++ b/day_1/exercise7.rb @@ -0,0 +1,38 @@ +# Outputs a string +puts "Mary had a little lamb" +# Outputs a string with substring insterted +puts "Its fleece was white as #{'snow'}." +# Outputs 10 consecutive '.' +puts "." * 10 # what'd that do? + +# Defines a variable +end1 = "C" +# Defines a variable +end2 = "h" +# Defines a variable +end3 = "e" +# Defines a variable +end4 = "e" +# Defines a variable +end5 = "s" +# Defines a variable +end6 = "e" +# Defines a variable +end7 = "B" +# Defines a variable +end8 = "u" +# Defines a variable +end9 = "r" +# Defines a variable +end10 = "g" +# Defines a variable +end11 = "e" +# Defines a variable +end12 = "r" + +# watch that pring vs. puts on this line what's it do? +# Outputs first 6 strings of letters consecutively, w/o a line break +print end1 + end2 + end3 + end4 + end5 + end6 +# Outputs the next 6 string of letters after the previous +puts end7 + end8 + end9 + end10 + end11 + end12 +#mod for 2nd commit diff --git a/day_1/exercises/interpolation.rb b/day_1/exercises/interpolation.rb index c7f4f47df..a92d9b3f3 100644 --- a/day_1/exercises/interpolation.rb +++ b/day_1/exercises/interpolation.rb @@ -15,11 +15,11 @@ speedy = "quick red fox" slow_poke = "lazy brown dog" -p # YOUR CODE HERE +p "The #{speedy} jumped over the #{slow_poke}" # YOUR CODE HERE # Write code that uses the variables below to form a string that reads # "In a predictable result, the tortoise beat the hare!": slow_poke = "tortoise" speedy = "hare" -# YOUR CODE HERE +p "In a predictable result, the #{slow_poke} beat the #{speedy}!" # YOUR CODE HERE diff --git a/day_1/exercises/loops.rb b/day_1/exercises/loops.rb index 90dc15ab1..f65a3ab01 100644 --- a/day_1/exercises/loops.rb +++ b/day_1/exercises/loops.rb @@ -5,14 +5,16 @@ # Example: Write code that prints your name five times: 5.times do - p "Hermione Granger" + p "Kevin Nguyen rules!" end # Write code that prints the sum of 2 plus 2 seven times: 7.times do - # YOUR CODE HERE + p 2 + 2 # YOUR CODE HERE end # Write code that prints the phrase 'She sells seashells down by the seashore' # ten times: -# YOUR CODE HERE +10.times do + p "She sells seashells down by the seashore" +end # YOUR CODE HERE diff --git a/day_1/exercises/numbers.rb b/day_1/exercises/numbers.rb index 9a5468a31..a1bae1c88 100644 --- a/day_1/exercises/numbers.rb +++ b/day_1/exercises/numbers.rb @@ -7,10 +7,10 @@ p 2 + 2 # Write code that prints the result of 7 subtracted from 83: -p #YOUR CODE HERE +p 83 - 7 #YOUR CODE HERE # Write code that prints the result of 6 multiplied by 53: -# YOUR CODE HERE +p 6 * 53 #YOUR CODE HERE # Write code that prints the result of the modulo of 10 into 54: -# YOUR CODE HERE +p 10.modulo(54) # YOUR CODE HERE diff --git a/day_1/exercises/strings.rb b/day_1/exercises/strings.rb index f2f903ffc..e56b073ae 100644 --- a/day_1/exercises/strings.rb +++ b/day_1/exercises/strings.rb @@ -4,10 +4,10 @@ # `ruby day_1/exercises/strings.rb` # Example: Write code that prints your name to the terminal: -p "Alan Turing" +p "Kevin Nguyen" #"Alan Turing" # Write code that prints `Welcome to Turing!` to the terminal: -p #YOUR CODE HERE +p "Welcome to Turing!" #YOUR CODE HERE # Write code that prints `99 bottles of pop on the wall...` to the terminal: -# YOUR CODE HERE +p "99 bottles of pop on the wall..." # YOUR CODE HERE diff --git a/day_1/exercises/variables.rb b/day_1/exercises/variables.rb index a1e45bb26..6b756216a 100644 --- a/day_1/exercises/variables.rb +++ b/day_1/exercises/variables.rb @@ -1,6 +1,6 @@ # In the below exercises, write code that achieves # the desired result. To check your work, run this -# file by entering the following command in your terminal: +# file by entering the following command in your terminal: # `ruby day_1/exercises/variables.rb` # Example: Write code that saves your name to a variable and @@ -11,19 +11,20 @@ # Write code that saves the string 'Dobby' to a variable and # prints what that variable holds to the terminal: house_elf = "Dobby" -# YOUR CODE HERE +p house_elf # YOUR CODE HERE # Write code that saves the string 'Harry Potter must not return to Hogwarts!' # and prints what that variable holds to the terminal: -# YOUR CODE HERE +what = "Harry Potter must not return to Hogwarts!" +p what # YOUR CODE HERE # Write code that adds 2 to the `students` variable and # prints the result: students = 22 -# YOUR CODE HERE +students = students + 2 # YOUR CODE HERE p students # Write code that subracts 2 from the `students` variable and # prints the result: -# YOUR CODE HERE +students = students - 2 # YOUR CODE HERE p students diff --git a/day_1/questions.md b/day_1/questions.md index 73700e323..ce268fab6 100644 --- a/day_1/questions.md +++ b/day_1/questions.md @@ -1,17 +1,25 @@ ## Day 1 Questions -1. How would you print the string `"Hello World!"` to the terminal? +`1.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.2 What character is used to indicate comments in a ruby file?` +# -1. Explain the difference between an integer and a float? +`1.3 Explain the difference between an integer and a float?` +Decimal point and decimal value -1. In the space below, create a variable `animal` that holds the string `"zebra"` +`1.4 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? +`1.5 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`. +`1.6 What is interpolation? Use interpolation to print a sentence using the variable `animal`.` +puts "The best creature is the #{animal}." -1. What method is used to get input from a user? +`1.7 What method is used to get input from a user?` +answer = get.chomp -1. Name and describe two common string methods: +`1.8 Name and describe two common string methods:' +.length and .split diff --git a/day_2/array_methods.md b/day_2/array_methods.md new file mode 100644 index 000000000..f1825936d --- /dev/null +++ b/day_2/array_methods.md @@ -0,0 +1,17 @@ +'.sort': sorts arrays of strings alphabetically and numbers in ascending order. + +'.each': .each {|a| print a -= 10, " "} array iterator that specifies and action to be done for each array element. + +'.join': or .join("-") returns all the element joined into one string with no space or a specified separator in between + +'.index': .index("a") returns the position (ie 0,1,2) for the first element that matches, or "nil" if it is not present. + +'.include?': .include?("a") searches if an element is present and returns "true" or "false" + +'.collect': .collect {|a| a + "!"} returns new values of each element after a specified action. + +'.first': or .first(2) returns the first or first number of elements in an array. + +'.last': or .last(2) returns the last or last number of elements in an array. + +'.shuffle': or .shuffle! randomizes order of elements temporarily or permanently with "!" diff --git a/day_2/exercises/arrays.rb b/day_2/exercises/arrays.rb index f572a5ae6..97f452acf 100644 --- a/day_2/exercises/arrays.rb +++ b/day_2/exercises/arrays.rb @@ -9,32 +9,39 @@ p animals # Write code that stores an array of states in a variable, -# then prints that array: -states = #YOUR CODE HERE +# then prints that array: #YOUR CODE HERE- +states = [ "Florida", "Texas", "Idaho", "Utah"] p states # Write code that stores an array of foods in a variable, -# then prints that array: -# YOUR CODE HERE +# then prints that array: # YOUR CODE HERE- +foods = [ "Burgers", "Fries", "Shakes"] +p foods # Example: Write code that prints the number of elements # in your above array of animals: p animals.count # Write code that prints the number of elements -# in your above array of foods: -# YOUR CODE HERE +# in your above array of foods: # YOUR CODE HERE- +p foods.count # Write code that prints "Zebra" from your animals array: -# YOUR CODE HERE +# YOUR CODE HERE- +p animals[0] # Write code that prints the last item of your foods array: -# YOUR CODE HERE +# 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 +# YOUR CODE HERE- +animals << "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 +# YOUR CODE HERE- or foods.pop() NOT foods.delete.last +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..bdcdc3ca2 --- /dev/null +++ b/day_2/exercises/boolean_practice.md @@ -0,0 +1,39 @@ +true && true TRUE +false && true FALSE +1 == 1 && 2 == 1 FALSE + +"test" = "test" TRUE +1 == 1 && 2 != 1 TRUE +true && 1 == 1 TRUE + +false && 0 != 0 FALSE +true || 1== 1 TRUE +"test" == "testing" FALSE + +1 != 0 && 2 == 1 FALSE +"test" != "testing" TRUE +"test" == 1 FALSE + +!(true && false) TRUE +!(1 == 1 && 0 != 1) FALSE +!(10 == 1 || 1000 == 1000) FALSE + +!(1 != 10 || 3 == 4) FALSE +!("testing" == "testing" && "Zed" == "Cool") TRUE +1 == 1 && (!("testing" == 1 || 1 == 0)) TRUE + +"chunky" == "bacon" && (!(3 == 4 || 3 == 3)) FALSE +3 == 3 && (!("testing" == "testing" || "Ruby" == "Fun")) FALSE + +STUDY DRILLS +1): == 'equal' != 'not equal' + < Less than <= 'Less or equal than' + # > 'Greater than' >= 'Greater or equal than' + <=> 'Greater, Equal, Or Less' + === test equality within when clause of case statement + += Add and assignment operator + -= Subtract and assignment operator + *= Multiply and assignment operator + /= Divide and assignment operator + %= Modulus and assignment operator + **= Exponent and assignment operator diff --git a/day_2/exercises/iteration.rb b/day_2/exercises/iteration.rb index a801cb4fc..36f9a30c2 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" # YOUR CODE HERE end -# Write code that stores an array of foods in a variable, +# Write code that stores an array of foods in a variable, # then iterates over that array to print -# "Add to shopping list" for each food item: -# YOUR CODE HERE +# "Add to shopping list" for each food item:# YOUR CODE HERE +foods = [ "Pears", "Nuts", "Salad", "Dressing"] +foods.each do |food| + p "Add #{food} to shopping list" +end +# or foods.each {|food| p "Add #{food} to shopping list"} -# 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 = [0, 1, 2, 3, 5, 7] # YOUR CODE HERE +numbers.each do |number| + p number * 2 +end +# or numbers.each {|number| p number * 2} diff --git a/day_2/exercises/iteration_each_ex.rb b/day_2/exercises/iteration_each_ex.rb new file mode 100644 index 000000000..b7ca0dce7 --- /dev/null +++ b/day_2/exercises/iteration_each_ex.rb @@ -0,0 +1,67 @@ +#Ex.1 Print double of each number +numbers = [1,2,3,4] +numbers.each do |number| + puts number * 2 +end +#Print triples of each numbers +numbers = [1,2,3,4] +numbers.each {|number| puts number * 3} + +#Ex.2 Print even numbers +numbers = [1,2,3,4] +puts numbers[1] +puts numbers[3] +#Print odd numbers +numbers = [1,2,3,4] +puts numbers[0] +puts numbers[2] + +#Ex.3 Create new array with values doubled +numbers = [1,2,3,4] +numbers = numbers.collect {|number| number * 2} +p numbers + +#Ex.4 Print each element line by line +names = [ "Alice Smith", "Bob Evans", "Roy Rogers" ] +names.each do |name| + puts name +end #or names.each {|name| puts name} + +#Print only the first names +names = [ "Alice Smith", "Bob Evans", "Roy Rogers" ] +names.each do |name| + name = name.split + puts name.first +end + +#Print only the last names +names = [ "Alice Smith", "Bob Evans", "Roy Rogers" ] +names.each do |name| + name = name.split + puts name.last +end + +#Print only the initials +names = [ "Alice Smith", "Bob Evans", "Roy Rogers" ] +names.each do |name| + name = name.split.map {|name| name[0]}.join + puts name +end + +#Print last names and number of characters in it +names = [ "Alice Smith", "Bob Evans", "Roy Rogers" ] +names.each do |name| + name = name.split + print name.last + puts name.last.length +end + +#Create integer representing total # characters +names = [ "Alice Smith", "Bob Evans", "Roy Rogers" ] +total = 0 +names.each do |name| + name = name.split + total = total + name.first.length + total = total + name.last.length +end +p total diff --git a/day_2/questions.md b/day_2/questions.md index a179f0b04..147ca9668 100644 --- a/day_2/questions.md +++ b/day_2/questions.md @@ -2,16 +2,29 @@ 1. Create an array containing the following strings: `"zebra", "giraffe", "elephant"`. +Arrays = ["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 << "lion" + 1. Name and describe two additional array methods: +.each do Instructs to 'do' the following action to each array element. +.last Returns the last or last(X) number of elements in an array. 1. What are the boolean values in Ruby? +The boolean values are true or false. 1. In Ruby, how would you evaluate if `2` is equal to `25`? What is the result of this evaluation? +2 == 25 outputs false 1. In Ruby, how would you evaluate if `25` is greater than `2`? What is the result of this evaluation? +25 z. 2 outputs true diff --git a/day_3/exercises/if_statements.rb b/day_3/exercises/if_statements.rb index a80b96840..501c388d8 100644 --- a/day_3/exercises/if_statements.rb +++ b/day_3/exercises/if_statements.rb @@ -3,15 +3,14 @@ # 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 = 'fair' if weather == 'sunny' p "sunscreen" elsif weather == 'rainy' @@ -26,7 +25,7 @@ # Experiment with manipulating the value held in variable 'weather' # to print something other than 'coat' - +#weather = 'fair' => "good to go!" ################## # Using the num_quarters variable defined below, determine @@ -35,21 +34,23 @@ # Right now, the program will print # out both "I have enough money for a gumball" and -# "I don't have enough money for a gumball". Write a +# "I don't have enough money for a gumball". Write a # conditional statement that prints only one or the other. # Experiment with manipulating the value held within num_quarters # to make sure both conditions can be achieved. -num_quarters = 0 - -puts "I have enough money for a gumball" -puts "I don't have enough money for a gumball" +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 +62,13 @@ # Experiment with manipulating the value held within both variables # to make sure all above conditions output what you expect. -cups_of_flour = 1 +cups_of_flour = 3 has_sauce = true + +if cups_of_flour <= 1 + print "I cannot make pizza" +elsif has_sauce == false + print "I cannot make pizza" +else + print "I can make pizza" +end diff --git a/day_3/if_study_drills.md b/day_3/if_study_drills.md new file mode 100644 index 000000000..35ae9225c --- /dev/null +++ b/day_3/if_study_drills.md @@ -0,0 +1,22 @@ +WHAT IF?: study drills +1) The 'if' checks if the required condition is fulfilled to proceed to the if action. Creates a branch. +2) It's indented two spaces to show it's a block, for better visibility. +3) Code still works. Without indenting, it looks like the if action, it is harder to distinguish the if block. Without the 'end', it's a syntax error. +4) if cats != dogs + p "There are enough dogs" + end +5) If the variable values are changed, different if actions would happen. + +ELSEIF AND IF?: study drills +1) 'else if' give another branch option to run if a second parameter is met. 'else' is what runs in the case the 'if' and 'else if' are not met. +2) people = 15 +cars = 15 +trucks = 15 +=> We can't decide. +We still can't decide. +Fine, let's stay home then. +3,4) "if the # of cars is more than or equal to the # of trucks and the # of trucks is more than people, then do the following" + if cars >= 2 * trucks && trucks > people + "or if there are more trucks than cars or more people than cars" + elseif trucks > cars || people > cars + diff --git a/day_3/questions.md b/day_3/questions.md index db6170fa7..8b2c22b10 100644 --- a/day_3/questions.md +++ b/day_3/questions.md @@ -1,13 +1,29 @@ ## Day 3 Questions 1. What is a conditional statement? Give three examples. +A conditional statement evaluates a variable requirement is true or false. 3 examples are: if x = 0, p "No value" + if dishes != 'clean', puts "Wash dishes" + if light = 'yellow', print "Apply brake" -1. Why might you want to use an if-statement? +2. Why might you want to use an if-statement? +To create a branch where another action occur if a specified requirement is met. -1. What is the Ruby syntax for an if statement? +3. What is the Ruby syntax for an if statement? + if x == 0 + p "Zero" + end -1. How do you add multiple conditions to an if statement? +4. How do you add multiple conditions to an if statement? +By adding one ore more 1elsif' conditions after the 'if' -1. Provide an example of the Ruby syntax for an if/elsif/else statement: +5. Provide an example of the Ruby syntax for an if/elsif/else statement: +if x > 0 + p "It's positive" +elsif x < 0 + p "It's negative" +else + p "It's zero" +end -1. Other than an if-statement, can you think of any other ways we might want to use a conditional statement? +6. Other than an if-statement, can you think of any other ways we might want to use a conditional statement? +A condition statement can be used to make decisions where there are more than two options and different actions for each option. diff --git a/day_4/exercises/method_lessons_work.md b/day_4/exercises/method_lessons_work.md new file mode 100644 index 000000000..e4628f220 --- /dev/null +++ b/day_4/exercises/method_lessons_work.md @@ -0,0 +1,118 @@ +EX. 19 STUDY DRILLS: Functions and Variables +1) Write an explanatory comment about each script line + +*Define the function, cheese_and_crackers, and its 2 argument +def cheese_and_crackers(cheese_count, boxes_of_crackers) +*First action of the function + puts "You have #{cheese_count} cheeses!" +*Second action of the function + puts "You have #{boxes_of_crackers} boxes of crackers!" +*Third action of the function + puts "Man that's enough for a party!" +*Fourth action of the function + puts "Get a blanket.\n" +*Signifies end of the function +end + +*First action in the main code +puts "We can just give the function numbers directly:" +*Calls cc function with 20 & 30 as arguments +cheese_and_crackers(20, 30) + +*Action in the main code +puts "OR, we can use variables from our script:" +*Defines value of a variable +amount_of_cheese = 10 +*Defines value of a second variable +amount_of_crackers = 50 + +*Calls cc function with the two new variables +cheese_and_crackers(amount_of_cheese, amount_of_crackers) + +*Action in the main code +puts "We can even do math inside too:" +*Calls cc function with numerical arguments +cheese_and_crackers(10 + 20, 5 + 6) + +*Action in the main code +puts "And we can combine the two, variables and math:" +*Calls cc function with variables and numbers computed as arguments +cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000) + + +FUNCTION RUN 10 WAYS + +def set_price(base_cost, labor_price, misc) + puts "Given cost inputs of #{base_cost}, #{labor_price} and #{misc}." + puts "The final cost is $ #{base_cost + labor_price + misc}." +end + +puts "Many factors determine the best pricing." +puts "The simplest price is basic at-cost price" +set_price(2.14, 1.76, 0.83) #1 way + +puts "Or an overall %20 mark-up of the at-cost price" +set_price(2.14 * 1.2, 1.76 * 1.2, 0.83 * 1.2) #2 way + +puts "Another price is based on specified values" +base_cost = 2.58 +labor_price = 2.04 +misc = 1.26 +set_price(base_cost, labor_price, misc) #3 way + +puts "An $0.50 up-charge can be added to each specified value" +set_price(base_cost + 0.5, labor_price + 0.5, misc + 0.5) #4 way + +puts "Or price factors can be input individually." +puts "What is the current base cost?" +print "> " +base_cost = $stdin.gets.chomp.to_f +puts "What is the current labor price?" +print "> " +labor_price = $stdin.gets.chomp.to_f +puts "What is the current misc cost?" +print "> " +misc = $stdin.gets.chomp.to_f +set_price(base_cost, labor_price, misc) #5 way + +puts "Price adjustments can be made to the inputs as well." +puts "What percent increase should be use for the input prices?" +print "> " +price_adj = $stdin.gets.chomp.to_f/100 + 1 +set_price(base_cost * price_adj, labor_price * price_adj, misc * price_adj) #6 + +puts "And varying up-charges can be added to the price inputs." +set_price(base_cost + 0.4, labor_price + 0.6, misc + 0.8) #7 way + +puts "Also, price adjustments to direct prices" +set_price(2.14 * price_adj, 1.76 * price_adj, 0.83 * price_adj) #8 way + +price_bump = 1.1 +puts "Price bumps to direct prices as well" +set_price(2.14 + price_bump, 1.76 + price_bump, 0.83 + price_bump) #9 way + +puts "And price bumps to input prices" +set_price(base_cost + price_bump, labor_price + price_bump, misc + price_bump) #10 way + +EX. 21: Functions-Return +1) +def calculate(x, y) + return (3*x-y)/2 +end + +2) +def puzzle(age, height, weight, iq) + return age+(height-(weight*(iq/2))) +end + +3) (3x-y)/2 +def multiply_subtract(x) + return 3*x-y +end + +def divide(z) + return z/2 +end +divide(multiply_subtract(x,y)) + +4) Scripts works even without 'Return' written explicitly. diff --git a/day_4/exercises/methods.rb b/day_4/exercises/methods.rb index 6ed338e5d..d5e6ba564 100644 --- a/day_4/exercises/methods.rb +++ b/day_4/exercises/methods.rb @@ -12,16 +12,26 @@ def print_name # Write a method that takes a name as an argument and prints it: def print_name(name) - # YOUR CODE HERE + p name # YOUR CODE HERE 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 sum_fun(x,y) # YOUR CODE HERE + puts x + y +end + +sum_fun(12,34) -# 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 concatenate(first_string, second_string) + puts "Greetings #{first_string}, shall we play #{second_string}?" +#OR puts "Greetings " + first_string + ", shall we play " + second_string + "?" +end + +concatenate("human", "a game") diff --git a/day_4/questions.md b/day_4/questions.md index af17ab4da..d754c33c7 100644 --- a/day_4/questions.md +++ b/day_4/questions.md @@ -2,10 +2,24 @@ 1. In your own words, what is the purpose of a method? -1. Create a method named `hello` that will print `"Sam I am"`. +A method defines an action(s) to perform with specified input)s) -1. Create a method named `hello_someone` that takes an argument of `name` and prints `"#{name} I am"`. +2. Create a method named `hello` that will print `"Sam I am"`. -1. How would you call or execute the method that you created above? +def hello() + puts "Sam I am" +end -1. What questions do you have about methods in Ruby? +3. Create a method named `hello_someone` that takes an argument of `name` and prints `"#{name} I am"`. + +def hello_someone(name) + puts "#{name} I am" +end + +4. How would you call or execute the method that you created above? + +hello_someone() + +5. What questions do you have about methods in Ruby? + +Is it a fair summary to say, as methods get more complex, more notation or documentation is necessary? diff --git a/day_5/exercises/drills_hashes.md b/day_5/exercises/drills_hashes.md new file mode 100644 index 000000000..8fc3e65a5 --- /dev/null +++ b/day_5/exercises/drills_hashes.md @@ -0,0 +1,11 @@ +EX.39 - HASHES: Study Drills (LRTHW) +1) Colorado is abbreviated CO, CO has the city Denver + states["Colorado"] = "CO" + New Mexico is abbreviated NM, NM has the city Taos + states["New Mexico"] = "NM" + +2) cities.merge({CO => Denver, NM => Taos}) + cities.size 5 + cities.keys ["CA", "MI", "FL", "CO", "NM"] + +3) Hashes seem to not work in the opposite direction, calling the value (after the hash rocket) to get the key. diff --git a/day_5/exercises/hashes.rb b/day_5/exercises/hashes.rb index 99fcebb77..e42f1d242 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 = {lions: 5, tigers: 11, bears: 7}#YOUR CODE HERE p zoo -# Write code that prints all of the 'keys' of the zoo variable +# Write code that prints all of the 'keys' of the zoo variable # you created above: -# YOUR CODE HERE +p zoo.keys # YOUR CODE HERE -# 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 # YOUR CODE HERE -# 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[:lions] # YOUR CODE HERE -# 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[:ferrets] = 1 # YOUR CODE HERE +p zoo diff --git a/day_5/questions.md b/day_5/questions.md index d059e12c6..3ebb49584 100644 --- a/day_5/questions.md +++ b/day_5/questions.md @@ -2,12 +2,24 @@ 1. What is a Hash, and how is it different from an Array? -1. In the space below, create a Hash stored to a variable named `pet_store`. This hash should hold an inventory of items and the number of that item that you might find at a pet store. +A Hash matches a key to a value, and the order does not matter like in an array. -1. Given the following `states = {"CO" => "Colorado", "IA" => "Iowa", "OK" => "Oklahoma"}`, how would you access the value `"Iowa"`? +2. In the space below, create a Hash stored to a variable named `pet_store`. This hash should hold an inventory of items and the number of that item that you might find at a pet store. -1. With the same hash above, how would we get all the keys? How about all the values? +pet_store = {crickets: 82, lizards: 24, snakes: 9} -1. What is another example of when we might use a hash? In your example, why is a hash better than an array? +3. Given the following `states = {"CO" => "Colorado", "IA" => "Iowa", "OK" => "Oklahoma"}`, how would you access the value `"Iowa"`? -1. What questions do you still have about hashes? +states["IA"] + +4. With the same hash above, how would we get all the keys? How about all the values? + +states.keys + +5. What is another example of when we might use a hash? In your example, why is a hash better than an array? + +Hashes are very useful for any type of listing, like in an inventory or supplies. Hashes are better than arrays because the inventory value can be called from the name or key, while in an array, one needs the position in the array of the name or item. + +6. What questions do you still have about hashes? + +Are several nested hashes the most effective method for keeping track of more than 2 or 10 listings of values/info to be associated with each key or name? diff --git a/day_6/exercises/burrito.rb b/day_6/exercises/burrito.rb index 967f68b6c..f630046ad 100644 --- a/day_6/exercises/burrito.rb +++ b/day_6/exercises/burrito.rb @@ -1,19 +1,37 @@ -# 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 # 3. change_protein class Burrito - attr_reader :protein, :base, :toppings + attr_reader :base, :toppings #3.removed :protein + attr_accessor :protein #3.new + def initialize(protein, base, toppings) @protein = protein @base = base @toppings = toppings end + def add_topping(toppings) #1.new + @toppings << toppings + end + def remove_topping(toppings) #2.new + @toppings.delete(toppings) + end + def change_protein(protein) #3.new + @protein = protein + end end dinner = Burrito.new("Beans", "Rice", ["cheese", "salsa", "guacamole"]) -p dinner.protein -p dinner.base -p dinner.toppings +puts dinner.protein +puts dinner.base +puts dinner.toppings.join(", ") +dinner.add_topping("sour cream") #1.new +puts dinner.toppings.join(", ") #1.new +dinner.remove_topping("salsa") #2.new +puts dinner.toppings.join(", ") #2.new +dinner.change_protein("Pork") #3.new +puts "I changed my protein to " + dinner.protein #3.new +puts "My dinner burrito has #{dinner.base} and #{dinner.protein} on bottom with #{dinner.toppings.join(", ")} on top. Yum!" diff --git a/day_6/exercises/dog.rb b/day_6/exercises/dog.rb index 03221314d..29d9027b4 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 @@ -16,7 +16,13 @@ def bark p "woof!" end + def play #new + p "#{@name} just played, I wonder if they're hungry..." + @hungry = true + end + def eat + p "#{@name} gets kibble, nom." @hungry = false end end @@ -28,3 +34,5 @@ def eat p fido.hungry fido.eat p fido.hungry +fido.play #new +p fido.hungry #new diff --git a/day_6/exercises/person.rb b/day_6/exercises/person.rb index 2c26e9570..e7725d06f 100644 --- a/day_6/exercises/person.rb +++ b/day_6/exercises/person.rb @@ -1,5 +1,72 @@ -# 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, :job, :is_tired + +def initialize(name, job, is_tired) + @name = name + @job = job + @is_tired = false +end + +def sleep + @is_tired = false +end + +def graduate_turing + @job = "programmer" + @is_tired = true +end + +end + +cody = Person.new("Cody 'Nruby", "civilian", true) +print cody.name +p " was a " + cody.job +p "Is #{cody.name} tired? " + cody.is_tired.to_s +cody.graduate_turing +p cody.name + " is now a " + cody.job +p "Is #{cody.name} tired? " + cody.is_tired.to_s +cody.sleep +p "Is #{cody.name} tired? " + cody.is_tired.to_s + + + + + +class Person +attr_accessor :name, :job + +def initialize(name, job) + @name = name + @job = job + @is_tired = false +end + +def sleep + @is_tired = false + if @is_tired == false + zombie = "No" + else + zombie = "Yes" + end +end + +def graduate_turing + @job = "programmer" + @is_tired = true + if @is_tired == false + zombie = "No" + else + zombie = "Yes" + end +end + +if @is_tired == false + zombie = "No" + else + zombie = "Yes" +end diff --git a/day_6/questions.md b/day_6/questions.md index f58ca5f71..6c01645fa 100644 --- a/day_6/questions.md +++ b/day_6/questions.md @@ -1,13 +1,37 @@ ## Day 6 Questions 1. In your own words, what is a Class? +A class a broad category that is named, and used to organize objects within. -1. What is an attribute of a Class? +2. What is an attribute of a Class? +An attribute is one characteristic of an object in a class. -1. What is behavior of a Class? +3. What is behavior of a Class? +A behavior is an action or method a class can undergo. -1. In the space below, create a Dog class with at least 2 attributes and 2 behaviors: +4. In the space below, create a Dog class with at least 2 attributes and 2 behaviors: -1. How do you create an instance of a class? +class Dog + attr_accessor :size, :weight -1. What questions do you still have about classes in Ruby? + def initialize(size, weight) + @size = size + @weight = weight + @tired = false + end + def play_fetch + p "Go get the ball." + @tired = true + end + def take_nap + p "Sleepy-time." + @tired = false + end +end + + +5. How do you create an instance of a class? + An instance is created by defining all the attributes of an object in the class. General form is: instance_name = class_name.new(attribute_inputs) + +6. What questions do you still have about classes in Ruby? +What are specific applications when self.name is needed instead of @name? diff --git a/day_7/10_speckled_frogs.rb b/day_7/10_speckled_frogs.rb new file mode 100644 index 000000000..b368767ee --- /dev/null +++ b/day_7/10_speckled_frogs.rb @@ -0,0 +1,41 @@ +# > 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. + +def SpeckledFrog (number) + while number >= 2 + p "#{number} speckled frogs sat on a log" + p "eating some most delicious bugs." + p "One jumped in the pool where its nice and cool," + number -= 1 + p "then there were #{number} speckled frogs." + puts + end + p "#{number} speckled frogs sat on a log" + p "eating some most delicious bugs." + p "One jumped in the pool where its nice and cool," + p "then there were no more speckled frogs." +end + +SpeckledFrog(10) + +# +# ### Extension 1 +# Print word versions of each number in the first and fourth lines, for example, the first verse in the above example would print 'Three speckled frogs...' and 'were two speckled frogs'. +# +# ### Extension 2 +# Make your program work for any number of frogs. diff --git a/day_7/caesar_cipher.rb b/day_7/caesar_cipher.rb new file mode 100644 index 000000000..41e7f5e4b --- /dev/null +++ b/day_7/caesar_cipher.rb @@ -0,0 +1,27 @@ +def cipher(input, shift) + input_to_ascii_array = input.chars.map {|char| char.ord} + shifted = input_to_ascii_array.map {|char| char+shift} + shifted.map { |char| char.chr }.join +end + +p cipher("Pat is Mafia!", 6) + +#Or an alternate, less direct, version that includes 'class' +# +#class Secret +#attr_accessor :input, :shift + +# def initialize(input, shift) +# @input = input +# @shift = shift +# end +# def scramble(input, shift) +# input_ascii = input.chars.map {|char| char.ord} +# shifted = input_ascii.map {|char| char+shift} +# shifted.map { |char| char.chr }.join +# end +# end + +# message = Secret.new("Pat is Mafia!", 6) +# p message.input +# p message.scramble("Pat is Mafia!", 6) diff --git a/day_7/checker_board.rb b/day_7/checker_board.rb new file mode 100644 index 000000000..186e8483e --- /dev/null +++ b/day_7/checker_board.rb @@ -0,0 +1,30 @@ +print "What size (# rows/columns)? => " #prompt for size input +size = gets.chomp() #get input as 'size' +y = size.to_i / 2 #make new variable half of 'size' + +def print_rows(size,y) #define method, uses size & xx + if size % 2 == 0 #for even 'size' do this block + while size >=1 #loop counter rows + if size % 2 == 1 #print this line for odd rows + puts " X" * y #prints pair ' X' y #of-times + size -= 1 + else #print this line for even rows + puts "X " * y #prints pair 'X ' y #of-times + size -= 1 + end + end + else #for odd 'size' do this block + while size >=1 #loop counter rows + if size % 2 == 1 #print this line for odd rows + print "X " * y #prints pair 'X ' y #of-times + puts "X" #print last odd 'X' + size -= 1 + else #print this line for even rows + print " X" * y #prints pair ' X' y #of-times + puts " " #print last odd ' ' + size -= 1 + end + end + end +end +print_rows(size.to_i,y.to_i) #run the method for inputed 'size' diff --git a/day_7/fizzbuzz.rb b/day_7/fizzbuzz.rb new file mode 100644 index 000000000..2f54455c8 --- /dev/null +++ b/day_7/fizzbuzz.rb @@ -0,0 +1,46 @@ +#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. + +#The output of your program will look something like this: +#``` +#=> 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, ..., 98, Fizz, Buzz +#``` + +### Bonus +#Can you write the program so that it will run for any range of numbers? + +def FizzBuzz (number,high) + while number <= high - 1 + if number % 15 == 0 + print 'FizzBuzz, ' + elsif number % 3 == 0 + print 'Fizz, ' + elsif number % 5 == 0 + print 'Buzz, ' + else + print "#{number}, " + end + number += 1 + end + print number +end + +FizzBuzz(1,100) + +# 100.times do |number| +# number += 1 +# if number % 15 == 0 +# print 'FizzBuzz, ' +# elsif number % 3 == 0 +# print 'Fizz, ' +# elsif number % 5 == 0 +# print 'Buzz, ' +# else +# print "#{number}, " +# end +# print number +# end diff --git a/day_7/high_level.md b/day_7/high_level.md new file mode 100644 index 000000000..c2aff6e7b --- /dev/null +++ b/day_7/high_level.md @@ -0,0 +1,37 @@ +## Caesar Cipher Write Up + +The Ceasar Cipher is a shift method at it's core. Instead of substituting one letter for another, the method takes each letter in the string and shifts it a specified number of spaces to the left or right. The first step is to define the overall shift method, and it's input string and number to shift. + +`def cipher(input, shift)` + +To keep track of how many spaces to shift, a way to track the current position of each letter in needed, then the number to shift over can be applied. First, we break up the "input" string into individual characters. I found the precise coding from google [primary reference link](https://medium.com/@alexander.virga/ruby-simple-string-encryption-shift-caesar-cipher-encoder-rot-9dedf06374d1): + +`input_to_ascii_array = input.chars` + +Then add-on to map each letter to a number with ASCII. + +`input_to_ascii_array = input.chars.map {|char| char.ord}` + +Now, we apply the number to "shift". + +`shifted = input_to_ascii_array.map {|char| char+shift}` + +Last step is mapping the shifted numbers back to letters. And "end" the method + +`shifted.map { |char| char.chr }.join` +`end` + +### The full method is below: + +``` +def cipher(input, shift) + input_to_ascii_array = input.chars.map {|char| char.ord} + shifted = input_to_ascii_array.map {|char| char+shift} + shifted.map { |char| char.chr }.join +end +``` + +**For Example** + +p cipher("Pat is Mafia!", 6) +=> "Vgz&oy&Sglog'"