Re:Lesser known parts of Python standard library – Trickster...
by hansvm on Thursday September 05, 2024 @02:29PM
(#41459237)
Sometimes mutations in comprehensions are very expressive.
There are lots of other short ways to write `scan`, but I don't think any of them map so clearly to a naive definition of what it's supposed to do.
[ Parent
| on HN ]
Re:Lesser known parts of Python standard library – Trickster...
by judicious on Thursday September 05, 2024 @03:03PM
(#41459529)
That's incredibly clever, generators are underrated. I once challenged my friend to do leetcode problems with only expressions. Here's levenshtein distance, however it's incredibly clunky.
levenshtein_distance = lambda s1, s2: [matrix := [[0] * (len(s2) + 1) for _ in
range(len(s1) + 1)], [
[
(matrix[i].__setitem__(j, min(matrix[i-1][j] + 1, matrix[i][j-1] +
1, matrix[i-1][j-1] + (0 if s1[i-1] == s2[j-1] else 1))), matrix[i][-1])[1]
for j in range(1, len(s2) + 1)
]
for i in range(1, len(s1) + 1)
], matrix[-1][-1]][-1]