Skip to content

Instantly share code, notes, and snippets.

@chrisma
Last active March 10, 2020 12:16
Show Gist options
  • Save chrisma/7e459437c418f2a18a01a55a734c0cbd to your computer and use it in GitHub Desktop.
Save chrisma/7e459437c418f2a18a01a55a734c0cbd to your computer and use it in GitHub Desktop.
Coding exercise
#!/usr/bin/env python3
# In this bonus challenge, you'll need to check the winner of the battle but this time, a sorcerer wins if he succeeds in winning 3 spell clashes in a row.
#
# 1. Create variables POWER, gandalf and saruman as seen above.
#
# spells have names, dictionary that associates that name to a power.
POWER = {
'Fireball': 50,
'Lightning bolt': 40,
'Magic arrow': 10,
'Black Tentacles': 25,
'Contagion': 45
}
gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt', 'Magic arrow', 'Fireball',
'Magic arrow', 'Lightning bolt', 'Fireball', 'Fireball', 'Fireball']
saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles',
'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']
#
# 1.a Create a variable called spells to store the number of spells that the sorcerers cast.
#
# Not entirely sure what this is needed for
spells = 0
#
# 2. Create two variables called gandalf_wins and saruman_wins. Set both of them to 0
#
gandalf_wins = 0
saruman_wins = 0
#
# 3. Create two variables called gandalf_power and saruman_power to store the list of spell powers of each sorcerer.
#
gandalf_power = []
saruman_power = []
# Iterate over the spell names and look up their power in the POWER dictionary
for spell_name in gandalf:
# Alternative: gandalf_power = gandalf_power + [POWER[spell_name]]
# Alternative: gandalf_power += [POWER[spell_name]]
gandalf_power.append(POWER[spell_name])
for spell_name in saruman:
saruman_power.append(POWER[spell_name])
print(f"Gandalf's spell powers: {gandalf_power}")
print(f"Saruman's spell powers: {saruman_power}")
#
# 4. The battle starts! Using the variables you've created above, code the execution of spell clashes.
# Remember that a sorcerer wins if he succeeds in winning 3 spell clashes in a row.
# If a clash ends up in a tie, the counter of wins in a row is not restarted to 0.
# Remember to print who is the winner of the battle
#
print(f"Gandalf will cast {len(gandalf_power)} spells")
print(f"Saruman will cast {len(saruman_power)} spells")
# Make sure that the amount of spells is the same, so that we can use the same index for both
assert len(gandalf_power) == len(saruman_power)
print(f"{'*'*10} BATTLE START {'*'*10}")
# Store last round's winner to know if it's a win in a row
last_winner = None
# Use a counting variable for battle rounds to look up wizard's spell powers for each round
# There are as many rounds as there are spells
for battle_round in range(len(gandalf_power)):
# Get the spell powers for this round
g_spell_power = gandalf_power[battle_round]
s_spell_power = saruman_power[battle_round]
print(f"> Round {battle_round}! Gandalf: {g_spell_power}, Saruman: {s_spell_power}")
# Nothing happens if it's a tie
if g_spell_power == s_spell_power:
print(f"It's a tie!")
if g_spell_power > s_spell_power:
# If the wizard didn't win last round, this is his first win in a row
if last_winner != 'Gandalf':
last_winner = 'Gandalf'
gandalf_wins = 1
else:
# The wizard won last round as well
gandalf_wins = gandalf_wins + 1
print(f"Gandalf wins the round! {gandalf_wins} round(s) in a row.")
if g_spell_power < s_spell_power:
if last_winner != 'Saruman':
last_winner = 'Saruman'
saruman_wins = 1
else:
saruman_wins = saruman_wins + 1
print(f"Saruman wins the round! {saruman_wins} round(s) in a row.")
# Wizards win the spell clash if they win 3 rounds in a row
if gandalf_wins == 3:
print("GANDALF WINS THE BATTLE!")
# Someone won, break out of the loop
break
if saruman_wins == 3:
print("SARUMAN WINS THE BATTLE!")
break
# If the loop finishes without breaking and this code is executed, noone reached 3 wins
print("Noone won 3 rounds in a row yet.")
#!/usr/bin/env python3
# In this bonus challenge, you'll need to check the winner of the battle but this time, a sorcerer wins if he succeeds in winning 3 spell clashes in a row.
POWER = {
'Fireball': 50,
'Lightning bolt': 40,
'Magic arrow': 10,
'Black Tentacles': 25,
'Contagion': 45
}
gandalf = ['Fireball', 'Lightning bolt', 'Lightning bolt', 'Magic arrow', 'Fireball',
'Magic arrow', 'Lightning bolt', 'Fireball', 'Fireball', 'Fireball']
saruman = ['Contagion', 'Contagion', 'Black Tentacles', 'Fireball', 'Black Tentacles',
'Lightning bolt', 'Magic arrow', 'Contagion', 'Magic arrow', 'Magic arrow']
assert len(gandalf) == len(saruman)
last_winner = None
wins = {}
print(f"{'*'*10} BATTLE START {'*'*10}")
for gandalf_spell, saruman_spell in zip(gandalf, saruman):
print(f"> Round start! Gandalf's {gandalf_spell} vs. Saruman's {saruman_spell}")
# A tie does not reset the consecutive wins counter
if POWER[gandalf_spell] == POWER[saruman_spell]:
print("It's a tie!")
continue
if POWER[gandalf_spell] > POWER[saruman_spell]:
winner = 'Gandalf'
if POWER[gandalf_spell] < POWER[saruman_spell]:
winner = 'Saruman'
if last_winner != winner:
wins[winner] = 1
else:
wins[winner] += 1
last_winner = winner
print(f"{winner} wins the round! {wins[winner]} round(s) in a row.")
if wins[winner] == 3:
print(f"{winner.upper()} WINS THE BATTLE!")
break
print("Noone won 3 rounds in a row yet.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment