Created
January 31, 2025 13:32
-
-
Save HasanKhatib/99887d0daff03ccd1d7aaab9d2aad92a to your computer and use it in GitHub Desktop.
ai-data-management-lab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Exercise A.0.1 - Cinnamon Buns Price Calculator | |
PRICE_REGULAR = 35 | |
DISCOUNT_RATE = 0.60 | |
num_buns = int(input("Enter the number of day-old Kanelbulle: ")) | |
# Price calculations | |
regular_price = num_buns * PRICE_REGULAR | |
discount_amount = regular_price * DISCOUNT_RATE | |
total_price = regular_price - discount_amount | |
print(f"Regular Price: {regular_price:8.2f} SEK") | |
print(f"Discount: {discount_amount:8.2f} SEK") | |
print(f"Total Price: {total_price:8.2f} SEK") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Exercise A.0.2 - Human Years to Dog Years | |
def human_to_dog_years(human_years): | |
if human_years < 0: | |
return "Error: Age cannot be negative." | |
elif human_years <= 2: | |
return human_years * 10.5 | |
else: | |
return (2 * 10.5) + ((human_years - 2) * 4) | |
human_years = float(input("Enter the human years: ")) | |
dog_years = human_to_dog_years(human_years) | |
print(f"Equivalent dog years: {dog_years}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Exercise A.0.3 - Prime Number Checker | |
# determines whether a given integer is a prime number. | |
def is_prime(n): | |
if n < 2: | |
return False | |
for i in range(2, int(n ** 0.5) + 1): | |
if n % i == 0: | |
return False | |
return True | |
num = int(input("Enter a number: ")) | |
if is_prime(num): | |
print(f"{num} is a prime number.") | |
else: | |
print(f"{num} is not a prime number.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Exercise A.0.4 - Find the Next Prime Number | |
# finds and returns the first prime number greater than a given integer. Uses code from previous file | |
from lab3 import is_prime | |
def next_prime(n): | |
n += 1 | |
while not is_prime(n): | |
n += 1 | |
return n | |
num = int(input("Enter a number: ")) | |
print(f"The next prime number after {num} is {next_prime(num)}.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Exercise A.0.5 - Coin Flip Until 3 Consecutive Matches | |
import random | |
def flip_coin(): | |
return "H" if random.choice([True, False]) else "T" | |
flips = [] | |
count = 0 | |
while True: | |
outcome = flip_coin() | |
flips.append(outcome) | |
count += 1 | |
if len(flips) >= 3 and flips[-3:] in (["H", "H", "H"], ["T", "T", "T"]): | |
break | |
print(" ".join(flips)) | |
print(f"Total flips: {count}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Exercise A.0.6 - Creating and Shuffling a Deck of Cards | |
# generates a standard 52-card deck and shuffles it. | |
import random | |
def create_deck(): | |
suits = ['s', 'h', 'd', 'c'] | |
values = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'] | |
return [v + s for v in values for s in suits] | |
def shuffle_deck(deck): | |
shuffled_deck = deck[:] | |
for i in range(len(shuffled_deck)): | |
j = random.randint(0, len(shuffled_deck) - 1) | |
shuffled_deck[i], shuffled_deck[j] = shuffled_deck[j], shuffled_deck[i] | |
return shuffled_deck | |
deck = create_deck() | |
shuffled_deck = shuffle_deck(deck) | |
print("Before shuffling:", deck) | |
print("After shuffling:", shuffled_deck) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Exercise A.0.7 - Dealing Cards Using a Class | |
# creates a deck, shuffles it, and deals a specified number of hands. | |
import random | |
class Cards: | |
def __init__(self): | |
self.cards = [] | |
def create(self): | |
suits = ['♠ spade', '♥ heart', '♦ diamond', '♣ club'] | |
values = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'] | |
self.cards = [v + ' ' + s for v in values for s in suits] | |
def shuffle(self): | |
random.shuffle(self.cards) | |
def deal(self, hands, card_num): | |
if hands * card_num > len(self.cards): | |
print("Not enough cards to deal.") | |
return [] | |
dealt_hands = [[] for _ in range(hands)] | |
for i in range(card_num): | |
for j in range(hands): | |
dealt_hands[j].append(self.cards.pop(0)) | |
return dealt_hands | |
deck = Cards() | |
deck.create() | |
deck.shuffle() | |
num_players = int(input("Enter the number of players: ")) | |
num_cards = int(input("Enter the number of cards per player: ")) | |
dealt_hands = deck.deal(num_players, num_cards) | |
for i, hand in enumerate(dealt_hands, 1): | |
print(f"Player {i}: {hand}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment