Last active
September 28, 2017 13:02
-
-
Save wiput1999/03444b97fcaa22f65f69cad157bd4635 to your computer and use it in GitHub Desktop.
PSIT #60 Midterm Exam (Blackjack, Turnstile, Donut, Sequence XXX)
This file contains 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
""" | |
PSIT Midterm Examination - Blackjack | |
Solutions by Teerapat Kraisrisirikul | |
""" | |
def main(): | |
""" Main function """ | |
#Counting variables | |
points = 0 | |
ace = 0 | |
#Count card points, must be 2 or 3 cards. | |
for _ in range(int(input())): | |
card = input() | |
if card == 'K' or card == 'Q' or card == 'J': | |
#King, Queen or Jack card. | |
points += 10 | |
elif card == 'A': | |
#Ace card, which can either be 1 or 11 points. | |
points += 1 | |
ace += 1 | |
else: | |
#Normal number cards. | |
points += int(card) | |
#Add 10 more points for each Ace card if it should be added. | |
while points <= 11 and ace > 0: | |
points += 10 | |
ace -= 1 | |
#Output | |
print(points) | |
main() |
This file contains 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
""" Blackjack """ | |
def main(): | |
""" Calculate the best result of 2 or 3 cards """ | |
amount = int(input()) | |
# Store total points | |
result = 0 | |
# Collect how many ace | |
ace = 0 | |
for _ in range(amount): | |
card = input() | |
# Add 10 to result for J Q K card | |
if card in "JQK": | |
result += 10 | |
continue | |
# Add 1 to result for A card | |
if card == "A": | |
result += 1 | |
ace += 1 | |
continue | |
# Add point depend on number card | |
else: | |
result += int(card) | |
# Change ACE to 11 when has ACE card and point need to less than 21 | |
if ace >= 1 and result <= 11: | |
result += 10 | |
print(result) | |
main() |
This file contains 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
""" | |
PSIT Midterm Examination - Donut | |
Solutions by Teerapat Kraisrisirikul | |
""" | |
def main(): | |
""" Main function """ | |
price, amount, free, demand = int(input()), int(input()), int(input()), int(input()) | |
demand_pack = demand//(amount+free) #Demanded packages of donut. | |
demand %= (amount+free) #Demanded extra donuts. | |
#Calculating on extra donuts | |
if demand < amount: | |
#Demand is less than the promotion amount. | |
pay = demand * price | |
get = demand | |
elif demand >= amount: | |
#Demand reaches the promotion amount. | |
pay = amount * price | |
get = amount+free | |
#Calculating on donut packages | |
pay += demand_pack * amount * price | |
get += demand_pack * (amount+free) | |
#Output | |
print(pay, get) | |
main() |
This file contains 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
""" | |
PSIT Midterm Examination - Sequence XXX | |
Solutions by Teerapat Kraisrisirikul | |
""" | |
def main(size, num): | |
""" Main function """ | |
#Spacing variables | |
side = 0 | |
center = size-4 | |
#Upper borders | |
print(("*" * size + " ") * num) | |
#Upper lines | |
for _ in range((size-3)//2): | |
print(("*" + " "*side + "*" + " "*center + "*" + " "*side + "* ") * num) | |
side += 1 | |
center -= 2 | |
#Middle line | |
if size > 1: | |
print(("*" + " "*side + "*" + " "*side + "* ") * num) | |
#Lower lines | |
for _ in range((size-3)//2): | |
side -= 1 | |
center += 2 | |
print(("*" + " "*side + "*" + " "*center + "*" + " "*side + "* ") * num) | |
#Lower borders | |
if size > 1: | |
print(("*" * size + " ") * num) | |
main(int(input()), int(input())) |
This file contains 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
""" Turnstile """ | |
def main(): | |
""" Count people that passed the door """ | |
# 0 = Unlocked, 1 = Locked (default = 1) | |
lock = 1 | |
# Initialize people passed the door with 0 | |
count = 0 | |
while True: | |
# Actions C is insert coin, P is push the door | |
action = input() | |
# Action END is the sign to print result and terminate program | |
if action == "END": | |
print(count) | |
break | |
# Insert coin to unlock the door when door is locked | |
if lock == 1 and action == "C": | |
lock = 0 | |
continue | |
# Push the door when door is locked can't pass the door | |
if lock == 1 and action == "P": | |
continue | |
# Insert coin when door is unlocked the door still unlocked | |
if lock == 0 and action == "C": | |
continue | |
# Push the unlocked door person can pass the door and reset door to lock | |
if lock == 0 and action == "P": | |
count += 1 | |
lock = 1 | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment