Skip to content

Instantly share code, notes, and snippets.

@kmandreza
Created September 21, 2013 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmandreza/6654798 to your computer and use it in GitHub Desktop.
Save kmandreza/6654798 to your computer and use it in GitHub Desktop.
LET'S PLAY
# 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.
@kmandreza
Copy link
Author

def fancy_pants(first_name, middle_name, last_name)
return "My full name is #{first_name} #{middle_name} #{last_name}."
end

def ugly()
x = ( y = "foo")
end

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

p 41; foobar(41)
p 42; foobar(42)
p 43; foobar(43)

b = fancy_pants("Khara", "Mandy", "Muniz")

puts b

puts ugly()

@kmandreza
Copy link
Author

def new_cat(str1, str2)
return str1 + str2
end

p new_cat("Mor", "dor")

@kmandreza
Copy link
Author

def product(int_array)
prod = 1
int_array.each do |i|
prod *= i
end
return prod
end

def product_odd(int_array)
int_array.select { |i| i.odd? }.each do |i|
prod *= i
end
return prod
end

def product_odd1(int_array)
a = int_array.select { |i|
i.odd?
}
product(a)
end

a = [1,2,3]
b = [4,5,6,44,664]
c = [4,-1,23,45,665]

puts product_odd(a)
p product_odd(b)
p product_odd(c)

@kmandreza
Copy link
Author

def fizzblam(arr)
arr.each do |i|
if (i%7 == 0)
if (i%5 == 0)
puts "fizzblam"
else
puts "blam"
end
elsif (i%5 == 0)
puts "fizz"
else
puts i
end
end
end

fizzblam(1..36)

@kmandreza
Copy link
Author

Exercise 10

=begin

  1. For each number in the array, method_1 multiples
    each number by 100.
    =end

def method_1(array)
array.each do |num|
puts num*100
end
end

arr_intg = [1,2,3,4]
method_1(arr_intg)

=begin
2. Method_2 compares the first number in the array,
foo, with each number of the array. If the number
is greater than foo, that number is assigned to the
variable foo. When foo is returned, it returns the
greatest value in the array.
=end

def method_2(array)
foo = array.first

array.each do |num|
    if num > foo
    foo = num
end

end
return foo
end

arr_intg2 = [4,3,2,7]
p = method_2(arr_intg2)
puts p

=begin

=end

@kmandreza
Copy link
Author

Exer 10

def method_2(array)
foo = array.first

array.each do |num|
    if num > foo
    foo = num
end

end
return foo
end

arr_intg2 = [4,3,2,7]
p = method_2(arr_intg2)
puts p

=begin
3. Method_3 evaluates each number in the array and
pushes/append the numbers greater than zero in the
results array.
=end

def method_3(array)
results = []

array.each do |num|
    if num > 0
    results << num
    end
end 
return results

end

arr_intg3 = [-1,-2,2,4,6]
p method_3(arr_intg3)

def method_3refactored(array)
array.keep_if { |v| v > 0}
end

p method_3refactored(arr_intg3)

def method_4(array)
results = []

array.each do |num|
    if num % 2 == 0
    results << num
    end
end

return results

end

p method_4(arr_intg3)

=begin
4. Method_4 uses modulo to push the even numbers
in an array into the results array.
=end

@kmandreza
Copy link
Author

Exer 11

def print_value(hash, key)
puts hash[key].inspect
end

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)

@kmandreza
Copy link
Author

Exer 12

=begin
Return lets you specify the value you wish to output
from the method while puts outputs the last value
assigned in the method.

=end

@kmandreza
Copy link
Author

=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

@kmandreza
Copy link
Author

#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

@kmandreza
Copy link
Author

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