fork download
  1. import timeit
  2. import sys
  3.  
  4. #the preferences of the knights and ladies presented as dictionaries
  5. knightPreferences = {}
  6. ladyPreferences = {}
  7.  
  8. #the unwed knights and ladies as arrays
  9. unwedKnights = []
  10. unwedLadies = []
  11.  
  12. #the group of engaged couples. We say "maybe" because they are liable
  13. #to change
  14. maybeEngaged = []
  15.  
  16. #the array of taken
  17. takenMatch = []
  18.  
  19. #reading the knights and ladies preferences from the file and into
  20. #their respective dictionaries
  21. def SetKnightsAndLadies(fileName):
  22. #reading the file
  23. input_file = open(fileName)
  24. lines = input_file.readlines()
  25. #getting n from the file
  26. n = int(lines[0])
  27. data = lines[1:]
  28. i = 0
  29. #the initialization invariant of this loop is that both dictionaries
  30. #are empty. The maintenance invariant is that the knight/lady and
  31. #their preferences are added to the respective dictionary. The
  32. #termination invariant is that both dictionaries are full
  33. for line in data:
  34. if(i < n):
  35. #adding the first n lines as the knights' preferences
  36. knightPreferences[line.split()[0]] = line.split()[1:]
  37. if(i >= n):
  38. #adding the next n lines as the ladies' preferences
  39. ladyPreferences[line.split()[0]] = line.split()[1:]
  40. i = i + 1
  41.  
  42. #setting all the knights and ladies as unwed initially
  43. def freeKnightsAndLadies():
  44. #the two loops here add the knights and ladies to their respective
  45. #arrays that mark them as unwed. The initialization invariants in
  46. #both these loops are that there are absolutely no pairs so far
  47. for key, value in knightPreferences.items():
  48. #the maintenance invariant is that the knight or lady who's
  49. #preferences are being considered is marked as unwed
  50. unwedKnights.append(key)
  51. #the termination invariant for both loops is that all the knights
  52. #and ladies are marked as unwed
  53. for key, value in ladyPreferences.items():
  54. unwedLadies.append(key)
  55.  
  56. def matchKnightWithLady(knight):
  57. #the initialization invariant of this loop is that there are no preferences
  58. #that would conflict with the current match. The maintenance invariants would
  59. #be that either the a knight and a lady become a couple, or a lady who is
  60. #currently matched is matched again with someone who's higher on their
  61. #priority list. The termination invariant is that the lady and the knight
  62. #are in their first match, or a new one.
  63. for lady in knightPreferences[knight]:
  64. #for each lady preferred by the knight, a boolean value is taken to
  65. #see if they are already in a match
  66. takenMatch = [match for match in maybeEngaged if lady in match]
  67. #if the lady is single, then she and the knight are added as
  68. #maybe engaged, and the loop breaks
  69. if len(takenMatch) == 0:
  70. maybeEngaged.append([knight,lady])
  71. unwedKnights.remove(knight)
  72. #print("matching knight and lady")
  73. break
  74. #else, if the lady is already in a match, then the preference of the
  75. #lady is tested to see if the knight is higher on her preference list
  76. elif len(takenMatch) > 0:
  77. currentHusband = ladyPreferences[lady].index(takenMatch[0][0])
  78. potentialHusband = ladyPreferences[lady].index(knight)
  79. #comparing the current and potential husbands. If the potential
  80. #husband is preferred, then their positions are switched, and
  81. #the loop breaks. If not, nothing is changed
  82. if (currentHusband > potentialHusband):
  83. unwedKnights.remove(knight)
  84. unwedKnights.append(takenMatch[0][0])
  85. takenMatch[0][0] = knight
  86. break
  87.  
  88.  
  89. #the overall matching algorithm
  90. def stableMatch():
  91. #the first loop in the matching algorithm, the initialization invariant
  92. #is that the length of the array unwedKnights is greater than 0. The
  93. #maintenance invariant is that a knight and lady are paired
  94. #together and no longer unwed. The termination invariant is
  95. #that there are no more unwed knights and ladies
  96. while len(unwedKnights) > 0:
  97. #the initialization invariant for this loop is that the knight
  98. #is currently unwed. the maintenance invariants are that the
  99. #knight is either the maintenance invariants are that the knight
  100. #is either the maintenance invariants are that the knight is either
  101. #wed to a lady or remains free. the termination invariant is that
  102. #the knight is probably married
  103. for knight in unwedKnights:
  104. #the part of the matching algorithm that works for one
  105. #knight at a time
  106. matchKnightWithLady(knight)
  107.  
  108. #the main function
  109. def main():
  110. inputFile = input(str("Enter the name of the file: "))
  111.  
  112. #calculating the runtime
  113. start = timeit.default_timer()
  114.  
  115. if not inputFile.endswith('.txt'):
  116. exit(1)
  117. SetKnightsAndLadies(inputFile)
  118. freeKnightsAndLadies()
  119. stableMatch()
  120.  
  121. #printing the runtime
  122. stop = timeit.default_timer()
  123. sys.stdout.write(str(stop - start) + '\n')
  124.  
  125. for i in range(len(maybeEngaged)):
  126. sys.stdout.write(str(maybeEngaged[i][0]) + " " + str(maybeEngaged[i][1]) + '\n')
  127.  
  128. main()
  129.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: '.' expected
import timeit
             ^
Main.java:2: error: ';' expected
import sys
      ^
Main.java:4: error: illegal character: '#'
#the preferences of the knights and ladies presented as dictionaries
^
Main.java:4: error: class, interface, or enum expected
#the preferences of the knights and ladies presented as dictionaries
     ^
Main.java:8: error: illegal character: '#'
#the unwed knights and ladies as arrays
^
Main.java:12: error: illegal character: '#'
#the group of engaged couples. We say "maybe" because they are liable
^
Main.java:13: error: illegal character: '#'
#to change
^
Main.java:16: error: illegal character: '#'
#the array of taken
^
Main.java:19: error: illegal character: '#'
#reading the knights and ladies preferences from the file and into
^
Main.java:20: error: illegal character: '#'
#their respective dictionaries
^
Main.java:22: error: illegal character: '#'
    #reading the file
    ^
Main.java:25: error: illegal character: '#'
    #getting n from the file
    ^
Main.java:29: error: illegal character: '#'
    #the initialization invariant of this loop is that both dictionaries
    ^
Main.java:30: error: illegal character: '#'
    #are empty. The maintenance invariant is that the knight/lady and
    ^
Main.java:31: error: illegal character: '#'
    #their preferences are added to the respective dictionary. The
    ^
Main.java:32: error: illegal character: '#'
    #termination invariant is that both dictionaries are full
    ^
Main.java:35: error: illegal character: '#'
            #adding the first n lines as the knights' preferences
            ^
Main.java:35: error: unclosed character literal
            #adding the first n lines as the knights' preferences
                                                    ^
Main.java:38: error: illegal character: '#'
            #adding the next n lines as the ladies' preferences
            ^
Main.java:38: error: unclosed character literal
            #adding the next n lines as the ladies' preferences
                                                  ^
Main.java:42: error: illegal character: '#'
#setting all the knights and ladies as unwed initially
^
Main.java:44: error: illegal character: '#'
    #the two loops here add the knights and ladies to their respective
    ^
Main.java:45: error: illegal character: '#'
    #arrays that mark them as unwed. The initialization invariants in
    ^
Main.java:46: error: illegal character: '#'
    #both these loops are that there are absolutely no pairs so far
    ^
Main.java:48: error: illegal character: '#'
        #the maintenance invariant is that the knight or lady who's
        ^
Main.java:48: error: unclosed character literal
        #the maintenance invariant is that the knight or lady who's
                                                                 ^
Main.java:49: error: illegal character: '#'
        #preferences are being considered is marked as unwed
        ^
Main.java:51: error: illegal character: '#'
    #the termination invariant for both loops is that all the knights
    ^
Main.java:52: error: illegal character: '#'
    #and ladies are marked as unwed
    ^
Main.java:57: error: illegal character: '#'
    #the initialization invariant of this loop is that there are no preferences
    ^
Main.java:58: error: illegal character: '#'
    #that would conflict with the current match. The maintenance invariants would
    ^
Main.java:59: error: illegal character: '#'
    #be that either the a knight and a lady become a couple, or a lady who is
    ^
Main.java:60: error: illegal character: '#'
    #currently matched is matched again with someone who's higher on their
    ^
Main.java:60: error: unclosed character literal
    #currently matched is matched again with someone who's higher on their
                                                        ^
Main.java:61: error: illegal character: '#'
    #priority list. The termination invariant is that the lady and the knight
    ^
Main.java:62: error: illegal character: '#'
    #are in their first match, or a new one.
    ^
Main.java:64: error: illegal character: '#'
        #for each lady preferred by the knight, a boolean value is taken to
        ^
Main.java:65: error: illegal character: '#'
        #see if they are already in a match
        ^
Main.java:67: error: illegal character: '#'
        #if the lady is single, then she and the knight are added as
        ^
Main.java:68: error: illegal character: '#'
        #maybe engaged, and the loop breaks
        ^
Main.java:72: error: illegal character: '#'
            #print("matching knight and lady")
            ^
Main.java:74: error: illegal character: '#'
        #else, if the lady is already in a match, then the preference of the
        ^
Main.java:75: error: illegal character: '#'
        #lady is tested to see if the knight is higher on her preference list
        ^
Main.java:79: error: illegal character: '#'
            #comparing the current and potential husbands. If the potential
            ^
Main.java:80: error: illegal character: '#'
            #husband is preferred, then their positions are switched, and
            ^
Main.java:81: error: illegal character: '#'
            #the loop breaks. If not, nothing is changed
            ^
Main.java:89: error: illegal character: '#'
#the overall matching algorithm
^
Main.java:91: error: illegal character: '#'
    #the first loop in the matching algorithm, the initialization invariant
    ^
Main.java:92: error: illegal character: '#'
    #is that the length of the array unwedKnights is greater than 0. The
    ^
Main.java:93: error: illegal character: '#'
    #maintenance invariant is that a knight and lady are paired
    ^
Main.java:94: error: illegal character: '#'
    #together and no longer unwed. The termination invariant is
    ^
Main.java:95: error: illegal character: '#'
    #that there are no more unwed knights and ladies
    ^
Main.java:97: error: illegal character: '#'
        #the initialization invariant for this loop is that the knight
        ^
Main.java:98: error: illegal character: '#'
        #is currently unwed. the maintenance invariants are that the
        ^
Main.java:99: error: illegal character: '#'
        #knight is either the maintenance invariants are that the knight
        ^
Main.java:100: error: illegal character: '#'
        #is either the maintenance invariants are that the knight is either
        ^
Main.java:101: error: illegal character: '#'
        #wed to a lady or remains free. the termination invariant is that
        ^
Main.java:102: error: illegal character: '#'
        #the knight is probably married
        ^
Main.java:104: error: illegal character: '#'
            #the part of the matching algorithm that works for one
            ^
Main.java:105: error: illegal character: '#'
            #knight at a time
            ^
Main.java:108: error: illegal character: '#'
#the main function
^
Main.java:112: error: illegal character: '#'
    #calculating the runtime 
    ^
Main.java:115: error: unclosed character literal
    if not inputFile.endswith('.txt'):
                              ^
Main.java:115: error: unclosed character literal
    if not inputFile.endswith('.txt'):
                                   ^
Main.java:121: error: illegal character: '#'
    #printing the runtime
    ^
65 errors
stdout
Standard output is empty