Skip to content

Commit 1f44607

Browse files
committed
docs: add Python-style psuedocode
1 parent d5c9bcb commit 1f44607

13 files changed

+136
-2
lines changed

levels/bogo_sort.gd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ Keep on hitting RIGHT ARROW to CONTINUE and hope for the best!
1111
class_name BogoSort
1212
extends ComparisonSort
1313

14+
const CODE = """
15+
def bogosort(a):
16+
while not a.sorted():
17+
a.shuffle()
18+
"""
19+
1420
func _init(array).(array):
1521
pass
1622

levels/bubble_sort.gd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ them. Otherwise, hit RIGHT ARROW to continue.
1616
class_name BubbleSort
1717
extends ComparisonSort
1818

19+
const CODE = """
20+
def bubble_sort(a):
21+
swapped = true
22+
while swapped:
23+
swapped = false
24+
for i in range(len(a) - 1):
25+
if a[i] > a[i + 1]:
26+
a.swap(i, i + 1)
27+
swapped = true
28+
"""
1929
const ACTIONS = {
2030
"SWAP": "Left",
2131
"CONTINUE": "Right",

levels/cocktail_sort.gd

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ them. Otherwise, hit RIGHT ARROW to continue.
1313
class_name CocktailSort
1414
extends ComparisonSort
1515

16+
const CODE = """
17+
def cocktail_sort(a):
18+
swapped = true
19+
while swapped:
20+
swapped = false
21+
for i in range(len(a) - 1):
22+
if array[i] > array[i + 1]:
23+
a.swap(i, i + 1)
24+
swapped = true
25+
26+
if not swapped:
27+
break
28+
29+
swapped = false
30+
for i in range(len(a) - 1, 0, -1)
31+
if a[i - 1] > a[i]:
32+
a.swap(i - 1, i)
33+
swapped = true
34+
"""
1635
const ACTIONS = {
1736
"SWAP": "Left",
1837
"CONTINUE": "Right",

levels/comb_sort.gd

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ them. Otherwise, hit RIGHT ARROW to continue.
1212
class_name CombSort
1313
extends ComparisonSort
1414

15+
const CODE = """
16+
def comb_sort(a):
17+
gap = len(a)
18+
swapped = true
19+
while gap != 1 or swapped:
20+
gap = max(gap / 1.3, 1)
21+
for i in range(len(a) - gap):
22+
if a[i] > a[i + gap]:
23+
a.swap(i, i + gap)
24+
swapped = true
25+
"""
1526
const SHRINK_FACTOR = 1.3
1627
const ACTIONS = {
1728
"SWAP": "Left",

levels/cycle_sort.gd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ Otherwise, hit RIGHT ARROW.
1414
class_name CycleSort
1515
extends ComparisonSort
1616

17+
const CODE = """
18+
def cycle_sort(a):
19+
for i in range(len(a)):
20+
while True:
21+
position = 0
22+
for j in a:
23+
if a[j] > a[i]:
24+
position += 1
25+
if i == position:
26+
break
27+
a.swap(i, position)
28+
"""
1729
const ACTIONS = {
1830
"SMALLER": "Left",
1931
"BIGGER": "Right",

levels/insertion_sort.gd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ advance.
1717
class_name InsertionSort
1818
extends ComparisonSort
1919

20+
const CODE = """
21+
def insertion_sort(a):
22+
for i in range(len(a)):
23+
j = i
24+
while j > 0 and a[j - 1] > a[j]:
25+
a.swap(j - 1, j)
26+
j -= 1
27+
"""
2028
const ACTIONS = {
2129
"SWAP": "Left",
2230
"CONTINUE": "Right",

levels/merge_sort.gd

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ the other side's ARROW KEY.
1717
class_name MergeSort
1818
extends ComparisonSort
1919

20+
const CODE = """
21+
def merge_sort(a):
22+
size = 1
23+
while size < len(array):
24+
for block in range(len(array) / size / 2):
25+
merged = []
26+
begin = size * 2 * block
27+
i = begin
28+
j = begin + size
29+
while len(merged) != size * 2:
30+
if i >= begin + size or a[j] < a[i]:
31+
merged.append(a[j])
32+
j += 1
33+
else:
34+
merged.append(a[i])
35+
i += 1
36+
a[begin:begin + size] = merged
37+
"""
2038
const ACTIONS = {
2139
"LEFT": "Left",
2240
"RIGHT": "Right",

levels/odd_even_sort.gd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ them. Otherwise, hit RIGHT ARROW to continue.
1313
class_name OddEvenSort
1414
extends ComparisonSort
1515

16+
const CODE = """
17+
def odd_even_sort(a):
18+
swapped = true
19+
while swapped:
20+
swapped = false
21+
for i in range(1, len(a) - 1, 2):
22+
if a[i] > a[i + 1]:
23+
a.swap(i, i + 1)
24+
swapped = true
25+
for i in range(0, len(a) - 1, 2):
26+
if a[i] > a[i + 1]:
27+
a.swap(i, i + 1)
28+
swapped = true
29+
"""
1630
const ACTIONS = {
1731
"SWAP": "Left",
1832
"CONTINUE": "Right",

levels/quick_sort.gd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ RIGHT ARROW to move on.
1919
class_name QuickSort
2020
extends ComparisonSort
2121

22+
const CODE = """
23+
def quicksort(array, low=0, high=len(a) - 1):
24+
if low < high:
25+
pivot = a[high]
26+
pointer = low
27+
for i in range(low, high):
28+
if a[i] < pivot:
29+
a.swap(i, pointer)
30+
pointer += 1
31+
a.swap(pointer, high)
32+
quicksort(a, low, pointer - 1)
33+
quicksort(a, pointer + 1, high)
34+
"""
2235
const ACTIONS = {
2336
"SWAP": "Left",
2437
"CONTINUE": "Right",

levels/selection_sort.gd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ repeat.
1818
class_name SelectionSort
1919
extends ComparisonSort
2020

21+
const CODE = """
22+
def selection_sort(a):
23+
for i in range(len(a)):
24+
smallest = i
25+
for j in range(i, len(a)):
26+
if a[j] < a[smallest]:
27+
smallest = j
28+
a.swap(i, smallest)
29+
"""
2130
const ACTIONS = {
2231
"SWAP": "Left",
2332
"CONTINUE": "Right",

levels/shell_sort.gd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,24 @@ by gaps.
88
99
Hit LEFT ARROW to swap the two highlighted elements as long as they are
1010
out of order. When this is no longer the case, hit RIGHT ARROW to
11+
advance.
1112
"""
1213

1314
class_name ShellSort
1415
extends ComparisonSort
1516

17+
const CODE = """
18+
def shell_sort(a):
19+
gap = len(a)
20+
while gap != 1:
21+
gap = max(gap / 2, 1)
22+
for i in range(gap):
23+
for j in range(i, len(a) - gap, gap):
24+
k = j
25+
while k > gap and a[k - gap] > a[k]:
26+
a.swap(k - gap, k)
27+
k -= gap
28+
"""
1629
const ACTIONS = {
1730
"SWAP": "Left",
1831
"CONTINUE": "Right",

scripts/levels.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func _reload():
4848
_restart()
4949
_load_scores(_level)
5050
$NamesContainer/Names/Current.text = _level.NAME
51-
$Level/Left/Code.text = _level.DESCRIPTION
51+
$Level/Left/Code.text = _level.DESCRIPTION + "\n" + _level.CODE
5252
$Level/Right/Info/ControlsContainer/Controls.text = _level.CONTROLS
5353

5454
func _restart():
@@ -77,7 +77,7 @@ func _load_scores(level):
7777
data.get_node("Times").text = ""
7878
for i in data.get_node("Sizes").text.split("\n"):
7979
var time = str(GlobalScore.get_time(level.NAME, int(i)))
80-
data.get_node("Times").text += time
80+
data.get_node("Times").text += "%.3f" % float(time)
8181
if int(i) != MAX_SIZE:
8282
data.get_node("Times").text += "\n"
8383

scripts/menu.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ func _ready():
66
$Buttons/Start.grab_focus()
77
randomize()
88
$Display.add_child(ArrayView.new(_level), true)
9+
AudioServer.set_bus_mute(AudioServer.get_bus_index("Master"), true)
910

1011
func _on_Start_pressed():
1112
GlobalScene.change_scene("res://scenes/levels.tscn")

0 commit comments

Comments
 (0)