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| def grouper(group_membre_number, the_list, default=None): | |
| return map(default, *(iter(the_list), ) * group_membre_number) | |
| def build_dict(group_list): | |
| result = {} | |
| for group in group_list: | |
| temp = iter(group[1:]) | |
| result[group[0]] = dict(zip(temp, temp)) |
| # 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' |
| #!/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'] |
| #!/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 A(object): | |
| def __init__(self): | |
| print 'A init' | |
| @classmethod | |
| def foo(cls): | |
| print 'A foo' |
| #!/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)" |
| # 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; |
| # swift alias | |
| alias swift='/Applications/Xcode6-Beta3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift' |
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| // Exercise: Loops and Functions | |
| package main | |
| import ( | |
| "fmt" | |
| ) | |
| func Sqrt(x float64) float64 { | |
| z := 1.0 | |
| for i := 0; i < 10; i++ { |