Re: Probability Problem

2006-04-25 Thread Elliot Temple
I think I got it. I noticed my code is essentially the same as Tim  
Peter's (plus the part of the problem he skipped). I read his code 20  
minutes before recreating mine from Alex's hints. Thanks!

def main():
 ways = ways_to_roll()
 total_ways = float(101**10)
 running_total = 0
 for i in range(1000-390+1):
 j = i + 390
 running_total += ways[i] * ways[j]
 print running_total / total_ways**2
 print ways[:10]

def ways_to_roll():
 result = [1]
 for i in xrange(10):
 result = combine([1] * 101, result)
 return result

def combine(a, b):
 results = [0] * (len(a) + len(b) - 1)
 for i, ele in enumerate(a):
 for j, ele2 in enumerate(b):
 results[i+j] += ele * ele2
 return results

main()
# output: 3.21962542309e-05 and
# [1, 10, 55, 220, 715, 2002, 5005, 11440, 24310, 48620]
# 3.21962542309e-05 is 32 out of a million



On Apr 24, 2006, at 9:14 PM, Alex Martelli wrote:

 Elliot Temple [EMAIL PROTECTED] wrote:

 On Apr 24, 2006, at 8:24 PM, Alex Martelli wrote:

 Lawrence D'Oliveiro [EMAIL PROTECTED] wrote:

 In article [EMAIL PROTECTED],
  Elliot Temple [EMAIL PROTECTED] wrote:

 Problem: Randomly generate 10 integers from 0-100 inclusive,  
 and sum
 them. Do that twice. What is the probability the two sums are 390
 apart?

 I think the sum would come close to a normal distribution.

 Yes, very close indeed, by the law of large numbers.

 However, very close (in a math course at least) doesn't get the  
 cigar.

 You can compute the requested answer exactly with no random number
 generation whatsoever: compute the probability of each result from
 0 to
 1000, then sum the probabilities of entries that are exactly 390
 apart.

 That was the plan, but how do I get the probability of any given
 result? (in a reasonable amount of time)

 BTW I'm not in a math course, just curious.

 OK, I'll trust that last assertion (sorry for the hesitation, but it's
 all too easy to ``help'' somebody with a homework assignment and
 actually end up damaging them by doing it FOR them!-).


 I'm still going to present this in a way that stimulates thought,  
 rather
 than a solved problem -- humor me...!-)


 You're generating a uniformly distributed random number in 0..100 (101
 possibilities), 10 times, and summing the 10 results.

 How do you get a result of 0?  Only 1 way: 0 at each attempt --
 probability 1 (out of 1010 possibilities).

 How do you get a result of 1?  10 ways: 1 at one attempt and 0 at each
 of the others - probability 10 (again in 1010'ths;-).

 How do you get a result of 2?  10 ways for '2 at one attempt and 0 at
 each of the others', plus, 10*9/2 ways for '1 at two attempts and 0 at
 each of the others' -- probability 55 (ditto).

 ...and so forth, but you'd rather not work it out...


 So, suppose you start with a matrix of 101 x 10 entries, each of  
 value 1
 since all results are equiprobable (or, 1/1010.0 if you prefer;-).

 You want to compute the number in which you can combine two rows.  How
 could you combine the first two rows (each of 101 1's) to make a  
 row of
 201 numbers corresponding to the probabilities of the sum of two  
 throws?

 Suppose you combine the first entry of the first row with each  
 entry of
 the second, then the second entry of the first row with each entry of
 the second, etc; each time, you get a sum (of two rolls) which  
 gives you
 an index of a entry (in an accumulator row starting at all zeros) to
 increment by the product of the entries you're considering...


 Can you generalize that?  Or, do you need more hints?  Just ask!


 Alex
 -- 
 http://mail.python.org/mailman/listinfo/python-list


-- Elliot Temple
http://www.curi.us/blog/



-- 
http://mail.python.org/mailman/listinfo/python-list


Probability Problem

2006-04-24 Thread Elliot Temple
Problem: Randomly generate 10 integers from 0-100 inclusive, and sum  
them. Do that twice. What is the probability the two sums are 390 apart?

I have code to do part of it (below), and I know how to write code to  
do the rest. The part I have calculates the number of ways the dice  
can come out to a given number. The problem is the main loop has 9  
iterations and it takes about 2.5 minutes to begin the 4th one, and  
each iteration is about 101 times longer than the previous one. So:

  x = 2.5 * 101**6
  x /= (60*24*365.25)
  x
5045631.5622908585

It'd take 5,000 millennia. (If my computer didn't run out of memory  
after about 4 minutes, that is.)

Any suggestions? Either a way to do the same thing much more  
efficiently (enough that I can run it) or a different way to solve  
the problem.

Code:

li = range(101)
li2 = []
range101 = range(101)
for x in xrange(9):
 print x is %s % x
 li2 = []
 for y in li:
 for z in range101:
 li2 += [y+z]
 li = li2
print li.count(800)
# prints how many ways the dice can add to 800


This link may help:
http://www.math.csusb.edu/faculty/stanton/m262/intro_prob_models/ 
calcprob.html

-- Elliot Temple
http://www.curi.us/blog/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Probability Problem

2006-04-24 Thread Elliot Temple

On Apr 24, 2006, at 8:24 PM, Alex Martelli wrote:

 Lawrence D'Oliveiro [EMAIL PROTECTED] wrote:

 In article [EMAIL PROTECTED],
  Elliot Temple [EMAIL PROTECTED] wrote:

 Problem: Randomly generate 10 integers from 0-100 inclusive, and sum
 them. Do that twice. What is the probability the two sums are 390  
 apart?

 I think the sum would come close to a normal distribution.

 Yes, very close indeed, by the law of large numbers.

 However, very close (in a math course at least) doesn't get the cigar.

 You can compute the requested answer exactly with no random number
 generation whatsoever: compute the probability of each result from  
 0 to
 1000, then sum the probabilities of entries that are exactly 390  
 apart.

That was the plan, but how do I get the probability of any given  
result? (in a reasonable amount of time)

BTW I'm not in a math course, just curious.

-- Elliot Temple
http://www.curi.us/blog/



-- 
http://mail.python.org/mailman/listinfo/python-list


Scope

2005-06-03 Thread Elliot Temple
I want to write a function, foo, so the following works:

def main():
 n = 4
 foo(n)
 print n

#it prints 7

if foo needs to take different arguments, that'd be alright.

Is this possible?



I already tried this (below), which doesn't work.  foo only changes  
the global n.


n = 3
def main():
 def foo(var, context, c2):
 exec var +  = 7 in context, c2

 n = 4
 foo(n, locals(), globals())
 print n

if __name__ == '__main__': main()

print n


And of course I tried:

  def inc(n):
...  n += 3
...
  a = 4
  inc(a)
  a
4

-- Elliot Temple
http://www.curi.us/


---
[This E-mail scanned for viruses by Declude Virus]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Scope

2005-06-03 Thread Elliot Temple

On Jun 4, 2005, at 2:13 AM, Leif K-Brooks wrote:

 Elliot Temple wrote:

 I want to write a function, foo, so the following works:

 def main():
 n = 4
 foo(n)
 print n

 #it prints 7


 What's wrong with:

 def foo(n):
 return 7

 def main():
 n = 4
 n = foo(n)
 print n

 Anything else (including the tricks involving mutable objects that  
 will
 no doubt be posted) will result in ugly, hard to maintain code.

Nothing is wrong with it in this case.  I just want to know if Python  
can do what I said.

-- Elliot Temple
http://www.curi.us/


---
[This E-mail scanned for viruses by Declude Virus]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Moving Places, Subtracting from slices/lists

2005-06-02 Thread Elliot Temple

On Jun 2, 2005, at 12:12 AM, Mark Sargent wrote:

 Hi All,

 getting closer, me thinks.


 hotcat = ['Cat', 'roof', 'on', 'a', 'hot', 'tin']
 for x in hotcat[:]:

 ... if x == 'roof': hotcat.insert(6,x)
 ... del hotcat[x]
 ...
 Traceback (most recent call last):
   File stdin, line 3, in ?
 TypeError: list indices must be integers

 How do I get that x to be an integer b4 it is entered into the  
 indice.?
 Cheers.

if you add print x to the loop you will see that X is the various  
words.

to get an integer, you could search the list for the index of x.  but  
that's lame.

btw hotcat[:] is a *copy* of hotcat, so just leave out [:]

enumerate is a function that adds indexes to a list.  observe:

hotcat = ['Cat', 'roof', 'on', 'a', 'hot', 'tin']
for index, word in enumerate(hotcat):
 if word == 'roof': del hotcat[index]

you could also use a list comprehension

hotcat = ['Cat', 'roof', 'on', 'a', 'hot', 'tin']
hotcat = [x for x in hotcat if x != 'roof']

-- Elliot Temple
http://www.curi.us/


---
[This E-mail scanned for viruses by Declude Virus]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: (OT) lincense protection generator

2005-06-02 Thread Elliot Temple
Why not check if all files you use are in appropriate directories,  
but not worry about same computer?

You could also use some kind of hash on all your files, and check  
that they haven't been changed using that (ie, do they still hash to  
the same value they are supposed to?).  (I know very little about  
hashes, so don't ask me for details.)

On Jun 2, 2005, at 9:33 PM, flupke wrote:

 I'm going to be distributing a program based on wxPython  python in a
 few weeks time. The code will run on windows machines.

 Because i don't want the users to move the folders around or mess with
 the program or taking copies home to fiddle with it, i was thinking  
 of a
 way to check when the program starts that it's still on the same
 directory and same computer.

 That way i at least avoid unnecessary phone calls asking me for help
 when they messed the program up.

 I'm thinking of a function that will generate such a code and put  
 it in
 a file. Then when the program starts it checks this file and checks  
 the
 code in there with a code that it generates at that time again  
 based for
 instance on the current directory and other components unique to that
 computer. It could be a long string (but what info?) and then take a
 hash from it and store the hash value.

 How could i construct such a code without being a total pain? For
 instance i don't want to check for anything hardware related because
 then my program would fail to work once the people change their  
 hardware.
 Anyway, it doesn't need to be waterproof. (not possible anyway)

 Any ideas?

 Regards,
 Benedict
 -- 
 http://mail.python.org/mailman/listinfo/python-list



-- Elliot Temple
http://www.curi.us/


---
[This E-mail scanned for viruses by Declude Virus]

-- 
http://mail.python.org/mailman/listinfo/python-list


Pressing A Webpage Button

2005-06-01 Thread Elliot Temple
How do I make Python press a button on a webpage?  I looked at  
urllib, but I only see how to open a URL with that.  I searched  
google but no luck.

For example, google has a button   input type=submit value=Google  
Search name=btnG  how would i make a script to press that button?

Just for fun, is there any way to do the equivalent of typing into a  
text field like the google search field before hitting the button?   
(I don't actually need to do this.)

If someone could point me in the right direction it'd be appreciated.

-- Elliot Temple
http://www.curi.us/


---
[This E-mail scanned for viruses by Declude Virus]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Beginner question: Logs?

2005-06-01 Thread Elliot Temple

On Jun 1, 2005, at 9:04 PM, Svens wrote:

 Hey thanks...

 Still getting an error message though.  Here's what i'm doing:
 --
 import math
 log10(15625)
 --
 -It says that log10 is not defined, but it is since the module is
 imported, right?

do either

import math
math.log10(15625)


from math import *
log10(15625)

from math import log10
log10(15625)




-- Elliot Temple
http://www.curi.us/


---
[This E-mail scanned for viruses by Declude Virus]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: need help of RE

2005-05-29 Thread Elliot Temple

On May 29, 2005, at 12:39 AM, cheng wrote:

 hi all
 a string like

 (word1  (Word2|woRd3))

 how can i use the re to split it to

 ['word1', 'word2', 'word3']

Could you be more exact about what the string is like?  Does it  
literally contain the characters '' and '|' ?  If so, just split at  
them.

-- Elliot Temple
http://www.curi.us/


---
[This E-mail scanned for viruses by Declude Virus]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: need help of RE

2005-05-29 Thread Elliot Temple

On May 29, 2005, at 12:57 AM, cheng wrote:

 im sorry, my engilsh is not vell well,

 the string not only contain '' and '|' and it can be anyting

 i just want to split out the whole word inside the string

If the string could be anything, how do you know where the words are?

If it's whitespace that separates words, try out str.split()

-- Elliot Temple
http://www.curi.us/


---
[This E-mail scanned for viruses by Declude Virus]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Case Sensitive, Multiline Comments

2005-05-29 Thread Elliot Temple

On May 29, 2005, at 11:44 AM, Arthur wrote:

 On 26 May 2005 17:33:33 -0700, Elliot Temple [EMAIL PROTECTED]
 wrote:


 Thanks for the link on case sensitivity.  I'm curious about the  
 person
 who found case sensitivity useful though: what is it useful for?


 I am curious about why you find case sensitivity annoying.  But just
 mildly curious.

I'm glad you asked ;-)

Case insensitivity gives you more choice about how to type keywords  
that you have no control over.  if or If.  for or For.  i don't think  
being inconsistent within a single program is a good idea, but having  
your choice of which to do is nice.  I personally think all lowercase  
is good, but some languages have capitalised keywords, so apparently  
other people prefer that.

I don't think the case sensitivity hurts beginners argument is  
persuasive.  Anyone who seriously wants to program can look up the  
correct capitalisation of everything.  *If* having to look up or keep  
track of capitalisation is annoying, *then* that argument applies to  
experienced programmers (who are devoting memory to the issue) just  
as much as beginners.

-- Elliot Temple
http://www.curi.us/


---
[This E-mail scanned for viruses by Declude Virus]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String manipulations

2005-05-28 Thread Elliot Temple

On May 28, 2005, at 2:52 PM, Lorn wrote:

 Yes, that would get rid of the decimals... but it wouldn't get rid of
 the extraneous precision. Unfortunately, the precision out to the ten
 thousandth is noise... I don't need to round it either as the numbers
 are artifacts of an integer to float conversion. Basically, I need to
 know how many decimal places there are and then make the necessary
 deletions before I can normalize by adding zeros, multiplying, etc.

 Thanks for your suggestion, though.

for s in numbers:
 decimal_index = s.find('.')
 decimal_places = len(s) - decimal_index - 1

Anything wrong with this?  (it will mess up if there isn't a decimal  
but you can fix that if you want)

-- Elliot Temple
http://www.curi.us/


---
[This E-mail scanned for viruses by Declude Virus]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Case Sensitive, Multiline Comments

2005-05-27 Thread Elliot Temple
One other interesting thing about case sensitivity I don't think  
anyone has mentioned: in Python keywords are all lowercase already  
(the way I want to type them).  In some other languages, they aren't...

-- Elliot Temple
http://www.curi.us/


---
[This E-mail scanned for viruses by Declude Virus]

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Get number of lines in file

2005-05-27 Thread Elliot Temple

On May 27, 2005, at 12:17 PM, [EMAIL PROTECTED] wrote:

 I have read in a file and need to get the number of lines.

 cpn_file = open('Central Part number list.txt')
 cpn_version = cpn_file.read().split('\n')

 I want to know the number of elements in cpn_version.

Could you use:

count_lines = len(cpn_file.readlines())

-- Elliot Temple
http://www.curi.us/


---
[This E-mail scanned for viruses by Declude Virus]

-- 
http://mail.python.org/mailman/listinfo/python-list


Case Sensitive, Multiline Comments

2005-05-26 Thread Elliot Temple
Hi I have two questions.  Could someone explain to me why Python is
case sensitive?  I find that annoying.  Also, why aren't there
multiline comments?  Would adding them cause a problem of some sort?

Thanks,
Elliot

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strange Execution Times

2005-05-26 Thread Elliot Temple
hey FYI i found the problem:  i accidentally copied an output file for
my test data.  so all the passwords were exactly 32 chars long.  so
when replacing them with new 32 char passwords, it went much much
faster, I guess because the list kept the same number of chars in it
and didn't have to copy lots of data around.

-- 
http://mail.python.org/mailman/listinfo/python-list