I have a script that generates a randomised list of numbers between 0,60 and it is sorted in ascending order.
Essentially, I want to check if the difference between each element, and the element next to it is above 3, and if it is not, I want the list to be regenerated until the condition applies.
For example:
my_list = [1, 2, 3, 4, 5]
# this would not pass
my_list = [1, 5, 9, 13, 20]
# this would pass as the difference between each element and the next is more than 3
My code so far:
def generateList():
timeSlots = list(range(0, 60)) # generate random list
random.shuffle(timeSlots) # shuffle list
timeSlots = timeSlots[:9] # shorten list to 9 elements
timeSlots.sort() # sort list in ascending order
for cur, nxt in zip(timeSlots, timeSlots[1:]):
diff = (nxt - cur) # check difference
if diff < 3:
# HELP HERE
# regenerate timeSlots until the sum of each element and the next element is bigger than 3
return timeSlots
Aucun commentaire:
Enregistrer un commentaire