Reputation: 101
import random
L = [random.randrange(1, 7) for i in range(20)]
print(L)
inRun = False
for i in range(len(L)):
if inRun:
if L[i] != L[i-1]:
print (')', end = '')
inRun = False
if not inRun:
if i != len(L)-1:
if L[i] == L[i+1]:
print('(', end = '')
inRun = True
print(L[i], end = '')
if inRun:
print(')', end = '')
Input list : [4, 5, 2, 6, 6, 5, 5, 5, 6, 2, 2, 1, 6, 5, 2, 1, 3, 3, 6, 3]
Output: 452(66)(555)6(22)16521(33)63
Should output : 45266(555)622165213363
I am having troubles marking the longest run rather than marking every number adjacent that is the same.
[edit]Thanks for the solutions everyone! To build on this code how can I return the indexes of the longest run?
Upvotes: 0
Views: 4120
Reputation: 362
The issue with your code is that you mark every run of numbers. I would suggest you run your loop then edit it with brackets. So before you mark every run you need to store all the runs and then pick the longest. I would recommend using a built-in function called sorted:
L = ["99", "888", "1"]
print(sorted(L, key=len))
Using the function we are able to track the longest run. Try the following code:
import random
L = [random.randrange(1, 7) for i in range(20)]
runs = []
print(L)
inRun = False
#count the run lenght
run = ""
for i in range(len(L)):
if inRun:
if L[i] != L[i-1]:
# add the last run we have collected during the loop
runs.append(run)
# research for more runs
run = ""
inRun = False
continue
else:
run += str(L[i])
if not inRun:
if i != len(L)-1:
if L[i] == L[i+1]:
run += str(L[i])
inRun = True
print("\n")
#sort runs so the longest run is in index -1 (last)
# key=len means we sort by len
runs = sorted(runs, key=len)
# create a new string variable holding our list so we could use replace function
# we must use the string value of every int using str(x)
result = ""
if (len(runs) > 0):
result = result.join(str(x) for x in L)
#replacing the run with the same value but with "()"
result = result.replace(runs[-1], "(" + runs[-1] + ")" , 1) # use 1 to tell python not mark more than one run if possible
print(result)
Please tell me if it solved your issue. All the best;
Upvotes: 1
Reputation: 36598
You are pretty close. The logic for your on/off switch for the grouping is not working quite right. Consider creating the subgroups and a variable that tracks the longest one seen. Then iterate of the subgroups and add parens around the one with the correct (longest) length.
import random
L = [random.randrange(1, 7) for i in range(20)]
groups = []
current_run = []
longest_run = 0
for x in L:
if (not current_run) or (x == current_run[-1]):
current_run.append(x)
if longest_run < len(current_run):
longest_run += 1
else:
groups.append(current_run)
current_run = [x]
for g in groups:
g_str = ''.join(map(str, g))
if len(g) == longest_run:
print(f'({g_str})', end='')
else:
print(g_str, end='')
For you example of L = [4, 5, 2, 6, 6, 5, 5, 5, 6, 2, 2, 1, 6, 5, 2, 1, 3, 3, 6, 3]
, you would see the following:
groups
# returns:
[[4],
[5],
[2],
[6, 6],
[5, 5, 5],
[6],
[2, 2],
[1],
[6],
[5],
[2],
[1],
[3, 3],
[6]]
# printed output:
45266(555)62216521336
Upvotes: 0