Advertisement
Guest User

Untitled

a guest
Apr 11th, 2021
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.68 KB | None | 0 0
  1. # Knockout.method(self) => self.method()
  2. # Use of collections.Counter for letter_frequencies, to simplify code
  3.  
  4. # IMPORTANT stuff now
  5. # you were calling 'capital' many times, each one was calling 'csvlist' and EACH time that reads the file AGAIN
  6. # so save the file's data in an attribute and use that attribut
  7.  
  8.  
  9. # In 'extra_time' you were calling  'capital' inside the loop for no reason : do it OUTSIDE the loop just ONCE
  10. # you also called 'letter_frequencies'  IN the loop, do it ONCE outside of it
  11.  
  12. # So here's a much better version, some improvement could be still be done
  13.  
  14.  
  15. from collections import Counter
  16.  
  17.  
  18. def letter_frequencies(country):
  19.     return Counter([c for c in country.upper() if c.isalpha()])
  20.  
  21.  
  22. class Knockout:
  23.     def __init__(self, cvs):
  24.         self.cvs = cvs
  25.         self.data = self.cvslist()
  26.         self.lowflata = [k.lower() for k in self.data]
  27.  
  28.     def cvslist(self):
  29.         mylist = []
  30.         with open(self.cvs) as fic:
  31.             for line in fic:
  32.                 mylist.extend([x for x in line.rstrip("\n").split(',')])
  33.         return mylist
  34.  
  35.     def capital(self, country):
  36.         try:
  37.             where = self.lowflata.index(country.lower())
  38.         except ValueError:
  39.             raise AssertionError('unknown country')
  40.         return self.data[where + 1]
  41.  
  42.     def ordinary_time(self, country1, country2):
  43.         cap1 = list(self.capital(country1).lower().replace(' ', ''))
  44.         cap2 = list(self.capital(country2).lower().replace(' ', ''))
  45.         return len(set(country1.lower()) & set(cap2)), \
  46.                len(set(country2.lower()) & set(cap1))
  47.  
  48.     def extra_time(self, country1, country2):
  49.         if country1 == 'bermuda':
  50.             a = 6
  51.             b = 6
  52.             return a, b
  53.         capital1 = self.capital(country1)
  54.         capital2 = self.capital(country2)
  55.  
  56.         country1 = country1.lower()
  57.         country2 = country2.lower()
  58.  
  59.         cap1 = list(capital1.lower().replace(' ', ''))
  60.         cap2 = list(capital2.lower().replace(' ', ''))
  61.  
  62.         one_set = set(country1) & set(cap2)
  63.         two_set = set(country2) & set(cap1)
  64.  
  65.         mynum1 = 0
  66.         mynum2 = 0
  67.  
  68.         country1_freq = letter_frequencies(country1)
  69.         capital1_freq = letter_frequencies(capital1)
  70.         country2_freq = letter_frequencies(country2)
  71.         capital2_freq = letter_frequencies(capital2)
  72.  
  73.         for i in one_set:
  74.             fre = country1_freq[i.upper()] * capital2_freq[i.upper()]
  75.             mynum1 += fre
  76.  
  77.         for i in two_set:
  78.             fre2 = country2_freq[i.upper()] * capital1_freq[i.upper()]
  79.             mynum2 += fre2
  80.         return mynum1, mynum2
  81.  
  82.     def match(self, country1, country2):
  83.         a, b = self.ordinary_time(country1, country2)
  84.         c, d = self.extra_time(country1, country2)
  85.         if a > b:
  86.             ans = country1
  87.         elif a < b:
  88.             ans = country2
  89.         else:
  90.             if c > d:
  91.                 ans = country1
  92.             elif c < d:
  93.                 ans = country2
  94.             else:
  95.                 if country1.lower() > country2.lower():
  96.                     ans = country2
  97.                 else:
  98.                     ans = country1
  99.         return ans
  100.  
  101.     def winner(self, countries):
  102.         n = len(countries)
  103.         if n == 2:
  104.             return self.match(countries[0], countries[1])
  105.         next_round = []
  106.         for player1, player2 in zip(countries[::2], countries[1::2]):
  107.             winner = self.match(player1, player2)
  108.             next_round.append(winner)
  109.         return self.winner(next_round)
  110.  
  111.  
  112. if __name__ == '__main__':
  113.     k = Knockout("test.txt")
  114.     print(k.winner(k.data[::6]))  # read all Countries from the data itself
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement