Answered step by step
Verified Expert Solution
Question
1 Approved Answer
You are given a list of lists. Each member is a list of combination of X , Y , Z . This is generated randomly
You are given a list of lists. Each member is a list of combination of XYZ This is generated
randomly from this code:
data XYZ
d
for in rangernrandint :
drnchoicedatarnchoicedatarnchoicedata
printd
One run gives:
YZYXXZYZYXXZ
YXZZYXZXY
You will implement the function cntharmoniclst that returns a dictionary with counts of XYZ
where:
If there is a majority of XY or Z then that letter gets a single vote.
If there is no majority, ie the list is some permutation of ZXY then no vote is
recorded.
While you could determine the maximum count of the letters using many different approaches,
the Python dictionary has a convenient way to find the key whose value is maximal. It uses the
function max and two parameters as shown in this session:
x :::
maxxkeyxget
x :::
maxxkeyxget
This is one caveat. When no maximum exists, it simply returns one of the keys are random:
x :::
maxxkeyxget
But you can programmatically deal with this. While there isnt any requirement to use this ap
proach, it does make the solution much simpler. Heres a run:
data XYZ
d
for in rangernrandint :
drnchoicedatarnchoicedatarnchoicedata
printd
printcntharmonicd
producing
XZXYZXZXYXZY
YZXYZXYZXXYZ
ZXXYYY
X: Y: Z:
Requirements
We only use letters XYZ
You are allowed to use max and count
def cntharmoniclst:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started