Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Maximum Occurrence Letter
#3
Using collections is probably cheating, but using a dict might be fine:
>>> def max_char(string):
...     chars = {string.count(ch): ch for ch in string}
...     most_used = max(chars.keys())
...     return chars[most_used]
...
>>> max_char("foo bar")
'o'
>>> max_char("spam eggs")
's'
>>> max_char("on a bright sunny day in august")
' '
The important part, is that you actually understand how it works, whichever one you turn in.
Reply


Messages In This Thread
Maximum Occurrence Letter - by GalaxyCR - Nov-27-2017, 02:08 PM
RE: Maximum Occurrence Letter - by DeaD_EyE - Nov-27-2017, 02:43 PM
RE: Maximum Occurrence Letter - by nilamo - Nov-27-2017, 09:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get unique entries in a list and the count of occurrence james2009 5 2,995 May-08-2022, 04:34 AM
Last Post: ndc85430
  Selecting the first occurrence of a duplicate knight2000 8 5,225 May-25-2021, 01:37 AM
Last Post: knight2000
  Checking for one or more occurrence in a list menator01 3 2,695 May-18-2020, 06:44 AM
Last Post: DPaul
  count occurrence of numbers in a sequence and return corresponding value python_newbie09 6 3,490 May-20-2019, 06:33 PM
Last Post: python_newbie09
  Word co-occurrence matrix for a string (NLP) JoeB 2 11,643 Feb-27-2018, 11:21 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020