Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions 1] guessing game/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from random import randint as ri

def run():
hidden_number = ri(1, 100)

while True:
try:
user_number = int(input("Guess the number: "))
except ValueError as err:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as err je zbytečné, když s tím dále nepracuješ

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm jo to tam nemusi byt no

print(f"Your input: {user_number} is not a real number. Try again")
continue

if user_number < hidden_number:
print("too small")
elif user_number > hidden_number:
print("too big")
else:
print("you won")
return


if __name__ == "__main__":
run()
48 changes: 48 additions & 0 deletions 2] loto/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from random import choice

def collect_user_picks() -> set[int]:
print("Please pick 6 numbers. Each in range between 1-49\n")

picks = set()
count = 1
while count != 7:
try:
pick = int(input(f"Please pick {count}. number: "))
if not (1 <= pick <= 49):
print(f"Number {pick} has to be within 1-49 range, try again")
continue
if pick in picks:
print(f"Number {pick} can be picked only once, try again")
continue
except ValueError as e:
print("This is not a number, try again")
continue

count +=1
picks.add(pick)

return picks


def get_winning_numbers() -> set[int]:
all_numbers = [i for i in range(1, 50)]
winning_numbers = {choice(all_numbers) for _ in range(6)}

return winning_numbers


def run():

user_picks = collect_user_picks()
print(f"User picked: {sorted(user_picks)}")

winnig_numbers = get_winning_numbers()
print(f"Winning numbers are: {winnig_numbers}")

intersection = user_picks & winnig_numbers
print(f"user picked {len(intersection)} numbers that were in winning numbers.")



if __name__ == "__main__":
run()
25 changes: 25 additions & 0 deletions 3] guessing game 2/binary_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# this problem is classical application of binary search algorithm but add some more complexity because of the user inputs
# here is the version without user inputs (user is automated)

from random import randint as ri

def binary_search():
low, high = 1, 1000
user_number = ri(1, 1000)
print(f"I am the user, and I am thinking of the number {user_number}")

while low <= high:
mid = (low + high) >> 1

if user_number < mid:
print(f"You guessed {mid}, and it is too big")
high = mid - 1
elif user_number > mid:
print(f"You guessed {mid}, and it is too small")
low = mid + 1
else:
print(f"You guessed {mid}, and it is correct!")
return mid

if __name__ == "__main__":
binary_search()
35 changes: 35 additions & 0 deletions 3] guessing game 2/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# here is the version with the user inputs as asked in the course

def user_answer() -> str:
possible_answers = ["small", "big", "ok"]
while True:
answer = input()
if answer not in possible_answers:
print(f"You can answer only one of {possible_answers}")
continue
else:
return answer


def run():
low, high = 1, 1000
print(f"OK, I (the user) am thinking of number between {low} and {high} and you (the computer) must guess it")
print("After each guess I will give you answer, if your guess was too small or big\n'\n")

while low <= high:
mid = (low + high) >> 1

print(f"I (the computer) am guessing {mid}. Is it small or big?")

answer = user_answer()
if answer == "big":
high = mid - 1
elif answer == "small":
low = mid + 1
else:
return mid

if __name__ == "__main__":
run()


1 change: 1 addition & 0 deletions 4] guessing game 3/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nedelal jsem, je tam moc HTML
Empty file removed 5] 2001/main.py
Empty file.
33 changes: 33 additions & 0 deletions 5] dice/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import re
from random import randint as ri

def parse(code: str) -> tuple[str]:
pattern = re.compile(r'(\d*)D(3|4|6|8|10|12|20|100)([+-]\d+)?')
matched = pattern.match(code)

if matched:
rolls = int(matched.group(1)) if matched.group(1) else 1
dice = int(matched.group(2))
extras = int(matched.group(3)) if matched.group(3) else 0

return rolls, dice, extras

raise ValueError(f"The code {code} is invalid")


def simulate(code: str) -> int:
try:
rolls, dice, extras = parse(code)
except ValueError as ex:
raise ValueError(f"The code '{code}' is invalid, simulation cannot continue.") from ex

result = extras
for _ in range(rolls):
result += ri(1, dice)

return result


if __name__ == "__main__":

result = simulate("2D10+10")
1 change: 1 addition & 0 deletions 6] 2001/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nedelal jsem nebyl cas