Re: sudoku dictionary attack
"Oliver Albrecht" <[EMAIL PROTECTED]> wrote ... > Has some one an sodoku-task-generator? Sudoku puzzles can be generated (and solved) online at http://act365.com/sudoku/ -- http://mail.python.org/mailman/listinfo/python-list
number of different lines in a file
I have a million-line text file with 100 characters per line, and simply need to determine how many of the lines are distinct. On my PC, this little program just goes to never-never land: def number_distinct(fn): f = file(fn) x = f.readline().strip() L = [] while x<>'': if x not in L: L = L + [x] x = f.readline().strip() return len(L) Would anyone care to point out improvements? Is there a better algorithm for doing this? -- http://mail.python.org/mailman/listinfo/python-list
Re: number of different lines in a file
"Tim Chase" <[EMAIL PROTECTED]> wrote ...
> 2) use a python set:
>
> s = set()
> for line in open("file.in"):
> s.add(line.strip())
> return len(s)
>
> 3) compact #2:
>
> return len(set([line.strip() for line in file("file.in")]))
>
> or, if stripping the lines isn't a concern, it can just be
>
> return len(set(file("file.in")))
>
> The logic in the set keeps track of ensuring that no
> duplicates get entered.
>
> Depending on how many results you *expect*, this could
> become cumbersome, as you have to have every unique line in
> memory. A stream-oriented solution can be kinder on system
> resources, but would require that the input be sorted first.
Thank you (and all the others who responded!) -- set() does
the trick, reducing the job to about a minute. I may play
later with the other alternatives people mentionsed (dict(),
hash(),...), just out of curiosity. I take your point about
the "expected number", which in my case was around 0-10 (as
it turned out, there were no dups).
BTW, the first thing I tried was Fredrik Lundh's program:
def number_distinct(fn):
return len(set(s.strip() for s in open(fn)))
which worked without the square brackets. Interesting that
omitting them doesn't seem to matter.
--
http://mail.python.org/mailman/listinfo/python-list
Can this program be shortened? Measuring program-length?
Can the following program be shortened? ...
def h(n,m):
E=n,
while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n
return n
h(9,9)
Note: Although it halts eventually in principle, this program can't be
expected to terminate on any machine in the universe, as it computes a
number larger than Graham's number -- assuming Python is extended (if
necessary?) to access unbounded storage.
Besides using one-letter names and no unneeded whitespace, can something
more be done to shorten it? ("Obfuscating" the code would be okay.)
Also, I'm not really sure how best to measure a program's length, but
this one is now 98 bytes long (or 102 bytes, depending on how newlines
are handled). Is there a better measure of program-length?
Thanks for any feedback.
--
http://mail.python.org/mailman/listinfo/python-list
Re: Can this program be shortened? Measuring program-length?
<[EMAIL PROTECTED]> wrote ... > "r.e.s." <[EMAIL PROTECTED]> wrote: >> Can the following program be shortened? ... >> >> def h(n,m): >> E=n, >> while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n >> return n >> h(9,9) >> > > Some ideas... > > # h is your version > def h(n,m): > E=n, > while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n > return n > > def g(n,m): > E=n, > while E*m:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n > return n > > def f(n,m): > E=n, > while E*m:n=h(n+1,m-1);E=(E[0]>0)*(E[0]-1,)*n+E[1:] > return n > > def e(n,m): > E=[n] > while E*m:n=h(n+1,m-1);E[:1]=(E[0]>0)*[E[0]-1]*n > return n > > # some tests > print h(1,1), h(2,1), h(0,2) > print g(1,1), g(2,1), g(0,2) > print f(1,1), f(2,1), f(0,2) > print e(1,1), e(2,1), e(0,2) Very instructive! Thank you for the "step-by-step". -- http://mail.python.org/mailman/listinfo/python-list
Re: Can this program be shortened? Measuring program-length?
"r.e.s." <[EMAIL PROTECTED]> wrote ... > <[EMAIL PROTECTED]> wrote ... >> "r.e.s." <[EMAIL PROTECTED]> wrote: >>> Can the following program be shortened? ... >>> >>> def h(n,m): >>> E=n, >>> while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n >>> return n >>> h(9,9) >>> >> >> Some ideas... >> >> # h is your version >> def h(n,m): >> E=n, >> while (E!=())*m>0:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n >> return n >> >> def g(n,m): >> E=n, >> while E*m:n=h(n+1,m-1);E=E[:-1]+(E[-1]>0)*(E[-1]-1,)*n ^ g >> return n >> >> def f(n,m): >> E=n, >> while E*m:n=h(n+1,m-1);E=(E[0]>0)*(E[0]-1,)*n+E[1:] ^ f >> return n >> >> def e(n,m): >> E=[n] >> while E*m:n=h(n+1,m-1);E[:1]=(E[0]>0)*[E[0]-1]*n ^ e >> return n >> >> # some tests >> print h(1,1), h(2,1), h(0,2) >> print g(1,1), g(2,1), g(0,2) >> print f(1,1), f(2,1), f(0,2) >> print e(1,1), e(2,1), e(0,2) > > Very instructive! Thank you for the "step-by-step". I forgot to mention the obvious typos. Thanks again. -- http://mail.python.org/mailman/listinfo/python-list
