0

I want to convert a string in specified format (start-stop-dataNumber) to a array. datanumber is a number of data in that range from start to stop.

for example: input="1-10:5" output=[1,3,5,7,9]

thanks for your attention

1 Answer 1

1

If I understand your question correctly, you could do it like this:

def format_input(input):
parts = input.split(':')

start_stop = parts[0].split('-')
start = int(start_stop[0])
stop = int(start_stop[1])
data_number = int(parts[1])

if start > stop:
    return []

# the range is now like this: [start,stop)
# if you want the interval to include the stop: [start,stop]
# use the commented out line

step_size = (stop - 1 - start) / (data_number - 1)
#step_size = (stop - start) / (data_number - 1)
output = [round(start + i * step_size) for i in range(data_number)]

return output

You parse the string first and then simply generate the array by calulating the necessary step size and multiplying this with i for the i-th element in your output array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.