Guest User

Untitled

a guest
Jan 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. string = "a11aawww1cccertgft1tzzzzzz1ggg111"
  2. mylist_char = []
  3. mylist_count = []
  4. char = None
  5. count = 0
  6.  
  7. for ch in string:
  8. temp_char = ch
  9. temp_count = 0
  10. for ch1 in string:
  11. if temp_char == ch1:
  12. temp_count += 1
  13.  
  14. if temp_count > count:
  15. count = temp_count
  16. char = temp_char
  17. mylist_char.append(char)
  18. mylist_count.append(count)
  19. for x in range(len(string)):
  20. for ch in string:
  21. temp_char = ch
  22. temp_count = 0
  23. for ch1 in string:
  24. if temp_char == ch1:
  25. temp_count += 1
  26. if temp_count == count and not(temp_char in mylist_char):
  27. mylist_char.append(temp_char)
  28. mylist_count.append(temp_count)
  29. for x in range(len(mylist_char)):
  30. print("Character", mylist_char[x], "occurred", mylist_count[x], "times")
  31.  
  32. string = "a11aawww1cccertgft1tzzzzzz1ggg111"
  33.  
  34. dictionary = {}
  35.  
  36. for character in list(string):
  37. if character in dictionary:
  38. dictionary[character] += 1
  39. else:
  40. dictionary[character] = 1
  41.  
  42. results = []
  43.  
  44. for key, value in dictionary.items():
  45. results.append((value, key))
  46.  
  47. for value, key in reversed(sorted(results)):
  48. print("Character", key, "occurred", value, "times")
  49.  
  50. # Most frequent Character
  51.  
  52. def most_frequent(a_string):
  53.  
  54. # Create a string of symbols to exclude from counting.
  55. symbols = ' ,.-/?'
  56. characters = []
  57. characters_count = []
  58.  
  59. # Check each individual character in the string.
  60. for ch in a_string:
  61.  
  62. # Check that the character is not one of the symbols.
  63. if ch not in symbols:
  64.  
  65. # If its not and we haven't seen it already,
  66. # append it to the characters list.
  67. if ch not in characters:
  68. characters.append(ch)
  69.  
  70. # And in the same index in the characters_count list
  71. characters_count.append(1)
  72. else:
  73. # If it is in the characters list, find its index
  74. # and add 1 to the same index at characters_count
  75. position = characters.index(ch)
  76. characters_count[position] = characters_count[position] + 1
  77. # find the largest value in the character_count list, it's index
  78. # and show the character at the same index at the characters list.
  79. print(characters[characters_count.index(max(characters_count))])
  80.  
  81. def main():
  82. # Get a string from the user.
  83. text = input('Give me some text and I will find you the most frequent character: ')
  84. most_frequent(text)
  85.  
  86. # Call main
  87. main()
Add Comment
Please, Sign In to add comment