0

I need to write a function called updateHand(hand, word) which does this:

Assumes that 'hand' has all the letters in word. In other words, this assumes that however many times a letter appears in 'word', 'hand' has at least as many of that letter in it.

Updates the hand: uses up the letters in the given word and returns the new hand, without those letters in it.

Has no side effects: does not modify hand.

word: string hand: dictionary (string -> int) returns: dictionary (string -> int)

I wrote the code and everything is working except the fact that when 'hand' is returned, it is not in the same order:

updateHand({'u': 1, 'q': 1, 'a': 1, 'm': 1, 'l': 2, 'i': 1}, 'quail')

{'u': 0, 'i': 0, 'm': 1, 'a': 0, 'l': 1, 'q': 0}

Could someone give me the solution or even just a hint to this problem because I don't understand...

Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0

A dict will not returns the items by insertion order.

What you need is OrderedDict:

from collections import OrderedDict

my_dict = OrderedDict()
my_dict['a'] = 1
...

This concerns only python version < 3.6. From python 3.6, the insertion order is kept.

tetouani63
  • 47
  • 5