Question

Count word dominators def count_word_dominators(words): If you already solved the earlier count_dominators problem, you might notice...

Count word dominators

def count_word_dominators(words):

If you already solved the earlier count_dominators problem, you might notice that even though the problem was originally stated for lists of integers, the logic of domination did not depend on this fact in any way. As long as the elements can be compared with each other for order, the Pythonic spirit of duck typing allows the very same count_dominators function to handle a list of strings just as well as it handles a list of integers! For example, the call count_dominators(['dog', 'emu', 'cat', 'bee']) would return 3, since 'emu', 'cat' and 'bee' dominate all words after them in the lexicographical ordering. If your count_dominators does not pass this hurdle, go back and edit it to have no baked-in assumptions about elements being specifically integers.

However, things become more interesting if we define domination between words of equal length with a rule that says that for a word to dominate another word, for more than half of the positions the character in the first word is strictly greater than the corresponding character in the other word. This definition makes the domination a partial ordering so that, for example, the word 'dog' dominates the word 'cat', but neither word 'dog' and 'arg' dominates the other. Note also the intentional wording "more than half" to break ties between words of even length such as 'aero' and 'tram', so that no two words can possibly both dominate each other.

words

Expected result

['sky', 'yat']

2

['pun', 'ean', 'fiz', 'coe']

3

['toph', 'prow', 'koku', 'okey']

2

['ufo', 'hny', 'hun', 'ess', 'kab']

3

['cagit', 'libri', 'sured', 'birls', 'golgi', 'shank', 'bailo', 'senex', 'cavin', 'ajiva', 'babby']

5

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python3 code:

def count_word_dominators(words):

   # helper function that given two words
   # return whether word1 dominate word2 or not
   def dominate(word1, word2):
       # word1 and word2 should have same length
       if len(word1)!=len(word2):
           return False

      
       num_of_dominating_char=0
       for i in range(len(word1)):
           # 1 is added to num_of_dominating_char whenever character from
           # word1 comes after corresponding character from word2
           if word1[i]>word2[i]:
               num_of_dominating_char+=1

           # 1 is subtracted from num_of_dominating_char whenever character from
           # word1 comes before corresponding character from word2
           elif word1[i]<word2[i]:
               num_of_dominating_char-=1

       # if num_of_dominating_char>0 then more than half of the positions
       # the character in the first word is strictly greater than
       # the corresponding character in the other word
       if num_of_dominating_char>0:
           return True
       else:
           return False

   count_dominating_words=0
   # for every word check whether it dominates all words following it
   for word1_index in range(len(words)):
       word1=words[word1_index]
       is_dominating_word=True

       for word2 in words[word1_index+1:]:
           # check if word1 dominate word2
           # if not word1 can't be word dominator
           if not dominate(word1, word2):
               is_dominating_word=False
               break

       if is_dominating_word:
           count_dominating_words+=1

   return count_dominating_words


# for testing on the given examples
if __name__ == '__main__':
   words=['sky', 'yat']
   print(words, count_word_dominators(words))

   words=['pun', 'ean', 'fiz', 'coe']
   print(words, count_word_dominators(words))

   words=['toph', 'prow', 'koku', 'okey']
   print(words, count_word_dominators(words))

   words=['ufo', 'hny', 'hun', 'ess', 'kab']
   print(words, count_word_dominators(words))

   words=['cagit', 'libri', 'sured', 'birls', 'golgi', 'shank', 'bailo', 'senex', 'cavin', 'ajiva', 'babby']
   print(words, count_word_dominators(words))

i def count_word_dominators (words): 2 3 # helper function that given two words 4 # return whether wordi dominate word2 or no

else: return false count_dominating_words=0 # for every word check whether it dominates all words following it for word1_inde

Execution on samples:

[sky, yat] 2 count dominating words [pun, ean, fiz, coe] 3 [toph, prow, koku, okey] 2 [ufo, hny, hun

Add a comment
Know the answer?
Add Answer to:
Count word dominators def count_word_dominators(words): If you already solved the earlier count_dominators problem, you might notice...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT