Hacker News new | past | comments | ask | show | jobs | submit | kaunta's comments login

The C version is in Modules/_bisectmodule.c, whereas the Python version is in Lib/bisect.py.

Python version: https://github.com/python/cpython/blob/master/Lib/bisect.py

C version: https://github.com/python/cpython/blob/master/Modules/_bisec...


Thanks. So it seems both were updated with 'key' at the same time: https://github.com/python/cpython/commit/871934d4cf00687b3d1...

Now I'm wondering - what is the point of the python version, if it will always be overridden by the C implementation? Are there circumstances (platforms, compile flags, ...) under which the C version would be unavailable when the python runtime is compiled?


It may be that other Python implementations use CPython's standard library, or at least part of it.

I think that PyPy in particular does this, but I'm not 100% sure. I know for certain that it uses pure Python implementation of some modules from somewhere. One program I took great pains to be PyPy compatible ended up being a lot slower in it, and it turned out to be that the built-in sqlite3 module has a C implementation in CPython that's faster than the pure-Python version even when runing in PyPy.


I should just write O(n).

Thanks for catching the mistake, @dmurray. I'll fix that.


Garrett, thank you.

* * *

My inspiration for writing this came from watching 3Blue1Brown's YouTube video, "Simulating an epidemic":

https://www.youtube.com/watch?v=gxAaO2rsdIs

His simulations cover several scenarios:

- Simulating social distancing.

- Having people travel to a central location (e.g. a grocery store).

- What if the government is able to identify and isolate cases?

- Simulating multiple communities with travel, that put quarantine measures in place.

- What happens when people get tired of social distancing.

* * *

Thanks for sharing your code.

I see that you are using gnuplot to visualize the output of the script. Have you considered using the csv module (https://docs.python.org/3/library/csv.html) to output the data, instead of using the print() function? That way, you wouldn't have to manually add in spaces around the data and stringify the numeric data.

  >>> import csv, sys
  >>> writer = csv.writer(sys.stdout, delimiter=" ")
  >>> writer.writerow(["#", "T", "S", "I", "R"])
  # T S I R
In the script, usage would look something like this:

  import csv
  import sys
  
  ...
  
  writer = csv.writer(sys.stdout, delimiter=" ")
  
  writer.writerow(["#", "T", "S", "I", "R"])
  writer.writerow([T, S, I, R])
  while I > 1000:
  
      ...
  
      writer.writerow([T, S, I, R])


Python.org has an official tutorial on their website:

https://docs.python.org/3/tutorial/


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: