Skip to content

Instantly share code, notes, and snippets.

@Temerold
Created January 10, 2025 21:49
Show Gist options
  • Save Temerold/e2a12474681e77479b2438d16916681a to your computer and use it in GitHub Desktop.
Save Temerold/e2a12474681e77479b2438d16916681a to your computer and use it in GitHub Desktop.
from itertools import product
from math import ceil, floor
def calc_prob(values, new_card):
lower, higher = [], []
for c in values:
if int(c) < int(new_card):
lower.append(c)
elif int(c) > int(new_card):
higher.append(c)
if len(lower) < len(higher):
result = "Högre"
elif len(lower) > len(higher):
result = "Lägre"
else:
result = "50 / 50"
combined = lower + higher
prob_lower = len(lower) / len(combined)
prob_higher = len(higher) / len(combined)
return result, prob_lower, prob_higher
def continuedError(type, message):
try:
exec(f"raise {type}(message)")
except Exception as error:
print(error)
def reset_cards():
return [str(v + 2) for _, v in product(range(4), range(13))]
def round(n, digits):
part = n * 10**digits
delta = part - int(part)
if delta >= 0.5 or -0.5 < delta <= 0:
part = ceil(part)
else:
part = floor(part)
values = reset_cards()
errors = {
"input_not_card_or_blank": "Ogiltig input! Input inte ett giltigt kort eller en"
"blankrad."
}
replace_dict = {
"j": "11",
"d": "12",
"q": "12",
"k": "13",
"e": "14",
"1": "14",
}
accepted_values = values + list(replace_dict.keys())
while True:
input_, new_card = None, None
cards = []
while input_ != "":
input_ = input(": ")
if input_ in accepted_values:
cards.append(input_)
accepted_values.remove(input_)
elif input_ == "del":
cards.pop(-1)
elif input_[:3] == "add":
values.append(str(input_[4:]))
print(f"Lade till {values[-1]}.")
elif input_ != "":
continuedError("ValueError", errors["input_not_card_or_blank"])
while new_card not in accepted_values:
new_card = input("Nytt kort: ")
if new_card in accepted_values:
cards.append(new_card)
else:
continuedError("ValueError", errors["input_not_card_or_blank"])
for i in replace_dict.items():
for index, item in enumerate(cards):
if item == i[0]:
cards[index] = i[1]
for c in cards:
values.remove(c)
result, prob_lower, prob_higher = calc_prob(values, cards[-1])
print(f"Lägre: {round(prob_lower * 100, 2)} %")
print(f"Högre: {round(prob_higher * 100, 2)} %")
print(f">{result}")
if not len(values):
print("Kortlek slut; startar om.")
values = reset_cards()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment