Skip to content

Instantly share code, notes, and snippets.

@wewert
Last active February 10, 2017 22:06
Show Gist options
  • Save wewert/53311b9a5739c498a84353f4ab6aef92 to your computer and use it in GitHub Desktop.
Save wewert/53311b9a5739c498a84353f4ab6aef92 to your computer and use it in GitHub Desktop.
Ken Lee Week 2 and 3 Diagnostic
Week 2 and 3 Diagnostic
This exercise is intended to help you assess your progress with the concepts and techniques we’ve covered during the week.
For these questions, write a short snippet of code that meets the requirement. In cases where the question mentions a “given” data value, use the variable given to refer to it (instead of re-writing the information).
Use single (`) and triple backticks (```) to container code snippets.
Define a class called PizzaOven which has a method cook_pizza which returns the string "mmm 'za".
class PizzaOven
def cook_pizza
"mmm 'za"
end
end
Define a class called Student which is instantiated with a “name” value and which has a method name that returns this value
class
end
Given an array of the numbers [1,2,3,4,5], find the sum of the doubles of all the numbers
sum = 0
a = [1,2,3,4,5]
add = a.map { |x| x*2 }
sum_all = add.each { |y| sum+=y }
Give the command to create a new Git repository in a directory on your machine
git init
Pizza
Given a hypothetical Pizza class which has an instance method is_tasty? that always returns true, write a simple Minitest test that tests this behavior.
def test_is_tasty_true
pizza = Pizza.new
assert true, pizza.is_tasty?
end
class Pizza
def is_tasty?
true
end
end
Suppose the Pizza class also has a method style which randomly returns one of: "supreme", "mediterranean", or "cheese". Write a test that confirms that the returned pizza style is within this list.
def test_pizza_selection
pizza = Pizza.new
x = ["supreme", "mediterranean", "cheese"]
assert x.include?(pizza.style)
end
def style
types = ["supreme", "mediterranean", "cheese"]
types.sample
end
Give the Git commands needed to stage and then commit a set of changes to a file
git commit -m "message"
Student
Define a Student class which, when created, has an attitude attribute. attitude should start out with the value “cheerful”, and the Student class should provide a “reader” method that allows us to access the value of its attitude.
class Student
attr_accessor :attitude #I choose attr_accessor because I assume the student's attitude will change over time but I would go with attr_reader otherwise
def initialize
@attitude = "cheerful"
@descriptions = []
end
def assign_homework(description)
@descriptions << description
if @attitude == "cheerful"
@attitude = "dubious"
elsif @attitude == "dubious"
@attitude = "perturbed"
else
@attitude = "dazed"
end
end
def assignments
@descriptions.join(", ")
end
end
class Student
def initialize
@attitudes = ['cheerful', 'dubious', 'perturbed', 'dazed']
@attitude_counter = 0
end
def assign_homework
@attitude_counter += 1 if @attitude_counter < 3
end
def attitude
@attitudes[@attitude_counter]
end
end
Additionally, add an assign_homework method to Student. When assign_homework is invoked, if the student’s attitude is "cheerful", it should become "dubious". If the value is currently "dubious" it should become "perturbed". If the value is currently "perturbed", it should become "dazed". Assigning homework to a "dazed" student has no effect.
Building on the Student class from the previous example, update the assign_homework method to accept an argument. The argument will be a String containing a short description of the assignment. For example we might use it like this:
s = Student.new
s.assign_homework("Write a linked list")
Then, add an assignments method to Student. assignments should return a list of all the assignments that have been given, separated by a comma and a space. For example:
s = Student.new
s.attitude
=> "cheerful"
s.assign_homework("write a linked list")
s.attitude
=> "dubious"
s.assign_homework("write a BST")
s.attitude
=> "perturbed"
s.assignments
=> "write a linked list, write a BST"
Given an array of 3 Student instances, generate a new string of all of their assignments
For example:
s1 = Student.new
s2 = Student.new
s3 = Student.new
s1.assign_homework("linked list")
s1.assign_homework("sorting algos")
s2.assign_homework("write a c compiler")
s2.assign_homework("write a pacman game")
s3.assign_homework("headcount")
s3.assign_homework("sales engine")
students = [s1,s2,s3]
# YOUR CODE HERE
[s1.assignments, s2.assignments, s3.assignments].join(", ")
=> "linked list, sorting algos, write a c compiler, write a pacman game, headcount, sales engine"
What does the following code output?
=> x: 4
=> b: 12
def print_variables(x)
puts "x: #{x}"
puts "b: #{b}"
end
def b
12
end
a = 4
print_variables(a)
Working with files: given a text file located at "~/Documents/pizza.txt", write code to read the file from the filesystem and print each line one at a time.
file = File.open("~/Documents/pizza.txt", "r") file.each{ |x| puts x }
Writing Files: given a text file located at "~/Documents/pizza.txt", write code to read the file from the filesystem, then write a new file at "~/Documents/line_count.txt" containing the number of lines in the original file.
file = File.open("~/Documents/pizza.txt", "r")
file.write("~/Documents/line_count.txt")file.each{ |x| x.length }
Imagine a simple ruby class designed to represent a Corgi dog. Write a test for each of the following features:
A Corgi can be created with no arguments
def test_no_arguments
corgi = Corgi.new
assert_equal nil, corgi.no_arg
end
A Corgi can be assigned a name
def test_it_has_a_name
corgi = Corgi.new("Jojo")
assert corgi.name
end
A Corgi can be asked for its name
def test_ask_name
corgi = Corgi.new("Jojo")
assert_equal name, corgi.ask_name
end
A Corgi can be asked for its posture, which should default to “standing”
def test_posture
corgi = Corgi.new
assert_equal "standing", corgi.posture
end
A Corgi can be asked to lie down, which should change its posture to “laying”
def test_laying
corgi = Corgi.new
assert_equal "laying", corgi.posture
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment