Digest from here.
The general way, if weights is a sorted list, it will be faster:
def weighted_choice_sub(weights):
rnd = random.random() * sum(weights)
for i, w in enumerate(weights):
rnd -= w
if rnd < 0:
return i
# 1.py | |
# coding: utf-8 | |
def sum1(a): | |
sum = 0 | |
for i in a: | |
sum += i | |
return sum | |
def sum2(a): |
// Exercise: Loops and Functions | |
package main | |
import ( | |
"fmt" | |
) | |
func Sqrt(x float64) float64 { | |
z := 1.0 | |
for i := 0; i < 10; i++ { |
Digest from here.
The general way, if weights is a sorted list, it will be faster:
def weighted_choice_sub(weights):
rnd = random.random() * sum(weights)
for i, w in enumerate(weights):
rnd -= w
if rnd < 0:
return i
# swift alias | |
alias swift='/Applications/Xcode6-Beta3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift' |
# auto ssh-add | |
SSH_ENV="$HOME/.ssh/environment" | |
function start_agent { | |
echo "Initialising new SSH agent..." | |
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}" | |
echo succeeded | |
chmod 600 "${SSH_ENV}" | |
. "${SSH_ENV}" > /dev/null | |
/usr/bin/ssh-add; |
#!/bin/sh | |
echo | |
count=0 | |
echo "Scanning...Conflict Files List:" | |
for f in `git diff HEAD --name-only --diff-filter=M`; do | |
if [ -f $f ]; then | |
if grep -Fxq "<<<<<<<" $f; then | |
echo " $(tput setaf 1)$f$(tput sgr0)" |
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
class A(object): | |
def __init__(self): | |
print 'A init' | |
@classmethod | |
def foo(cls): | |
print 'A foo' |
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
class A(object): | |
def __init__(self, key): | |
print 'A key', key | |
@classmethod | |
def foo(cls): | |
print 'A foo' |
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
class Enum(object): | |
"""Enum simulation class with auto increment id | |
>>> a = Enum(['type_1', 'type_2', 'type_3']) | |
>>> a.type_1 | |
1 | |
>>> a['type_1'] |
# replace rm command with mv to Trash to protect remove files or directory miss | |
function __protect_rm { | |
mv ${@: -1} ~/.Trash #get last function argument | |
} | |
alias rm='__protect_rm' |