Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class UrlShortener
- {
- public static readonly string Alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
- public static readonly int Base = Alphabet.Length;
- public static string Encode(int i)
- {
- if (i == 0) return Alphabet[0].ToString();
- var s = string.Empty;
- while (i > 0)
- {
- s += Alphabet[i % Base];
- i = i / Base;
- }
- return string.Join(string.Empty, s.Reverse());
- }
- public static int Decode(string s)
- {
- var i = 0;
- foreach (var c in s)
- {
- i = (i * Base) + Alphabet.IndexOf(c);
- }
- return i;
- }
- }
- public class Program
- {
- public static void main()
- {
- // Insert URL to be shortened into database and get it's record ID
- // e.g. "http://www.foo.bar" => 155885
- // Encode 155885 which results in the string "dmkf"
- // This is the parameter to your Url shortener
- // e.g. "http://www.url.me/dmkf"
- // Decode the parameter to get the original record ID
- // Redirect the user to the URL represented by the record ID
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement