Post Reply 
Little explorations with HP calculators (no Prime)
12-27-2018, 10:20 PM
Post: #323
RE: Little explorations with HP calculators (no Prime)
(12-27-2018 04:59 PM)DavidM Wrote:  please be kind

Here's a variant of your program:
Code:
import random

def match_count(thelist):
    return sum(1 for blue in
        (sum(thelist[i:i+70])
            for i in range(71))
                if blue == 50)

def main(n):
    thelist = [1]*100 + [0]*40
    result = [0] * 72
    for _ in range(n):
        random.shuffle(thelist)
        result[match_count(thelist)] += 1
    for i, value in enumerate(result[1:], start=1):
        print('%d\t%d' % (i, value))

if __name__ == '__main__':
    main(1000000)

The function match_count uses generator expressions.
And since random.shuffle shuffles in place we can reuse thelist.
You can use enumerate to iterate over a list when both index and value are needed.

Cheers
Thomas
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Little explorations with HP calculators (no Prime) - Thomas Klemm - 12-27-2018 10:20 PM



User(s) browsing this thread: 1 Guest(s)