Learn Languages (Python!) Idiomatically

Footnotes under the Solution of Leetcode — 1299. Replace Elements with Greatest Element on Right Side

Pavol Kutaj
2 min readJun 3, 2022

The aim of this page📝is to provide a commentary on the solution of a puzzle from Leetcode — 1299. Replace Elements with Greatest Element on Right Side — LeetCode

Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1. After doing so, return the array.

<!-- EXAMPLE -->
Input: arr = [17,18,5,4,6,1]
Output: [18,6,6,6,1,-1]

1. SOLUTION

2. LESSON: LEARNING LANGUAGES IDIOMATICALLY

  • The lesson seems to be that you must know pythons idiomatically, where it combines the pythonic backward traversal with range(len(arr) -1,-1,-1) and value swapping via tuple unpacking
  • Even though Paul Graham suggests (rightly?) that

When people say that in their experience all programming languages are equivalent, they’re making a statement not about languages but about the kind of programming they’ve done. 99.5% of programming consists of gluing together calls to library functions. All popular languages are equally good at this. So one can easily spend one’s whole career operating in the intersection of popular programming languages.

— Weird Languages

  • I counter “equivalence of languages” with the example above, boosted with prof. Sussman’s observation that essential to learning a language is not just to learn about its primitives, collections, and syntax, but — essentially — about its idioms

If you think about this, what you’ve learned so far is the rules of some language, some primitive, some means of combination, almost all of them, the means of abstraction, almost all of them. But what you haven’t learned is common patterns of usage. Now, most of the time, you learn idioms when learning a language, which is a common pattern that means things that are useful to know in a flash. And if you build a great number of them, if you’re a FORTRAN programmer, of course, everybody knows how to — what do you do, for example, to get an integer which is the biggest integer in something. It’s a classic thing. Every FORTRAN programmer knows how to do that. And if you don’t know that, you’re in real hot water because it takes a long time to think it out.

— SICP Lecture 2A: Higher-order Procedures — YouTube

3. LINKS

--

--

No responses yet