简体   繁体   中英

python: how to extract the consecutive slice with different length in string without a space

for example, in a string which is 789101112 ,

i = 1 
j = 0 
x = '789101112'  
while i < 3:
    while j < len(x):
        m = int(x[j: j+i])
        n = int(x[j+i: j+i+i])
        if n - m ==1:
            print(m, n)
            j +=i

The output is:

7 8
8 9

The output I want is:

7 8
8 9
9 10
10 11
11 12

What should I need to do based on my codes?

I had a bit of rough idea on the algorithm but I guess it took a bit more time than I anticipated to write a sample program.

Note: I have assumed the maximum count of missing consecutive numbers in the series to be 1. You can modify the code and apply a loop for maximum count as infinite or any other number you would prefer.

Its past midnight here so the code is not thoroughly tested. Here it is:

def get_first_num(string):
    first_num=0
    for i in range(int(len(string)/2)):
        if string.startswith(string[0:i+1]+str(int(string[0:i+1])+1)) or string.startswith(string[0:i+1]+str(int(string[0:i+1])+2)):    #max difference between any 2 consecutive numbers must be 1 or 2 i.e only 1 missing number in sequence is allowed
            assumed_first_num, temp, count, flag=int(string[0:i+1]), string, 0, False

            temp=temp.replace(str(assumed_first_num), '', 1)
            count += 1

            for _ in range(100):
                changed = False
                if(temp.startswith(str(assumed_first_num+1)) or temp.startswith(str(assumed_first_num+2))):
                    next_assumed_first_num=assumed_first_num+1 if temp.startswith(str(assumed_first_num+1)) else assumed_first_num+2
                    temp=temp.replace((str(assumed_first_num+1) if temp.startswith(str(assumed_first_num+1)) else str(assumed_first_num+2)), '', 1)
                    assumed_first_num, changed, count=next_assumed_first_num, True, count+1
                if len(temp) == 0:
                    flag=True
                    break
                if not changed:
                    flag=False
                    break
            if(flag):
                first_num=int(string[0:i+1])
                break
            else:
                continue
    return first_num

test_strings=["789101112", "910111213", "91112131415", "1214161820", "891089118912", "890892893894", "123451234712348", "1234567123456812345691234570"]
for string in test_strings:
    print("First number "+str(get_first_num(string))+" determined from string: "+string)

And the output for the above program is:

$ python3 script.py 
First number 7 determined from string: 789101112
First number 9 determined from string: 910111213
First number 9 determined from string: 91112131415
First number 12 determined from string: 1214161820
First number 8910 determined from string: 891089118912
First number 890 determined from string: 890892893894
First number 12345 determined from string: 123451234712348
First number 1234567 determined from string: 1234567123456812345691234570

The main challenge and tricky part in the problem was to determine the 1st number in the series; so as of now I have written only the function to determine the first number. I will be going to bed now, but I will extend the program to determine the missing numbers in the sequence which shouldn't take much time compared to this.

Also the 100 in the innermost for loop and the count for that loop literally counts the consistency of the given series with a maximum of 100 elements taken as sample which are guaranteed to be consistent.

I think the problem with your code is the last line j+=i , i was never incremented! So it would always be 1. Also, when during the sequence, say from 9 to 10, it becomes 2 digits all of a sudden, then you also need to provision the next digit to see if it is still alright.

since the sequence is garanteed to be incremented by one, this alternative code might be a lazy solution:

def start_seq(num_str):
    '''determines the number the sequence starts with (minimum 3 numbers
    inside sequence)'''
    for i in range(1,len(num_str)//3):
        num1 = int(num_str[0:i])
        num2 = num1 + 1
        num3 = num2 + 1
        seq = str(num1)+str(num2)+str(num3)
        if num_str.startswith(seq):
            return num1
        else:
            continue
    return -1

def output_sequence(num_str):
    num1 = start_seq(num_str)
    if num1 == -1:            
        print('not a valid sequence')
        raise ValueError

    processed_seq = str(num1)

    counter = 0 # for printing '\n'
    #continue until reaching the end of sequence.
    while processed_seq !=num_str:
        print(num1,end=' ')
        if counter%2 == 1:
            print('\n',end='')
        num1 += 1
        processed_seq += str(num1)
        counter += 1

if __name__ == '__main__':
    output_sequence('789101112')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM