Help me with python code | Sololearn: Learn to code for FREE!

+1

Help me with python code

You need to process an input string accepted from standard input. Find the frequency of occurrence of the tokens that are present in the string. Consider tokens to be sequence of printable characters separated by space(s). Calculate the maximum frequency and print the token with that frequency. If there are multiple tokens of same frequency as that maximum frequency, then print that token which occurred first in the original string. Input: You need to read a line from STDIN Output: Print the token having the maximum frequency (following the constraint mentioned above) Test Cases: Input: w1 w2 w3 w1 w4 w2 w3 w1 w1 Output: w1

9/27/2021 4:50:46 AM

Màñøj

15 Answers

New Answer

+12

Aleksei Radchenkov , you are showing a really nice code to help someone solving a task. we all appreciate this very much!  (this is my personal statement - but it is not to criticize someone !!) but there is an issue by giving a ready solution, when the op has not done and shown his attempt first. as a part of the community i believe that learning by doing is more helpful, than just using a shared solution. we should act more as a mentor.  it is a very successful way when we are giving hints and help, so that the op is able to solve his task by himself. it would be great if you are going to help us to keep sololearn what it is: ▪︎a unique place to learn (we are a "self" learning platform!) ▪︎an absolutely great community that is willing to help other people  ▪︎a great resource of people which has an unbelievable knowledge in coding and a long lasting experience ▪︎a place to meet people and to discuss with them all the possible points of view thanks for your understanding

+5

Manoj , there is very simple way to solve it, just one line of code. but we are still waiting to see your attempt first...

+2

Lothar Yeah of course you are correct bro))

+2

this is the code i mentioned: inp = input().split() # "w1 w2 w3 w1 w4 w2 w3 w1 w1" => "w1", "w2 w3 w1 w4 w2 w3 w1 w1 w2 w2 w1" => "w2" print(max(inp, key=inp.count))

+1

Post your code in the code playground and link it here. https://www.sololearn.com/discuss/333866/?ref=app https://www.sololearn.com/discuss/1316935/?ref=app

+1

Manoj, show us your attempt first. Don't ask us to do it for you.

+1

Nested for loop is a workaround but it's never the only solution. It would have a huge impact on performance as the size of the list increases. I'd suggest looping through the list once and storing each item in a dictionary. Furthermore, it's easier to check what exactly has appeared the most and for those cases when there are 2 and more items of the same amount.

+1

Nick, yeah bro I know;) I did it cuz it's the quickest way to do it.... Obviously writing it in a dictionary is way better for performance... I just wanted to make it as quick as possible))) There are a thousand better ways to do it.....

+1

Sorry guys I don't know about this platform, next time onwards definitely I'll post qus with my attempt

+1

Manoj n=str(input()).split() max="" for i in n: if n.count(i)>n.count(max): max=i print(max)

+1

MH.Hemmat , it is still nested loops, so performance will be awful for big strings. As Nick said adding elements to the dictionary first is way better.

+1

Aleksei Radchenkov Manoj Nick ''' > without use max() method of dictionary. > Find the largest items that have the most repeat. ''' text = str(input()).split() dict = {} max=1 for n in text: dict[n]=text.count(n) if dict.get(n)>max: max=dict[n] for n in dict: if dict[n]==max: print(n,end=' ')

0

This shall work: str = input() nstr = str.split(' ') max = 0 mn = "" for i in nstr: c = 0 for j in nstr: if i == j: c+=1 if c > max: mn = i max = c print(mn) But next time post your attempt here first ;)

0

Python has built-in function for this problem: from collections import Counter a= input().split() count = Counter(a) m = max(count.values()) print(list(count.keys())[list(count.values()).index(m)])

-2

some things that ur saying arnt makeing sense to me because im a begginer