Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I know no one asked for it, but here's a slightly simplified version of this script in Python:

    from itertools import count
    from statistics import mean

    INITIAL_PAYMENT = 5  # dollars paid if first guess is correct, minus 1 for each incorrect guess
    RANGE = range(1, 101)  # the range of numbers to guess from

    def simulate_guess(target):
        low = min(RANGE)
        high = max(RANGE)
        for bad_guesses in count():
            match guess := (low + high) // 2:
                case _ if guess < target:
                    low = guess + 1
                case _ if guess > target:
                    high = guess - 1
                case target:
                    return INITIAL_PAYMENT - bad_guesses

    average_payout = mean(map(simulate_guess, RANGE))
    print(f"\nExpected value per game: ${average_payout:0.2f}")


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: