Skip to content

Instantly share code, notes, and snippets.

@tonsh
tonsh / Adoption.sol
Last active September 29, 2022 11:55
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/Strings.sol";
// @title The pet adption contract.
// @author tonsh
contract Adoption {
// The total number of pets should not exceed 15
uint8 constant TOTAL_PETS = 15;
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
)
type (
@tonsh
tonsh / test_while_condition.php
Created July 7, 2014 15:51
PHP 中,while 循环条件的表达式重复执行!
<?php
function count_test($num) {
$arr = range(0, $num);
$start = microtime();
while($i < count($arr)) {
$i += 1;
continue;
}
$end = microtime();
echo 'Count '.$num.': '.($end - $start).PHP_EOL;
@tonsh
tonsh / prime.py
Created May 19, 2014 04:26
<编程珠玑> 计算 1..n 之间地素数。
# coding=utf-8
import math
def prime(n):
if n == 1:
return True
# 优化1: 2, 3, 5 特殊值比较,可节约约 3/4 地计算量
if n % 2 == 0:
@tonsh
tonsh / local_variable.py
Last active August 29, 2015 13:59
变量作用域
var = 'outer'
def A():
global var
var = 'inner'
print var
# inner
var = 'outer'
def B():
@tonsh
tonsh / counter.py
Last active January 2, 2016 16:09
多线程计数器
# coding=utf-8
import logging
import random
import threading
import time
class Counter(object):
_count = 0
@tonsh
tonsh / bash_shell_summary.txt
Last active January 1, 2016 07:19
Bash shell 常用命令
* Ctrl-a 光标移至行首
* Ctrl-e 光标移至行尾
* Ctrl-b 光标向左移动一个字符(backward)
* Ctrl-f 光标向右移动一个字符(forward)
* Ctrl-k 删除从光标开始至行尾地所有字符
* Ctrl-u 删除从光标开始至行首的所有字符
* Ctrl-d 删除光标所在地字符
* Ctrl-h 删除光标左侧的一个字符
* Ctrl-w 删除光标右侧的单词
@tonsh
tonsh / variable_addreess.c
Last active December 28, 2015 23:59
C语言整型变量地址测试!
#include<stdio.h>
int main() {
int a = 1;
printf("%p\n", &a); // --> 0x7fff614d1a74
a++;
printf("%p\n", &a); // --> 0x7fff614d1a74
int b = a;
printf("%p\n", &b); // --> 0x7fff658c8a70
return 0;
# coding=utf-8
import datetime
import unittest
def strptime(date_str):
""" 不确定 date_str 格式的字符串与日期转换 """
if isinstance(date_str, datetime.datetime):
return date_str
@tonsh
tonsh / common_functions.php
Last active December 22, 2015 03:38
PHP Common Functions!
<?php
/**
* @author tonsh
*
* A set of common used functions
*/
/**
* 将数字字符串每3位添加一个逗号(从右向左)