Basic challenge (simple): Write a script that produces all possible 4-digit numbers (0000...9999), then put them into a random order and save the output into a text file. Make sure you include leading zeroes where necessary and make also sure that each number is on a separate line in the text file.
Bonus challenge (for daring programmers only): Do the basic challenge and then do the following: Call the generated file leaked_pins.txt, and email it to one of your co-workers. In the description of the email, say that all the PIN numbers have been leaked, and they should be cautious and check, whether their PIN is contained in the file or not.
Share your code below. If any of you tries the bonus challenge, I am happy to hear any stories :)
Latest comments (47)
Just coming across this. I like finding and using old gems. For this,
jotdoes just great.Or for those on a Mac using GNU coreutils (probably from
brew), you'd usegshufinstead ofshuf:Of course, Ruby is just as elegant:
... but on my machine the Ruby solution is about 95% slower than the
jotversion: 148ms vs. 8ms, respectively.Vim/Bash:
AWK:
Love the bonus challenge!
Sent such a list to a friend working for a bank years ago.
This made it to the board of directors within minutes.
Still laughing and gasping - almost can't believe this impact :-D.
Hacky Perl/sh oneliner
Generates a list of pins (@v) and then selects a random entry for every entry in the list (hacky use of map). %x is used to mark which entries already have been returned.
Easier to read and works with embedded Linux systems (BusyBox):
I didn't know this for a long time, but you can use strings to build ranges in Ruby as well, avoiding the need for any fancy padding with zeros!
Pharo
Clojure?
F#
Some comments may only be visible to logged-in visitors. Sign in to view all comments.