Home > Back-end >  I want to have 10 different time moments that I have written below and i want to chose two of them.
I want to have 10 different time moments that I have written below and i want to chose two of them.

Time:06-26

I want to have 10 different time moments that I have written below (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) and i want to chose two of them by pressing for example # 3 and #5 and then the code will say which one of the two moments i picked happened earlier by using an if loop for example so it can check hours first then if they are the same to go to minutes and if not to go to seconds,and by using this time format print([dt.strftime('%H:%M:%S') for dt in dts]) but im not sure how to write it all up together

import random

n = 10
moments = []
hours_tracker = set()
for _ in range(10):
    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))

sorted_moments < sorted(moments)

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)]

CodePudding user response:

so to choose, we can just edit the code to get the first two or get the user to choose it.

In this case we'll edit the code because it takes random samples and it's equal.

just add a line saying:

moments = moments[:2]

to see the earliest, we'll just sort them with a key that calculates the seconds.

sorted_moments = sorted(moments,key=lambda x:3600*x[0] 60*x[1] x[2])

so to calculate the earliest, we'll just take the first item.

earliest = sorted_moments[0]

in your case, this is the code that we'll use to calculate that.

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)]
moments = moments[:2]
sorted_moments = sorted(moments,key=lambda x:3600*x[0] 60*x[1] x[2])
earliest = sorted_moments[0]
print(earliest)

output:

(6, 14, 39)

CodePudding user response:

First, let's fix the way you sort moments:

moments.sort(key = lambda x: (x[0],x[1],x[2]))

With this line of code, the list, moments, will be sorted first by hours, minutes, then seconds.

However, it is unnecessary to sort by minutes and seconds because of this block in your code:

while hours in hours_tracker:
    hours = random.randint(0, 23)

This block prevents us from choosing any two times with the same hour, so if you want to keep it this way, we can just sort like this:

moments.sort(key = lambda x: x[0])

However, this means that we do not need to "check hours first then if they are the same to go to minutes and if not to go to seconds", since no two times will have the same hour.

Regardless, once we sort the list, whichever index comes first will have the earlier time:

import random

n = 10
moments = []
hours_tracker = set()
for _ in range(24):
    hours = random.randint(0, 23)
    #if you want no hours to repeat, else 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))

moments.sort(key = lambda x: x[0])
#use moments.sort(key = lambda x: (x[0],x[1],x[2])) if you want hours to repeat

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

if t1 < t2:
  print(str(moments[t1])   " occured before "   str(moments[t2]))
elif t2 < t1:
  print(str(moments[t2])   " occurred before "   str(moments[t1]))
else:
  print("Please select two different times")
  • Related