Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Module 1 Week 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).
- ##### 1. What does the following code output?
- ```ruby
- def print_variables(x)
- puts "x: #{x}"
- puts "b: #{b}"
- end
- def b
- 12
- end
- a = 4
- print_variables(a)
- ```
- ```
- x: 4
- b: 12
- ```
- ##### 2. 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.
- ```
- lines = File.readlines("~/Documents/pizza.txt")
- lines.each do |line|
- puts line
- end
- lines.close
- ```
- ##### 3. 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.
- ```
- lines = File.readlines("~/Documents/pizza.txt")
- num_lines = 0
- lines.each { |line| num_lines += 1 }
- lines_file = File.new("lines.txt", 'w')
- lines_file << "There are #{num_lines} lines in 'pizza.txt'"
- lines.close
- lines_file.close
- ```
- ##### 4. Corgis
- 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"
- ```
- require 'minitest/autorun'
- require 'minitest/pride'
- require './lib/corgi'
- class CorgiTest < Minitest::Test
- def test_it_can_be_created_with_no_arguments
- new_corg = Corgi.new
- assert_instance_of Corgi, new_corg
- end
- def test_it_can_be_assigned_a_name
- new_corg = Corgi.new
- assert new_corg.name = "Daniel"
- end
- def test_it_can_be_asked_for_its_name
- new_corg = Corgi.new
- new_corg.name = "Daniel"
- assert_equal "Daniel", new_corg.name
- end
- def test_it_has_a_default_posture_of_standing
- new_corg = Corgi.new
- assert_equal "standing", new_corg.posture
- end
- def test_it_can_lie_down_and_change_posture_to_laying
- new_corg = Corgi.new
- new_corg.lie_down
- assert_equal "laying", new_corg.posture
- end
- end
- ```
- ##### 5. Counting Words
- Given an array of words `["dog", "cat", "gerbil", "cat", "hamster", "rabbit", "rabbit"]`,
- create a Hash containing the individual words as keys and the number of times
- the word appears in the list as values. That is:
- ```
- {"dog" => 1, "cat" => 2, "gerbil" => 1, "hamster" => 1, "rabbit" => 2}
- ```
- ```
- new_hash = Hash[ given.map { |animal| [animal, given.count(animal)] } ]
- ```
- ##### 6. Reading Files
- Given a text file located at `"~/Documents/pizza.txt"`, write code to read the
- file from the filesystem, then process the file's lines so that:
- * Even lines go into an array called `even`
- * Odd lines go into an array called `odd`
- (Assume the first line is numbered `0`, and is thus even)
- ```
- file = File.readlines("~/Documents/pizza/txt")
- line_num = 0
- even = []
- odd = []
- file.each do |line|
- if line_num.even?
- even << line
- else
- odd << line
- end
- line_num += 1
- end
- ```
- ##### 7. Stacks
- Given the following code, draw a simple diagram representing the stack
- frames that the program will generate as it is run. In order to show
- change in the stack over time, you may need to re-copy the lower frames
- into a new diagram.
- ```ruby
- def wrap_it(x)
- "<<<" + x + ">>>"
- end
- def string_it(x)
- x.to_s
- end
- def churn_it(x)
- wrap_it(string_it(x))
- end
- churn_it(10)
- ```
- ```
- - As the methods are defined self is main, local variables are empty, and current method is main
- - When it reaches churn_it(10) self remains as main, current method becomes churn_it, and the local variable becomes 10
- - Upon evaluating churn_it, self remains main, local variable remains 10 (passed as an arg to string_it) and our current method becomes string_it
- - When we move to string_it we pass the local variable remains 10, self remains, main, and current method is string_it
- - As string_it is evaluated, our local variable is 10, self is main, and our current method is .to_s, creating "10", which is returned
- - All things string_it come off the stack and we are back inside of churn_it, where our returned value "10" will be the argument passed to wrap_it. Before we enter into wrap_it, self is still main, local variables are 10 and "10", and our current method is churn_it
- - We pass "10" into wrap_it. Self is main, local variable is "10", and current method is wrap_it. Wrap_it evaluates to "<<<< 10 >>>>" and returns that string.
- - With wrap_it completed, it comes off the stack and we move back to churn_it, where our local variable remains 10, self is main, current method is churn_it, and it will return "<<<< 10 >>>>".
- - Churn_it returns "<<<< 10 >>>>", local variables are empty, self is main, current method is main, and everything else is off the stack.
- - With the program completed, main comes off and the program exits.
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement