Skip to content
Advertisement

The Number Stream detection

How to create a function NumberStream(str) that takes the str parameter being passed which will contain the numbers 2 through 9, and determine if there is a consecutive stream of digits of at least N length where N is the actual digit value.

If so, program should return the string true, otherwise, program should return the string false. For example: if str is “6539923335” then the program should return the string true because there is a consecutive stream of 3’s of length 3.

I have written the code but it needs some changes to perform as desired:

def NumberStream(str):
    global j
    l = len(str)
    count = 0
    res = str[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 str[i] != str[j]:
                break
            cur_count += 1
            print(str[i], " and ", str[j], " with cur_count = ", cur_count, " and count = ", count)
        if str[i] == str[j-1] and str[j-1] == cur_count:
            return


print(NumberStream("6539923335"))

Advertisement

Answer

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)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement