Skip to content

Instantly share code, notes, and snippets.

@Nagasaki45
Nagasaki45 / sa.py
Created November 12, 2013 21:53
Simplest simulated annealing algorithm.
import numpy as np
class Annealer():
def __init__(self, step_function, energy_function):
self.step_function = step_function
self.energy_function = energy_function
def run(
self, state, temperature, room_temperature, cooling_factor
@Nagasaki45
Nagasaki45 / cow.py
Created November 28, 2013 15:44
Countries of the world exercise with pandas
# Countries of the world exercise.
# download cow.txt and put it in your project directory
import pandas as pd
def closes_to(data, abbreviated_name):
country = data[data.ISO3166A2 == abbreviated_name]
latitude = country.BGNc_latitude.values[0]
longitude = country.BGNc_longitude.values[0]
data['distance_from_{}'.format(abbreviated_name)] = \
t9 = {'a': '2',
'b': '22',
'c': '222',
'd': '3',
'e': '33',
'f': '333',
'g': '4',
'h': '44',
'i': '444',
'j': '5',
def find_items_to_buy(credit, items):
for i, item in enumerate(items[:-1]):
another = credit - item
if another in items[i + 1:]:
return i, items[i + 1:].index(another) + i + 1
assert (1, 2) == find_items_to_buy(100, [5, 75, 25])
assert (0, 3) == find_items_to_buy(200, [150, 24, 79, 50, 88, 345, 3])
assert (3, 4) == find_items_to_buy(8, [2, 1, 9, 4, 4, 56, 90, 3])
SD card setup
=============
http://elinux.org/RPi_Easy_SD_Card_Setup
Headless first time run (without router)
========================================
http://pihw.wordpress.com/guides/direct-network-connection/
sudo ifconfig eth0 169.254.0.1
ifconfig eth0
@Nagasaki45
Nagasaki45 / stupid_django_views.py
Last active August 29, 2015 13:57
Start to write django views without implementation. Add it later, one by one.
def index(request):
return HttpResponse('in index')
def detail(request, item_pk):
return HttpResponse('in detail. pk = {}'.format(item_pk))
def comment(request, item_pk):
return HttpResponse('in comment. pk = {}'.format(item_pk))
{% extends 'base.html' %}
{% block content %}
<h1 class="text-center">Add new comment for: {{ news_item.title }}</h1>
<form method="POST">
{% csrf_token %}
<input type="text" class="form-control" placeholder="Write your comment here" name="content">
<input type="submit" class="form-control">
</form>
@Nagasaki45
Nagasaki45 / mro.py
Created March 24, 2014 07:45
Another try to understand python MRO. Depth first?
class A1(object):
def __init__(self):
print('A1>')
super(A1, self).__init__()
class B1(A1):
def __init__(self):
@Nagasaki45
Nagasaki45 / simcity_bursa.py
Created April 18, 2014 18:55
Bursa GUI module for the simcity project
from Tkinter import *
class StocksGuiBuilder(object):
class Stock(object):
MODES = ('Up', 'Down', 'Unchanged')
def __init__(self, parent):
@Nagasaki45
Nagasaki45 / Exercise.java
Created April 19, 2014 11:35
Solves mathematical exercises
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
class Exercise {
public List<String> ex;
public Exercise(String[] input) {
ex = new ArrayList<String>(Arrays.asList(input));