Skip to content

Commit 9377abf

Browse files
JostMigendaRobadob
authored andcommitted
fix bug in manual parser example
1 parent b0e96d0 commit 9377abf

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

episodes/optimisation-using-python.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def manualSplit():
309309

310310
energy_found = line.find(".", end_time, -1)
311311
begin_energy = line.rfind(" ", end_time, energy_found)
312-
end_energy = line.find(" ", energy_found, -1)
312+
end_energy = line.find(" ", energy_found)
313313
if end_energy == -1:
314314
end_energy = len(line)
315315

@@ -322,8 +322,6 @@ def manualSplit():
322322

323323
Can you find a shorter, more easily understandable way to write this in Python?
324324

325-
<!-- Did you spot the bug in the manual implementation? ;) -->
326-
327325
:::::::::::::::::::::::: hint
328326

329327
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():
342340
return data
343341
```
344342

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.
347347

348348
```python
349349

0 commit comments

Comments
 (0)