Skip to content
Mark Sun edited this page Nov 8, 2016 · 18 revisions

FAQs - Module 2

Calculate Word Frequency

Practice Assignment - Collections

Github Repository for Module 2

  • [How to download a single folder from github repo?] (#q-how-to-download-a-single-folder-from-github-repo)

Flow of Control

  • [I am having trouble understanding the following problem given in the Flow of Control lecture] (#q-i-am-having-trouble-understanding-the-following-problem-given-in-the-flow-of-control-lecture)

Calculate Maximum Word Frequency

Q: Why do I get a "undefined method 'highest_wf_words'" message?

A: The error:

1) Solution#calculate_line_with_highest_frequency calculates highest 
   count words across lines to be will, it, really
  
  ...
  
  NoMethodError:
    undefined method 'highest_wf_words' for "really":String

The explanation:

  1. Per technical requirement 8, the Solution class is supposed to have the analyzers array; which means that it has ALL LineAnalyzers objects.

a. highest_count_words_across_lines which is ALSO an array of LineAnalyzers, but only those objects whose words have the highest frequency. It is NOT an array of strings. So, basically, it is a SUBSET of analyzers array.

b. Per technical requirement 12, after calling calculate_line_with_highest_frequency method, the highest_count_words_across_lines attribute of the Solution class should be populated (see the item 1.a what should be contained inside the highest_count_words_across_lines attribute)

  1. Here is what the following line
solution.highest_count_words_across_lines.map(&:highest_wf_words).flatten

is doing when testing your solution:

Let's break it up:

**a.** `solution.highest_count_words_across_lines` - looks up the `highest_count_words_across_lines` attribute of `solution`. This attribute should contain... see **1.a**.

**b.** `map(&:highest_wf_words)` - is equivalent to `map { |elem| elem.highest_wf_words }`

It basically says, "extract `highest_wf_words` property of each element inside `highest_count_words_across_lines`" - which makes sense, since each element in there is a `LineAnalyzer` object - "and create another array of just `highest_wf_words` values"

**c.** `highest_wf_words` property is itself an **array**! So, if you have something like the following:
```
[["one", "two"], ["three"], ["five", "six"]] 
```
What `flatten` will do is to make it one happy array as follows 
```
["one", "two", "three", "four", "five", "six"]
```

Practice Assignment - Collections

Q: How to pass the test that expects array to contain 3 lines?

A: The first test expects to find 3 printed lines when it runs the file module2_lesson2_formative.rb. In order to pass the test, do not delete anything from the original file. Just add your solution to the end of this file.

GitHub Repository for Module 2

Q: How to download a single folder from github repo?

A: You cannot download a single folder from the repo (as far as I know). You have 2 options:

  1. When you go to https://github.com/jhu-ep-coursera/fullstack-course1-module2 there is a green button in the top right "Clone or Download" - you can download a zip file of the repo and then extract what you need
  2. You can clone the repo by typing the following on the command line:

git clone https://github.com/jhu-ep-coursera/fullstack-course1-module2.git

Flow of Control

Q: I am having trouble understanding the following problem given in the Flow of Control lecture

times_2 = 2
times_2 *= 2 while times_2 < 100 
puts times_2 #=>?

A: The best way to solve any problem is to break it up into smaller pieces. So we have essentially these three lines:

times_2 = 2
times_2 *= 2 while times_2 < 100
puts times_2

On line 1, we simply declare a variable of times_2 and assign it a value of 2.

On line 2, we have two pieces:

First one is times_2 *= 2.

This statement essentially is the short form for the following:

times_2 = times_2 * 2

So what we're doing here is taking times_2 and multiplying that by 2.

Next, we have the while modifier and the condition that follows, which is:

times_2 < 100

You can read more about the while modifier here. https://www.tutorialspoint.com/ruby/ruby_loops.htm

Essentially, when the 2nd line is executed, it first runs times_2 *= 2. Next, when it sees the while modifier, it checks to see if the condition is met. If the condition is met, then it repeats the first part of the statement which is times_2 *= 2. If the condition is not met, then it will move to the next line of code.

With this info, try to figure out the expected output!