I'm a relatively new coder. I took a few classes in high school, but it's been years, so a lot slipped my mind. I'm trying to write a code where you input text into a text box, click submit, then the program highlights or changes the font color of the most common 1, 2, 3 and 4 letter combinations. So for example, if the input text read "The thick thesaurus thumped around", I want the code to highlight all the "TH"s and have a readout at the bottom that says something like "TH=4". Is something like that even possible? I'm sure this is a really dumb question, but hopefully you guys can help me out. Thanks so much.
In order to solve this problem, you need to define it in sufficient detail to eliminate ambiguities. For example, in this text ...
The thick thesaurus thumped around
... the letter combination,
th, does indeed occur four times. However, the combination,
the, occurs twice, and both those occurrences, of course, include the
th combination. If you intend to highlight or color code the most common combinations, how will you handle such overlaps?
Hello, again, @johobus28 ,
# LetterCombinations.py # March 18, 2016 one = dict() # dictionary of one-letter combination frequencies two = dict() # dictionary of two-letter combination frequencies three = dict() # dictionary of three-letter combination frequencies four = dict() # dictionary of four-letter combination frequencies text = input("Enter the text: ") text = text.lower() # make this case-insensitive # one-letter combinations for i in range(len(text) - 0): slice = text[i: i + 1] if slice.isalpha(): one[slice] = one.get(slice, 0) + 1 # two-letter combinations for i in range(len(text) - 1): slice = text[i: i + 2] if slice.isalpha(): two[slice] = two.get(slice, 0) + 1 # three-letter combinations for i in range(len(text) - 2): slice = text[i: i + 3] if slice.isalpha(): three[slice] = three.get(slice, 0) + 1 # four-letter combinations for i in range(len(text) - 3): slice = text[i: i + 4] if slice.isalpha(): four[slice] = four.get(slice, 0) + 1 # display dictionary contents print(one) print(two) print(three) print(four)