-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_recursion.py
59 lines (44 loc) · 1.63 KB
/
test_recursion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
maxGridColumnsByWidth = 1
class thumbnailGridView:
count = 0
class Math:
min = min
def calculateColumnCount():
# respect screenGeometry
c = Math.min(thumbnailGridView.count, maxGridColumnsByWidth)
residue = thumbnailGridView.count % c
if residue == 0:
gridColumns = c
return gridColumns
# start greedy recursion
gridColumns = columnCountRecursion(c, c, c - residue)
return gridColumns
# step for greedy algorithm
def columnCountRecursion(prevC, prevBestC, prevDiff):
c = prevC - 1
# don't increase vertical extent more than horizontal
# and don't exceed maxHeight
if prevC * prevC <= thumbnailGridView.count + prevDiff:
# Ignored maxHeight check: maxHeight < Math.ceil(thumbnailGridView.count / c) * thumbnailGridView.cellHeight
return prevBestC
residue = thumbnailGridView.count % c
# halts algorithm at some point
if residue == 0:
return c
# empty slots
diff = c - residue
# compare it to previous count of empty slots
if diff < prevDiff:
return columnCountRecursion(c, c, diff)
elif diff == prevDiff:
# when it's the same try again, we'll stop early enough thanks to the landscape mode condition
return columnCountRecursion(c, prevBestC, diff)
# when we've found a local minimum choose this one (greedy)
return columnCountRecursion(c, prevBestC, diff)
for y in range(1, 30):
maxGridColumnsByWidth = y
for x in range(1, 30):
thumbnailGridView.count = x
c = calculateColumnCount()
if not (c == y or x <= y):
print("{} / {} = {}".format(x, y, c))