First part
This first part was easy. Having a basic grasp of built-in Python modules like itertools
and collections
is a real boon.
with open('input') as file_input:
INPUT = file_input.read().splitlines()
LINE_LENGTH = len(INPUT[0])
def make_transpose(lines: list[str]):
return [[line[i] for line in lines] \
for i in range(LINE_LENGTH)]
print("".join(list(map(lambda x: Counter(x).most_common(1)[0][0],
make_transpose(INPUT)))))
Second Part
For the second part, instead of taking the most common occurrence, we want the least common; in that case, there is not a handy built-in method like most_common
for the Counter
class object. I'm thinking of running the min
function with a custom key
on each Counter
object.
There is only some slight modifications on the print
statement:
print("".join(list(map(lambda x: min(list(Counter(x).items()),
key=lambda y: y[1])[0],
make_transpose(INPUT)))))
Good stuff!
(after later checking some solutions, I obviously missed the zip(*input)
trick)