secret
Last active

  • Download Gist
thiagomassa.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
require 'time'
 
def how_much_seconds(d)
total=(d.day*24*3_600)+(d.hour*3_600)+(d.min*60)
end
 
def print_time(s,f)
days=s / 86400;
hours=(s / 3600) - (days * 24);
minutes=(s / 60) - (days * 1440) - (hours * 60);
if days>f
hours+=12
end
print hours
print ":"
if minutes <=9
print "0"
end
print minutes
if hours < 12
print "am"
else
print "pm"
end
print "\n"
end
 
def average_time_of_day(d)
total=0
loop=0
loop2=0
 
firstday= Time.parse(d[0]).day
seconds=Array.new(d)
 
while loop < d.size
#puts Time.parse(d[loop])
seconds[loop]=how_much_seconds(Time.parse(d[loop]))
loop+=1
end
loop=0
 
 
while loop < d.size-1
if Time.parse(d[loop+1]) < Time.parse(d[loop])
loop2=loop+1
while (loop2) < d.size
seconds[loop2]+=24*3600
loop2+=1
end
loop2=0
end
loop+=1
end
loop=0
 
while loop < d.size
total += seconds[loop]
loop+=1
end
total /= d.size
print_time(total,firstday)
 
end
 
 
average_time_of_day(["11:51pm", "11:56pm", "12:01am", "12:06am", "12:11am"])
 
average_time_of_day(["6:41am", "6:51am", "7:01am"])
 
average_time_of_day(["1:00am", "1:30am", "2:00am"])
 
average_time_of_day(["1:00am", "1:30am"])
 
average_time_of_day(["1:00am"])
 
average_time_of_day(["6:00pm","6:00am"])

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.