-4

Suppose we have two numbers a and b we need to calculate the continuous matching digits between the two numbers.

some examples are shown below:

  1. a = 123456 b = 456 ==> I need count as : 3 digits matching
  2. a = 556789 b = 55678 ==> I need count as : 5 digits matching

I don't want unique but continuous matching numbers and need the count. Also display the matching ones will be helpful. Also can we can we do in two different lists if numbers?

I am very new to python and trying out few things. Thanks

Tejas wagh
  • 19
  • 2
  • 11
  • Can you explain more? I couldn't understand a thing – Bruno Mello Apr 02 '20 at 11:50
  • Suppose we take two number a = 123456 b = 456 and we can see 3 number i.e. 456 are continuous and matching. so need the count how many digits are matching. Eg2: a = 556789 b = 55678 are numbers and 5 digits are consecutively matching so we get count as 5. – Tejas wagh Apr 02 '20 at 11:58
  • 1
    Looking at what you wrote and at your test cases I think it's sufficient to say: `len(str(b))`. But of course you mean something else (and @BrunoMello probably got it right), so if you can please edit your question to make it clearer. – UJIN Apr 02 '20 at 12:04
  • Sorry i was unable to ask the question properly. If anyone of you feel its not correct can suggest me to change. Also i will learn from it and keep things in mind but that doesn't mean i should not asked. At the end i learned something and why to down vote each time. It discourages one who is trying to learn. Also i got a sufficient amount of answer. – Tejas wagh Apr 02 '20 at 12:59
  • 1
    Well, we are happy you got the help you requested, but according to me the downvotes are deserved. This is not a rant, just an explanation on what was wrong and an advice on how to ask things properly next time. In this post there is **no research effort**, there is **not** even a **single line of code**, the text is **badly formatted**, and the question **is unclear**. [This guide](https://stackoverflow.com/help/how-to-ask) may help you next time to ask a more well received question. – UJIN Apr 02 '20 at 13:07

2 Answers2

3

Given two numbers a and b:

a = 123456
b = 456

First you need to covert them to strings:

a_str = str(a)
b_str = str(b)

Then you need to check if there is a continuous match of b_str in a_str:

if b_str in a_str:
   ...

Finally you can check the length of b_str:

len(b_str)

This is the complete function:

def count_matching_elements(a, b):
    a_str, b_str = str(a), str(b)
    if b_str in a_str:
        return len(b_str)
    else:
        return -1 # no matches
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
  • Thanks its working fine for all the scenarios i have added. suppose if conditions are a = 99641 b = 8996241 Then it wont give exact matching count digits. like if b_str contains more digits then a then also can we find the counts? – Tejas wagh Apr 02 '20 at 12:28
  • 1
    You can do something like this at the beginning: `a, b = a, b if a > b else b, a` – Riccardo Bucco Apr 02 '20 at 12:30
1

What you want here is know as the Longest common substring, you can find it like this (this code can be found here Find common substring between two strings, just a little difference that you actually want the len(answer)) :

def longestSubstringFinder(string1, string2):
    answer = ""
    len1, len2 = len(string1), len(string2)
    for i in range(len1):
        match = ""
        for j in range(len2):
            if (i + j < len1 and string1[i + j] == string2[j]):
                match += string2[j]
            else:
                if (len(match) > len(answer)): answer = match
                match = ""
    return len(answer)

Note that a and b would have to be strings

Bruno Mello
  • 4,448
  • 1
  • 9
  • 39