From a65673de75201e1ca7da5530b037fcf40dbee37b Mon Sep 17 00:00:00 2001 From: Yitzchak Scott-Thoennes Date: Mon, 19 May 2025 18:00:02 -0400 Subject: challenge 322 better python solution for challenge 2 by ysth --- challenge-322/ysth/python/ch-2.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'challenge-322') diff --git a/challenge-322/ysth/python/ch-2.py b/challenge-322/ysth/python/ch-2.py index 75301f87a5..058872ecca 100644 --- a/challenge-322/ysth/python/ch-2.py +++ b/challenge-322/ysth/python/ch-2.py @@ -1,17 +1,19 @@ import sys +import operator integers = [int(value) for value in sys.argv[1:]] +# python doesn't have a clean dict slice syntax; usually a comprehension +# is used (e.g. (mydict[key] for key in mykeys)), but that evaluates the +# dict expression more than once, so use itemgetter or a map + print( " ".join( - list( - map( - { - value: str(i + 1) - for i, value in enumerate(sorted(set(integers))) - }.__getitem__, - integers, - ) + operator.itemgetter(*integers)( + { + value: str(i + 1) + for i, value in enumerate(sorted(set(integers))) + } ) ) ) -- cgit