You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Can you find a shorter, more easily understandable way to write this in Python?
324
324
325
-
<!-- Did you spot the bug in the manual implementation? ;) -->
326
-
327
325
:::::::::::::::::::::::: hint
328
326
329
327
Python strings have a lot of methods to perform common operations, like removing suffixes, replacing substrings, joining or splitting, stripping whitespaces, and much more. See Python's [string methods documentation](https://docs.python.org/3/library/stdtypes.html#string-methods) for a full list.
@@ -342,8 +340,10 @@ def builtinSplit():
342
340
return data
343
341
```
344
342
345
-
This is much more readable.
346
-
The code that’s executed by CPython may use similar indexing steps as in `manualSplit`; however, since this is all happening “under the hood” in C code, it is once again faster.
343
+
This code is not just much more readable; it is also more flexible, since it does not rely on the precise formatting of the input strings.
344
+
(For example, the line `first_char = line.find("0")` in the original code assumes that the time bin starts with the digit 0. That code would likely malfunction if the input file had more than 1000 time bins.)
345
+
346
+
The code that’s executed by CPython may use a similar approach as in `manualSplit()`; however, since this is all happening “under the hood” in C code, it is once again faster.
0 commit comments