Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. void main() {
  2. assert(identical(score(''), 0));
  3. assert(identical(score('A'), 1));
  4. assert(identical(score('AED'), 4));
  5. assert(identical(score('DG'), 4));
  6. assert(identical(score('BCMP'), 12));
  7. assert(identical(score('QA'), 11));
  8. }
  9.  
  10. int score(String str) {
  11. int score = 0;
  12. //Map declaration and definition
  13. Map<List<String>, int> scrabbleDat = {
  14. //list as a key and int as a value
  15. ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T']: 1,
  16. ['D', 'G']: 2,
  17. ['B', 'C', 'M', 'P']: 3,
  18. ['F', 'H', 'V', 'W', 'Y']: 4,
  19. ['K']: 5,
  20. ['J', 'X']: 8,
  21. ['Q', 'Z']: 10
  22. };
  23.  
  24. for (int i = 0; i < str.length; ++i) {
  25. //Trying to access map based on the str parameter
  26. //Adding the value returned to score variable
  27. score += scrabbleDat[scrabbleDat.keys.firstWhere((list) => list.contains(str[i]))];
  28. }
  29. return score; // returning the score
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement