Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #56350 > unrolled thread
| Started by | Chris Angelico <rosuav@gmail.com> |
|---|---|
| First post | 2013-10-08 17:17 +1100 |
| Last post | 2013-10-08 10:58 -0700 |
| Articles | 20 — 10 participants |
Back to article view | Back to comp.lang.python
Code golf challenge: XKCD 936 passwords Chris Angelico <rosuav@gmail.com> - 2013-10-08 17:17 +1100
Re: Code golf challenge: XKCD 936 passwords sprucebondera@gmail.com - 2013-10-07 23:45 -0700
Re: Code golf challenge: XKCD 936 passwords sprucebondera@gmail.com - 2013-10-07 23:48 -0700
Re: Code golf challenge: XKCD 936 passwords Chris Angelico <rosuav@gmail.com> - 2013-10-08 17:52 +1100
Re: Code golf challenge: XKCD 936 passwords Roy Smith <roy@panix.com> - 2013-10-08 08:33 -0400
Re: Code golf challenge: XKCD 936 passwords Denis McMahon <denismfmcmahon@gmail.com> - 2013-10-08 15:36 +0000
Re: Code golf challenge: XKCD 936 passwords Tim Chase <python.list@tim.thechases.com> - 2013-10-08 11:07 -0500
Re: Code golf challenge: XKCD 936 passwords Tobiah <toby@tobiah.org> - 2013-10-08 10:38 -0700
Re: Code golf challenge: XKCD 936 passwords Steve Simmons <square.steve@gmail.com> - 2013-10-08 08:02 +0100
Re: Code golf challenge: XKCD 936 passwords Chris Angelico <rosuav@gmail.com> - 2013-10-08 18:13 +1100
Re: Code golf challenge: XKCD 936 passwords Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-10-08 08:48 +0100
Re: Code golf challenge: XKCD 936 passwords random832@fastmail.us - 2013-10-08 11:47 -0400
Re: Code golf challenge: XKCD 936 passwords sprucebondera@gmail.com - 2013-10-08 13:27 -0700
Re: Code golf challenge: XKCD 936 passwords Chris Angelico <rosuav@gmail.com> - 2013-10-09 07:35 +1100
Re: Code golf challenge: XKCD 936 passwords Roy Smith <roy@panix.com> - 2013-10-08 21:38 -0400
Re: Code golf challenge: XKCD 936 passwords Chris Angelico <rosuav@gmail.com> - 2013-10-09 13:10 +1100
Re: Code golf challenge: XKCD 936 passwords Roy Smith <roy@panix.com> - 2013-10-08 22:52 -0400
RE: Code golf challenge: XKCD 936 passwords Nick Cash <nick.cash@npcinternational.com> - 2013-10-09 16:07 +0000
Re: Code golf challenge: XKCD 936 passwords Roy Smith <roy@panix.com> - 2013-10-09 20:29 -0400
Re: Code golf challenge: XKCD 936 passwords Tobiah <toby@tobiah.org> - 2013-10-08 10:58 -0700
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-10-08 17:17 +1100 |
| Subject | Code golf challenge: XKCD 936 passwords |
| Message-ID | <mailman.828.1381213051.18130.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | sprucebondera@gmail.com |
|---|---|
| Date | 2013-10-07 23:45 -0700 |
| Message-ID | <68365e43-498f-4ad2-bac3-6a02938159c7@googlegroups.com> |
| In reply to | #56350 |
On Monday, October 7, 2013 8:17:21 PM UTC-10, Chris Angelico wrote:
> 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.
[toc] | [prev] | [next] | [standalone]
| From | sprucebondera@gmail.com |
|---|---|
| Date | 2013-10-07 23:48 -0700 |
| Message-ID | <d9df49e7-f3c9-4f10-9321-1417b8d2b725@googlegroups.com> |
| In reply to | #56352 |
On Monday, October 7, 2013 8:45:39 PM UTC-10, spruce...@gmail.com wrote:
> On Monday, October 7, 2013 8:17:21 PM UTC-10, Chris Angelico wrote:
>
> > 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.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-10-08 17:52 +1100 |
| Message-ID | <mailman.830.1381215134.18130.python-list@python.org> |
| In reply to | #56353 |
On Tue, Oct 8, 2013 at 5:48 PM, <sprucebondera@gmail.com> wrote: > 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
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-10-08 08:33 -0400 |
| Message-ID | <roy-DEB2CE.08334808102013@news.panix.com> |
| In reply to | #56354 |
In article <mailman.830.1381215134.18130.python-list@python.org>, Chris Angelico <rosuav@gmail.com> wrote: > On Tue, Oct 8, 2013 at 5:48 PM, <sprucebondera@gmail.com> wrote: > > 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 In the old days, it used to be /usr/dict/words. Port Python to v6, and save another 6 characters :-)
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2013-10-08 15:36 +0000 |
| Message-ID | <l318qi$cl6$1@dont-email.me> |
| In reply to | #56390 |
On Tue, 08 Oct 2013 08:33:48 -0400, Roy Smith wrote: > In article <mailman.830.1381215134.18130.python-list@python.org>, > Chris Angelico <rosuav@gmail.com> wrote: > >> On Tue, Oct 8, 2013 at 5:48 PM, <sprucebondera@gmail.com> wrote: >> > 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 :) > 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 ;) -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-10-08 11:07 -0500 |
| Message-ID | <mailman.864.1381248360.18130.python-list@python.org> |
| In reply to | #56417 |
On 2013-10-08 15:36, Denis McMahon wrote: > On Tue, 08 Oct 2013 08:33:48 -0400, Roy Smith wrote: > > 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 ;) You prodigal...wasting a "/". I just symlinked it from my current working directory so it exists at "w". ;-) -tkc
[toc] | [prev] | [next] | [standalone]
| From | Tobiah <toby@tobiah.org> |
|---|---|
| Date | 2013-10-08 10:38 -0700 |
| Message-ID | <uqX4u.39321$D22.36359@fx27.iad> |
| In reply to | #56427 |
On 10/08/2013 09:07 AM, Tim Chase wrote: > On 2013-10-08 15:36, Denis McMahon wrote: >> On Tue, 08 Oct 2013 08:33:48 -0400, Roy Smith wrote: >>> 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 ;) > > 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.
[toc] | [prev] | [next] | [standalone]
| From | Steve Simmons <square.steve@gmail.com> |
|---|---|
| Date | 2013-10-08 08:02 +0100 |
| Message-ID | <mailman.833.1381216100.18130.python-list@python.org> |
| In reply to | #56353 |
[Multipart message — attachments visible in raw view] — view raw
Chris Angelico <rosuav@gmail.com> wrote: >On Tue, Oct 8, 2013 at 5:48 PM, <sprucebondera@gmail.com> wrote: >> 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 >-- >https://mail.python.org/mailman/listinfo/python-list Typical MUD Master - making up rules as you go along :-) Sent from a Galaxy far far away
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-10-08 18:13 +1100 |
| Message-ID | <mailman.834.1381216429.18130.python-list@python.org> |
| In reply to | #56353 |
On Tue, Oct 8, 2013 at 6:02 PM, Steve Simmons <square.steve@gmail.com> wrote: > Typical MUD Master - making up rules as you go along :-) Totally. Under the auspices of Rule Zero: http://tvtropes.org/pmwiki/pmwiki.php/Main/RuleOfFun :) ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-10-08 08:48 +0100 |
| Message-ID | <mailman.837.1381218515.18130.python-list@python.org> |
| In reply to | #56353 |
On 08/10/2013 07:48, sprucebondera@gmail.com wrote:
> On Monday, October 7, 2013 8:45:39 PM UTC-10, spruce...@gmail.com wrote:
>> On Monday, October 7, 2013 8:17:21 PM UTC-10, Chris Angelico wrote:
>>
>>> 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.
>
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
[toc] | [prev] | [next] | [standalone]
| From | random832@fastmail.us |
|---|---|
| Date | 2013-10-08 11:47 -0400 |
| Message-ID | <mailman.860.1381247279.18130.python-list@python.org> |
| In reply to | #56352 |
On Tue, Oct 8, 2013, at 2:45, sprucebondera@gmail.com wrote:
> On Monday, October 7, 2013 8:17:21 PM UTC-10, Chris Angelico wrote:
> > print(*__import__("random").sample(open("/usr/share/dict/words").read().split("\n"),4)) # 87
>
> 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.
[toc] | [prev] | [next] | [standalone]
| From | sprucebondera@gmail.com |
|---|---|
| Date | 2013-10-08 13:27 -0700 |
| Message-ID | <49d2ed27-660d-4a42-bf1d-f8117aeb002e@googlegroups.com> |
| In reply to | #56420 |
On Tuesday, October 8, 2013 5:47:56 AM UTC-10, rand...@fastmail.us wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-10-09 07:35 +1100 |
| Message-ID | <mailman.872.1381264533.18130.python-list@python.org> |
| In reply to | #56441 |
On Wed, Oct 9, 2013 at 7:27 AM, <sprucebondera@gmail.com> wrote: > On Tuesday, October 8, 2013 5:47:56 AM UTC-10, rand...@fastmail.us wrote: >> 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. 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
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-10-08 21:38 -0400 |
| Message-ID | <roy-32EAD8.21381208102013@news.panix.com> |
| In reply to | #56352 |
On Monday, October 7, 2013 8:17:21 PM UTC-10, Chris Angelico wrote:
> > print(*__import__("random").sample(open("/usr/share/dict/words").read().spli
> > t("\n"),4))
In article <68365e43-498f-4ad2-bac3-6a02938159c7@googlegroups.com>,
sprucebondera@gmail.com wrote:
> 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 :-)
Ż
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-10-09 13:10 +1100 |
| Message-ID | <mailman.885.1381284610.18130.python-list@python.org> |
| In reply to | #56460 |
On Wed, Oct 9, 2013 at 12:38 PM, Roy Smith <roy@panix.com> wrote:
> 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
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-10-08 22:52 -0400 |
| Message-ID | <roy-406378.22523608102013@news.panix.com> |
| In reply to | #56462 |
In article <mailman.885.1381284610.18130.python-list@python.org>,
Chris Angelico <rosuav@gmail.com> wrote:
> On Wed, Oct 9, 2013 at 12:38 PM, Roy Smith <roy@panix.com> wrote:
> > 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.
[toc] | [prev] | [next] | [standalone]
| From | Nick Cash <nick.cash@npcinternational.com> |
|---|---|
| Date | 2013-10-09 16:07 +0000 |
| Message-ID | <mailman.901.1381334893.18130.python-list@python.org> |
| In reply to | #56460 |
>> # 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-randomization out 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
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-10-09 20:29 -0400 |
| Message-ID | <roy-07A5E9.20290009102013@news.panix.com> |
| In reply to | #56502 |
In article <mailman.901.1381334893.18130.python-list@python.org>,
Nick Cash <nick.cash@npcinternational.com> wrote:
> >> # 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.
Exactly. I *did* preface this with:
>>> If you're willing to accept a sub-optimal random number generator:
[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!
[toc] | [prev] | [next] | [standalone]
| From | Tobiah <toby@tobiah.org> |
|---|---|
| Date | 2013-10-08 10:58 -0700 |
| Message-ID | <_IX4u.69249$yx6.3797@fx26.iad> |
| In reply to | #56350 |
On 10/07/2013 11:17 PM, Chris Angelico wrote:
> 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web