-
-
Save auscompgeek/241e1a4bf8783f34fe71 to your computer and use it in GitHub Desktop.
UNSW ProgComp 2014 open round - drop_table_teams' solutions
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
#!/usr/bin/env python3 | |
# Task 1 | |
# drop_table_teams | |
from urllib.parse import unquote | |
num_cases = int(input()) | |
for i in range(num_cases): | |
print(unquote(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
#!/usr/bin/env python3 | |
# Task 2 | |
# drop_table_teams | |
despair = [] | |
def next_happy(n): | |
return sum(int(x)**2 for x in str(m)) | |
def happiness(n): | |
assert n > 0 | |
seq = set() | |
result = n == 1 | |
num_steps = 0 | |
while not result and n not in seq: | |
seq.add(n) | |
n = next_happy(n) | |
result = n == 1 | |
num_steps += 1 | |
if not result: | |
while n not in despair: | |
despair.append(n) | |
n = next_happy(n) | |
return result, num_steps | |
assert happiness(49)[0] | |
max_happy_steps = 0 | |
max_unhappy_steps = 0 | |
max_happy = 0 | |
max_unhappy = 0 | |
num_happy = 0 | |
for num in range(1, 1000): | |
is_happy, num_steps = happiness(num) | |
if is_happy: | |
num_happy += 1 | |
if num_happy == 10: | |
print('10th happy number:', num) | |
if num >= 100 and num_steps > max_happy_steps: | |
max_happy_steps = num_steps | |
max_happy = num | |
else: | |
if num >= 100 and num_steps > max_unhappy_steps: | |
max_unhappy_steps = num_steps | |
if num_steps == max_unhappy_steps: | |
max_unhappy = num | |
print('Despair:', despair) | |
print('Max happy steps:', max_happy_steps) | |
print('Smallest such happy num:', max_happy) | |
print('Max unhappy steps:', max_unhappy_steps) | |
print('Largest such unhappy num:', max_unhappy) |
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
#!/usr/bin/env python3 | |
# Task 3 | |
# drop_table_teams | |
table_values = ['111', '112', '113', '121', '122', '123', '131', '132', '133', '211', '212', '213', '221', '222', '223', '231', '232', '233', '311', '312', '313', '321', '322', '323', '331', '332', '333'] | |
pp_chars = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
num_cases = int(input()) | |
passphrase = input() | |
pp_dict = {} | |
i = 0 | |
for letter in passphrase: | |
if letter not in pp_dict: | |
pp_dict[letter] = table_values[i] | |
i += 1 | |
for letter in pp_chars: | |
if letter not in pp_dict: | |
pp_dict[letter] = table_values[i] | |
i += 1 | |
pp_dict_enc = dict(zip(pp_dict.values(), pp_dict.keys())) | |
# silly me did this first. *shrug* | |
def encode(msg): | |
temp = '' | |
for letter in msg: | |
temp += pp_dict[letter] | |
rows = [temp[::3], temp[1::3], temp[2::3]] | |
result = '' | |
for row in rows: | |
for i in range(len(row)//3): | |
n = row[3*i:3*i+3] | |
result += pp_dict_enc[n] | |
return result | |
def decode(msg): | |
temp = '' | |
for letter in msg: | |
temp += pp_dict[letter] | |
l = len(msg) | |
cols = [temp[:l], temp[l:2*l], temp[2*l:]] | |
result = '' | |
for i in range(l): | |
n = cols[0][i] + cols[1][i] + cols[2][i] | |
result += pp_dict_enc[n] | |
return result | |
for i in range(num_cases): | |
msg = input() | |
print(decode(msg)) |
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
#!/usr/bin/env python3 | |
# Task 4 - Shuffle validation | |
# drop_table_teams | |
SOLVED = 0 | |
SEQ_ERROR = 1 | |
SUM_ERROR = 2 | |
m, n = map(int, input().split()) | |
mn = m*n | |
rows = [] | |
result = SOLVED # assume solved unless we find otherwise | |
possible_nums_left = set(range(1, mn + 1)) | |
for i in range(m): | |
line = [int(x) for x in input().split()] | |
row = line[:-1] | |
row_sum = line[-1] | |
rows.append(row) | |
if result == SOLVED: | |
# check for sequence error | |
for i in row: | |
if i in possible_nums_left: | |
possible_nums_left.remove(i) | |
else: | |
result = SEQ_ERROR | |
if result == SOLVED: | |
# check for row sum error | |
if sum(row) != row_sum: | |
result = SUM_ERROR | |
sum_cols = [int(x) for x in input().split()] | |
if result == SOLVED: | |
# check the column sums | |
for i in range(n): | |
s = 0 | |
for row in rows: | |
s += row[i] | |
if sum_cols[i] != s: | |
result = SUM_ERROR | |
if result == SEQ_ERROR: | |
print("sequence error") | |
elif result == SUM_ERROR: | |
print("sum error") | |
else: | |
print("solved") |
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
#!/usr/bin/env python3 | |
# Task 4 - Shuffle validation | |
# drop_table_teams' solution refactored to use early returns | |
def main(): | |
m, n = map(int, input().split()) | |
rows = [] | |
possible_nums_left = set(range(1, m*n + 1)) | |
for i in range(m): | |
line = [int(x) for x in input().split()] | |
row = line[:-1] | |
row_sum = line[-1] | |
rows.append(row) | |
# check for sequence error | |
for i in row: | |
if i not in possible_nums_left: | |
print("sequence error") | |
return | |
possible_nums_left.remove(i) | |
# check for row sum error | |
if sum(row) != row_sum: | |
print("sum error") | |
return | |
sum_cols = [int(x) for x in input().split()] | |
# check the column sums | |
for i in range(n): | |
s = 0 | |
for row in rows: | |
s += row[i] | |
if sum_cols[i] != s: | |
print("sum error") | |
return | |
print("solved") | |
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
#!/usr/bin/env python3 | |
# Task 4 - Shuffle solving | |
# drop_table_teams | |
# We never actually finished this. This is just the code we had at the end. | |
m, n = map(int, input().split()) | |
rows = [] | |
sum_rows = [] | |
possible_nums_left = set(range(1, m*n + 1)) | |
for i in range(m): | |
line = input().split() | |
row = line[:-1] | |
row_sum = int(line[-1]) | |
rows.append(row) | |
sum_rows.append(row_sum) | |
for i in row: | |
if i != '.': | |
possible_nums_left.remove(int(i)) | |
sum_cols = [int(x) for x in input().split()] | |
print('nums left:', possible_nums_left) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment