For work, I am cycling through ~24 billion combinations, and checking 3 things on each combination. Hence, I need my currently working code to be made as computationally efficient as possible so that this can be run as quickly as possible. p.s. I decided to run it just to see how slow it is. It's done roughly 180x3 million combinations in ~20 minutes (didn't record start time). Honestly, this is MUCH faster than I expected... but it's still too slow. The code will run on windows 10 in cmd with Python 3, if that matter at all.
However, as an example, I have made it 243x3.
Basically, what the script does is cycle through each combination in
I can't be bothered changing the names of all my variables, so if there is anything you don't understand, please ask. The general way I name my variables are type_name, e.g.
Also, if any clarification is required on what I'm doing in the code, I can add a comment to help explain it.
The main ones which need to be efficient are
Code:
However, as an example, I have made it 243x3.
Basically, what the script does is cycle through each combination in
sa_data[][]
, then print the array positions and which line matches. It checks if the same word appears 5x in one row. Think of it as a 3-row, 5-column table connect-5 game (https://en.wikipedia.org/wiki/Connect_Four).I can't be bothered changing the names of all my variables, so if there is anything you don't understand, please ask. The general way I name my variables are type_name, e.g.
ia_lines
= int array of lines. s
would be string, t
would be temp. Note: I call my lists "array"s (probably bad practice in python).Also, if any clarification is required on what I'm doing in the code, I can add a comment to help explain it.
The main ones which need to be efficient are
getNum()
and checkWin()
.Code:
ia_lines = [[0,0,0,0,0], [-1,-1,-1,-1,-1], [1,1,1,1,1]] #[0] checks current line in array, [1] checks previous line in array, [2] checks next line in array. sa_data = [] for x in range(5): sa_data.append([]) def getNum(x, y, i): if y + ia_lines[i][x] == len(sa_data[x]): return 0 else: return y + ia_lines[i][x] def checkWin(): #fout = open('Log.txt', 'w+') #read comment below about print() and fout.write() for a in range(len(sa_data[0])): for b in range(len(sa_data[1])): for c in range(len(sa_data[2])): for d in range(len(sa_data[3])): for e in range(len(sa_data[4])): for i in range(len(ia_lines)): if sa_data[0][getNum(0, a, i)] == sa_data[1][getNum(1, b, i)] == sa_data[2][getNum(2, c, i)] == sa_data[3][getNum(3, d, i)] == sa_data[4][getNum(4, e, i)]: print(str(a) + "," + str(b) + "," + str(c) + "," + str(d) + "," + str(e) + " on line " + str(i + 1) + " with 5 " + sa_data[0][getNum(0, a, i)]) #fout.write(str(a) + "," + str(b) + "," + str(c) + "," + str(d) + "," + str(e) + " on line " + str(i + 1) + " with 5 " + sa_data[0][getNum(0, a, i)] + '\n') #I will replace print() with fout.write() after the script finishes running (I don't want to make changes just in case it affects anything). #fout.close() with open('Text.txt', 'r') as fin: ti_reelNum = 0 for s_Line in fin: s_Line = s_Line.strip().split('"') if len(s_Line) < 2: continue elif s_Line[1][0] == 'R': ti_reelNum += 1 else: sa_data[ti_reelNum - 1].append(s_Line[1]) checkWin()Text.txt:
<tag1> <t1 name = "R 1"> <tag3 name="korea"/> <tag4 name="china"/> <tag5 name="japan"/> </t1> <t2 name = "R 2"> <tag3 name="japan"/> <tag4 name="china"/> <tag5 name="korea"/> </t2> <t3 name = "R 3"> <tag3 name="china"/> <tag4 name="korea"/> <tag5 name="japan"/> </t3> <t4 name = "R 4"> <tag3 name="china"/> <tag4 name="japan"/> <tag5 name="korea"/> </t4> <t5 name = "R 5"> <tag3 name="korea"/> <tag4 name="japan"/> <tag5 name="china"/> </t5> </tag1>Thank you in advance.