Check if a sequence of numbers is present in another

Asked

Viewed 534 times

-1

I have two numerical sequences that have some similar digit sequences.

a = 0000000724050613
b = 07240001980055060113

Example: the sequences 724 and 506, for example, they are contained in a and b

I want to know how do I check those sequences of numbers on a are present in b.

  • 1

    But they also have 13, 000, 07240, and several digits in common. What defines this similarity? Does it have to be a three-digit sequence? The largest possible sequence? It has to be different from 0?

  • It could be the largest possible sequence. For example: 07240, 000 and 506. Check whether these 3 sequences in a also occur in b.

2 answers

1

[ Updated answer ] I made this code below that walks through a variable of X positions X times, that is, a variable of 5 positions is analyzed 25 times and at each time it checks the heading[1,2,3,4,5] until the position[1,2,3,4,5] + the quantity that the analysis is being repeated. That’s basically it:

a = "12345"

analisado    | Quantidade de análise
1,2,3,4,5    | 1
12,23,34,45  | 2
123,234,345  | 3
1234,2345    | 4
12345        | 5

That way he’ll analyze the whole set of A, then just use one:
if (analysed in b):

'''script que verifica quais elementos de A estão contidos em B '''

a = "0000000724050613"
b = "07240001980055060113"

x=1
loop = len(a)
while loop>=x:
 y=1
 while loop>=y:
  if y-1+x>loop:
   y=loop
  else:
   sequencia = a[y-1:y-1+x]
   if (sequencia in b):
    print("A sequência {} está contida em b".format(sequencia))
  y=y+1
 x=x+1

I put it in my Github: https://github.com/gabriel-gregorio-da-silva/estaContidoEmB

This script has no filters, so if the number 7230 is contained in the B, it will show the 7,2,3 and 0 that are contained in the first analysis, then the 72,23 and 30 that are contained in the second analysis, then the 723,230 in the third analysis and then the 7230 in the fourth analysis.

You can build a filter logic, you take the values in each analysis and check if they are contained in the following analysis the 7, 2, 3 and 0 for example are contained in the second analysis as 73, 23 and 30, ie they would already be eliminated.

Ai in the third analysis, the 73, 23 and 30 would be eliminated, since they are in the third as 723 and 230.

In the fourth analysis, the 723 and the 230 would be eliminated because they are contained as 7230.

  • is precisely to analyze the combinations in common, autonomously. Perhaps I could not abstract the question right.

-1

You can convert the values of a and b into a string and treat them the way you want, example:

Switching to string:

str(a);

Finding the value:

result = re.search('This', test)

We can extract the result using the group method:

result.group(0)
'This'

You’re probably wondering about the group method and why we gave it zero. It’s simple, and I’ll explain.Look, patterns are organized in groups, this way:

>>> result = re.search ('(Th)(is)',test)

Here there are two groups within parentheses. We can extract them using the group method.

>>> result.group(1)
'Th'
>>> result.group(2)
'is'

After performing the treatments can convert back to number:

int('a');

Reference:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.