Code golf challenge: XKCD 936 passwords

S

sprucebondera

Who's up for some fun? Implement an XKCD-936-compliant password

generator in Python 3, in less code than this:



print(*__import__("random").sample(open("/usr/share/dict/words").read().split("\n"),4))



Second challenge: Use it for generating all your passwords :)



[1] https://en.wikipedia.org/wiki/Code_golf

[2] http://xkcd.com/936/



ChrisA

Well, here's a start:

import random as r
print(*r.sample(open("/usr/share/dict/words").readlines(),4))

Shaves off 6 characters.
 
S

sprucebondera

Who's up for some fun? Implement an XKCD-936-compliant password
generator in Python 3, in less code than this:
print(*__import__("random").sample(open("/usr/share/dict/words").read().split("\n"),4))

Second challenge: Use it for generating all your passwords :)
[1] https://en.wikipedia.org/wiki/Code_golf
[2] http://xkcd.com/936/
ChrisA



Well, here's a start:



import random as r

print(*r.sample(open("/usr/share/dict/words").readlines(),4))



Shaves off 6 characters.

And if we were actually trying then that filename should just be "/w". Would get rid of another 19 chars.
 
C

Chris Angelico

And if we were actually trying then that filename should just be "/w". Would get rid of another 19 chars.

I'm working this on the assumption that the dictionary file already
exists (that's where it is on my Debian Linux systems, for instance)
and shouldn't be moved :)

ChrisA
 
S

Steve Simmons

Chris Angelico said:
"/w". Would get rid of another 19 chars.

I'm working this on the assumption that the dictionary file already
exists (that's where it is on my Debian Linux systems, for instance)
and shouldn't be moved :)

ChrisA

Typical MUD Master - making up rules as you go along :)

Sent from a Galaxy far far away
 
M

Mark Lawrence

Who's up for some fun? Implement an XKCD-936-compliant password
generator in Python 3, in less code than this:
print(*__import__("random").sample(open("/usr/share/dict/words").read().split("\n"),4))
Second challenge: Use it for generating all your passwords :)

Well, here's a start:

import random as r
print(*r.sample(open("/usr/share/dict/words").readlines(),4))
Shaves off 6 characters.

And if we were actually trying then that filename should just be "/w". Would get rid of another 19 chars.

Very impressive, you've saved a total of 25 characters on one line and
added too many lines to count to your emails, which I've snipped.
Please read and digest this
https://wiki.python.org/moin/GoogleGroupsPython, thanks in anticipation.

--
Roses are red,
Violets are blue,
Most poems rhyme,
But this one doesn't.

Mark Lawrence
 
R

Roy Smith

Chris Angelico said:
I'm working this on the assumption that the dictionary file already
exists (that's where it is on my Debian Linux systems, for instance)
and shouldn't be moved :)

ChrisA

In the old days, it used to be /usr/dict/words. Port Python to v6, and
save another 6 characters :)
 
D

Denis McMahon

In the old days, it used to be /usr/dict/words. Port Python to v6, and
save another 6 characters :)

Doesn't matter where it is, a link to it exists at "/w" now ;)
 
R

random832

import random as r
print(*r.sample(open("/usr/share/dict/words").readlines(),4)) # 80

How about this? My version is also portable to systems with different
file locations, and localizable to different language dictionaries (Some
assembly required).

import sys,random
print(*map(str.strip,random.sample(list(sys.stdin),4))) # 73

Importing random as r doesn't actually save anything, since " as r" is
the same five characters you saved from the one use of it.
 
T

Tim Chase

Doesn't matter where it is, a link to it exists at "/w" now ;)

You prodigal...wasting a "/". I just symlinked it from my current
working directory so it exists at "w". ;-)

-tkc
 
T

Tobiah

You prodigal...wasting a "/". I just symlinked it from my current
working directory so it exists at "w". ;-)

-tkc

Yeah, but that's a lot of pixels! Link it to "'" in the current directory.
 
T

Tobiah

Who's up for some fun? Implement an XKCD-936-compliant password
generator in Python 3, in less code than this:

print(*__import__("random").sample(open("/usr/share/dict/words").read().split("\n"),4))

Second challenge: Use it for generating all your passwords :)

[1] https://en.wikipedia.org/wiki/Code_golf
[2] http://xkcd.com/936/

ChrisA

So how about finding the last word that starts with
each lower case letter of the alphabet in turn:

azures
bywords
czars
....

Tobiah
 
S

sprucebondera

Importing random as r doesn't actually save anything, since " as r" is
the same five characters you saved from the one use of it.

I realize, it just looks nicer than the original __import__, and since it doesn't add any characters...
The optimization was using readlines.
 
C

Chris Angelico

I realize, it just looks nicer than the original __import__, and since it doesn't add any characters...
The optimization was using readlines.

Are you aware that readlines keeps the \n at the end of each line,
though? Looks a lot less clean in its output that way. That's why I
used .split() in the first place.

ChrisA
 
R

Roy Smith

import random as r
print(*r.sample(open("/usr/share/dict/words").readlines(),4))

Shaves off 6 characters.


If you're willing to accept a sub-optimal random number generator:

# Python-2, sorry
import os
print list(set(open('/usr/share/dict/words')))[os.getpid():][:4]

I'll also point out that on my OSX box, I could have used
/usr/share/dict/web2 and saved one more character :)
¯
 
C

Chris Angelico

If you're willing to accept a sub-optimal random number generator:

# Python-2, sorry
import os
print list(set(open('/usr/share/dict/words')))[os.getpid():][:4]

So that steps by your pid? That's going to drastically reduce the
entropy in the result, which is kinda the point of XKCD 936. I'll call
that one dubious... though it's still better than Rob's entry. (But
thanks Rob, I do like that one :) )

ChrisA
 
R

Roy Smith

Chris Angelico said:
If you're willing to accept a sub-optimal random number generator:

# Python-2, sorry
import os
print list(set(open('/usr/share/dict/words')))[os.getpid():][:4]

So that steps by your pid? That's going to drastically reduce the
entropy in the result,

Well, I did say it was a sub-optimal random number generator :)

I've been experimenting with os.urandom() as an entropy source, but
can't get the (source code) character count down far enough.
 
N

Nick Cash

# Python-2, sorry
import os
print list(set(open('/usr/share/dict/words')))[os.getpid():][:4]
So that steps by your pid?

Not really. It seems to rely on list(set(...)) kinda randomizing order... which is definitely not safe without hash randomization.

But this brings up an interesting concept... if we can assume Python 2.7 with the -R flag, or Python 3.3+, I think we do get true pseudo-randomizationout of list(set(...))... which means we can trim some!

print(list(set(open('/usr/share/dict/words')))[:4])

No module imports needed! Although it's not as pretty as the original post,but neither was Roy's.

-Nick Cash
 
R

Roy Smith

# Python-2, sorry
import os
print list(set(open('/usr/share/dict/words')))[os.getpid():][:4]
So that steps by your pid?

Not really. It seems to rely on list(set(...)) kinda randomizing order...
which is definitely not safe without hash randomization.[/QUOTE]

Exactly. I *did* preface this with:

[Nick, again]
But this brings up an interesting concept... if we can assume Python 2.7 with
the -R flag, or Python 3.3+, I think we do get true pseudo-randomization out
of list(set(...))... which means we can trim some!

print(list(set(open('/usr/share/dict/words')))[:4])

Excellent! I didn't know about -R before this, so not only has this
been fun, it's been educational too!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top