Home > OS >  generate a sequence with different lengths
generate a sequence with different lengths

Time:08-05

I am trying to write code that can generate a sequence that can be generalized with any sequence:

1     0:0
2     0:1
3     0:2
.     .
.     .
59    0:58
60    0:59
61    1:0
62    1:1
63    1:2
.     .
.     .
119   1:58
120   1:59
121   2:0
122   2:1
123   2:2
.     .
.     .
.     .
16383 273:2     
16384 273:3

The whole data sample can be found publicly in Google Sheets.

I could find out that the sequence always starts with fixed sequence 0..59 but at the end it is just 0..3 which made it difficult for me to write the formula that can be generalized to any sequence of any length (in this example the sequence length is 16385 but it can be of any length where the sequence 0..59 is always there)

I tried the following:

s = list()
for h in range(16385): # this index can be different; like 20000 or any
    for m in range(60): # always fixed number = 60
        s.append(f'{h}:{m}')

This code snippet even when it reaches the end with 0..59, it continues and these are the last 5 elements of s;

s[-5:] => ['16384:55', '16384:56', '16384:57', '16384:58', '16384:59']

Whereas the expected output should be like: s[-2:] => ['273:2', '273:3'] I am not sure how to generalize this.

CodePudding user response:

I am not sure this is exactly what you were asking for, but it may help you to play around with some approaches to find your solution.

# A short way
print([f'{x%(60*60)}:{x`}' for x in range(20000)])

# A little bit more precise
s = list()
amount = 20000
[s.append(f'{x%(60*60)}:{x`}') for x in range(amount)]
print(s)

# A precise way
s = list()
amount = 20000
for x in range(amount):
    hours = x%(60*60)
    minutes = x`
    s.append(f"{hours}:{minutes}")
print(s)

UPD: Maybe I understood you. you just need hrs and mins, hence it's better just to iterate:

s = list()
amount = 2000
for h in range(amount):
    hours = h%(60*60)
    for m in range(60):
        minutes = m
        s.append(f"{hours}:{minutes}")
print(s)

CodePudding user response:

Try this : [f'{(x-1)//60}:{(x-1)`}' for x in (1,100, 16381)]

or [f'{int((x-1)/60)}:{(x-1)`}' for x in (1,100, 16381)]

CodePudding user response:


behind_tuple = tuple(i for i in range(60))
front_tuple = tuple(i for i in range(280))
final = tuple(f'{f}:{b}' for f in front_tuple for b in behind_tuple)
print(final.index('273:2'))

"""
16382
"""

  • Related