Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Counting Letters in a String

Write a program or function that takes in a string and outputs a count of each modern English alphabet letter in the string, case-insensitive.

Input: A string consisting of printable ASCII characters (code points 32-126).

Output: A list of pairs, where each pair consists of a letter and its count in the string. The list should be sorted in alphabetical order by letter. The list shall omit letters occurring zero times.

Test Cases

Input: "hello world"
Output: [('d', 1), ('e', 1), ('h', 1), ('l', 3), ('o', 2), ('r', 1), ('w', 1)]
Input: "The quick brown fox jumps over the lazy dog"
Output: [('a', 1), ('b', 1), ('c', 1), ('d', 1), ('e', 3), ('f', 1), ('g', 1), ('h', 2), ('i', 1), ('j', 1), ('k', 1), ('l', 1), ('m', 1), ('n', 1), ('o', 4), ('p', 1), ('q', 1), ('r', 2), ('s', 1), ('t', 2), ('u', 2), ('v', 1), ('w', 1), ('x', 1), ('y', 1), ('z', 1)]

Code Golf Specifics:

Your goal is to write the shortest possible code in bytes that correctly solves the problem. Your code must be able to handle any valid input within the given constraints. Note that your code should treat uppercase and lowercase letters as equivalent.

Good luck!

Answer*

Draft saved
Draft discarded
Cancel
6
  • 1
    \$\begingroup\$ Thanks! Actually, I was including the assignment in the header, since I've seen other examples of that. I've now moved it to back to the code and updated the byte count. \$\endgroup\$ Commented Mar 27, 2023 at 13:43
  • 1
    \$\begingroup\$ This would be fine, since it is an anonymous function, and the header is just to store it, but what you did was a snippet, not a function (not valid Julia code by itself). If other answers are not valid, I try to point it out (not many people check Julia answers) \$\endgroup\$ Commented Mar 28, 2023 at 15:43
  • 1
    \$\begingroup\$ It is probably golfable further, but this works for 62 bytes \$\endgroup\$ Commented May 10, 2023 at 10:33
  • 1
    \$\begingroup\$ -6 bytes by avoiding the way too long uppercase and using Pairs instead of Tuples Try it online! \$\endgroup\$ Commented May 18, 2023 at 19:07
  • 1
    \$\begingroup\$ @MarcMush Brilliant! I can improve several of my other posts with those suggestions. I used your ideas to trim 11 bytes off my 62 byte solution, so now it's the best so far. \$\endgroup\$ Commented May 24, 2023 at 23:11

AltStyle によって変換されたページ (->オリジナル) /