Skip to content

Instantly share code, notes, and snippets.

@BinaryMuse
Created April 25, 2011 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BinaryMuse/941152 to your computer and use it in GitHub Desktop.
Save BinaryMuse/941152 to your computer and use it in GitHub Desktop.
$ ./php.php
578260286050
58677803167
3520668190027
8542342158446695842
$ ./ruby.rb
578260286050
58677803167
3520668190027
8542342158446695842
#!/usr/bin/env php
<?php
function getIDFromShortenedURL1 ($string, $base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
{
$length = strlen($base);
$size = strlen($string) - 1;
$string = str_split($string);
$out = strpos($base, array_pop($string));
foreach($string as $i => $char)
{
$out += strpos($base, $char) * pow($length, $size - $i);
}
return $out;
}
echo getIDFromShortenedURL1("abcdefg") . "\n";
echo getIDFromShortenedURL1("1234567") . "\n";
echo getIDFromShortenedURL1("ZYXWVUT") . "\n";
echo getIDFromShortenedURL1("ab2165AZefg") . "\n";
?>
#!/usr/bin/env ruby
def getIDFromShortenedURL1(string, base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
length = base.length # $length = strlen($base);
size = string.length - 1 # $size = strlen($string) - 1;
string = string.split '' # $string = str_split($string);
out = base.index string.pop # $out = strpos($base, array_pop($string))
string.each_with_index do |char, i| # foreach($string as $i => $char);
# $out += strpos($base, $char) * pow($length, $size - $i);
out << base.index(char) * (length ** (size - i))
end
out # return $out;
end
puts getIDFromShortenedURL1('abcdefg')
puts getIDFromShortenedURL1('1234567')
puts getIDFromShortenedURL1('ZYXWVUT')
puts getIDFromShortenedURL1('ab2165AZefg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment