Scientific Computing with Python Projects - Probability Calculator

Tell us what’s happening:
I’m getting an import error when using this with replit, the error is on line 41

import random
class Hat:
    def __init__(self, **kwargs):
        colors = list()
        for c,o in kwargs.items():
            for i in range(o):
                colors.append(c)
        self.contents = colors

    def draw(self, count):
        c = 0
        for i in range(len(self.contents)): c += 1
        c_1 = c
        c -= 1
        rb = list()
        for i in range(count):
            if c_1 < count:
                return self.contents
            rb.append(self.contents.pop(random.randint(0,c)))
            c -= 1
        return rb

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    expected_balls = list(expected_balls.items())
    m = 0
    j = hat
    ittm = 0
    itsm = 0
    for i in range(len(expected_balls)): ittm += 1
    rl = dict()
    for irr in range(num_experiments):
        e = j.draw(num_balls_drawn)
        for i in e:
            rl[i] = rl.get(i, 0) + 1
        rl = list(rl.items())
        for g in range(len(expected_balls)):
            for l in range(len(rl)):
                if rl[l][0] == expected_balls[g][0] and rl[l][1] >= expected_balls[g][1]:
                    itsm += 1
        if ittm == itsm:
            m += 1
        e = None
        rl = dict()
    return m/num_experiments

**Your code so far**
import random
class Hat:
    def __init__(self, **kwargs):
        colors = list()
        for c,o in kwargs.items():
            for i in range(o):
                colors.append(c)
        self.contents = colors

    def draw(self, count):
        c = 0
        for i in range(len(self.contents)): c += 1
        c_1 = c
        c -= 1
        rb = list()
        for i in range(count):
            if c_1 < count:
                return self.contents
            rb.append(self.contents.pop(random.randint(0,c)))
            c -= 1
        return rb

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    expected_balls = list(expected_balls.items())
    m = 0
    j = hat
    ittm = 0
    itsm = 0
    for i in range(len(expected_balls)): ittm += 1
    rl = dict()
    for irr in range(num_experiments):
        e = j.draw(num_balls_drawn)
        for i in e:
            rl[i] = rl.get(i, 0) + 1
        rl = list(rl.items())
        for g in range(len(expected_balls)):
            for l in range(len(rl)):
                if rl[l][0] == expected_balls[g][0] and rl[l][1] >= expected_balls[g][1]:
                    itsm += 1
        if ittm == itsm:
            m += 1
        e = None
        rl = dict()
    return m/num_experiments

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Scientific Computing with Python Projects - Probability Calculator

Link to the challenge:

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

What’s the error exactly?

this:

  File "main.py", line 2, in <module>
    import prob_calculator
  File "/home/runner/boilerplate-probability-calculator/prob_calculator.py", line 2, in <module>
    import random
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/random.py", line 41, in <module>
    from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
ImportError: /nix/store/65hafbsx91127farbmyyv4r5ifgjdg43-glibc-2.33-117/lib/libpthread.so.0: undefined symbol: __libc_siglongjmp, version GLIBC_PRIVATE

Open the .replit file (it’s hidden, to see hidden files on replit click the three dots at the right of files tab, and then select Show hidden files ), then change the channel under the [nix] to stable-21_11

The test suite does use a fixed seed to ensure reproduciblity. Is that what you are asking about? If not, more details would be helpful.

Tell us what’s happening:
Every time I run the code, the list returned from the draw is random for about the first or second list and then it just defaults to three green or three reds

Your code so far
Main.py

from unittest import main

prob_calculator.random.seed(95)
hat = prob_calculator.Hat(blue=4, red=2, green=6)
probability = prob_calculator.experiment(
    hat=hat,
    expected_balls={"blue": 2,
                    "red": 1},
    num_balls_drawn=4,
    num_experiments=3000)
print("Probability:", probability)

# Run unit tests automatically
main(module='test_module', exit=False)
prob_calculator.py
import copy
import random
# Consider using the modules imported above.

class Hat:
    def __init__(self, **kwargs):
            colors = list()
            for c,o in kwargs.items():
                for i in range(o):
                    colors.append(c)
            self.contents = colors
    
    def draw(self, count):
        c = 0
        for i in range(len(self.contents)): c += 1
        c_1 = c
        c -= 1
        rb = list()
        for i in range(count):
            if c_1 < count:
                return self.contents
            rb.append(self.contents.pop(random.randint(0,c)))
            c -= 1
        return rb

def experiment(hat, expected_balls, num_balls_drawn, num_experiments):
    expected_balls = list(expected_balls.items())
    m = 0
    j = hat
    ittm = 0
    itsm = 0
    for i in range(len(expected_balls)): ittm += 1
    rl = dict()
    for irr in range(num_experiments):
        e = j.draw(num_balls_drawn)
        for i in e:
            rl[i] = rl.get(i, 0) + 1
        rl = list(rl.items())
        for g in range(len(expected_balls)):
            for l in range(len(rl)):
                if rl[l][0] == expected_balls[g][0] and rl[l][1] >= expected_balls[g][1]:
                    itsm += 1
        if ittm == itsm:
            m += 1
        e = None
        rl = dict()
    return m/num_experiments

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Scientific Computing with Python Projects - Probability Calculator

Link to the challenge:

I don’t see how you are resetting the hat between experiments

Each experiment consists of starting with a hat containing the specified balls, drawing several balls, and checking if you got the balls you were attempting to draw.