URLs are getting too long. So, you must implement an algorithm to shorten a URL.
i. The Structure of a URL
A URL has 2 main parts: a domain and a path. A domain is the part of the URL before the first slash. You may assume that the URL does not include a protocol. The path is everything else.
ii. The Domain
The domain of a URL will be something like: xkcd.com
meta.codegolf.stackexcchhannnge.cooom
. Each part is period-seperated, e.g. in blag.xkcd.com
, the parts are "blag", "xkcd", and "com". This is what you will do with it:
If it contains more than two parts, put the last two aside and concatenate the first letter of the rest of the parts.
Then, concatenate that to the first letter to the second-to-last part.
Add a period and the second and third letter of the second-to-last part.
Discard the last part.
iii. The Path
The path will be like: /questions/2140/
/1407/
. As before, "parts" are seperated by slashes. For each part in the path, do:
Add a slash
If it is completely made of base-ten digits, interpret it as a number and convert to a base-36 integer.
Otherwise, add the first letter of the part.
At the end, add a slash.
iv. Misc.
- This is code-golf, so shortest code wins.
- The path can be empty, but the URL will always end with a slash.
- There will not be a protocol (e.g.
http://
,file:///
) - There will never be less than two parts in the domain.
- Standard loopholes apply.
Examples
In: xkcd.com/72/
Out: x.kc/20/
In: math.stackexchange.com/a/2231/
Out: ms.ta/a/1pz/
In: hello.org/somecoolcodeintrepreteriijjkk?code=3g3fzsdg32,g2/
Out: h.el/s/
kk
and everything starting with?
is a query string, which shouldn't end with a slash? Also not all URLs will end with a slash/
, likewww.something.com/path
. Or is this irrelevant for the purpose of this challenge? \$\endgroup\$