The Python Oracle

The Number Stream detection

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Life in a Drop

--

Chapters
00:00 The Number Stream Detection
00:51 Accepted Answer Score 1
02:11 Answer 2 Score 0
02:32 Answer 3 Score 1
02:50 Thank you

--

Full question
https://stackoverflow.com/questions/5903...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python

#avk47



ACCEPTED ANSWER

Score 1


Your code is almost correct. But first of all I strongly advise you not to use inbuilt functions as variable names. By using the name str you overwrite the function str() and therefore the shown code can't work. All you have to do is in your comparison before the return put a str(cur_count) instead of the direct variable. You are otherwise comparing an integer with a string and it will always return False. For that however you have to change the variable name str to something else like string the code would look like this:

def NumberStream(string):
    global j
    l = len(string)
    count = 0
    res = string[0]
    print("Length of the string is ", l)
    for i in range(l):
        count += 1
        cur_count = 1
        print("Looping")
        for j in range(i+1, l):
            if string[i] != string[j]:
                break
            cur_count += 1
            print(string[i], " and ", string[j], " with cur_count = ", cur_count, " and count = ", count)
        if string[i] == string[j-1] and string[j-1] == str(cur_count):
            return "True"
    return "False"


print(NumberStream("6539923335"))

Here some other solutions for it:

A very simple solution for this would be to just generate the number of consecutive numbers as a string and test, if they are in the string.

test_str = "6539923335"

def NumberStream(input_str):
    for i in range(10):
        consec_num = str(i)*i
        if consec_num in input_str:
            print("Found", consec_num)
            return "True"
    return "False"

print(NumberStream(test_str))

Or if you want to do it using a for loop:

test_str = "6539923335"

def NumberStream(input_str):
    consec_count = 1
    for j, number in enumerate(input_str):
        if j == 0:
            continue
        if number == input_str[j-1]:
            consec_count += 1
        else:
            consec_count = 1
        if str(consec_count) == number:
            print ("Found consecutive numbers:", number)
            return "True"
    return

NumberStream(test_str)



ANSWER 2

Score 1


If permitted, a regular expression can replace all the counting and accounting logic:

import re

def number_stream(s):
    pattern = '|'.join([f'{n}{{{n}}}' for n in range(2, 10)])
    return str(bool(re.search(pattern, s)))

pattern is an alternation of each digit and the required number of repetitions: '2{2}|3{3}|4{4}...'.




ANSWER 3

Score 0


t = "5723399999999"
def function(j):
    k = len(j)
    count = 1
    a = []
    for i in j:
        i = int(i)
        a.append(i)
    for index,i in enumerate(a): 
        try:
            if a[index]== a[index+1]:
                count = count+1
                if count == a[index+1]:
                    count = 1
                    return True
            if a[index]!=a[index+1]:
                count = 1
        except IndexError:
            pass
    return False

  function(t)

here is the code for Number Stream detection or continuous number in list