Reputation: 39
I actually have three task out of which two has been completed now I am really stuck with the last one.
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)
Upvotes: 1
Views: 85
Reputation: 35
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'
Upvotes: 1