From 15ddf5024d1a7fb7b27edb3bb80b93d916b0806b Mon Sep 17 00:00:00 2001 From: hakanyorukk Date: Thu, 27 Aug 2026 11:25:46 +0300 Subject: [PATCH] add pig game --- dsa/d1/exercise_4.py | 22 +++++ dsa/d2/exercise_4.py | 21 +++++ dsa/d3/exercise_3.py | 21 +++++ dsa/d4/exercise_5.py | 26 ++++++ dsa/d5/exercise_4.py | 17 ++++ hashing/task5/exercise_6.py | 13 +++ hashing/task5/exercise_7.py | 33 ++++++++ oop/exercise_2/main.py | 1 - pig_game/exercise_1.py | 43 ++++++++++ pig_game/exercise_2.py | 70 ++++++++++++++++ pig_game/exercise_3.py | 46 +++++++++++ pig_game/exercise_4.py | 82 +++++++++++++++++++ pig_game/exercise_5.py | 71 ++++++++++++++++ .../Employee.py | 0 .../exercise_1.py | 0 word-pair-frequency/exercise_1.py | 49 +++++++++++ word-pair-frequency/exercise_2.py | 5 ++ word-pair-frequency/sample.txt | 3 + 18 files changed, 522 insertions(+), 1 deletion(-) create mode 100644 dsa/d1/exercise_4.py create mode 100644 dsa/d2/exercise_4.py create mode 100644 dsa/d3/exercise_3.py create mode 100644 dsa/d4/exercise_5.py create mode 100644 dsa/d5/exercise_4.py create mode 100644 hashing/task5/exercise_6.py create mode 100644 hashing/task5/exercise_7.py create mode 100644 pig_game/exercise_1.py create mode 100644 pig_game/exercise_2.py create mode 100644 pig_game/exercise_3.py create mode 100644 pig_game/exercise_4.py create mode 100644 pig_game/exercise_5.py rename searching_sorting/{builtin_search => builtin_sort}/Employee.py (100%) rename searching_sorting/{builtin_search => builtin_sort}/exercise_1.py (100%) create mode 100644 word-pair-frequency/exercise_1.py create mode 100644 word-pair-frequency/exercise_2.py create mode 100644 word-pair-frequency/sample.txt diff --git a/dsa/d1/exercise_4.py b/dsa/d1/exercise_4.py new file mode 100644 index 0000000..664713e --- /dev/null +++ b/dsa/d1/exercise_4.py @@ -0,0 +1,22 @@ +def main(): + nums = [2, 7, 11, 15, 3, 4, 8, 1] + target = 11 + + print(two_sum(nums, target)) + +def two_sum(nums, target): + # value, index + num_map = {} + seen = set() + for i in range(len(nums)): + required_num = target - nums[i] + + if required_num in num_map: + #return [nums[i], required_num] + seen.add((min(nums[i], required_num), max( nums[i], required_num))) + num_map[nums[i]] = i + #return None + return sorted(seen) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/dsa/d2/exercise_4.py b/dsa/d2/exercise_4.py new file mode 100644 index 0000000..a0c2744 --- /dev/null +++ b/dsa/d2/exercise_4.py @@ -0,0 +1,21 @@ +def main(): + s = "xyzxy" + print(longest_substring(s)) + +def longest_substring(s): + best_start=0 + max_length=0 + left = 0 + seen = set() + for right in range(len(s)): + while s[right] in seen: + seen.remove(s[left]) + left+=1 + seen.add(s[right]) + if right - left +1 >max_length: + max_length=right-left+1 + best_start=left + return s[best_start:best_start+max_length] + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/dsa/d3/exercise_3.py b/dsa/d3/exercise_3.py new file mode 100644 index 0000000..5c2a15e --- /dev/null +++ b/dsa/d3/exercise_3.py @@ -0,0 +1,21 @@ +def main(): + s = "race a car" + print(palindrome(s)) + +def palindrome(s): + clean_s ="" + for c in s: + if c.isdigit() or c.isalpha(): + clean_s+=c + + left = 0 + right = len(clean_s)-1 + while left < right: + if clean_s[left] != clean_s[right]: + return False + left+=1 + right-=1 + return True + +if __name__ == "__main__": + main() diff --git a/dsa/d4/exercise_5.py b/dsa/d4/exercise_5.py new file mode 100644 index 0000000..66c4174 --- /dev/null +++ b/dsa/d4/exercise_5.py @@ -0,0 +1,26 @@ +def main(): + nums = [-2, 1, -3, 4, -1, 2, 1, -5, 4] # → [4, -1, 2, 1] + print(kadane_algorithm(nums)) + +def kadane_algorithm(nums): + window_sum = nums[0] + max_sum = nums[0] + temp_start = 0 + best_start = 0 + best_end = 0 + for i in range(1, len(nums)): + + if window_sum + nums[i] > nums[i]: + window_sum = window_sum + nums[i] + else: + window_sum = nums[i] + temp_start = i + if window_sum > max_sum: + max_sum = window_sum + best_start = temp_start + best_end = i + + return nums[best_start:best_end+1] + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/dsa/d5/exercise_4.py b/dsa/d5/exercise_4.py new file mode 100644 index 0000000..33eed0b --- /dev/null +++ b/dsa/d5/exercise_4.py @@ -0,0 +1,17 @@ +from collections import defaultdict + + +def main(): + words = ["eat", "tea", "tan", "ate", "nat", "bat"] + print(algorithm(words)) + +def algorithm(words): + word_dict = defaultdict(list) + for word in words: + key = "".join(sorted(word)) + word_dict[key].append(word) + + return dict(word_dict) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/hashing/task5/exercise_6.py b/hashing/task5/exercise_6.py new file mode 100644 index 0000000..9f2aa0d --- /dev/null +++ b/hashing/task5/exercise_6.py @@ -0,0 +1,13 @@ +def main(): + s = "rat" + t = "tar" + print(is_anagram(s,t)) + +def is_anagram(s,t): + + if "".join(sorted(s)) != "".join(sorted(t)): + return False + return True + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/hashing/task5/exercise_7.py b/hashing/task5/exercise_7.py new file mode 100644 index 0000000..5463bc6 --- /dev/null +++ b/hashing/task5/exercise_7.py @@ -0,0 +1,33 @@ +from collections import Counter + + +def main(): + s = "rat" + t = "tar" + print(is_anagram_2(s,t)) + +def is_anagram(s,t): + if len(s) != len(t): + return False + char_count = {} + # value, count + for i in s: + char_count[i] = char_count.get(i, 0) + 1 + + for j in t: + if j not in char_count or char_count[j] == 0: + return False + char_count[j] = char_count.get(j, 0) - 1 + return True + +def is_anagram_2(s,t): + # count_s = Counter(c for c in s) + # count_t = Counter(c for c in t) + # + # if count_s==count_t: + # return True + # return False + return Counter(s)==Counter(t) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/oop/exercise_2/main.py b/oop/exercise_2/main.py index 40b7a19..ddb07b9 100644 --- a/oop/exercise_2/main.py +++ b/oop/exercise_2/main.py @@ -36,6 +36,5 @@ def main(): print(len([(x, y) for x in log for y in log]) == 36) # nested loop → re-iterable - if __name__ == "__main__": main() \ No newline at end of file diff --git a/pig_game/exercise_1.py b/pig_game/exercise_1.py new file mode 100644 index 0000000..ff27488 --- /dev/null +++ b/pig_game/exercise_1.py @@ -0,0 +1,43 @@ +import random + +def main(): + totals = [0,0] + current = 0 + mode = input("Play against bot (b) or two players (h)? ") + while True: + print(f"\n--- Player {current}'s turn ---") + is_bot = (mode == "b" and current == 1) + totals[current] += play_turn(is_bot) + print(f"Player {current} total: {totals[current]}") + if totals[current] >= 100: + winner = "Bot" if is_bot else "Player" + print(f"{winner} {current} won!") + break + current = 1-current + +def play_turn(is_bot): + turn_score = 0 + while True: + die = roll_die() + if die == 1: + print("Rolled 1 Bust!") + return 0 + turn_score += die + print(f"Rolled {die}, turn score: {turn_score}") + + if is_bot: + choice = bot_decision(turn_score) + print(f"Bot {'holds' if choice == 'h' else 'rolls again'}") + else: + choice = input("roll again? (r/h)") + + if choice == "h": + return turn_score + +def roll_die(): + return random.randint(1,6) + +def bot_decision(turn_score): + return "h" if turn_score >= 20 else "r" +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/pig_game/exercise_2.py b/pig_game/exercise_2.py new file mode 100644 index 0000000..aa49feb --- /dev/null +++ b/pig_game/exercise_2.py @@ -0,0 +1,70 @@ +import random +from abc import ABC, abstractmethod + +class Player(ABC): + def __init__(self, name): + self.name = name + self.total = 0 + + @abstractmethod + def decide(self, turn_score): + pass + +class HumanPlayer(Player): + def decide(self, turn_score): + return input("Hold or roll again (h/r) ") + +class BotPlayer(Player): + def __init__(self, name, hold_at=20): + super().__init__(name) + self.hold_at=hold_at + + def decide(self, turn_score): + choice = "h" if turn_score >= self.hold_at else "r" + print(f"{self.name} {'holds' if choice == 'h' else 'rolls again'}") + return choice + +class Game: + def __init__(self, players, target=100): + self.players=players + self.target=target + self.current=0 + + def play_turn(self,player): + turn_score = 0 + + while True: + die = random.randint(1,6) + if die == 1: + print("Rolled 1 - Bust!") + return 0 + + turn_score += die + print(f"Die: {die} - Turn score: {turn_score}") + + if player.decide(turn_score) == "h": + return turn_score + + def play(self): + while True: + player = self.players[self.current] + print(f"\n--- {player.name}'s turn ---") + player.total += self.play_turn(player) + print(f"{player.name} total: {player.total}") + if player.total >= self.target: + print(f"\n{player.name} won!") + break + self.current = 1 - self.current + +def main(): + mode = input("Play against bot (b) or two players (p)? ") + if mode == "b": + players = [HumanPlayer("You"), BotPlayer("Bot")] + else: + players = [HumanPlayer("Player 1"), HumanPlayer("Player 2")] + + game = Game(players) + game.play() + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/pig_game/exercise_3.py b/pig_game/exercise_3.py new file mode 100644 index 0000000..49c2994 --- /dev/null +++ b/pig_game/exercise_3.py @@ -0,0 +1,46 @@ +import random + +def main(): + totals = [0, 0] + current = 0 + mode = input("Play against player(p) or bot(b)") + + while True: + print(f"\n--- Player {current} rolls ---") + is_bot = True if mode == "b" and current == 1 else False + totals[current]+=play(is_bot) + print(f"Player: {current}, score: {totals[current]}") + + if totals[current] >= 100: + winner = "Bot" if is_bot else "Player" + print(f"{winner} won!") + break + + current = 1 - current + +def play(is_bot): + turn_score = 0 + + while True: + die = roll_dice() + if die == 1: + print("Rolled 1 - bust!") + return 0 + turn_score+=die + print(f"Rolled {die}, turn score: {turn_score}") + if is_bot: + decision = bot_decision(turn_score) + else: + decision = input("Roll(r) again or hold(h)") + if decision == "h": + return turn_score + + +def roll_dice(): + return random.randint(1,6) + +def bot_decision(turn_score): + return "h" if turn_score >= 20 else "r" + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/pig_game/exercise_4.py b/pig_game/exercise_4.py new file mode 100644 index 0000000..ca6c2b4 --- /dev/null +++ b/pig_game/exercise_4.py @@ -0,0 +1,82 @@ +import random +from abc import ABC, abstractmethod + +class Player(ABC): + def __init__(self, name): + self.name = name + self.total_score = 0 + + @abstractmethod + def decide(self, current_score): + pass + +class HumanPlayer(Player): + + def decide(self, current_score): + while True: + decision = input("Roll again (r) or hold (h): ").lower() + + if decision in ("r", "h"): + return decision + + print("Invalid input. Enter 'r' or 'h'.") + +class BotPlayer(Player): + def __init__(self, name, hold_at=20): + super().__init__(name) + self.hold_at=hold_at + + def decide(self,current_score,): + return "h" if current_score>=self.hold_at else "r" + +class Game: + + def __init__(self, players, target=100): + self.players = players + self.target = target + self.current = 0 + + def play_turn(self, player): + turn_score = 0 + while True: + die = random.randint(1, 6) + + if die == 1: + print("Rolled 1 - Bust!") + return 0 + + turn_score+=die + print(f"Rolled {die}, turn score: {turn_score}") + + decision = player.decide(turn_score) + + if decision == "h": + return turn_score + + def play(self): + + while True: + player = self.players[self.current] + print(f"\n---{player.name} turns ---") + player.total_score += self.play_turn(player) + print(f"{player.name}, score = {player.total_score}") + + if player.total_score >= self.target: + print(f"{player.name} won!") + break + self.current = 1 - self.current + + + +def main(): + + mode = input("Play against another player(p) or bot(b)") + if mode == "b": + players = [HumanPlayer("You"), BotPlayer("Bot")] + else: + players = [HumanPlayer("Player 1"), HumanPlayer("Player 2")] + game = Game(players) + game.play() + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/pig_game/exercise_5.py b/pig_game/exercise_5.py new file mode 100644 index 0000000..c7bb620 --- /dev/null +++ b/pig_game/exercise_5.py @@ -0,0 +1,71 @@ +import random +from abc import ABC, abstractmethod + + +class Player(ABC): + + def __init__(self, name): + self.name = name + self.total = 0 + @abstractmethod + def decide(self, turn_score): + pass + +class HumanPlayer(Player): + + def decide(self, turn_score): + return input("Roll again(r) or hold(h)") + +class BotPlayer(Player): + + def __init__(self, name, hold_at=20): + super().__init__(name) + self.hold_at = hold_at + + + def decide(self, turn_score): + choice = "h" if turn_score >= self.hold_at else "r" + print(f"{self.name} {'holds' if choice == 'h' else 'rolls again'}") + return choice + +class Game(): + + def __init__(self, players, targe=100): + self.players = players + self.target = 100 + self.current = 0 + + def play_turn(self, player): + turn_score = 0 + while True: + die = random.randint(1,6) + + if die == 1: + print("Rolled 1 - Bust!") + return 0 + turn_score+=die + print(f"Rolled: {die}, score: {turn_score}") + + if player.decide(turn_score) == "h": + return turn_score + + def play(self): + while True: + player = self.players[self.current] + print(f"\n--- {player.name} turns ---") + player.total += self.play_turn(player) + print(f"{player.name} total: {player.total}") + if player.total >= self.target: + print(f"\n{player.name} won!") + break + self.current = 1 - self.current + +def main(): + mode = input("Play against bot(b) or human(h)") + if mode == "b": + game = Game([HumanPlayer("You"), BotPlayer("Bot")]) + else: + game = Game([HumanPlayer("Player 1"), BotPlayer("Player 2")]) + game.play() +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/searching_sorting/builtin_search/Employee.py b/searching_sorting/builtin_sort/Employee.py similarity index 100% rename from searching_sorting/builtin_search/Employee.py rename to searching_sorting/builtin_sort/Employee.py diff --git a/searching_sorting/builtin_search/exercise_1.py b/searching_sorting/builtin_sort/exercise_1.py similarity index 100% rename from searching_sorting/builtin_search/exercise_1.py rename to searching_sorting/builtin_sort/exercise_1.py diff --git a/word-pair-frequency/exercise_1.py b/word-pair-frequency/exercise_1.py new file mode 100644 index 0000000..7092849 --- /dev/null +++ b/word-pair-frequency/exercise_1.py @@ -0,0 +1,49 @@ +from collections import Counter + +def normalize(text): + words = text.split() + stripped_words = [] + for word in words: + stripped_word = "".join(ch.lower() for ch in word if ch.isalpha()) + if stripped_word: + stripped_words.append(stripped_word) + return stripped_words + +def word_pairs(words): + pairs = [] + for i in range(len(words)-1): + pairs.append((words[i], words[i+1])) + return pairs + +def top_pairs(text, n=10): + words = normalize(text) + return Counter(word_pairs(words)).most_common(n) + +def read_text(path): + # file-> python object load + + try: + with open(path, "r") as file: + return file.read() + + except FileNotFoundError as e: + print(f"Path not found {str(e)}") + return "" + +def main(): + text = read_text("sample.txt") + + print(normalize("The dog. The DOG!") == ["the", "dog", "the", "dog"]) + print(word_pairs(["a", "b", "c"]) == [("a", "b"), ("b", "c")]) + print(word_pairs(["only"]) == []) + print(word_pairs([]) == []) + print(len(normalize(text)) == 23) + print(len(word_pairs(normalize(text))) == 22) + print(top_pairs(text, 3) == [(('the', 'quick'), 3), (('quick', 'brown'), 3), (('brown', 'fox'), 3)]) + print(len(top_pairs(text, 100)) == 19) # fewer pairs exist than requested + print(top_pairs("", 5) == []) + print(top_pairs("hello", 5) == []) # one word = no pairs + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/word-pair-frequency/exercise_2.py b/word-pair-frequency/exercise_2.py new file mode 100644 index 0000000..e118d85 --- /dev/null +++ b/word-pair-frequency/exercise_2.py @@ -0,0 +1,5 @@ +def main(): + pass + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/word-pair-frequency/sample.txt b/word-pair-frequency/sample.txt new file mode 100644 index 0000000..37c3eeb --- /dev/null +++ b/word-pair-frequency/sample.txt @@ -0,0 +1,3 @@ +The quick brown fox jumps over the lazy dog. +The quick brown fox is quick! +the dog sleeps; the quick brown fox runs. \ No newline at end of file