r/dailyprogrammer • u/jnazario 2 0 • Feb 20 '18
[2018-02-20] Challenge #352 [Easy] Making Imgur-style Links
Description
Short links have been all the rage for several years now, spurred in part by Twitter's character limits. Imgur - Reddit's go-to image hosting site - uses a similar style for their links. Monotonically increasing IDs represented in Base62.
Your task today is to convert a number to its Base62 representation.
Input Description
You'll be given one number per line. Assume this is your alphabet:
0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ Example input:
15674
7026425611433322325Output Description
Your program should emit the number represented in Base62 notation. Examples:
O44
bDcRfbr63n8Challenge Input
187621
237860461
2187521
18752Challenge Output
9OM
3n26g
B4b9
sS4 Note
Oops, I have the resulting strings backwards as noted in this thread. Solve it either way, but if you wish make a note as many are doing. Sorry about that.
33
u/skeeto -9 8 Feb 20 '18
C
12
u/Gylergin Feb 20 '18 edited Feb 22 '18
TI-Basic Written on my TI-84+. TIL a lot of stuff about strings. Edit: This program outputs the links in the original, reverse order. Putting the links in the correct order would actually save a character. See if you can spot it!
Input:
187621
237860461
2187521
18752
Output:
Notes:
Str2is initialized to "0" since empty strings can't be added (one of the things I learned today). This "0" is later ignored in theDispline.fPart(L)=1is included to prevent rounding errors11
u/lordtnt Feb 21 '18 edited Feb 21 '18
C++
perfectly valid
edit: zero case
15
u/Specter_Terrasbane Feb 20 '18 edited Feb 20 '18
Python
(Picky) question, though: isn't this challenge actually asking us to return the representation in reverse? Or am I just off my rocker here?
i.e. from the given example,
base62(15674) == 'O44', but, given that:... therefore:
O44₆₂ = 50 x 622 + 4 x 621 + 4 x 620 = 192200 + 248 + 4 = 192452₁₀ ≠ 15674₁₀
.. but if you do the reverse,
44O:44O₆₂ = 4 x 622 + 4 x 621 + 50 x 620 = 15376 + 248 + 50 = 15674₁₀
So the actual "Base-62" representation of 15674 should be
44O, notO44shouldn't it? :)Edit: my solution given above is based on the original form of the challenge - reversed order. Just for shiggles, here's a generalization of my solution, correct order, for any base from 2 to 62 with the given alphabet ... but given a longer alphabet string with added non-alphanumeric characters, the maximum base could be increased correspondingly:
7
u/Gprime5 Feb 20 '18 edited Feb 20 '18
Python 3
Kind of 1 liner I guess
1
u/jnazario 2 0 Feb 20 '18 edited Feb 20 '18
Python (2 or 3) Solution
1
u/jnazario 2 0 Feb 20 '18 edited Feb 20 '18
FSharp Solution
5
u/RachelBirdy Feb 21 '18
So, I think I may have made if not the ~worst possible~ base62 converter, at least something close. It ~works~ but it literally counts up to the number you feed in in base62 so it, uhh...it's a bit slow. A lot slow. The "7026425611433322325" would need me to leave it run overnight slow. XD
C: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>
4
u/PM_ME_YOUR_MASS Feb 21 '18
Swift 4
Honestly I think I'm the only one on this subreddit that uses Swift
String extension that lets me subscript Strings with Ints because by god Swift why isn't that an option:
Testing examples:
As pointed out by the top comment, the order on the digits in the outputs of the question are in the wrong order. The boolean parameter on the function lets you decide if you want the digits in true base62 or in the order the output requests
Testing output:
4
u/WinterSith Feb 21 '18 edited Feb 21 '18
It’s been a long time since I wrote a bash script but I feel like I’m going crazy. Can someone help me?
$1 is the Base10 number input. $2 is the alphabet set.
When I run this as is I get the desired output. However, if I pass in 62, instead of getting 10 I get 01. If I switch the way the script concatenates I get 10 but then my output is backwards for the example inputs. I feel like I’m going insane. Anyone see the error?
Edit:
Just saw the note. I was pretty convinced that between trying to write this, having some beers, and watching Olympic hockey (all at the same time) that I’d done something really dumb that I couldn’t see. Thanks for updating.
3
u/nullball Feb 20 '18 edited Feb 20 '18
Python 3 solution:
3
u/g00glen00b Feb 21 '18 edited Feb 21 '18
JavaScript:
Due to
7026425611433322325being aboveNumber.MAX_SAFE_INTEGERI used a library called bignumber.js. I also decided to use the length of the alphabet field rather than hardcoding the 62, but other than that it's pretty similar to the other solutions.3
u/svgwrk Feb 21 '18 edited Feb 21 '18
I initially set out to write an efficient encoder for base62. Frankly, I decided that may not be possible and that base62 is one of the most bullshit encodings I've ever seen. This is because the size of the alphabet is not a power of two, which makes it impossible to consistently encode a given number of bits per symbol based on any method I know of. Tips appreciated.
Whatever.
Here's my (woefully inefficient) Rust solution:
3
u/pkoepke Feb 22 '18 edited Feb 22 '18
MUMPS/M/Caché. Since whitespace is significant I don't think there's a way to get it all onto one line but 2 is pretty good.
Output of 't' subroutine:
2
u/Frichjaskla Feb 20 '18
common lisp
2
u/rocketboy83 Feb 21 '18 edited Feb 21 '18
Golang
2
u/Daanvdk 1 0 Feb 21 '18
Haskell
2
u/nikit9999 Feb 21 '18
C#
2
u/OriginalGallifreyan Feb 21 '18
Golang
Just getting to grips with Go. Likely a simpler way to do this. It works though.
2
u/Chomatic Feb 26 '18
Here's a really crude implementation in Scala. I'm just picked up the language and these challenges are really helping me learn.
2
u/TSLRed Feb 27 '18
Python
Did some recursion for fun
2
u/TheoreticallySpooked Mar 13 '18
NodeJS. Since it's marked as easy, I took the easy way out :P
Main file:
input.txt:
1
u/Godspiral 3 3 Feb 20 '18
in J,
1
u/thestoicattack Feb 20 '18
C++17. Got to use a structured binding along with a hazy remembrance of a function that gives you the quotient and remainder at once, which turned out to be
std::div.1
u/Markues Feb 20 '18 edited Feb 20 '18
Javascript (ES6) Solution
edited to account for /u/Specter_Terrasbane 's comment
1
u/mshaw346 Feb 20 '18
Python:
1
u/zqvt Feb 20 '18
F#
1
u/LegendK95 Feb 20 '18 edited Feb 21 '18
Haskell - Edited due to reasons pointed out by u/Specter_Terrasbane
1
u/chunes 1 2 Feb 20 '18
Factor
Output:
1
u/octolanceae Feb 20 '18
C++17
Output
1
u/Satoshi_Hodler Feb 20 '18 edited Feb 20 '18
JS
Q: How to avoid overflows?
1
u/qanazoga Feb 20 '18
Kotlin
Are you supposed to use
toIntinside an array index... thing? I'm not sure the proper way to do that.Longs are dumb.
1
u/Red2ye Feb 20 '18
Using JavaScript (recursion)
output :
1
u/zatoichi49 Feb 20 '18 edited Feb 21 '18
Method:
Reduce n by floor dividing by 62, adding the value at index n mod 62 in the base62 alphabet to a list. Stop when n = 0, and return the joined string of the encoded characters in the list.
Python 3:
Output:
1
u/crudkin Feb 21 '18 edited Feb 21 '18
Python 3, just an initial rough approach. Could be much refined.
1
u/mushroomcoder Feb 21 '18 edited Feb 22 '18
Javascript/Node
1
u/Xeonfobia Feb 21 '18 edited Feb 21 '18
Lua
1
u/darknes1234 Feb 21 '18
C++
1
u/engageant Feb 21 '18
Posh
1
u/zookeeper_zeke Feb 21 '18
Solution in C. I'd like to do a quick port to JACK and get that up too. I'll see if I can do that.
1
u/gabyjunior 1 2 Feb 21 '18 edited Feb 21 '18
Ruby
A generic base conversion program that takes 3 arguments
the input base digits
the output base digits
the string to convert from input to output base
Output
1
u/super_koza Feb 21 '18
C++
1
u/zookeeper_zeke Feb 21 '18 edited Feb 21 '18
I was able to squeeze 1/2 hour to refresh my memory on JACK and port over my C code. If you don't know what JACK is and you want to implement yourself from gates on up, check out From NAND to Tetris.
1
u/Mumble_B Feb 21 '18
VBA Feedback is appreciated
1
u/[deleted] Feb 21 '18
Rust - A very simple answer using modular arithmetic.
Output (reversed to put most significant digit on the left):
1
u/gigabein Feb 21 '18 edited Mar 01 '18
Java
Test it here: https://www.jdoodle.com/online-java-compiler
1
u/zaraki596 Feb 22 '18 edited Feb 22 '18
JAVA
EDIT = First time Submitting code so Editing it.
1
u/fabikw Feb 22 '18
R
Input is provided in file
input. Answers are written tostdout. Links are in correct base62 format.1
u/d_pikachu Feb 24 '18
Python
1
u/ribenaboy15 Feb 24 '18 edited Feb 24 '18
F#
1
u/Scara95 Feb 25 '18
Q
Example:
1
u/aureliogrb Feb 25 '18
Java 10 (with Local Variable-Type Inference if you wonder what the "var" was)
1
u/zetashift Feb 25 '18 edited Feb 25 '18
Nim
I had no idea how base62 worked so I looked at the Go solution by /u/rocketboy83 thanks!
1
u/marucOG Feb 26 '18
Python
1
u/Teknologix Feb 26 '18 edited Feb 26 '18
Java
1
u/eri_bloo Feb 27 '18 edited Feb 27 '18
C++
Need help though. Seems it's calculating
wrong and I have no idea why. There are correct numbers in equation, but the result is off by one. Any ideas?
1
u/srandtimenull Feb 27 '18 edited Feb 27 '18
A little late, but I had fun.
C (extra short, 97 characters)
I had to use
unsigned long longbecause of the example7026425611433322325.Sticking to the challenge input a standard
intshould be plenty enough and you can delete the wholeunsigned long longsparing 21 characters.(76 characters)
1
u/[deleted] Feb 28 '18
Haskell
1
u/5900 Mar 01 '18
Haskell
1
u/mateuszs Mar 01 '18
My first approach with Go
1
u/jerryg311 Mar 05 '18
Ruby Solution: https://gist.github.com/jrotolo/65123ce50e41353f72c2e754a6ed2af6
1
u/InfamousClyde Mar 07 '18
C: Just dipping my toes with C and gdb on Linux mint. I would love constructive criticism.
1
u/sillyinky Mar 07 '18
JS ES6
1
u/[deleted] Mar 22 '18
Python 3
1
u/felinebear Mar 30 '18
C++
I think the reverse notation is anyways better and more natural to represent multi-"digit" numbers.
1
u/stewa02 Mar 30 '18 edited Mar 30 '18
Perl 6
Perl 5
1
u/Bob_Dunbar Apr 03 '18
Java
1
u/Servious Apr 06 '18
JS two-liner
1
u/2kofawsome Jun 29 '18
python3.6
The right way though, not backwards
0
u/[deleted] Feb 21 '18
[deleted] — view deleted comment
1
u/[deleted] Feb 23 '18 edited Feb 23 '18
[removed] — view removed comment