Answered step by step
Verified Expert Solution
Link Copied!

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 X,Y,Z. This is generated
randomly from this code:
1 data =[X,Y,Z]
2 d_=[]
3 for _ in range(rn.randint (4,10)):
4 d_+=[[rn.choice(data),rn.choice(data),rn.choice(data)]]
5 print(d_)
One run gives:
1[[Y,Z,Y],[X,X,Z],[Y,Z,Y],[X,X,Z],[
Y,X,Z],[Z,Y,X],[Z,X,Y]]
You will implement the function cnt_harmonic(lst) that returns a dictionary with counts of X,Y,Z
where:
If there is a majority of X,Y, or Z, then that letter gets a single vote.
If there is no majority, i.e., the list is some permutation of [Z,X,Y], 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:
1>>> x ={1:3,2:1,3:1}
2>>> max(x,key=x.get)
31
4>>> x ={1:1,2:1,3:3}
5>>> max(x,key=x.get)
63
This is one caveat. When no maximum exists, it simply returns one of the keys are random:
1>>> x ={1:1,2:1,3:1}
2>>> max(x,key=x.get)
31
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:
1 data =[X,Y,Z]
2 d_=[]
3 for _ in range(rn.randint (4,10)):
4 d_+=[[rn.choice(data),rn.choice(data),rn.choice(data)]]
5 print(d_)
6 print(cnt_harmonic(d_))
producing
1[[X,Z,X],[Y,Z,X],[Z,X,Y],[X,Z,Y],[
Y,Z,X],[Y,Z,X],[Y,Z,X],[X,Y,Z],
[Z,X,X],[Y,Y,Y]]
2{X: 2,Y: 1,Z: 0}
Requirements
1. We only use letters X,Y,Z.
2. You are allowed to use max() and count().
def cnt_harmonic(lst):

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students also viewed these Databases questions

Question

What is a learning curve and how can it be used?

Answered: 1 week ago

Question

What's the wildcard mask of 192.168.223.0 255.255.224.0?

Answered: 1 week ago

Question

What are the major social responsibilities of business managers ?

Answered: 1 week ago

Question

What are the skills of management ?

Answered: 1 week ago

Question

What is the basis for Security Concerns in Cloud Computing?

Answered: 1 week ago

Question

Describe the three main Cloud Computing Environments.

Answered: 1 week ago