largest time for given digits

🏠
 1 from itertools import permutations
 2 
 3 class Solution:
 4     def largestTimeFromDigits(self, arr: List[int]) -> str:
 5         def is_valid(h, m):
 6             return 0 <= h <= 23 and 0 <= m <= 59
 7         last_valid = ""
 8         for p in permutations(sorted(arr)):
 9             p1, p2, p3, p4 = p
10             h, m = p1*10 + p2, p3 * 10 + p4
11             if is_valid(h, m):
12                 last_valid = f"{p1}{p2}:{p3}{p4}"
13         return last_valid