Question

Can u guys help me solve this problem by using Python Write function q1(listOfLists, infoDict). listOfLists...

Can u guys help me solve this problem by using Python

Write function q1(listOfLists, infoDict). listOfLists is a list of zero or more lists of zero or more numbers. infoDict is a dictionary with numbers as keys. The values associated with keys can be of any type.


A number is considered "red" if the number is a key in the dictionary and has value "red". A number is considered "blue" if it is a key in the dictionary and has value "blue". Other numbers, whether or not they exist as keys in the dictionary, are considered "green".

A sublist of listOfLists is considered "red" if contains more red items than blue ones and more red items than green ones. A sublist is considered "blue" if it contains more blue items than red ones and more blue items than green ones. Other sublists are considered "green".

q1 returns a list that is the same length as listOfLists and such that item i is

  • the largest element in listOfLists[i] if listOfLists[i] is red,
  • the smallest element in listOfLists[i] if listOfLists[i] is blue,
  • the most common element in listOfLists[i] if listOfLists[i] is green (if there is a tie for most common, take the smallest one, and use None if there are no elements)

For example

>>> q1([[4], [2,3,3], [1,2,3], [17]], {1:"purple", 2:"red", 3:"blue", 25:"red"})
[4, 2, 1, 17]
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Your given snippet is in python, so I am writing in python

Code-

import collections

def q1(listOfLists:list, infoDict:dict):

    # result array

    res=[]

    # loop over all lists in listOfLists

    for lst in listOfLists:

        # variable to count the reds and blues

        red = 0

        blue = 0

        for ele in lst:

            if infoDict.get(ele, "") == 'red':

                red+=1

            elif infoDict.get(ele, "") == 'blue':

                blue +=1

        if red > blue:

            # If more red, insert larget element to list

            res.append(max(lst))

        elif blue > red:

            # If more blue, insert smallest element

            res.append(min(lst))

        else:

            # else, insert frequent element

            # counter element group same elements with their frequencies

            counter = collections.Counter(lst)

            # get most common element

            res.append(counter.most_common(1)[0][0])

    return res

print(q1([[4], [2,3,3], [1,2,3], [17]], {1:"purple", 2:"red", 3:"blue", 25:"red"}))

OUTPUT-

I hope it helps. For any doubt, feel free to ask in comments, and give upvote if u get the answer.

Edit:

Else block replacement-

else:

            # else, insert frequent element

            # counter element group same elements with their frequencies

            counter={}

            for ele in lst:

                #add elements to list to store their frequencies

                count[ele] = counter.get(ele, 0)+1

            #variable to store most frequent value

            mx_key=-1

            for key, value in counter.items():

                if value> counter.get(mx_key, 0):

                    mx_key = key

            res.append(mx_key)

Add a comment
Know the answer?
Add Answer to:
Can u guys help me solve this problem by using Python Write function q1(listOfLists, infoDict). listOfLists...
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