From 7387456d4ff937ecddedb87174c903ba75448090 Mon Sep 17 00:00:00 2001 From: SoumadeepChoudhury <87203832+SoumadeepChoudhury@users.noreply.github.com> Date: Fri, 13 Aug 2021 00:33:06 +0530 Subject: [PATCH] Change the program to a simple one . Changed the program to a simple one from the use of a dictionary .. This code will run faster than before. --- Programs/P06_CharCount.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Programs/P06_CharCount.py b/Programs/P06_CharCount.py index ae13486..35a94b2 100644 --- a/Programs/P06_CharCount.py +++ b/Programs/P06_CharCount.py @@ -4,14 +4,11 @@ def charFrequency(userInput): '''This fuction helps to count the char frequency in the given string ''' userInput = userInput.lower() #covert to lowercase - dict = {} - for char in userInput: - keys = dict.keys() - if char in keys: - dict[char] += 1 - else: - dict[char] = 1 - return dict + count=0 + while userInput !='': + count=userInput.count(userInput[0]) + print(userInput[0],": ",count ) + userInput=userInput.replace(userInput[0],'') if __name__ == '__main__': userInput = str(input('Enter a string: '))