Answer to Question #298704 in Python for Aman

Question #298704

Write a python function C-F(X) which takes a dictionary containing numbers as input. The function will return a dictionary containing key as a number and value as frequency of how many times the number is repeating in the dictionary. For Ex: 1) {'V': 10, 'VI': 10, 'VII': 40, 'VIII': 20, 'IX': 70, 'X':80, 'XI': 40, 'XII': 20 } is input , then output is { 10: 2 , 20: 2 , 40:2 , 70: 1 }.


2) input: {1: 10, 2: 20, 3: '30', 4: '10', 5: 40, 6: 40} output: {10: 1, 20: 1, '30': 1, '10': 1, 40: 2} .


Function should be general and should work for any finite items dictionary.

1
Expert's answer
2022-02-16T14:54:57-0500
#  in the task in example 1), the value 80 is not calculated 

def C_F(X):
    tempDict = X.copy()
    dic_freq = {}
    for key, val in X.items():
        count = 0
        for key2, val2 in tempDict.items():
            if (val == val2) & (val not in dic_freq.values()):
                count += 1               
        if count > 0:
            dic_freq[val] = count             
    return dic_freq
 
input1 = {'V': 10, 'VI': 10, 'VII': 40, 'VIII': 20, 'IX': 70, 'X': 80, 'XI': 40, 'XII': 20} 
print('input:', input1)
print('output: ', end='')
print(C_F(input1))
print()

input2 = {1: 10, 2: 20, 3: '30', 4: '10', 5: 40, 6: 40}
print('input:', input2)
print('output: ', end='')
print(C_F(input2))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS