Skip to content

Instantly share code, notes, and snippets.

View redraiment's full-sized avatar
🏠
Working from home

Zhang, Zepeng (redraiment) redraiment

🏠
Working from home
View GitHub Profile
@redraiment
redraiment / count-primes.c
Created October 31, 2022 04:47
使用容斥原理计素数个数
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define BOUND 2000000000
int primes_count = 0;
int primes[5000] = {0};
char is_composites[45000] = {0}; // > sqrt(2e9)
@redraiment
redraiment / brainfuck.c
Created August 22, 2022 09:59
Brainfuck interpreter in pure ANSI C
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define TOKENS "><+-.,[]"
#define CODE_SEGMENT_SIZE 30000
#define STACK_SEGMENT_SIZE 1000
#define DATA_SEGMENT_SIZE 30000
#define ERROR_CODE_MISSING_FILE 1
@redraiment
redraiment / llvm-sum.c
Created August 19, 2022 05:34
A compiler sample with LLVM C API: it create an executable file, which accept two integer from stdin, and output the result of sum.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <llvm-c/Core.h>
#include <llvm-c/Target.h>
#include <llvm-c/TargetMachine.h>
#define FALSE 0
#define TRUE 1
@redraiment
redraiment / hello-world.ll
Last active August 18, 2022 17:05
Hello world of Brainfuck in LLVM IR
; Translate "Hello world of brainfuck" to LLVM IR manually.
; ++++++++++
; [>+++++++>++++++++++>+++>+<<<<-]
; >++.>+.+++++++..+++.>++.<<+++++++++++++++.
; >.+++.------.--------.>+.>.
; ModuleID = 'helloworld.bf'
source_filename = "helloworld.bf"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
@redraiment
redraiment / daughter.prolog
Created April 9, 2020 12:06
通过人物实体、性别事实、父母关系来推算女儿关系。
% 知识库
%% 性别的实体
female(ann).
female(eve).
%% 父母的实体
parent(ann, mary).
parent(tom, eve).
parent(tom, mary).
@redraiment
redraiment / elisp.fish
Last active February 4, 2020 06:23
zsh的配置:elisp
#!/usr/bin/env fish
function perror --description '显示信息到错误输出'
if test (count $argv) -gt 0
printf -- $argv >&2
end
echo >&2
end
function raise --description '抛出异常消息并退出'
@redraiment
redraiment / check.sh
Created August 21, 2019 15:23
从重复到重用
#!/bin/sh
if [ $# -eq 0 ]; then
 echo "usage: $0 <c-source-file>" >&2
 exit -1
fi
input=$(cat <<EOF
William 35 25000
Kishore 41 35000
Wallace 37 30000
Bruce 39 29999
@redraiment
redraiment / Chart.py
Created August 7, 2019 18:23
基于Matplotlib的图表库简单封装,方便绘制直方图、圆饼图、折线图、散点图和箱形图。
#!/usr/bin/env python3
# -- 非线程安全 --
from matplotlib.font_manager import FontProperties
from operator import itemgetter
from itertools import groupby
import matplotlib.pyplot as canvas
import numpy as np
@redraiment
redraiment / web.py
Created May 29, 2018 06:15
提供简单的上传和下载功能的Python Web服务
#!/usr/bin/env python
import BaseHTTPServer
import cgi
import mimetypes
import os
import posixpath
import shutil
if not mimetypes.inited:
@redraiment
redraiment / spoils.1.sql
Last active May 10, 2018 03:01
用SQL解海盗分金问题
create table spoils (
id int primary key,
amount int
);
comment on table spoils is '分赃表';
comment on column spoils.id is '海盗编号';
comment on column spoils.amount is '可获得的最高金额,null代表死亡';
create or replace function divide(amount_total int, pirate_count int)
returns setof spoils as $$