Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
43
Task/Text-processing-1/Ruby/text-processing-1-1.rb
Normal file
43
Task/Text-processing-1/Ruby/text-processing-1-1.rb
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
filename = "readings.txt"
|
||||
total = { "num_readings" => 0, "num_good_readings" => 0, "sum_readings" => 0.0 }
|
||||
invalid_count = 0
|
||||
max_invalid_count = 0
|
||||
invalid_run_end = ""
|
||||
|
||||
File.new(filename).each do |line|
|
||||
num_readings = 0
|
||||
num_good_readings = 0
|
||||
sum_readings = 0.0
|
||||
|
||||
fields = line.split
|
||||
fields[1..-1].each_slice(2) do |reading, flag|
|
||||
num_readings += 1
|
||||
if Integer(flag) > 0
|
||||
num_good_readings += 1
|
||||
sum_readings += Float(reading)
|
||||
invalid_count = 0
|
||||
else
|
||||
invalid_count += 1
|
||||
if invalid_count > max_invalid_count
|
||||
max_invalid_count = invalid_count
|
||||
invalid_run_end = fields[0]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
printf "Line: %11s Reject: %2d Accept: %2d Line_tot: %10.3f Line_avg: %10.3f\n",
|
||||
fields[0], num_readings - num_good_readings, num_good_readings, sum_readings,
|
||||
num_good_readings > 0 ? sum_readings/num_good_readings : 0.0
|
||||
|
||||
total["num_readings"] += num_readings
|
||||
total["num_good_readings"] += num_good_readings
|
||||
total["sum_readings"] += sum_readings
|
||||
end
|
||||
|
||||
puts ""
|
||||
puts "File(s) = #{filename}"
|
||||
printf "Total = %.3f\n", total['sum_readings']
|
||||
puts "Readings = #{total['num_good_readings']}"
|
||||
printf "Average = %.3f\n", total['sum_readings']/total['num_good_readings']
|
||||
puts ""
|
||||
puts "Maximum run(s) of #{max_invalid_count} consecutive false readings ends at #{invalid_run_end}"
|
||||
32
Task/Text-processing-1/Ruby/text-processing-1-2.rb
Normal file
32
Task/Text-processing-1/Ruby/text-processing-1-2.rb
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
Reading = Struct.new(:date, :value, :flag)
|
||||
|
||||
DailyReading = Struct.new(:date, :readings) do
|
||||
def good() readings.select(&:flag) end
|
||||
def bad() readings.reject(&:flag) end
|
||||
def sum() good.map(&:value).inject(0.0) {|sum, val| sum + val } end
|
||||
def avg() good.size > 0 ? (sum / good.size) : 0 end
|
||||
def print_status
|
||||
puts "%11s: good: %2d bad: %2d total: %8.3f avg: %6.3f" % [date, good.count, bad.count, sum, avg]
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
daily_readings = IO.foreach(ARGV.first).map do |line|
|
||||
(date, *parts) = line.chomp.split(/\s/)
|
||||
readings = parts.each_slice(2).map {|pair| Reading.new(date, pair.first.to_f, pair.last.to_i > 0)}
|
||||
DailyReading.new(date, readings).print_status
|
||||
end
|
||||
|
||||
all_readings = daily_readings.flat_map(&:readings)
|
||||
good_readings = all_readings.select(&:flag)
|
||||
all_streaks = all_readings.slice_when {|bef, aft| bef.flag != aft.flag }
|
||||
worst_streak = all_streaks.reject {|grp| grp.any?(&:flag)}.sort_by(&:size).last
|
||||
|
||||
total = good_readings.map(&:value).reduce(:+)
|
||||
num_readings = good_readings.count
|
||||
puts
|
||||
puts "Total: %.3f" % total
|
||||
puts "Readings: #{num_readings}"
|
||||
puts "Average %.3f" % total./(num_readings)
|
||||
puts
|
||||
puts "Max run of #{worst_streak.count} consecutive false readings from #{worst_streak.first.date} until #{worst_streak.last.date}"
|
||||
Loading…
Add table
Add a link
Reference in a new issue