1

I actually have three task out of which two has been completed now I am really stuck with the last one.

  1. Check two consecutive 66 (Working)
  2. Longest sequence of rolls with 6 (Working)
  3. Want to check the number for most frequent length of 5's and 6's. For example in 5533661656 the 656 is the longest but there is only one series of length three but answer should be 2 as 55, 66 are there. Similarly 456116513656124566 have length of 2 and 3 which are occuring twice. Now program should print the longest length and answer should be 3

here is code


trial = int(randint(1, 500))
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):
    s = str(result).split('6')
    l = 0
    for i in s:
        if l < len(i):
            l = len(i)
        if len(i) > l:
            l = i
    return (l)
print(no_six(result))
#
# def lucky_series(result)

1 Answers1

1

This can probably be simplified with regex but I don't know how to:
Here's my solution

result = str(322226555453442214634442111625423563466312534536531251541656625421252464254536351661534211622365625514522561513611151366313122241413512123614521)

Replace the numbers 1 to 4 with spaces then split it to turn it into a list.

result = result.replace('1',' ').replace('2',' ').replace('3',' ').replace('4',' ').split()  
#['6555', '5', '6', '6', '5', '56', '66', '5', '5', '65', '5', '5', '6566', '5', '5', '6', '5', '5', '6', '5', '66', '5', '6', '656', '55', '5', '56', '5', '6', '5', '66', '5', '6', '5']  

then find the length of each 5-6 item in that list then store them into another list:

lengths = [len(a) for a in result] #list-comprehension
#[4, 1, 1, 1, 1, 2, 2, 1, 1, 2, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 3, 2, 1, 2, 1, 1, 1, 2, 1, 1, 1]

Count the similar length items using list.count starting from 2 together with their frequency

total = [[i , lengths.count(i)] for i in range(1000)[2:] if lengths.count(i) != 0]
#[[2, 7], [3, 1], [4, 2]]

then use max() to find the length with the highest frequency(get max based on second item)

max(total, key = lambda x: x[1])
#[2, 7]

Function:

def most56(result):
    result = result.replace('1',' ').replace('2',' ').replace('3',' ').replace('4',' ').split()
    lengths = [len(a) for a in result]
    total = [ [i, lengths.count(i)] for i in range(1000)[2:] if lengths.count(i) != 0]
    if total:
        most = max(total, key = lambda x: x[1])
        return most[0]
    return 'no long numbers'
rEsonansDx
  • 35
  • 1
  • 6
  • The above function is not working " max() arg is an empty sequence" is the error. – Waqar Rafique May 02 '21 at 07:08
  • I fixed the errors, including the missing string.split() at the end. It should be working now. – rEsonansDx May 02 '21 at 07:42
  • Thanks buddy ... please explain "range(1000)[2:]". Thanks – Waqar Rafique May 02 '21 at 08:06
  • the [2:] is a slice notation. It tells the range to start from 2 and stop until the end. – rEsonansDx May 02 '21 at 08:08
  • for this "3554521341111343663236636" number the result is 3. Its wrong – Waqar Rafique May 02 '21 at 08:23
  • [Link]https://stackoverflow.com/questions/67354389/updated-code-need-to-print-the-most-frequent-length-of-5-s-and-6-s-in-dice-rol. Here is the final code that I have. please help me with the last part as there is still errors in it. Thanks – Waqar Rafique May 02 '21 at 08:36
  • note that the "result" parameter must be a number string. I edited my answer to display the length with the highest frequency btw. Also, in your other question, you should also add replace('6', ' '). The point there is to isolate the numbers you want to measure. For instance, if you only want 5, then replace all numbers except number 5 into whitespace. – rEsonansDx May 02 '21 at 09:03
  • The new edit should fix the error where no 2-length is found. – rEsonansDx May 02 '21 at 09:22
  • Can you please help me understand " most = max(total, key = lambda x: x[1]) " part – Waqar Rafique May 02 '21 at 09:29
  • All I know is max accepts a key function, i don't know how it works. I just used this as reference https://stackoverflow.com/questions/4800419/finding-max-value-in-the-second-column-of-a-nested-list – rEsonansDx May 02 '21 at 09:37