Editorial for Andyman's Golf Course - Hole 4 - Blackjack
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
This problem was designed to demonstrate the walrus operator.
The walrus operator :=
in Python essentially assigning a variable's value and using the variable itself within the same expression. Here is an example:
s = 0
while (s := s + 1) < 5:
print(s)
which outputs
1
2
3
4
What this code is doing is first initializing a variable s
as . Then, while
s
is less than , output its value. However, before we check if the value of
s
is less than , we first increment
s
by . This is possible because of the walrus operator.
Another hint for an optimization is that boolean expressions are evaluated as for false and
for true. This can be used for indexing as follows:
print(["This is false", "This is true"][3 < 5])
which outputs
This is True
This code essentially creates a -element list, and we access the value at index
since the expression
evaluates as a true statement.
The remaining optimizations for a full score are left as an exercise to the reader.
Comments