Skip to content

Instantly share code, notes, and snippets.

@GSmes
Forked from worace/week_3.markdown
Last active May 26, 2016 18:53
Show Gist options
  • Save GSmes/0da3e4347bd069d2bc4323265681476f to your computer and use it in GitHub Desktop.
Save GSmes/0da3e4347bd069d2bc4323265681476f to your computer and use it in GitHub Desktop.
Module 1 Week 3 Diagnostic

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?
def print_variables(x)
  puts "x: #{x}"
  puts "b: #{b}"
end

def b
  12
end

a = 4
print_variables(a)

```ruby 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.


```ruby File.open("~/Documents/pizza.txt").readlines.each do |line| puts line end ```


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.


```ruby total_lines = 0 pizza = File.open("~/Documents/pizza.txt").readlines.each do |line| puts line total_lines += 1 end

File.write("~/Documents/line_count.txt", "#{total_lines}")

<br />
<br />
<br />

##### 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"

<br />
```ruby
def test_corgi_object_without_arguments
  corgi_1 = Corgi.new
  assert_instance_of Corgi, corgi_1
end

def test_corgi_has_a_name
  corgi_1 = Corgi.new("Chubbs")
  assert_equal "Chubbs", corgi_1.name
end

def test_corgi_can_return_name
  corgi_1 = Corgi.new("Chubbs")
  assert "Chubbs", corgi_1.name?
end

def test_corgi_can_return_posture_and_default_is_standing
  corgi_1 = Corgi.new
  assert_equal "standing", corgi.posture
end

def test_corgi_lie_down_changes_posture
  corgi_1 = Corgi.new
  corgi.lie_down
  assert_equal "laying", corgi.posture
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}

```ruby animal_hash = {}

given_animal_array.each do |animal| animal_hash[animal] = given_animal_array.count(animal) end

<br />
<br />
<br />

##### 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)

<br />
```ruby
read = File.open("~/Documents/pizza.txt")
line_number = 0
even = []
odd = []

read.each_line do |line|
  if line_number.even?
    even << line
  else
    odd << line
  end
  line_number += 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.

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)

```ruby
<br />
<br />
<br />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment