The Number Stream detection

Question:

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"))
Asked By: Abhishek Mengi

||

Answers:

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)
Answered By: LeoE
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

Answered By: Hemanth kumar
function NstreamsOfNumberN (str) {
    
    for (let i = 0; i < str.length; i++) {
        
        let numBeingConsidered = Number(str[i]);
                
        let numOfComparisonsToBeDone = numBeingConsidered - 1;
        
        for (let j = i; j < numOfComparisonsToBeDone + i; j++) {
                
            if (str[j] != str[j+1]) {break}//compare neigbourin nums

            else if ((j - i + 1) === numOfComparisonsToBeDone)  
            { let theNwithNstreams = numBeingConsidered
              return [str, (theNwithNstreams), true]} 

        //(j - i + 1) equals num of comparisons that has been done.
        }
    }
    return false 
}

NstreamsOfNumberN('334775555583444582')

9 streams of the number 9 
8 streams of the number 8 
7 streams of the number 7 ...
3 streams of the number 3
2 streams of the number 2.
Answered By: AYANTOLA TESLIM

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}...'.

Answered By: snakecharmerb
from collections import defaultdict
import operator
def NumberStream(string):
    count={}
    for i in range(len(string)-1):
        if string[i]==string[i+1]:
            count[str(i)]=string[i]
            count[str(i+1)]=string[i+1]
    print(count)   
    res=defaultdict(int)
    for key, item in count.items():
        res[item]+=1
    print(res)
    stream=0
    for key, item in res.items():
        if int(key)== item:
            stream=key
    
    if int(stream)> 0:
        return "true"
    else: return "false"


    

This is my solution

Answered By: leila khellouf
def NumberStream(string):

    j=0

    for i in string:

        j=i*int(i)

        if j in string:

            return True

            break

    else:
        return False


print(NumberStream("653999923335"))
Answered By: ANOOP KS
public static String numberStream(String s) {
    char[] ch = s.toCharArray();
    for (int i = 0; i < ch.length; i++) {
        int cur_count = 1;
        for (int j = i + 1; j < ch.length; j++) {
            if (ch[i] != ch[j]) {
                break;
            }
            cur_count += 1;
            if (Integer.toString(cur_count).equals(ch[i] + "")) {
                return "true";
            }
        }
    }
    return "false";
}
Answered By: RAKESH YEDLAPPALLI
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.