We'll round out our week of word processing with a slightly more challenging problem. Today's goal is to create a function that takes in a string and returns the maximum font size (as an integer) usable for that string to fit on 3 or less lines. A line is considered 960px wide. For this problem we will use a monospace font, meaning that all characters are of the same width (e.g. 'l' and 'm' take up the same horizontal space, as do space characters). Words cannot be split in half, so you will need to wrap to the next line if needed. Here is an example:
maxFontSize('Problem of the Day') > 137px
"Problem" is the longest word and can max out line 1 at 960px / 7 characters ≈ 137px/character. "of the" can fit on line 2 with "day" going on line 3. If the problem were to solve for only 2 lines then the longest line would be "of the Day" which would lead to a max font size of 960/10 = 96px.
Comments:
ujjl - 10 years, 7 months ago
Haskell
reply permalink