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 |
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))


Execution on samples:
![[sky, yat] 2 count dominating words [pun, ean, fiz, coe] 3 [toph, prow, koku, okey] 2 [ufo, hny, hun](http://img.homeworklib.com/questions/dbc4e7e0-e410-11ea-b1e1-358533b32d40.png?x-oss-process=image/resize,w_560)
Count word dominators def count_word_dominators(words): If you already solved the earlier count_dominators problem, you might notice...