Skip to content

Instantly share code, notes, and snippets.

@antonylu
antonylu / moving_average.py
Created April 5, 2023 07:10
Simple Moving Average
BUFFER_SIZE = 900
buffer = [0] * BUFFER_SIZE
index = 0
total = 0
count = 0
def moving_average(new_value):
global index, total, count
if count < BUFFER_SIZE:
@antonylu
antonylu / moving_average.c
Created April 5, 2023 06:14
Simple Moving Average for 900 values, return -1 before 900 values accumulated.
#include <stdio.h>
#define BUFFER_SIZE 900 // 循環緩衝區大小
// 移動平均結構體
typedef struct {
int buffer[BUFFER_SIZE]; // 循環緩衝區
int sum; // 總和
int count; // 目前個數
int index; // 當前索引
@antonylu
antonylu / git_pull.sh
Created June 9, 2021 16:17
git pull all repositories in current directories
#!/bin/bash
for dir in $PWD/*/ # list directories in the form "/xxx/dirname/"
do
dir=${dir%*/} # remove the trailing "/"
# echo "${dir##*/}" # print everything after the final "/"
pushd "${dir##*/}" && git pull --recurse-submodules && popd
done
@antonylu
antonylu / signimg2img.py
Created February 9, 2021 07:16
Android signed images extractor script
#!/usr/bin/env python3
# Android signed images extractor. To use the script:
#
# python3 signimg2img -u vendor-sign.img # to unpack any other image.
#
# display("USAGE: signimg2img.py -option:\n")
# print(" -u image-sign.img: Unsigns the given image. (If is system will unpack it)")
# print(" -o: Get image info (-o image_name).")
# print(" -c: Full cleanup (removes all!)")
# print("")
@antonylu
antonylu / repo_sync.sh
Created August 11, 2020 12:57
Repo sync until successful
#!/bin/bash
#cd /home/pi/space/source
while true; do
python2 .repo/repo/repo sync -f -n -c -j4 --no-repo-verify --optimized-fetch
#ls -la
RETVAL=$?
[ $RETVAL -eq 0 ] && echo "Repo sync success!" && exit 0
printf '%s\n' 'Repo sync failed, will start retry in 5 minutes...'
sleep 300
done
@antonylu
antonylu / pygame_simple_sample.py
Created March 16, 2020 03:09
A very simple pygame skeleton sample code with a background image
import pygame
FPS = 30
BG_IMG = "bg.jpg"
class SkyNet():
def __init__(self):
pygame.init()
pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
self._surface = pygame.display.get_surface()
self._running = True
self.bg = pygame.image.load(BG_IMG)
@antonylu
antonylu / convert_mpeg.py
Created February 20, 2020 13:26
Batch files conversion from MP4 to MPEG with ffmpeg
# -*- coding: utf-8 -*-
# D:ffmpeg -i "KTV 小幸運.mp4" -target ntsc-vcd final_video.mpg
import glob
import os
COMMAND = u'D:ffmpeg -i "{}" -target ntsc-vcd "{}.mpg"'
files = glob.glob('*.mp4')
for f in files:
c = COMMAND.format(f,f.split(".")[0])
"""
https://open.kattis.com/problems/aliennumbers
Input
#alien_number source_language target_language
4
9 0123456789 oF8
Foo oF8 0123456789
13 0123456789abcdef 01
CODE O!CDE? A?JM!.
@antonylu
antonylu / Gale-Shapley.ipynb
Created November 1, 2019 17:52
Stable Matching Algorithm
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@antonylu
antonylu / knapsack.py
Created October 30, 2019 03:17
Knapsack Solution
import numpy as np
#O(N*W)
def solve(v,wlist,totalWeight,totalLength):
c = np.zeros((totalLength+1,totalWeight+1),dtype=np.int32) # c(4,51)
for i in range(1,totalLength+1):
for w in range(1,totalWeight+1):
if wlist[i] > w:
c[i,w] = c[i-1,w]
else: