Given a string as input . Print the frequncy of each character in the string in alphabetical order of charactersConstraints:Input format :s1 -> string as input Output format :List of all characters along with its frequency in string Example:Input 1:googleOutput 1:['e', 1] ['g', 2] ['l', 1] ['o', 2]Input 2:PythonOutput 2:['P', 1] ['h', 1] ['n', 1] ['o', 1] ['t', 1] ['y', 1] Explanation:In first test case the input is google. In string 'google' character 'g' is repeated for 2 times and similarly all other frequencies are displayed in alphabetical orderPublic Test Cases:# INPUT EXPECTED OUTPUT1 google['e', 1] ['g', 2] ['l', 1] ['o', 2] 2 Python['P', 1] ['h', 1] ['n', 1] ['o', 1] ['t', 1] ['y', 1] Run CodeSubmit Code123456s=input()s=list(s)s.sort()l2=[]for i in range(len(s)): OutputCustom InputINPUT EXPECTED OUTPUT YOUR OUTPUT STATUSgoogle['e', 1] ['g', 2] ['l', 1] ['o', 2] ['e', 'g', 'g', 'l', 'o', 'o']Python['P', 1] ['h', 1] ['n', 1] ['o', 1] ['t', 1] ['y', 1] ['P', 'h', 'n', 'o', 't', 'y']Clear ResponseSave & Next
Question
Given a string as input . Print the frequncy of each character in the string in alphabetical order of charactersConstraints:Input format :s1 -> string as input Output format :List of all characters along with its frequency in string Example:Input 1:googleOutput 1:['e', 1] ['g', 2] ['l', 1] ['o', 2]Input 2:PythonOutput 2:['P', 1] ['h', 1] ['n', 1] ['o', 1] ['t', 1] ['y', 1] Explanation:In first test case the input is google. In string 'google' character 'g' is repeated for 2 times and similarly all other frequencies are displayed in alphabetical orderPublic Test Cases:# INPUT EXPECTED OUTPUT1 google['e', 1] ['g', 2] ['l', 1] ['o', 2] 2 Python['P', 1] ['h', 1] ['n', 1] ['o', 1] ['t', 1] ['y', 1] Run CodeSubmit Code123456s=input()s=list(s)s.sort()l2=[]for i in range(len(s)): OutputCustom InputINPUT EXPECTED OUTPUT YOUR OUTPUT STATUSgoogle['e', 1] ['g', 2] ['l', 1] ['o', 2] ['e', 'g', 'g', 'l', 'o', 'o']Python['P', 1] ['h', 1] ['n', 1] ['o', 1] ['t', 1] ['y', 1] ['P', 'h', 'n', 'o', 't', 'y']Clear ResponseSave & Next
Solution
Here is a Python solution for the problem:
def char_frequency(str1):
dict = {}
for n in str1:
keys = dict.keys()
if n in keys:
dict[n] += 1
else:
dict[n] = 1
return dict
s = input()
s = list(s)
s.sort()
freq_dict = char_frequency(s)
for key in sorted(freq_dict.keys()):
print([key, freq_dict[key]])
This program works by first defining a function char_frequency that takes a string and returns a dictionary where the keys are the characters in the string and the values are the frequencies of those characters.
Then, it takes an input string, converts it to a list of characters, and sorts it.
It then calls char_frequency on this sorted list to get the frequency dictionary.
Finally, it iterates over the keys of this dictionary in sorted order, printing each key and its corresponding value.
Similar Questions
Given a string as input . Print the frequncy of each character in the string in alphabetical order of characters
Define a function print_all_chars(string1). The function prints all the characters in a given string using while loop and return the number of charachters in the string.Exampleprint_all_chars('python') -> 6> p> y> t> h> o> nHintlen() returns the length of a string.the element on index i can be accessed by using string[i].
Write a python program to count the number of characters of alphabets 'O' and 'i' from the given file
Write a program to print each character of a string in incremental order.Write a program to print each character of a string in incremental order as shown in the sample test cases.Sample Test CasesTest Case 1:Expected Output:str:·Keyincremental·order:·KKeKeyTest Case 2:Expected Output:str:·Pythonincremental·order:·PPyPytPythPythoPython
Given a string without duplicates, print all the permutations of the string in a lexicographically order.Input FormatFirst line of input contains T - number of test cases. Its followed by T lines, each contains a single string consisting only of lowercase Enghish alphabets.ConstraintsFor each test case, print the test case number as shown, followed by all the permutations of the string in a lexicographically order, separated by newline.Output Format1 <= T <= 1001 <= len(str) <= 8Sample Input 02algodsSample Output 0Test Case #1:agloagolalgoalogaoglaolggalogaolglaogloagoalgolalagolaoglgaolgoaloaglogaoagloalgogaloglaolagolgaTest Case #2:dssd
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.