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:
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:
/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.
- 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.
- There will never be less than two parts in the domain.
- Standard loopholes apply.