28
3
People on this site really love to embellish their post titles...
Stewie's sequence: + * - / + * - /
However, when this title needs to be included in the page URL, it is simplified:
stewies-sequence
The Challenge
Your task is to create a program or function which, given a string representing a post title, outputs/returns its "URL-Friendly" conversion.
The algorithm is:
- Convert to lowercase (where applicable)
- Replace every space (
), period (
.
), comma(,
) or slash (/
) with a dash (-
) - Remove any non-alphanumeric characters, except dashes.
- Minify groups of adjacent dashes (
a---b -> a-b
), remove any that are leading/trailing.
Please note that this algorithm is a simplification, and may not always produce the same results as the site's real method.
Rules
- You can assume that input:
- Will not be empty.
- Will contain at least one alphanumeric character.
- Will only contain characters in the ASCII range 32-126 (printable)
- Full programs or functions are permitted.
- A builtin which does the exact task specification is not permitted.
- This is code-golf, so the shortest solution (in bytes) wins!
Test Cases
Most posts on this site will serve as tests, but here's a handy list:
Loading... Forever -> loading-forever
N(e(s(t))) a string -> nest-a-string
"Hello, World!" -> hello-world
URL-Friendly titles -> url-friendly-titles
C.U.S.R.S -> c-u-s-r-s
1+2+3+4+...+n = -1/12? -> 1234-n-1-12
How can I use cmp(a,b) -> how-can-i-use-cmpa-b
Some longer ones...
Export The $PATH Variable, Line-By-Line -> export-the-path-variable-line-by-line
Do n and n^3 have the same set of digits? -> do-n-and-n3-have-the-same-set-of-digits
Quine Anagrams! (Cops' Thread) -> quine-anagrams-cops-thread
The Golfer Adventure - Chapter 1 -> the-golfer-adventure-chapter-1
Bootloader golf: Brainf*** -> bootloader-golf-brainf
And some edge-case checking samples (feel free to suggest more):
0123 -> 0123
a a1 -> a-a1
2-1=1 -> 2-11
What about leading
-
s? Will they have to be removed? For example inasdf-
, will the last-
have to be removed? – user41805 – 2016-12-10T13:26:11.103Can we use a built-in function to check if the char is alphanumeric like this
if(isalphanum(ch))...
– Mukul Kumar – 2016-12-10T13:27:45.6301@KritixiLithos Minify groups of adjacent dashes (a---b -> a-b), remove any that are leading/trailing. I guess this should make you clear. – Mukul Kumar – 2016-12-10T13:47:16.237
And what about
_
underscores? My code works except when there are underscores. – user41805 – 2016-12-10T13:48:17.027@L3viathan Doesn't matter now, I changed my code so that even underscores will be removed – user41805 – 2016-12-10T14:07:47.497
@MukulKumar yes, that's fine. A built-in is only disallowed if it completes this exact algorithm. – FlipTack – 2016-12-10T14:36:14.267
@KritixiLithos I just did some poking around and it looks like they actually create dashes,, but because I don't want to invalidate answers, so I'll accept any behaviour with underscores. – FlipTack – 2016-12-10T14:39:24.520
Anything about underscores? – OldBunny2800 – 2016-12-10T16:24:47.097
@OldBunny2800 you can either replace them with a dash or treat them as invalid characters. really they should create dashes but I don't want to invalidate all the answers by changing the rules now. – FlipTack – 2016-12-10T16:36:08.897