From aa6e680666b179abf9149384a5a7accc01f29f1b Mon Sep 17 00:00:00 2001 From: ievgend2 Date: Mon, 30 May 2022 12:10:13 -0400 Subject: [PATCH 1/2] challenge is complete --- lib/challenge.rb | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/challenge.rb b/lib/challenge.rb index f7130bc..0a1353a 100644 --- a/lib/challenge.rb +++ b/lib/challenge.rb @@ -10,10 +10,28 @@ def initialize(file_path) end def parse - # Parse the file located at @file_path and set three attributes: - # - # earliest_time: the earliest time contained within the data set - # latest_time: the latest time contained within the data set - # peak_year: the year with the most number of timestamps contained within the data set + earliest_time = Time.now.to_i #converts current time into timestamp and saves it in the earlist_time local variable. + latest_time = 0 #asign latest time local variable to have a starting value. + peak_year = {} #declare a hash for peak year. + + File.open(@file_path, 'r').each do |line| # For loop to iterate through every line in timestamps.txt file + + timestamp_i = Time.xmlschema(line).to_i #converting 2022-05-30 11:56:52 -0400 into 1653926221 to reduce the runtime + timestamp = Time.xmlschema(line) + + #Logic for the earliest time in the data set + earliest_time = earliest_time < timestamp_i ? earliest_time : timestamp_i + + #Logic for the latest time contained in the data set + latest_time = latest_time > timestamp_i ? latest_time : timestamp_i + + #Logic for the peak year with the most number of timestamps contained in the data set + peak_year.key?(timestamp.year) ? peak_year[timestamp.year] += 1 : peak_year[timestamp.year] = 1 + + end + + @latest_time = Time.at(latest_time) + @earliest_time = Time.at(earliest_time) + @peak_year = peak_year.key(peak_year.values.max) end end From 2f418e85c9acb82e5880b66ab6825c86e1424e28 Mon Sep 17 00:00:00 2001 From: ievgend2 Date: Mon, 30 May 2022 12:12:01 -0400 Subject: [PATCH 2/2] revised comments --- lib/challenge.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/challenge.rb b/lib/challenge.rb index 0a1353a..ea2a7f2 100644 --- a/lib/challenge.rb +++ b/lib/challenge.rb @@ -16,7 +16,7 @@ def parse File.open(@file_path, 'r').each do |line| # For loop to iterate through every line in timestamps.txt file - timestamp_i = Time.xmlschema(line).to_i #converting 2022-05-30 11:56:52 -0400 into 1653926221 to reduce the runtime + timestamp_i = Time.xmlschema(line).to_i #converting 2022-05-30 11:56:52 -0400 into 1653926221 to reduce number off calls to Time and this will reduce the runtime timestamp = Time.xmlschema(line) #Logic for the earliest time in the data set