Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Create Intervals by hkgok1
def create_intervals(data):
"""
Create a list of intervals out of set of ints.
"""
# your code here
data1 = list(data)
data1.sort()
res = []
st = 0
ed = 0
for i in range(len(data)):
#print(data1[i])
if i==0:
st = data1[i]
ed = data1[i]
else :
if data1[i] == ed+1:
ed = data1[i]
else:
res.append((st,ed))
st = data1[i]
ed = data1[i]
if st not in res and st !=0:
res.append((st,ed))
return res
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert create_intervals({1, 2, 3, 4, 5, 7, 8, 12}) == [(1, 5), (7, 8), (12, 12)], "First"
assert create_intervals({1, 2, 3, 6, 7, 8, 4, 5}) == [(1, 8)], "Second"
print('Almost done! The only thing left to do is to Check it!')
March 14, 2022
Comments: