1- import sequtils, std / enumerate, random, strutils
1+ import sequtils, random, strutils
22const Pairs = 10
33const MNames = [" abe" , " bob" , " col" , " dan" , " ed" , " fred" , " gav" , " hal" , " ian" , " jon" ]
44const 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
3131func 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
3838func 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
4444const RecipientPrefs = getRecPreferences (FPreferences , MNames )
4545const ContenderPrefs = getContPreferences (MPreferences , FNames )
4646
4747proc 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
5151func pair (): (seq [int ], seq [int ]) =
@@ -82,7 +82,7 @@ proc randomPair(max: int): (int, int) =
8282proc 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