Skip to content

Instantly share code, notes, and snippets.

@IvanaGyro
IvanaGyro / pipe.cpp
Last active March 15, 2018 01:43
Pipe Linux command results
#include <stdexcept>
#include <string>
#include <cstdio>
std::string exec(const char *cmd)
{
int bufferSz = 512;
char buffer[bufferSz];
std::string result = "";
FILE *pipe = popen(cmd, "r");
@IvanaGyro
IvanaGyro / print_sp.c
Last active March 15, 2018 02:08
Print the values of the register sp
#include <stdio.h>
void print_sp(int startOffset, endOffset)
{
int i;
unsigned *pp = (unsigned*)&p;
register unsigned sp asm ("sp");
pp = (unsigned*)sp;
for (i = startOffset; i <= endOffset; i+=4) {
@IvanaGyro
IvanaGyro / electric_field_sphere.py
Last active July 27, 2018 10:21
Calculate the electric field induced from a spherical uniform charge distribution
#-*- coding:utf-8 -*-
import random
import math
from functools import reduce
def random_point_in_sphere(r):
while True:
x = random.uniform(-r, r)
y = random.uniform(-r, r)
z = random.uniform(-r, r)
@IvanaGyro
IvanaGyro / chinese_chess.c
Created August 27, 2018 13:32
Simple Chinese Dark Chess playing in the command line mode.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define BOARD_H 4
#define BOARD_W 8
typedef struct chess
{
#-*- coding:utf-8 -*-
import math
from pprint import pprint
combinations = {}
def combination(m, n):
if n > m: return 0
if n == 0 or n == m:
return 1
@IvanaGyro
IvanaGyro / mice_brust_force.py
Created December 4, 2018 04:39
Calculate the less number of mice to find out two poisons from N bottles by brust force. Python >= 3.6
#-*- coding:utf8 -*-
from pprint import pprint
from time import time
def bottle_arrange(bottle_num, mouse_num):
mice = list(range(0, mouse_num))
bottle_combination = 2**bottle_num
while True:
for i in range(mouse_num - 1, -1, -1):
if mice[i] < bottle_combination - mouse_num + i:
@IvanaGyro
IvanaGyro / descriptor_demo.py
Created April 9, 2019 03:41
Demo the behaviors of Python's descriptor which is implemented with __set__ and __get__ and demo the relations between instance methods and class functions.
import inspect
descriptor_records = []
class Descriptor(object):
def __init__(self, value=None):
self.value = value
def __get__(self, instance, owner): # owner == instance.__class__
@IvanaGyro
IvanaGyro / combinations.py
Created April 17, 2019 16:59
An other implementation of built-in generator `itertools.combinations` in pure Python. This function is faster than the official demostration in some cases.
def combinations(iterable, r, start=0):
'''
A generator that yields the combinations of the iterable object.
This generator has the same interface as the built-in generator
`itertools.combinations`. This function is faster than the official
demostration when the r is closed to the half of length of the input
iterable object.
'''
pool = tuple(iterable)
@IvanaGyro
IvanaGyro / tree_traversal.py
Last active May 26, 2019 10:01
List methods for traversaling trees in various orders
#!python3
from collections import deque
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
@IvanaGyro
IvanaGyro / energy_stone.py
Created April 25, 2019 12:30
The wrong answer to the second problem of 2019 Kick Start round B
from operator import itemgetter
def eat(stones):
max_e = {}
stones.sort(key=itemgetter(2), reverse=True)
def dp(time, i):
if i == len(stones):
return 0
if (time, i) not in max_e:
max_e[(time, i)] = max(