Created
September 21, 2013 22:26
-
-
Save kmandreza/6654798 to your computer and use it in GitHub Desktop.
LET'S PLAY
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Exercise 1 | |
Define three local variables: home_address, home_city, and home_state. Define and assign them the values of your own personal home address, city, and state, respectively. | |
# Exercise 2 | |
Explain the difference between the following lines of code in plain English. "Plain English" means use language Edward, your non-programming second cousin from Ames, IA, would understand. | |
If any of the lines are invalid, explain why. | |
first_name = "Khara" | |
first_name == "Khara" | |
"Muniz" = first_name | |
"Muniz" == first_name | |
# Exercise 3 | |
Define a method called fancy_pants that takes three arguments named first_name, middle_name, and last_name (in that order). The body of the method can be empty — we're just looking for the method definition. | |
# Exercise 4 | |
Explain in plain English what the following method does: | |
def foobar(arg) | |
if arg > 42 | |
puts "Danger! Danger!" | |
elsif arg == 42 | |
puts "Just right!" | |
else | |
puts "She needs more power, cap'n!" | |
end | |
end | |
# Exercise 5 | |
Consider the following array: | |
["ruby", "is", "the", "best", "programming", "language", "ever"] | |
Answer the following questions: | |
What value is stored at index 3? | |
What is the index of the word "ruby?" | |
What is the length of the array? | |
# Exercise 6 | |
Create a method new_cat which takes as its input two strings. It should concatenate the strings and return the concatenated value, without modifying either of the input strings. For example, | |
One does not simply walk into... | |
new_cat("Mor", "dor") # => "Mordor" | |
Call this method and send your first and last name as arguments. | |
# Exercise 7 | |
Write a method called product which takes as its input an array of integers and returns their product. For example | |
product([1,2,3]) # returns 6 | |
product([0,-1,-10]) # returns 0 | |
product([1,-1,-10]) # returns -11 | |
If you need to iterate over the array, please use Array#each. Don't use, e.g., inject. | |
# Exercise 8 | |
Write a method called product_odd which takes as its input an array of integers and returns the product of all the odd integers in the array. Remember that technically 0 is an even integer. | |
For example: | |
product_odd([1,2,3]) # returns 3, because 2 is even | |
product_odd([0,-1,-10]) # returns -1, because 0 and -10 are even | |
product_odd([1,2,3,4,5]) # returns 15, because 4 and 2 are even | |
If you need to iterate over the array, please use Array#each. Don't use, e.g., inject. | |
# Exercise 9 | |
Write a method called fizzblam that prints the numbers from 1 to 1000. But for multiples of 5 print "Fizz" instead of the number and for the multiples of 7 print "Blam". For numbers which are multiples of both 5 and 7 print "FizzBlam." | |
# Exercise 10 | |
Consider the following code examples. In each case assume the input array is an array of integers. | |
For each example, explain in a one or two plain-English sentences what the given method does when passed an array of positive integers. | |
Choose one method (not the first!) and refactor it into something shorter. Give the working code of the refactored method. | |
Hint: If you can't figure out what these methods do by reading the code, run them locally and try to figure it out by experimenting with the input parameters. | |
Code Examples for Exercise 10 | |
def method_1(array) | |
array.each do |num| | |
puts num*100 | |
end | |
end | |
def method_2(array) | |
foo = array.first | |
array.each do |num| | |
if num > foo | |
foo = num | |
end | |
end | |
return foo | |
end | |
def method_3(array) | |
results = [] | |
array.each do |num| | |
if num > 0 | |
results << num | |
end | |
end | |
return results | |
end | |
def method_4(array) | |
results = [] | |
array.each do |num| | |
if num % 2 == 0 | |
results << num | |
end | |
end | |
return results | |
end | |
# Exercise 11 | |
Consider the following method: | |
def print_value(hash, key) | |
puts hash[key].inspect | |
end | |
What will the following code print to the console and why? | |
hash = {:dog => 'woof', 'cat' => 'meow', 'duck' => 'quack'} | |
print_value(hash, :dog) | |
print_value(hash, 'dog') | |
print_value(hash, 'cat') | |
print_value(hash, 'quack') | |
print_value(hash, :duck) | |
# Exercise 12 | |
Explain in plain English the difference between return and puts. You're free to use code examples to illustrate your point. | |
# Exercise 13 | |
Using pseudocode, write a simple hangman game. The point of pseudocode is to represent the logical structure of a program without getting bogged down in the syntax of a particular language. | |
For example, a program to select the largest integer from an array of integers might be written like this in pseudocode | |
initialize max_so_far to the first element of the array | |
for each element of the array | |
if element is greater than max_so_far | |
set max_so_far to element | |
return max_so_far | |
The particular conventions are not important. The point is to express the logical structure in a kind of English that another programmer could both understand and, witih a little effort, translate into working code in their language of choice. | |
Whew, you're done! | |
Time for a nap. |
#13
def evaluate_guess(baby_array, answer_array, guess)
baby_array.each_index { |i|
if baby_array[i] == guess
puts "You got one!"
answer_array[i]=baby_array[i]
end }
answer_array
end
baby_array = ["b", "a", "b", "y"]
answer_array = ["", "", "", ""]
puts "We're playing hangman."
(1..6).each do |guesses|
puts "What is your #{guesses} guess?"
guess = gets.chomp
evaluate_guess(baby_array, answer_array, guess)
p answer_array
if answer_array == baby_array
puts "Hang Man!"
break
end
end
def evaluate_guess(baby_array, answer_array, guess)
baby_array.each_index { |place|
if baby_array[place] == guess
puts "You got one!"
answer_array[place]=baby_array[place]
end }
end
baby_array = ["b", "a", "b", "y"]
answer_array = ["", "", "", ""]
puts "We're playing hangman. What is your guess?"
guess = gets.chomp
baby_array = ["b", "a", "b", "y"]
answer_array = ["", "", "", ""]
evaluate_guess(baby_array, answer_array, guess)
p answer_array
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
=begin
Set count of wrong guesses to 0.
Set random to random word.
Set result as array of underscores length of random
for random to random.length
print underscore
next
Define a method to draw hangman where:
wrong guess 1 = head
wrong guess 2 = body
wrong guess 3 = left leg
wrong guess 4 = right leg
wrong guess 5 = left arm
wrong guess 6 = right arm
Until wrong > 6
Ask player for guess. Validate that guess is [a-z]
if guess is found in random, then print guesses[all]
else wrong =+ 1
draw
end
random.map { |x| x == "b" }
=> [true, false, true, false]
returns false, leave as a dash