Reputation: 125
I want to get the maxim date and maximum time when user inputs 10-12 numbers separated by space or comma
I tried the code for the date but I'm getting stuck in creating a logic for getting the maximum month with date what I did was first sorted the list in descending order and then check in the resultent list that if the number is less than or equal to 1 then it is placed on 0th index and then if the 0th index number is 1 then the number on 1st index will be less than or equal to 2
digits = [int(x) for x in input("Enter the digits").split(",")]
date = []
print(digits)
for x in range(0, len(digits)):
for y in range(0, len(digits) - x - 1):
if digits[y] <= digits[y + 1]:
temp = digits[y]
digits[y] = digits[y + 1]
digits[y + 1] = temp
print(digits)
for x in range(0, len(digits)):
if digits[x] <= 1:
date.insert(0, digits[x])
break
digits.remove(digits[x])
print("digits ", digits)
print(date)
for y in range(0, len(digits) - 1):
if date[0] == 1:
while digits[y] >= 0 and digits[y] <= 2:
if digits[y] >= digits[y + 1]:
date.insert(1, digits[y])
digits.remove(digits[y])
break
if date[0] < 1:
while digits[y] >= 0 and digits[y] <= 9:
if digits[y] >= digits[y + 1]:
date.insert(1, digits[y])
digits.remove(digits[y])
break
break
print("digits", digits)
for x in range(0, len(digits)):
if digits[x] <= 3:
date.insert(2, digits[x])
break
digits.remove(digits[x])
print(date)
print("digits", digits)
for y in range(0, len(digits)):
if date[2] == 3:
while digits[y] >= 0 and digits[y] <= 1:
if digits[y] >= digits[y + 1]:
date.insert(3, digits[y])
digits.remove(digits[y])
break
if date[2] < 3:
while digits[y] >= 0 and digits[y] <= 9:
if digits[y] >= digits[y + 1]:
date.insert(3, digits[y])
digits.remove(digits[y])
break
break
print(date)
now in the above code when I give inputs
input : 1,0,0,1,1
output :
[1, 0, 0, 1, 1]
[1, 1, 1, 0, 0]
digits [1, 1, 0, 0]
[1]
digits [1, 0, 0]
[1, 1, 1]
digits [0, 0]
[1, 1, 1, 0]
So here 1,1,1,0 is 11 month and 10 is the days So the format should be MMDD including zeros but when I give other input like
input : 1,2,3,1,1
output :
[1, 2, 3, 1, 1]
[3, 2, 1, 1, 1]
digits [3, 2, 1, 1]
[1]
digits [3, 2, 1, 1]
[1, 3]
digits [2, 1, 1]
Traceback (most recent call last):
File "date.py", line 51, in <module>
if date[2] == 3:
IndexError: list index out of range
Here I wanted 1,2,3,1 as 12 as month and 31 as days
Upvotes: 0
Views: 1387
Reputation: 978
import datetime, itertools
dates = []
try:
digits = [int(x) if int(x) < 10 else 100 for x in input("Enter the digits").split(",")]
if 100 in digits:
raise ValueError('Must be digits and not numbers')
date_perms = itertools.permutations(digits, 4)
for date in date_perms:
dates.append((date[0]*10+date[1], date[2]*10+date[3]))
except ValueError as e:
print(e)
for i, date in enumerate(dates):
try:
dates[i] = datetime.datetime(year=1, month=date[0], day=date[1])
except ValueError as e:
dates[i] = datetime.datetime(1,1,1,1)
if len(dates) > 0:
maximum = max(dates)
if maximum.hour == 1:
print('None of the combinations created a valid date')
else:
print('Month:', maximum.month, 'Day:', maximum.day)
I think this might do what you need. itertools.permutations
creates all the possible combinations of the given digits.
The 2nd try-except block will replace the faulty dates with Year=1, month=1, day=1, hour=1
. The others will have 0 hour.
Also, notice I replaced your input list comprehension a bit. It previously allowed non-int input as well as non-digit. When a non-digit is entered (>9) I place a 100 in the list and then check whether there is one and throw an exception.
Edit based on comment:
dates = []
try:
digits = [int(x) if int(x) < 10 else 100 for x in input("Enter the digits").split(",")]
if 100 in digits:
raise ValueError('Must be digits and not numbers')
datetime_perms = itertools.permutations(digits, 8)
for date_time in datetime_perms:
convert_date = []
for skip in range(0,8,2):
# Create a list that will look like [Month, Day, Hour, Minute]
convert_date.append(date_time[skip]*10+date_time[skip+1])
dates.append(convert_date)
except ValueError as e:
print(e)
for i, date in enumerate(dates):
try:
dates[i] = datetime.datetime(year=1, month=date[0], day=date[1], hour=date[2], minute=date[3])
except ValueError as e:
print(dates[i])
dates[i] = datetime.datetime(1,1,1,1,1,1,1)
if len(dates) > 0:
maximum = max(dates)
if maximum.microsecond == 1:
print('None of the combinations created a valid date')
else:
print('Month:', maximum.month, 'Day:', maximum.day, 'Hour:', maximum.hour, 'Minute:', maximum.minute)
This should do what you need. For the input of 1,2,3,4,3,2,1,0
I received the output of Month: 12 Day: 31 Hour: 23 Minute: 40
Upvotes: 2