Skip to content

Commit cce1dfa

Browse files
committed
More style fixes
1 parent 57ad8fc commit cce1dfa

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

src/stable_marriage_gale_shapley.nim

100644100755
Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
import sequtils, random, strutils
2-
const Pairs = 10
3-
const MNames = ["abe", "bob", "col", "dan", "ed", "fred", "gav", "hal", "ian", "jon"]
4-
const FNames = ["abi", "bea", "cath", "dee", "eve", "fay", "gay", "hope", "ivy", "jan"]
5-
const MPreferences = [
6-
["abi", "eve", "cath", "ivy", "jan", "dee", "fay", "bea", "hope", "gay"],
7-
["cath", "hope", "abi", "dee", "eve", "fay", "bea", "jan", "ivy", "gay"],
8-
["hope", "eve", "abi", "dee", "bea", "fay", "ivy", "gay", "cath", "jan"],
9-
["ivy", "fay", "dee", "gay", "hope", "eve", "jan", "bea", "cath", "abi"],
10-
["jan", "dee", "bea", "cath", "fay", "eve", "abi", "ivy", "hope", "gay"],
11-
["bea", "abi", "dee", "gay", "eve", "ivy", "cath", "jan", "hope", "fay"],
12-
["gay", "eve", "ivy", "bea", "cath", "abi", "dee", "hope", "jan", "fay"],
13-
["abi", "eve", "hope", "fay", "ivy", "cath", "jan", "bea", "gay", "dee"],
14-
["hope", "cath", "dee", "gay", "bea", "abi", "fay", "ivy", "jan", "eve"],
15-
["abi", "fay", "jan", "gay", "eve", "bea", "dee", "cath", "ivy", "hope"]
16-
]
17-
const FPreferences = [
18-
["bob", "fred", "jon", "gav", "ian", "abe", "dan", "ed", "col", "hal"],
19-
["bob", "abe", "col", "fred", "gav", "dan", "ian", "ed", "jon", "hal"],
20-
["fred", "bob", "ed", "gav", "hal", "col", "ian", "abe", "dan", "jon"],
21-
["fred", "jon", "col", "abe", "ian", "hal", "gav", "dan", "bob", "ed"],
22-
["jon", "hal", "fred", "dan", "abe", "gav", "col", "ed", "ian", "bob"],
23-
["bob", "abe", "ed", "ian", "jon", "dan", "fred", "gav", "col", "hal"],
24-
["jon", "gav", "hal", "fred", "bob", "abe", "col", "ed", "dan", "ian"],
25-
["gav", "jon", "bob", "abe", "ian", "dan", "hal", "ed", "col", "fred"],
26-
["ian", "col", "hal", "gav", "fred", "bob", "abe", "ed", "jon", "dan"],
27-
["ed", "hal", "gav", "abe", "bob", "jon", "col", "ian", "fred", "dan"]
28-
]
2+
3+
const
4+
Pairs = 10
5+
MNames = ["abe", "bob", "col", "dan", "ed", "fred", "gav", "hal", "ian", "jon"]
6+
FNames = ["abi", "bea", "cath", "dee", "eve", "fay", "gay", "hope", "ivy", "jan"]
7+
MPreferences = [
8+
["abi", "eve", "cath", "ivy", "jan", "dee", "fay", "bea", "hope", "gay"],
9+
["cath", "hope", "abi", "dee", "eve", "fay", "bea", "jan", "ivy", "gay"],
10+
["hope", "eve", "abi", "dee", "bea", "fay", "ivy", "gay", "cath", "jan"],
11+
["ivy", "fay", "dee", "gay", "hope", "eve", "jan", "bea", "cath", "abi"],
12+
["jan", "dee", "bea", "cath", "fay", "eve", "abi", "ivy", "hope", "gay"],
13+
["bea", "abi", "dee", "gay", "eve", "ivy", "cath", "jan", "hope", "fay"],
14+
["gay", "eve", "ivy", "bea", "cath", "abi", "dee", "hope", "jan", "fay"],
15+
["abi", "eve", "hope", "fay", "ivy", "cath", "jan", "bea", "gay", "dee"],
16+
["hope", "cath", "dee", "gay", "bea", "abi", "fay", "ivy", "jan", "eve"],
17+
["abi", "fay", "jan", "gay", "eve", "bea", "dee", "cath", "ivy", "hope"]
18+
]
19+
FPreferences = [
20+
["bob", "fred", "jon", "gav", "ian", "abe", "dan", "ed", "col", "hal"],
21+
["bob", "abe", "col", "fred", "gav", "dan", "ian", "ed", "jon", "hal"],
22+
["fred", "bob", "ed", "gav", "hal", "col", "ian", "abe", "dan", "jon"],
23+
["fred", "jon", "col", "abe", "ian", "hal", "gav", "dan", "bob", "ed"],
24+
["jon", "hal", "fred", "dan", "abe", "gav", "col", "ed", "ian", "bob"],
25+
["bob", "abe", "ed", "ian", "jon", "dan", "fred", "gav", "col", "hal"],
26+
["jon", "gav", "hal", "fred", "bob", "abe", "col", "ed", "dan", "ian"],
27+
["gav", "jon", "bob", "abe", "ian", "dan", "hal", "ed", "col", "fred"],
28+
["ian", "col", "hal", "gav", "fred", "bob", "abe", "ed", "jon", "dan"],
29+
["ed", "hal", "gav", "abe", "bob", "jon", "col", "ian", "fred", "dan"]
30+
]
2931

3032
# recipient's preferences hold the preference score for each contender's id
3133
func getRecPreferences[N: static int](prefs: array[N, array[N, string]],
@@ -35,24 +37,26 @@ func getRecPreferences[N: static int](prefs: array[N, array[N, string]],
3537
result[r][c] = prefArray.find(MNames[c])
3638

3739
# contender's preferences hold the recipient ids in descending order of preference
38-
func getContPreferences[N: static int](prefs: array[N, array[N, string]], names: openArray[
39-
string]): array[N, array[N, int]] {.compileTime.} =
40+
func getContPreferences[N: static int](prefs: array[N, array[N, string]],
41+
names: openArray[string]): array[N, array[N, int]] {.compileTime.} =
4042
for c, pref_seq in pairs(prefs):
4143
for r, pref in pairs(pref_seq):
4244
result[c][r] = names.find(pref)
4345

44-
const RecipientPrefs = getRecPreferences(FPreferences, MNames)
45-
const ContenderPrefs = getContPreferences(MPreferences, FNames)
46+
const
47+
RecipientPrefs = getRecPreferences(FPreferences, MNames)
48+
ContenderPrefs = getContPreferences(MPreferences, FNames)
4649

4750
proc printCoupleNames(contPairs: seq[int]) =
4851
for c, r in pairs(contPairs):
4952
echo MNames[c] & " 💑" & FNames[contPairs[c]]
5053

5154
func pair(): (seq[int], seq[int]) =
5255
# double booking to avoid inverse lookup using find
53-
var recPairs = newSeqWith(10, -1)
54-
var contPairs = newSeqWith(10, -1)
55-
proc engage(c, r: int) =
56+
var
57+
recPairs = newSeqWith(10, -1)
58+
contPairs = newSeqWith(10, -1)
59+
template engage(c, r: int) =
5660
#echo FNames[r] & " accepted " & MNames[c]
5761
contPairs[c] = r
5862
recPairs[r] = c
@@ -61,7 +65,7 @@ func pair(): (seq[int], seq[int]) =
6165
for c in 0..<Pairs:
6266
if contPairs[c] == -1:
6367
let r = ContenderPrefs[c][contQueue[c]] #proposing to first in queue
64-
contQueue[c]+=1 #increment contender's queue for future iterations
68+
contQueue[c] += 1 #increment contender's queue for following iterations
6569
let curPair = recPairs[r] # current pair's index or -1 = vacant
6670
if curPair == -1:
6771
engage(c, r)
@@ -74,14 +78,14 @@ func pair(): (seq[int], seq[int]) =
7478

7579
proc randomPair(max: int): (int, int) =
7680
let a = rand(max)
77-
var b = rand(max-1)
81+
var b = rand(max - 1)
7882
if b == a:
7983
b = max
80-
result = (a,b)
84+
result = (a, b)
8185

8286
proc perturbPairs(contPairs, recPairs: var seq[int]) =
8387
randomize()
84-
let (a,b) = randomPair(Pairs-1)
88+
let (a, b) = randomPair(Pairs-1)
8589
echo("Swapping ", MNames[a], " & ", MNames[b], " partners")
8690
swap(contPairs[a], contPairs[b])
8791
swap(recPairs[contPairs[a]], recPairs[contPairs[b]])
@@ -90,8 +94,9 @@ proc checkPairStability(contPairs, recPairs: seq[int]): bool =
9094
for c in 0..<Pairs: # each contender
9195
let curPairScore = ContenderPrefs[c].find(contPairs[c]) # pref. score for current pair
9296
for preferredRec in 0..<curPairScore: # try every recipient with higher score
93-
let checkedRec = ContenderPrefs[c][preferredRec]
94-
let curRecPair = recPairs[checkedRec] # current pair of checked recipient
97+
let
98+
checkedRec = ContenderPrefs[c][preferredRec]
99+
curRecPair = recPairs[checkedRec] # current pair of checked recipient
95100
# if score of the curRecPair is worse (>) than score of checked contender
96101
if RecipientPrefs[checkedRec][curRecPair] > RecipientPrefs[checkedRec][c]:
97102
echo("💔 ", MNames[c], " prefers ", FNames[checkedRec], " over ", FNames[contPairs[c]])

0 commit comments

Comments
 (0)