fork download
  1. from collections import defaultdict
  2.  
  3. def check_letters(s):
  4. d = {'M': 2, 'I': 9, 'V': 2, 'F': 2, 'J': 1, 'G': 3, 'T': 6, 'D': 4,'P': 2,
  5. 'Q': 1, 'B': 2, 'Z': 1, 'H': 2, '_': 2, 'X': 1, 'R': 6, 'L': 4, 'N': 6,
  6. 'Y': 2, 'E': 12, 'K': 1, 'S': 4, 'C': 2, 'A': 9, 'U': 4, 'W': 2, 'O': 8}
  7.  
  8. for c in s:
  9. d[c] -= 1
  10. if d[c] < 0:
  11. print("Invalid input. More {}'s have been taken from the bag than possible.".format(c))
  12. return
  13.  
  14. d2 = defaultdict(list)
  15. for p in [(v,k) for k,v in d.items()]:
  16. d2[p[0]].append(p[1])
  17.  
  18. for c in sorted(d2.items(), reverse=True):
  19. print('{}: {}'.format(c[0], ', '.join(sorted(c[1]))))
  20.  
  21. # Test cases:
  22. print('* * * * Test 1:')
  23. check_letters('PQAREIOURSTHGWIOAE_')
  24. print('\n* * * * Test 2:')
  25. check_letters('LQTOONOEFFJZT')
  26. print('\n* * * * Test 3:')
  27. check_letters('AXHDRUIOR_XHJZUQEE')
Success #stdin #stdout 0.02s 10240KB
stdin
Standard input is empty
stdout
* * * * Test 1:
10: E
7: A, I
6: N, O
5: T
4: D, L, R
3: S, U
2: B, C, F, G, M, V, Y
1: H, J, K, P, W, X, Z, _
0: Q

* * * * Test 2:
11: E
9: A, I
6: R
5: N, O
4: D, S, T, U
3: G, L
2: B, C, H, M, P, V, W, Y, _
1: K, X
0: F, J, Q, Z

* * * * Test 3:
Invalid input. More X's have been taken from the bag than possible.