Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1.  
  2. class Code:
  3.     def __init__(self, code):
  4.     self.code = code
  5.         pass
  6.  
  7.     def get_letter_count(self, letter):
  8.         count=0
  9.     for x  in self.code:
  10.         if letter==x:
  11.             count+=1
  12.         return count
  13.  
  14.     def get_counts(self):
  15.         counts = {}
  16.     for letter in self.code:
  17.         if letter in counts:
  18.             counts[letter]+=1
  19.         else:
  20.             counts[letter]=1
  21.         return counts
  22.  
  23.     def get_exact_matches(self, other):
  24.         matches = 0
  25.     index=0
  26.     while index<len(self.code):
  27.         if self.code[index]==other.code[index]:
  28.             matches+=1
  29.         index+=1
  30.         return matches
  31.  
  32.     def get_letter_matches(self, other):
  33.         matches = 0
  34.     OtherCounts =other.get_counts()
  35.     g=self.get_counts()
  36.     for letter in OtherCounts:
  37.         if letter in g:
  38.             if g[letter]>=OtherCounts[letter]:
  39.                 m=OtherCounts[letter]
  40.                 matches+=m
  41.             else:
  42.                 m=g[letter]
  43.                 matches+=m
  44.         return matches
  45.  
  46.     def get_misplacements(self, other):
  47.     x=self.get_exact_matches(other)
  48.     y=self.get_letter_matches(other)
  49.     z=y-x
  50.     if z<0:
  51.         return 0
  52.     else:
  53.             return z
  54.  
  55.     def __repr__(self):
  56.         return str("[" + self.code +"]")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement