Skip to content

Instantly share code, notes, and snippets.

@marcolussetti
Last active November 29, 2017 19:53
Show Gist options
  • Select an option

  • Save marcolussetti/2167321aeecdde862b54fa878cae127d to your computer and use it in GitHub Desktop.

Select an option

Save marcolussetti/2167321aeecdde862b54fa878cae127d to your computer and use it in GitHub Desktop.
Oamar_Notes.ipynb
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# coding: utf-8
# # Notes for Oamar
# This is a copy of what we run when figuring out the λ stuff.
#
# I tweaked a few things I didn't think about when running through things. Namely this makes a lot more sense w/ random integers than it does w/ 1 to 10 sorted :D
# ### Setup
# In[5]:
arr = list(range(1, 10))
arr
# In[11]:
# Comment this out if you want the sorted example we used
import random
random.shuffle(arr)
arr
# ### Zip
# In[12]:
["{}, {}".format(first, second) for first, second in zip(arr, arr[1:])]
# In[13]:
[(second-first)/first for first, second in zip(arr, arr[1:])]
# ### Lambda
# In[14]:
increase = lambda first, second: (second-first)/first
# In[15]:
[increase(first, second) for first, second in zip(arr, arr[1:])]
# ### Lambda for sort
# In[25]:
increases = [(first, second, (second - first) / first)
for first, second in zip(arr, arr[1:])]
# In[30]:
# I think here the lambda does help in the readability
increases = [(first, second, increase(first, second))
for first, second in zip(arr, arr[1:])]
# In[29]:
sorted(increases, key=lambda x: x[2])
# In[31]:
sorted(increases, key=lambda x: x[1])
# In[34]:
# Sort by the increase, smallest to largest
sorted(increases, key=lambda x: x[2])
# In[35]:
# Sort by the increase, largest to smallest
sorted(increases, key=lambda x: x[2], reverse=True)
# In[37]:
# Sort by the increase, largest to smallest by absolute variation
sorted(increases, key=lambda x: abs(x[2]), reverse=True)
rr = range(10)
arr
arr = list(range(10))
arr
["{}: {}".format(first, second) for first, second in zip(arr, arr[1:])
]
[(second-first)/first for first, second in zip(arr, arr[1:])]
[(second-first)/first for first, second in zip(arr, arr[1:])]
arr = list(range(1, 10))
[(second-first)/first for first, second in zip(arr, arr[1:])]
[(second-first)/first for first, second in zip(arr, arr[1:])]
lam = lambda first, second: (second - first) / first
[lam for first, second in zip(arr, arr[1:])]
[lam(first, second) for first, second in zip(arr, arr[1:])]
[(first, second, (second-first)/first) for first, second in zip(arr, arr[1:])]
a = [(first, second, (second-first)/first) for first, second in zip(arr, arr[1:])]
sorted(a, key=lambda x: x[2])
sorted(a, key=lambda x: x[1], reversed=True)
sorted(a, key=lambda x: x[1], reverse=True)
sorted(a, key=lambda x: x[1], reverse=False)
%history -f ~/Documents/oamar_printout.py
%history -f /home/marco/Documents/oamar_printout.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment