Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
54 symbols with detailed comments :)) solution in Clear category for Best Stock by tkachuk.constantine
def best_stock(data: dict) -> str:
#step 1 - list of tuples in the format (key, value)
#step 2 - built-in function "sorted" returns list sorted by creterion
# second element of each tuple, thus x[1]
#step 3 - returned list sorted in ascending order. We pick up the max, which
# is the last on - thus it`s index is [-1] and within it,
# as it`s tuple - pick up the first element with index [0]
return sorted(list(data.items()), key=lambda x: x[1])[-1][0]
if __name__ == '__main__':
print("Example:")
print(best_stock({"CAC": 10.0, "ATX": 390.2, "WIG": 1.2}))
# These "asserts" are used for self-checking and not for an auto-testing
assert best_stock({"CAC": 10.0, "ATX": 390.2, "WIG": 1.2}) == "ATX"
assert best_stock({"CAC": 91.1, "ATX": 1.01, "TASI": 120.9}) == "TASI"
print("Coding complete? Click 'Check' to earn cool rewards!")
June 7, 2021
Comments: