Click here to Skip to main content
15,902,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tried following code but it seems that it is only counting the numbers excluding six's and displaying the result. However what I want is to check the longest sequence without six's. I tried the Logic in my code but not working.

Function double_six () absolutely working fine but no_six() is not working.

here is sample output

10

4113644412

0

4

the 4 at the end should be 5 as longest sequence without six

What I have tried:

from random import *

trial = int(randint(1, 10))
print(trial)
result = ''
for i in range(trial):
    init_num = str(randint(1, 6))
    result += init_num
print(result)

def double_six(result):
    last_dice = '0'
    counter = 0
    for i in range(trial):
        if result[i] == '6' and last_dice == '6':
            counter += 1
            last_dice = '0'
        else:
            last_dice = result[i]
    return counter
print(double_six(result))

def no_six(result):
    counter = 0
    length = 0
    previous = 0
    for i in range(trial):
        if result[i] == '6':
            length = counter
            if length > previous:
                previous = length
        else:
            counter +=1

    return  previous
print(no_six(result))
Posted
Updated 1-May-21 0:26am

One of your bugs is that if result do not end with a 6, you have a count that can be better than previous.
Third question on same subject, time to learn how to debug your own code.
Quote:
I tried the Logic in my code but not working.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
Comments
Richard MacCutchan 1-May-21 5:42am    
Thanks, I already spotted the problem and deleted my solution.
Try this (thanks to Patrice for pointing out the error in my previous post):
Python
numbers = []
for i in range(40):
    numbers.append(random.randint(1,6))

print(numbers)

length = 0 # longest sequence
offset = 0 # where it starts
count = 0
sequence = 0
for i in range(len(numbers)):
    if numbers[i] == 6:
        if count > length:
            length = count     # new longest length
            offset = sequence  # start position of this set
        count = 0          # start a new count
        sequence = i + 1   # next sequence starts after this 6
    else:
        count += 1  # count all non-sixes

if count > length:    # check for a final sequence
    length = count
    offset = sequence

print("Longest sequence:", length, "starts at offset:", offset)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900