Skip to content

Instantly share code, notes, and snippets.

# This creates a generator.
def mygen():
for k in range(5):
# This yields None, and assigns to `a` the value that's sent in after.
a = yield
# This yields `a**2` and discards any value sent it.
yield a**2
# This returns the final value of k, which will be set as the StopIteration
# `err.value`, which in this case is not used. (It's 4)
# python 3 + numpy
from numpy import iinfo, int64
# iinfo(int64).max will give the maximum value of an 8-byte precision integer,
# which should be 2^63 - 1 = 9223372036854775807
# (there is one bit for the sign, and a shift by 1 because of zero)
# The trick is that a 8-byte float uses 11 bits for the exponent
@mverleg
mverleg / quiz_02.py
Last active November 23, 2016 20:32
def mygen():
for k in range(5):
a = yield
yield a**2
return k
r = mygen()
for k, x in enumerate(r):
# python 3 + numpy
from numpy import iinfo, int64
q = iinfo(int64).max // 2**10
q_lim = q + 3
while q < q_lim:
print('hello')
!!
!! f2py -c -m search search.f90
!!
subroutine find_first(needle, haystack, haystack_length, index)
!!
!! Find the first index of `needle` in `haystack`.
!!
implicit none
"""
http://stackoverflow.com/questions/7632963/numpy-find-first-index-of-value-fast
First you need to run:
f2py -c -m search search.f90
"""
from time import time
from matplotlib.pyplot import subplots, show