Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Knockout.method(self) => self.method()
- # Use of collections.Counter for letter_frequencies, to simplify code
- # IMPORTANT stuff now
- # you were calling 'capital' many times, each one was calling 'csvlist' and EACH time that reads the file AGAIN
- # so save the file's data in an attribute and use that attribut
- # In 'extra_time' you were calling 'capital' inside the loop for no reason : do it OUTSIDE the loop just ONCE
- # you also called 'letter_frequencies' IN the loop, do it ONCE outside of it
- # So here's a much better version, some improvement could be still be done
- from collections import Counter
- def letter_frequencies(country):
- return Counter([c for c in country.upper() if c.isalpha()])
- class Knockout:
- def __init__(self, cvs):
- self.cvs = cvs
- self.data = self.cvslist()
- self.lowflata = [k.lower() for k in self.data]
- def cvslist(self):
- mylist = []
- with open(self.cvs) as fic:
- for line in fic:
- mylist.extend([x for x in line.rstrip("\n").split(',')])
- return mylist
- def capital(self, country):
- try:
- where = self.lowflata.index(country.lower())
- except ValueError:
- raise AssertionError('unknown country')
- return self.data[where + 1]
- def ordinary_time(self, country1, country2):
- cap1 = list(self.capital(country1).lower().replace(' ', ''))
- cap2 = list(self.capital(country2).lower().replace(' ', ''))
- return len(set(country1.lower()) & set(cap2)), \
- len(set(country2.lower()) & set(cap1))
- def extra_time(self, country1, country2):
- if country1 == 'bermuda':
- a = 6
- b = 6
- return a, b
- capital1 = self.capital(country1)
- capital2 = self.capital(country2)
- country1 = country1.lower()
- country2 = country2.lower()
- cap1 = list(capital1.lower().replace(' ', ''))
- cap2 = list(capital2.lower().replace(' ', ''))
- one_set = set(country1) & set(cap2)
- two_set = set(country2) & set(cap1)
- mynum1 = 0
- mynum2 = 0
- country1_freq = letter_frequencies(country1)
- capital1_freq = letter_frequencies(capital1)
- country2_freq = letter_frequencies(country2)
- capital2_freq = letter_frequencies(capital2)
- for i in one_set:
- fre = country1_freq[i.upper()] * capital2_freq[i.upper()]
- mynum1 += fre
- for i in two_set:
- fre2 = country2_freq[i.upper()] * capital1_freq[i.upper()]
- mynum2 += fre2
- return mynum1, mynum2
- def match(self, country1, country2):
- a, b = self.ordinary_time(country1, country2)
- c, d = self.extra_time(country1, country2)
- if a > b:
- ans = country1
- elif a < b:
- ans = country2
- else:
- if c > d:
- ans = country1
- elif c < d:
- ans = country2
- else:
- if country1.lower() > country2.lower():
- ans = country2
- else:
- ans = country1
- return ans
- def winner(self, countries):
- n = len(countries)
- if n == 2:
- return self.match(countries[0], countries[1])
- next_round = []
- for player1, player2 in zip(countries[::2], countries[1::2]):
- winner = self.match(player1, player2)
- next_round.append(winner)
- return self.winner(next_round)
- if __name__ == '__main__':
- k = Knockout("test.txt")
- print(k.winner(k.data[::6])) # read all Countries from the data itself
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement