Skip to content

Instantly share code, notes, and snippets.

View andreagrandi's full-sized avatar
🏠
Working from home... permanently!

Andrea Grandi andreagrandi

🏠
Working from home... permanently!
View GitHub Profile
@andreagrandi
andreagrandi / dontshakeme.py
Created February 27, 2016 23:43
DontShakeMe is an example written in Python that uses microbit accelerometer
# DontShakeMe - Andrea Grandi (2016)
# License: MIT
from microbit import display, Image, accelerometer, sleep
# display an happy face by default
display.show(Image.HAPPY)
# if you shake the device, you make microbit sad
while True:
@andreagrandi
andreagrandi / microbit_light_sensor.py
Created February 7, 2016 21:51
Display a Sun or a Moon on the BBC micro:bit display, depending on the value read from the light sensor
import microbit as m
SUN_IMAGE = m.Image(
"90909\n"
"09990\n"
"99999\n"
"09990\n"
"90909")
MOON_IMAGE = m.Image(
"99900\n"
@andreagrandi
andreagrandi / microbit_buttons.py
Created February 7, 2016 20:39
BBC micro:bit example that scroll "A" or "B" depending which button is pressed
import microbit
while True:
if microbit.button_a.is_pressed() and microbit.button_b.is_pressed():
microbit.display.scroll("AB")
break
elif microbit.button_a.is_pressed():
microbit.display.scroll("A")
elif microbit.button_b.is_pressed():
microbit.display.scroll("B")
@andreagrandi
andreagrandi / mb_accel_happysad.py
Created January 25, 2016 23:16
MicroBit accelerometer example: show an happy face if kept straight or a sad one if bended
import microbit as m
SENSITIVITY = 250
while (True):
x = m.accelerometer.get_x()
if abs(x) < SENSITIVITY:
m.display.show(m.Image.HAPPY)
else:
@andreagrandi
andreagrandi / andreagrandi
Last active December 6, 2015 12:59
Nginx configuration file for andreagrandi.it
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name andreagrandi.it www.andreagrandi.it;
# Redirect all requests to HTTPS.
return 301 https://$host$request_uri;
}
# Define a lambda function that takes a number and double it
d = lambda x: x * 2
# Create a list containing: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l1 = [x for x in xrange(10)]
# Method #1 using map(...)
# output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
print map(d, l1)
2kpeople.co.uk
3aaa.co.uk
24-7recruitment.net
199rec.co.uk
365rec.com
aatomrecruitment.com
abrs.com
agendarecruitment.co.uk
andiamo-group.com
applauseit.co.uk
@andreagrandi
andreagrandi / memoize.py
Created June 14, 2015 12:27
Python memoize decorator: cache values already calculated
def memoize(function):
cache = {}
def decorated_function(*args):
if args in cache:
return cache[args]
else:
val = function(*args)
cache[args] = val
return val
return decorated_function
@andreagrandi
andreagrandi / linked_circle.py
Last active August 29, 2015 14:23
Python Linked List: prevent the list to become circular
def is_list_circular(l):
slower = l
faster = l.get_next()
while True:
# if faster pointer finds a None element
# then the list is not circular
if (faster is None) or (faster.get_next() is None):
return False
# if faster element catch up with the slower
@andreagrandi
andreagrandi / roman.py
Created June 8, 2015 19:11
Transform a Roman format number in a decimal one (Python)
n = 'MCMXCVII' # 1997
def roman_to_decimal(n):
roman_map = {'M': 1000, 'D': 500, 'C': 100, 'L': 50, 'X': 10, 'V': 5, 'I': 1}
result = 0
for i,c in enumerate(n):
num = roman_map[c]
if result == 0: