I think it would require, at a minimum, a lot of familiarity with the built in functions and their useage. Like in this case enumerate() and .join(). Because I'm unfamiliar I went straight to using a for loop and f strings.
That's one of the reasons I do these challenges though, it introduces useful things like enumerate() and how they can be used, or reminds you if you'd forgotten.
While not claiming that these types of solutions are necessarily best-practice, (and I am guilty of many of them), I will nonetheless assert that the inherent challenge to a coder unfamiliar with such constructions is rather useful; in that one needs to unravel the code, often using the google, and by the end of it will have learned 1 to 5 new concepts appropriate either to that one language in particular, or to coding paradigms in general.
Chat-gtp or a similar llm is usually a good explainer of code.
ignore this comment
lmao, right? Always something.
break it down.
It's basically:
for i, c in enumerate (s): etc...
These are called list comprehensions
This comment is hidden because it contains spoiler information about the solution
I think it would require, at a minimum, a lot of familiarity with the built in functions and their useage. Like in this case enumerate() and .join(). Because I'm unfamiliar I went straight to using a for loop and f strings.
That's one of the reasons I do these challenges though, it introduces useful things like enumerate() and how they can be used, or reminds you if you'd forgotten.
While not claiming that these types of solutions are necessarily best-practice, (and I am guilty of many of them), I will nonetheless assert that the inherent challenge to a coder unfamiliar with such constructions is rather useful; in that one needs to unravel the code, often using the google, and by the end of it will have learned 1 to 5 new concepts appropriate either to that one language in particular, or to coding paradigms in general.
It requires a lot of practice and, while one-liners are fun to conjure up, they're not effective to use when you're learning to think algorithmically.
Once you break down a problem into individual lines of code, you can figure out how to consolidate it into 1-2 lines if you really want to.