Skip to content

Instantly share code, notes, and snippets.

@nmcolome
Last active March 27, 2017 16:34
Show Gist options
  • Save nmcolome/38e59cdf3276aeb58360300525dad80e to your computer and use it in GitHub Desktop.
Save nmcolome/38e59cdf3276aeb58360300525dad80e to your computer and use it in GitHub Desktop.
diagnostic_week_2_natalia_colomé

Week 2 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
        puts "mmm 'za"
    end
    
end
  1. Define a class called Student which is instantiated with a "name" value and which has a method name that returns this value
class Student
    attr_reader :name

    def initialize (name)
        @name = name
    end
    
end
  1. Given an array of the numbers [1,2,3,4,5], how would you create a new array of all elements doubled? How would you return an array of all odd elements?
numbers = [1,2,3,4,5]
doubled = []
numbers.each do |number|
  doubled << number * 2
end

test = []
numbers.each do |num|
    test << num if num % 2 != 0
end
  1. Give the command to create a new Git repository in a directory on your machine

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 PizzaTest < Minitest::Test

  def test_if_is_tasty_always_true
    new_pizza = Pizza.new
    
    assert new_pizza.is_tasty?
    end

end
  1. 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 PizzaTest < Minitest::Test

  def test_if_is_style_is_within_allowed_list
    new_pizza = Pizza.new
    
    assert_equal "supreme", new_pizza.style || "mediterranean", new_pizza.style || "cheese", new_pizza.style
  end

end
  1. Give the Git commands needed to stage and then commit a set of changes to a file

stage git add <filename> or .(all) commit git commit -m "comment"

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_reader :attitude

    def attitude
        @attitude = "cheerful"
    end
    
end
  1. 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_reader :attitude

    def attitude
        @attitude = "cheerful"
    end
    
    def assign_homework
      @attitude = "dubious" if @attitude == "cheerful"
      @attitude = "perturbed" if @attitude == "dubious"
      @attitude = "dazed" if @attitude == "perturbed"
    end

end
  1. 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")
def assign_homework(homework)
      @attitude = "dubious" if @attitude == "cheerful"
      @attitude = "perturbed" if @attitude == "dubious"
      @attitude = "dazed" if @attitude == "perturbed"
      @homework = homework
    end

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"
def assignments
  assignments = []
  assignments << @homework
  puts assignments
    end
  1. 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

=> "linked list, sorting algos, write a c compiler, write a pacman game, headcount, sales engine"
  1. What does the following code output?
def print_variables(x)
  puts "x: #{x}"
  puts "b: #{b}"
end

def b
  12
end

a = 4
print_variables(a)

=> x: 4

  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.open("~/Documents/pizza.txt","r") dont know the rest

  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. dont know how to count lines

  3. 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"

@nmcolome
Copy link
Author

we do

@nmcolome
Copy link
Author

nmcolome commented Mar 27, 2017

  1. If I wan the string to return, don't include "puts" or "p" - they return nil.
  2. comment: attr_reader won't do a thing unless there is an instance variable with the same name in the initialize
    Also, attr_reader will only work inside classes, if I'm only defining methods outside a class, I don't need the attrs
doubled = [1,2,3,4,5].map do |element|
element * 2
end

odd = [1,2,3,4,5].find_all {|num| num.odd?} #or select - selects all the elements that returns true or truthy and creates a new array
  1. git init (I thought about gist instead of git %*^&#$()
def test_style
pizza = Pizza.new
list = ["supreme", "mediterranean", "cheese"]

assert list.include?(pizza.style)
assert_includes list, pizza.style
end

@nmcolome
Copy link
Author

  1. returns a and b -.-
    x: 4
    b: 12

@nmcolome
Copy link
Author

File.open("~/Documents/pizza.txt","r").each_line do |line|
  puts line
end

or

File.readlines("~/Documents/pizza.txt","r") do |line|
  puts line
end
file = File.open("~/Documents/pizza.txt","r")
num_lines = 0

file.each_line do |line|
  puts line
  num_lines += 1
end

File.write("~/Documents/line_count.txt", num_lines)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment