Skip to content

Instantly share code, notes, and snippets.

@lukyans
Last active February 12, 2017 16:20
Show Gist options
  • Save lukyans/3a8e7655d287844d873e70e92cdb35bf to your computer and use it in GitHub Desktop.
Save lukyans/3a8e7655d287844d873e70e92cdb35bf to your computer and use it in GitHub Desktop.
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.
1. Define a class called PizzaOven which has a method cook_pizza which returns the string "mmm 'za".
```
class PizzaOven
def cook_pizza
"mm 'za"
end
end
```
2. Define a class called Student which is instantiated with a “name” value and which has a method name that returns this value
```
class Student
def initialize(name)
@name = name
end
end
n = Student.new("name")
n.name
3. Given an array of the numbers [1,2,3,4,5], find the sum of the doubles of all the numbers
```
[1,2,3,4,5].each do |x|
puts x += x
end
```
4. Give the command to create a new Git repository in a directory on your machine
`
git init
`
Pizza
1. 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.
```
class Pizza
def is_tasty?
true
end
end
require "minitest/autorun"
require "minitest/pride"
require_relative "pizza"
class PizzaTest < Minitest::Test
def test_it_returns_true
p = PizzaTest.new
assert_equal true, p.is_tasty?
end
end
```
2. 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.
```
class Pizza
def style(style)
style_within_the_list = false
["supreme", "mediterranean", "cheese"].any? do |each_style|
if each_style == style
style_within_the_list = true
end
end
end
end
require "minitest/autorun"
require "minitest/pride"
require_relative "pizza"
class PizzaTest < Minitest::Test
def test_it_returns_true
p = Pizza.new
assert_equal true, p.style("cheese")
end
end
```
3. Give the Git commands needed to stage and then commit a set of changes to a file
`
git add .
git commit -m "committing a set of changes to a file"
`
Student
1. 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
def initialize(attitude)
@attitude = "cheerful"
end
2. Additionally, add an assign_homework method to Student. When assigned_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.
```
class Student
attr_accessor :attitude
def initialize(attitude)
@attitude = "cheerful"
end
def assign_homework
puts attitude
if attitude == "cheerful"
@attitude = "dubious"
puts attitude
return
end
if attitude == "dubious"
@attitude = "perturbed"
puts attitude
return
end
if attitude == "perturbed"
@attitude = "dazed"
puts "Assigning homework to a #{attitude} student has no effect."
end
end
end
```
3. 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:
```
class Student
attr_accessor :attitude, :assignment_list
def initialize(attitude)
@attitude = "cheerful"
@assignment_list = ""
end
def assign_homework(task)
@assignment_list += task
puts attitude
if attitude == "cheerful"
@attitude = "dubious"
puts attitude
return
end
if attitude == "dubious"
@attitude = "perturbed"
puts attitude
return
end
if attitude == "perturbed"
@attitude = "dazed"
puts "Assigning homework to a #{attitude} student has no effect."
end
end
def assignment
puts assignment_list
end
end
```
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
```
class Student
def assign_homework(homework)
homework
end
end
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")
s1=[s1.assign_homework("linked list"), s1.assign_homework("sorting algos")].join(", ")
s2 = [s2.assign_homework("write a c compiler"), s2.assign_homework("write a pacman game")].join(", ")
s3 = [s3.assign_homework("headcount"), s3.assign_homework("sales engine")].join(", ")
puts students = [s1,s2,s3].join
```
=> "linked list, sorting algos, write a c compiler, write a pacman game, headcount, sales engine"
What does the following code output?
```
def print_variables(x)
puts "x: #{x}" #4
puts "b: #{b}" #12
end
def b
12
end
a = 4
print_variables(a)
```
1. 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")
content = file.read
puts content
```
2. 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.
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
A Corgi can be assigned a name
A Corgi can be asked for its name
A Corgi can be asked for its posture, which should default to “standing”
A Corgi can be asked to lie down, which should change its posture to “laying”
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment