Skip to content

Instantly share code, notes, and snippets.

@sor4chi
Last active March 13, 2024 04:48
Show Gist options
  • Select an option

  • Save sor4chi/dc2de39e395a24f8747c52d0c93f466b to your computer and use it in GitHub Desktop.

Select an option

Save sor4chi/dc2de39e395a24f8747c52d0c93f466b to your computer and use it in GitHub Desktop.
import numpy as np
max_values = list(map(int, input().split()))
dice_counts = np.arange(1, 6)
prior_probs = np.ones(len(dice_counts)) / len(dice_counts)
def likelihood(max_val, dice_count):
return (max_val / 100.0) ** dice_count - ((max_val - 1) / 100.0) ** dice_count
for max_val in max_values:
likelihoods = np.array(
[likelihood(max_val, dice_count) for dice_count in dice_counts]
)
prior_probs *= likelihoods
prior_probs /= prior_probs.sum()
estimated_dice_count = dice_counts[np.argmax(prior_probs)]
print(estimated_dice_count)
import numpy as np
import random
def predict(max_values):
dice_counts = np.arange(1, 6)
prior_probs = np.ones(len(dice_counts)) / len(dice_counts)
def likelihood(max_val, dice_count):
return (max_val / 100.0) ** dice_count - ((max_val - 1) / 100.0) ** dice_count
for max_val in max_values:
likelihoods = np.array(
[likelihood(max_val, dice_count) for dice_count in dice_counts]
)
prior_probs *= likelihoods
prior_probs /= prior_probs.sum()
estimated_dice_count = dice_counts[np.argmax(prior_probs)]
return estimated_dice_count
problems = []
for i in range(100000):
dice_count = random.randint(1, 5)
dice_max_values = []
for j in range(10):
dice_values = [random.randint(1, 100) for _ in range(dice_count)]
dice_max_values.append(max(dice_values))
problems.append((dice_max_values, dice_count))
success_count = 0
for max_values, answer in problems:
if predict(max_values) == answer:
success_count += 1
print(success_count / len(problems)) # 0.56361
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment