From cafa56b1726fd98a6ee04d4961b31d131c9455ee Mon Sep 17 00:00:00 2001 From: ph Date: Fri, 13 Sep 2019 11:18:38 +1000 Subject: [PATCH 1/2] Added new version showing a histogram and average. --- frog-ph.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 frog-ph.py diff --git a/frog-ph.py b/frog-ph.py new file mode 100644 index 0000000..ae6f73f --- /dev/null +++ b/frog-ph.py @@ -0,0 +1,30 @@ +# Paul Holt +# pcholt@gmail.com +# hereby committed to the PUBLIC DOMAIN + +def _crossing_hop_count(hops_remaining): + count = 0 + while hops_remaining > 0: + hops_remaining -= random.randrange(1,hops_remaining+1) + count += 1 + return count + +def crossing_histogram(pond_size, samples): + hist = [0 for i in xrange(pond_size+1)] + for i in xrange(samples): + hist[_crossing_hop_count(pond_size)] += 1 + return hist + +def average(samples=10**5, pond_size=3): + histogram = crossing_histogram(pond_size,samples) + total = 0 + for hop_count, occurrences in enumerate(histogram): + total += hop_count * occurrences + return (total/float(samples), histogram) + +def main(): + for pond_size in xrange(200): + print pond_size, average(pond_size=pond_size) + +if __name__ == '__main__': + main() \ No newline at end of file From 0694c11c2b324057ee17af88d16cfc51653024ed Mon Sep 17 00:00:00 2001 From: ph Date: Fri, 13 Sep 2019 23:29:16 +1000 Subject: [PATCH 2/2] command line runner --- frog-2.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/frog-2.py b/frog-2.py index c0b0eec..6eca0f0 100644 --- a/frog-2.py +++ b/frog-2.py @@ -1,5 +1,7 @@ +import random + def frog(final_dist=100,max=6): - + # I wrote this quickly in a pub # Don't judge me @@ -7,8 +9,6 @@ def frog(final_dist=100,max=6): while total_dist <= final_dist: - import random - cap = 10**max trials = 0 @@ -33,6 +33,10 @@ def frog(final_dist=100,max=6): total_dist += 1 return "DONE" - + +def main(): + frog() - \ No newline at end of file +if __name__ == '__main__': + main() +