Skip to content

Instantly share code, notes, and snippets.

To install node.js and coffee-script inside a virtualenv and keep it self-contained:
1. Activate the virtualenv::
$ workon test
2. Move inside the virtualenv directory::
(test)$ cdvirtualenv
@eirenik0
eirenik0 / max_string.py
Last active December 17, 2015 23:58
Finding the maximum of a string
#-*-coding: utf-8-*-
K = 3
STRINGS = ['abacainstitutebac', 'mycabacrchinstituteve', 'acabacinstinstituterue']
def sort(i, outputs=[]):
string = ""
k = i
while len(string) < len(STRINGS[0])-i:
@eirenik0
eirenik0 / implement_to_staticmaps.py
Last active December 18, 2015 19:09
implement the function gmaps_img(points) that returns the google maps image
from collections import namedtuple
# make a basic Point class
Point = namedtuple('Point', ["lat", "lon"])
points = [Point(1,2),
Point(3,4),
Point(5,6)]
# implement the function gmaps_img(points) that returns the google maps image
# for a map with the points passed in. A example valid response looks like
@eirenik0
eirenik0 / cache.py
Last active December 18, 2015 19:09
complex_computation() simulates a slow function. time.sleep(n) causes the program to pause for n seconds. In real life, this might be a call to a database, or a request to another web service.
import time
# complex_computation() simulates a slow function. time.sleep(n) causes the
# program to pause for n seconds. In real life, this might be a call to a
# database, or a request to another web service.
def complex_computation(a, b):
time.sleep(.5)
return a + b
# QUIZ - Improve the cached_computation() function below so that it caches
@eirenik0
eirenik0 / round-robin.py
Last active December 18, 2015 19:19
the function get_server, which returns one element from the list SERVERS in a round-robin fashion on each call
#handy link
#http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them
#maybe a link to itertools
SERVERS = ['SERVER1', 'SERVER2', 'SERVER3', 'SERVER4']
# QUIZ - implement the function get_server, which returns one element from the
# list SERVERS in a round-robin fashion on each call. Note that you should
# comment out all your 'print get_server()' statements before submitting
# your code or the grading script may fail. For more info see:
@eirenik0
eirenik0 / format_json_friends.py
Last active December 18, 2015 20:38
From Json VK output, creating lists this format ['Name', 'Address']
from collections import namedtuple
friends_json = [{
'uid': 16734834,
'first_name': 'Alena',
'last_name': 'Grin',
'deactivated': 'banned',
'photo': 'http://vk.com/images/deactivated_c.gif',
'online': 0,
'user_id': 16734834,
'lists': [26]
@eirenik0
eirenik0 / location_ip.py
Last active December 19, 2015 03:19
From friend-on-map
IP_URL = "http://api.hostip.info/?ip="
'
def location(self, user):
"""
:param user: user id(uid)
:return: address from ndb, coordinates from IP
"""
address = '%s, %s' % (user.country, user.city)
coords = self.get_coords(self.request.remote_addr) # receive location from id
if coords == None:
@eirenik0
eirenik0 / gist:6971075
Created October 14, 2013 05:12
Enable Hibernate in Ubuntu
To do so on terminal:
gksu gedit /etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla
Replace gedit with your favorite editor’s command. This command will open a blank file. Copy and paste these lines:
[Enable Hibernate]
Identity=unix-user:*
Action=org.freedesktop.upower.hibernate
ResultActive=yes
@eirenik0
eirenik0 / gist:9788876
Created March 26, 2014 17:35
print table in console
def printTable( data, header=None ):
lens = [0]*len(data[0])
for row in data:
for numCol, column in enumerate(row):
cLen = len(str(column))
if lens[numCol] < cLen:
lens[numCol] = cLen
res = ""
spacer = lambda lenList: '+' + '+'.join( '-'*(x+2) for x in lenList ) + '+'
drawRow = lambda lenList, valueList: '|' + "".join(
@eirenik0
eirenik0 / search_min_On2.py
Last active August 29, 2015 13:57
O(n^2) min search
l=[3,11,2,4,2,1,3,4,62,6,67,8,2,3,2246]
tmp = l[0]
for i in l:
for j in l:
if i<j and i<tmp:
tmp = i
print tmp