Skip to content

Commit 0883085

Browse files
committed
Replace std/enumerate import with 'pairs' iterator
1 parent 3a0afd9 commit 0883085

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

β€Žsrc/stable_marriage_gale_shapley.nimβ€Ž

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import sequtils, std/enumerate, random, strutils
1+
import sequtils, random, strutils
22
const Pairs = 10
33
const MNames = ["abe", "bob", "col", "dan", "ed", "fred", "gav", "hal", "ian", "jon"]
44
const FNames = ["abi", "bea", "cath", "dee", "eve", "fay", "gay", "hope", "ivy", "jan"]
@@ -30,22 +30,22 @@ const FPreferences = [
3030
# recipient's preferences hold the preference score for each contender's id
3131
func getRecPreferences[N: static int](prefs: array[N, array[N, string]],
3232
names: openArray[string]): array[N, array[N, int]] {.compileTime.} =
33-
for r, prefArray in enumerate(prefs):
34-
for c, contender in enumerate(prefArray):
33+
for r, prefArray in pairs(prefs):
34+
for c, contender in pairs(prefArray):
3535
result[r][c] = prefArray.find(MNames[c])
3636

3737
# contender's preferences hold the recipient ids in descending order of preference
3838
func getContPreferences[N: static int](prefs: array[N, array[N, string]], names: openArray[
3939
string]): array[N, array[N, int]] {.compileTime.} =
40-
for c, pref_seq in enumerate(prefs):
41-
for r, pref in enumerate(pref_seq):
40+
for c, pref_seq in pairs(prefs):
41+
for r, pref in pairs(pref_seq):
4242
result[c][r] = names.find(pref)
4343

4444
const RecipientPrefs = getRecPreferences(FPreferences, MNames)
4545
const ContenderPrefs = getContPreferences(MPreferences, FNames)
4646

4747
proc printCoupleNames(contPairs: seq[int]) =
48-
for c, r in enumerate(contPairs):
48+
for c, r in pairs(contPairs):
4949
echo MNames[c] & " πŸ’‘" & FNames[contPairs[c]]
5050

5151
func pair(): (seq[int], seq[int]) =
@@ -82,7 +82,7 @@ proc randomPair(max: int): (int, int) =
8282
proc perturbPairs(contPairs, recPairs: var seq[int]) =
8383
randomize()
8484
let (a,b) = randomPair(Pairs-1)
85-
echo "Swapping " & MNames[a] & " & " & MNames[b] & " partners"
85+
echo("Swapping ", MNames[a], " & ", MNames[b], " partners")
8686
swap(contPairs[a], contPairs[b])
8787
swap(recPairs[contPairs[a]], recPairs[contPairs[b]])
8888

@@ -94,10 +94,8 @@ proc checkPairStability(contPairs, recPairs: seq[int]): bool =
9494
let curRecPair = recPairs[checkedRec] # current pair of checked recipient
9595
# if score of the curRecPair is worse (>) than score of checked contender
9696
if RecipientPrefs[checkedRec][curRecPair] > RecipientPrefs[checkedRec][c]:
97-
echo "πŸ’” " & MNames[c] & " prefers " &
98-
FNames[checkedRec] & " over " & FNames[contPairs[c]]
99-
echo "πŸ’” " & FNames[checkedRec] & " prefers " &
100-
MNames[c] & " over " & MNames[curRecPair]
97+
echo("πŸ’” ", MNames[c], " prefers ", FNames[checkedRec], " over ", FNames[contPairs[c]])
98+
echo("πŸ’” ", FNames[checkedRec], " prefers ", MNames[c], " over ", MNames[curRecPair])
10199
echo "βœ— Unstable"
102100
return false # unstable
103101
echo "βœ“ Stable"

0 commit comments

Comments
Β (0)