Skip to content

Challenge is complete #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions lib/challenge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 number off calls to Time and this will 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