INTERVIEW QUESTION - Count the frequency of words appearing in a string Using Python

Sdílet
Vložit
  • čas přidán 22. 01. 2020
  • INTERVIEW QUESTION - Count the frequency of words appearing in a string Using Python
    GitHub Link :- github.com/netsetos/python_co...
    ~-~~-~~~-~~-~
    Please watch: "LRU Cache (With Python Code) "
    • LRU Cache Implementati...
    ~-~~-~~~-~~-~

Komentáře • 45

  • @safariyakm
    @safariyakm Před 4 měsíci +9

    d={}
    s=str(input("enter a string:" ))
    sp=s.split()
    for i in sp:
    d[i]=sp.count(i)
    print(d)

  • @khetrabasitechvideo362
    @khetrabasitechvideo362 Před 2 lety +2

    Thanks for quality videos

  • @sharmishthadevi7637
    @sharmishthadevi7637 Před 3 lety +2

    Awesome video

  • @aminemixes9304
    @aminemixes9304 Před 2 lety +4

    you can just do this :
    str = input("Enter a string: ")
    li=str.split()
    for val in li:
    con=li.count(val)
    print(f" {val} : {con}")

    • @learnvik
      @learnvik Před 2 lety +3

      this doesn't work, it prints the occurrence twice of a single word. like if 'hello' is twice:
      it will show
      hello : 2
      hello : 2

    • @kiranhinge22
      @kiranhinge22 Před rokem

      very easy...it works

  • @techiewithcamera
    @techiewithcamera Před 10 měsíci +2

    My Solution:
    def f_words(s):
    dict={}
    for i in s:
    if i in dict.keys():
    dict[i]+=1
    else:
    dict[i]=1
    print(dict)

  • @Honey12682
    @Honey12682 Před 4 měsíci

    a=input().split(" ")
    c=dict()
    for i in a:
    c[i]=a.count(i)
    print(c)

  • @kamleshnizare4633
    @kamleshnizare4633 Před 2 měsíci

    s = input('Enter your string')
    output = { key:s.count(key) for key in s.split() }

  • @shantanuwanare5486
    @shantanuwanare5486 Před 2 lety +11

    To count frequency of letters : -
    def frequency_wor():
    s = input("Enter the Letter : ")
    d = {}
    for i in s:
    if i in d:
    d[i] = d[i] + 1
    else:
    d[i] = 1
    print(d)
    frequency_wor()

    • @necronlord52
      @necronlord52 Před 5 měsíci

      Yes, this one seems more elegant. Key initialization is an overkill even for beginners.

  • @nothymasango8331
    @nothymasango8331 Před 8 měsíci

    def Counting (sentence : str):
    results = {}
    string_list = sentence.strip().split()
    for i in string_list:
    results[i] = string_list.count(i)
    return results
    input = " Nothile loves eating mango apple , banana and aslo apple"
    print( Counting(input))

  • @pannagabm2100
    @pannagabm2100 Před 6 měsíci

    Thank you❤

  • @siddhigolatkar8558
    @siddhigolatkar8558 Před 7 měsíci

    Thank you

  • @shailesh__learning3848

    Thanks ..

  • @hbTrendsIn
    @hbTrendsIn Před 6 měsíci

    import re
    sentence = input("Enter a sentence: ")
    words = re.findall(r'\b\w+\b', sentence)
    word_counts = {}
    for word in words:
    count = sentence.count(word)
    word_counts[word] = count
    word_counts = {word: count for word, count in word_counts.items() if count > 1}
    print(word_counts)

  • @SumitKumar-ls6bq
    @SumitKumar-ls6bq Před 2 lety +1

    Well, if you want you may not add, "d.keys()", instead you can use only d.

  • @shantanuwanare5486
    @shantanuwanare5486 Před 2 lety +3

    def frequency_wor():
    str = input("Enter the words : ")
    sp = str.split()
    d = {}
    for i in sp:
    if i in d:
    d[i] = d.get(i) + 1
    else:
    d[i] = 1
    print(d)
    frequency_wor()

  • @andypandy1ify
    @andypandy1ify Před rokem +2

    Two liner:
    def freq_words():
    l=[i.strip(".").strip(".") for i in input("Enter a string: ").split()]
    for w in set(l): print(f"'{w}' : {l.count(w)}", end=", ")

    • @necronlord52
      @necronlord52 Před 5 měsíci

      Not a Pythonian way - explicit should be preferred over implicit, simple over complicted. It will work, probably. But you shouldn't nest operations without any necessity.

    • @andypandy1ify
      @andypandy1ify Před 5 měsíci

      @@necronlord52 I disagree. The fewer the lines of code, the better. The time complexity and the space complexity of my solution would be smaller than a longer solution - my solution solves the problem quicker and uses less resources

  • @user-qx8xn7et5l
    @user-qx8xn7et5l Před 5 měsíci +1

    a = input('enter string:')
    c={}
    for i in a:
    if i in c:
    c[i] += 1
    else:
    c[i] =1
    print(c)
    this is working in simple way

    • @shaktipattnayak
      @shaktipattnayak Před měsícem

      You need to split the string before iterating.

  • @hamzaahmad3387
    @hamzaahmad3387 Před rokem

    import re
    from collections import Counter
    s = s.strip('.')
    a = list(re.split('[.
    ]',s))
    a.remove('')
    a = Counter(a)
    for k,v in a.items():
    print(k,v)

  • @MrMukulpandey
    @MrMukulpandey Před 2 lety +1

    mam thats great...please keep making this videos as it will help us alot.

  • @ideatv2389
    @ideatv2389 Před 6 měsíci

    details explanation before go into short way.

  • @sumedhasen4534
    @sumedhasen4534 Před 3 lety +1

    It's a good tutorial. But ma'am can we consider fullstop as a word? Or can any other punctuation marks be considered as word?? please reply

    • @hbTrendsIn
      @hbTrendsIn Před 6 měsíci

      import re
      sentence = input("Enter a sentence: ")
      words = re.findall(r'\b\w+\b', sentence)
      word_counts = {}
      for word in words:
      count = sentence.count(word)
      word_counts[word] = count
      word_counts = {word: count for word, count in word_counts.items() if count > 1}
      print(word_counts)

  • @mrpresidentrestro
    @mrpresidentrestro Před rokem +1

    dct = {}
    st = input("")
    lst = st.split()
    for i in lst:
    dct[i] = lst.count(i)

  • @rohittiwari7019
    @rohittiwari7019 Před 9 měsíci +1

    I am not able to understand this solution

  • @PerfectBlue1412
    @PerfectBlue1412 Před 3 lety +2

    yeah I really like the tutorial but what is the intro for?

  • @jekhaarchana8207
    @jekhaarchana8207 Před 3 lety

    How to print this without colan?? I need to print as sheela1loves2mango2.... and so on..

    • @harishparamathmananda1385
      @harishparamathmananda1385 Před 4 měsíci

      #Let's split this words based on space and store it in a list
      Ls = Sentence.split(' ')
      #remove the dot and other symbols
      Found_words = []
      Repeated_times = []
      for word in ls:
      if word not in Found_words:
      Found_words.append(word)
      Repeated_times.append(1)
      else:
      Idx= Found_words.index(word)
      Repeated_times[Idx] = Repeated_times[Idx] + 1
      # now accessing the added words and based on the count greater than one, we are printing the result.
      For index, word in enumerate(Found_words):
      if Repeated_times[index] > 1:
      print(f"{Found_words[index]} founded {Repeated_times[index]}")
      ❤Happy coding 😊

  • @-SUSHMASAI
    @-SUSHMASAI Před 3 lety

    is this code working for anyone when iam trying to execute it not showing output

  • @shubhamchoudhary5461
    @shubhamchoudhary5461 Před 3 lety +6

    I don't think that kind of Questions asked in interviews...

    • @Lj-pk2ee
      @Lj-pk2ee Před 7 měsíci +3

      ye question aa gya bhai, tumhari advice nhi suni, toh kr diya 😂

    • @TorchwoodTurtle11
      @TorchwoodTurtle11 Před 5 měsíci +3

      I've had this exact question in two interviews now

    • @chavviM
      @chavviM Před 3 měsíci +1

      It has been asked for me in internal hiring for QA, QA is getting such questions.

    • @siddarthayadav8825
      @siddarthayadav8825 Před 19 dny +1

      then you don't watch

  • @sudarshansbhat8665
    @sudarshansbhat8665 Před 10 měsíci +1

    Very bad explanation😢