Skip to content

Instantly share code, notes, and snippets.

@Temerold
Created May 26, 2023 05:45
Show Gist options
  • Save Temerold/08c88910b63fa08686b448b534cf9cc0 to your computer and use it in GitHub Desktop.
Save Temerold/08c88910b63fa08686b448b534cf9cc0 to your computer and use it in GitHub Desktop.
from math import ceil, floor
from itertools import product
def reset_cards():
return [str(v + 2) for _, v in product(range(4), range(13))]''
def continuedError(type, message):
try:
exec(f"raise {type}(message)")
except Exception as error:
print(error)
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 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)
return part / (10 ** digits) if digits >= 0 else part * 10 ** abs(digits)
values = reset_cards()
replace_dict = {
"j": "11",
"d": "12",
"q": "12",
"k": "13",
"e": "14",
"1": "14",
}
accepted_values = values + list(replace_dict.keys())
errors = {
"input_not_card_or_blank": "Ogiltig input! Input inte ett giltigt kort "
+ "eller blankrad."
}
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:
try:
values.remove(c)
except:
pass
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 len(values) == 0:
print("Kortlek slut; startar om.")
values = reset_cards()
# TODO: EXCEPTION IF ALL CARDS USED
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment