From cc8135289ba3c9433644667fe10af75566290326 Mon Sep 17 00:00:00 2001 From: Bruno Fagundez Date: Fri, 22 Jul 2016 07:47:37 -0700 Subject: [PATCH 1/3] rough solution for the quiz, needs refactoring --- solutions/bfagundez/shakespeare.rb | 91 ++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 solutions/bfagundez/shakespeare.rb diff --git a/solutions/bfagundez/shakespeare.rb b/solutions/bfagundez/shakespeare.rb new file mode 100644 index 0000000..e4cfac5 --- /dev/null +++ b/solutions/bfagundez/shakespeare.rb @@ -0,0 +1,91 @@ +require 'benchmark' + +TASK = ARGV[0] +DIR_NAME = ARGV[1] + +filename_list = [] +Dir[File.dirname(__FILE__) + "/#{DIR_NAME}/*.txt"].each { |f| + filename_list << f +} + +if TASK == "1" + + + # let's get the distribution of words + letters = ('a'..'z').to_a + letter_word_counter = {} + letters.each do |l| + letter_word_counter[l] = 0 + end + + filename_list.each do |f| + File.open(f).readlines.each do |line| + # break line on whitespaces and ignore numbers + words = line.gsub(/[^a-zA-Z ]/,'').gsub(/ +/,' ').downcase.split + + words.each do |w| + letter_word_counter[w[0]] += 1 + end + end + end + + p "Counters of words starting with each letter: \n" + p letter_word_counter + + + +end + +if TASK == "2" + # top ten words + top_ten_words = {} + + filename_list.each do |f| + File.open(f).readlines.each do |line| + # break line on whitespaces and ignore numbers + words = line.gsub(/[^a-zA-Z ]/,'').gsub(/ +/,' ').downcase.split + + words.each do |w| + if top_ten_words[w].nil? + top_ten_words[w] = 1 + else + top_ten_words[w] += 1 + end + end + + end + end + + top_ten_purged = top_ten_words.sort_by {|k, v| v}.reverse! + p top_ten_purged[0..9] + +end + +if TASK == "3" + char_counters = {} + + filename_list.each do |f| + lines = File.open(f).readlines().join + onlychars = lines.gsub!(/\W+/, '').downcase + curr_char = 0 + last_char = 2 + while last_char < onlychars.length + this_sequence = onlychars[curr_char..last_char] + + if char_counters[this_sequence].nil? + char_counters[this_sequence] = 1 + else + char_counters[this_sequence] = char_counters[this_sequence] + 1 + end + + curr_char = curr_char + 1 + last_char = last_char + 1 + + end + + end + + + top_three_char = char_counters.sort_by {|k, v| v}.reverse! + p top_three_char[0..2] +end From 469a2b179218e843cd6a515340eeed2eea51cf54 Mon Sep 17 00:00:00 2001 From: Bruno Fagundez Date: Fri, 22 Jul 2016 07:49:15 -0700 Subject: [PATCH 2/3] remove benchmark, not used --- solutions/bfagundez/shakespeare.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/solutions/bfagundez/shakespeare.rb b/solutions/bfagundez/shakespeare.rb index e4cfac5..bf54e80 100644 --- a/solutions/bfagundez/shakespeare.rb +++ b/solutions/bfagundez/shakespeare.rb @@ -1,4 +1,3 @@ -require 'benchmark' TASK = ARGV[0] DIR_NAME = ARGV[1] From f7c893b8135ebaf21db1d6c8fecceb0553c3dec0 Mon Sep 17 00:00:00 2001 From: Bruno Fagundez Date: Fri, 22 Jul 2016 07:51:41 -0700 Subject: [PATCH 3/3] added times --- solutions/bfagundez/shakespeare.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/solutions/bfagundez/shakespeare.rb b/solutions/bfagundez/shakespeare.rb index bf54e80..7932f12 100644 --- a/solutions/bfagundez/shakespeare.rb +++ b/solutions/bfagundez/shakespeare.rb @@ -31,7 +31,7 @@ p "Counters of words starting with each letter: \n" p letter_word_counter - + # this is taking 2.005s total end @@ -58,6 +58,8 @@ top_ten_purged = top_ten_words.sort_by {|k, v| v}.reverse! p top_ten_purged[0..9] + # this is taking 2.466s total + end if TASK == "3" @@ -87,4 +89,6 @@ top_three_char = char_counters.sort_by {|k, v| v}.reverse! p top_three_char[0..2] + + # this is taking 3.220s total end