compare any two moments of the 10 moments of time

Question:

Im trying to write a program that has a data of n = 10 which is the moments of time of one day, these are: hours (values from 0
to 23), minutes (from 0 to 59) and seconds (from 0 to 59). Write a program that
compares any two moments of time (determines which of the moments occurred in
given day earlier). So in simpler words I want to have 10 random moments with hours minutes and seconds, hours can’t be repeating so for example lets say [14:34;24, 16:25:26…… and so on for 10 times, so if I want to take lets say the first number from the list and the 2nd from the list so that would be (14:34;24) and (16:25:26) and to see which one happened earlier between the 2. was able to state the numbers but not sure how to do the comparision and how to state it all.

import random
hours = (random.randint(0,23) for i in range (10))
minutes = (random.randint(0,59) for i in range (10))
seconds = (random.randint(0,59) for i in range (10))
num_of_H=list(zip(hours,minutes))
num_of_M=list(zip(hours,minutes))
print(dict(num_of_H))
nums=(i for i in range(10))
res=dict(zip(nums,num_of_H))
print(res)
while True:
Asked By: Kamel Avad

||

Answers:

Taking advantage of the fact that tuples are first sorted based on first index, then second index, then third index and so on…

import random

n = 10
moments = []
hours_tracker = set() # for requirement "hours can't be repeating"
for _ in range(n):
    hours = random.randint(0, 23)
    while hours in hours_tracker:
        hours = random.randint(0, 23)
    hours_tracker.add(hours)

    minutes = random.randint(0, 59)
    seconds = random.randint(0, 59)
    moments.append((hours, minutes, seconds)) # appending a tuple to list

sorted_moments = sorted(moments)

Output

>>> print(moments)
>>> [(6, 14, 39), (16, 4, 22), (14, 31, 3), (9, 27, 56), (17, 1, 33), (15, 10, 42), (7, 53, 47), (18, 11, 6), (8, 22, 45), (12, 32, 42)]

>>> print(sorted_moments)
>>> [(6, 14, 39), (7, 53, 47), (8, 22, 45), (9, 27, 56), (12, 32, 42), (14, 31, 3), (15, 10, 42), (16, 4, 22), (17, 1, 33), (18, 11, 6)]

Now you can use the sorted moments in any way or format you want.

Answered By: BhusalC_Bipin

You should have a look at the datetime library, it will help you manipulate dates in Python.

A datetime object needs additional information to be built (year, month, day), then you can generate the times of day you want with your code.

If we go back to your code, by first building a reference date (I took the current date, you can put what you want), you can generate your list of dates like that.

import random
import datetime

base_datetime = datetime.datetime.now()
dts = [base_datetime.replace(hour=random.randint(0, 23), minute=random.randint(0, 59), second=random.randint(0, 59)) for i in range(10)]

And print it (to make it look better during printing, you can manipulate the display format):

print([dt.strftime('%H:%M:%S') for dt in dts])
# an example of a result :
# return : ['00:29:03', '22:41:51', '00:04:06', '15:56:38', '03:41:36', '18:15:40', '19:22:33', '18:19:33', '18:39:19', '05:38:26']

Now you can compare each element of your list with the usual python operators (<,>,!= … ).

print(dts[0] < dts[1])
# from the previous example : '00:29:03' < '22:41:51'
# return: True

I hope I answered your question.

Answered By: Niels Borie

Thank you guys for the help I was able to merge all of your ideas and was able to come up with this, i guess it was hard for me to explain what i had in mind but here is what I meant.
import random

n = 10
moments = []
hours_tracker = set()
for _ in range(n):
    hours = random.randint(0, 23)
    #if you want hours to possibly repeat comment out while loop:
    while hours in hours_tracker:
        hours = random.randint(0, 23)
    hours_tracker.add(hours)

    minutes = random.randint(0, 59)
    seconds = random.randint(0, 59)
    moments.append((hours, minutes, seconds))

for i in range(len(moments)):
   print(str(i) + ": " + str(moments[i]))

print()

t1 = int(input("What first time would you like to select? "))
t2 = int(input("What second time would you like to select? "))

#CHECKING FOR HOURS
if ((moments[t1])[0] < (moments[t2])[0]):
  print(str(moments[t1]) + " occurred before " + str(moments[t2]))
elif ((moments[t1])[0] > (moments[t2])[0]):
  print(str(moments[t2]) + " occurred before " + str(moments[t1]))
Answered By: Kamel Avad
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.