Skip to main content
5 of 11
[Edit removed during grace period]
Daniel
  • 6.7k
  • 1
  • 21
  • 58

Golf a Sentence

Everybody loves golfing code. Code is written in English (eh...). Therefore, everybody loves golfing English!

Your task is to write a program or function that takes a string as input, and then returns or prints the golfed version. There will be no newlines or tabs in the input.

This is how you golf a string. The order matters!

  1. remove spaces directly before and after punctuation: "Hey there", he said ; he ' s a cat. -> "Hey there",he said;he's a cat. The following count as punctuation: .,?!"':;()

  2. replace he/she/it is with he's/she's/it's: It is unfortunate that she is not coming ever. -> It's unfortunate that she's not coming ever. Also, He is here should not become He's here; it should remain unchanged.

  3. words that start with capital letters in a sentence can be moved against the preceding word (omitting the space): The farmer told Fred that he should leave. -> The farmer toldFred that he should leave.

  4. trailing and leading whitespace should be removed: [ ]It's raining.[ ]-> It's raining. Ignore the brackets (annoying auto-formatting).

  5. Remove repeated punctuation that is the same: ""Here''s the cake",, said Fred..! -> "Here's the cake",saidFred.!

If we were to golf the first paragraph of this challenge, the output would be:

Everybody loves golfing code.Code is written inEnglish(eh.).Therefore,everybody loves golfingEnglish!

This is code golf––or rather English golf––so shortest code in bytes wins!

Daniel
  • 6.7k
  • 1
  • 21
  • 58