Skip to content

Instantly share code, notes, and snippets.

View KaiserKatze's full-sized avatar
🎯
Focusing

KaiserKatze KaiserKatze

🎯
Focusing
  • MiGo Future Tech Group
  • Chongqing, China
View GitHub Profile
@KaiserKatze
KaiserKatze / univ_code.py
Created October 27, 2021 15:59
统一社会信用代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 参照《GB 11714-1997》《GB 32100-2015》两个标准文件
class BaseCode(str):
MAX_LENGTH: int
def __new__(cls, *args, **kwargs):
_s = str.__new__(cls, *args, **kwargs)
@KaiserKatze
KaiserKatze / linear_regression.py
Created October 10, 2021 09:36
Utilize PyTorch in Linear Regression.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import torch
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
from torch import nn
class RandomLinearDataset(Dataset):
@KaiserKatze
KaiserKatze / levenshtein_distance.py
Created October 5, 2021 18:00
Calculate edit distance of two strings. #DynamicProgramming
def levenshtein_distance(s1: str, s2: str) -> int:
"""
求解两个字符串的莱文斯坦距离。
"""
len1, len2 = len(s1) + 1, len(s2) + 1
f = list(range(len2))
for i in range(1, len1):
t1 = f[0]
f[0] += 1
for j in range(1, len2):
@KaiserKatze
KaiserKatze / join.hpp
Last active December 20, 2020 10:11
Python-like functional functions implemented in C++
#include <sstream>
template <class _StrTyp, class _SeqTyp, class... _Arg>
_StrTyp join(_StrTyp&& seperator, _SeqTyp&& sequence, _Arg&&... args)
{
using _Elem = typename _StrTyp::value_type;
using _Traits = typename _StrTyp::traits_type;
using _Alloc = typename _StrTyp::allocator_type;
std::basic_stringstream<_Elem, _Traits, _Alloc> oss;
((oss << args), ...);
@KaiserKatze
KaiserKatze / queue.hpp
Created December 7, 2020 16:00
C++ Concurrency
#include <queue>
#include <mutex>
#include <condition_variable>
#include <type_traits>
namespace concurrent
{
template <class _Ty, class _Container>
class queue
: private std::queue<_Ty, _Container>
@KaiserKatze
KaiserKatze / netns.sh
Last active December 5, 2020 15:09
Build a virtual LAN with ip-netns
#!/usr/bin/bash
# @see: https://superuser.com/questions/1348508/can-i-create-three-veth-interfaces-to-build-a-virtual-lan-in-linux
# @see: https://unix.stackexchange.com/questions/255484/how-can-i-bridge-two-interfaces-with-ip-iproute2
ip -all netns del
ip netns add router
ip netns add net1
ip netns add net2
@KaiserKatze
KaiserKatze / concurrent_ostream.hpp
Last active November 26, 2020 14:06
[C++] Conccurrent ouput stream wrapper
#include <mutex>
#include <iostream>
#include <utility>
static std::mutex mtx_concurrent_stream;
struct concurrent_ostream
{
private:
std::ostream&& os;
// Question:
// @see: https://stackoverflow.com/questions/650162/why-the-switch-statement-cannot-be-applied-on-strings
//
// Solution:
// @see: https://blog.csdn.net/yozidream/article/details/22789147
#include <iostream>
namespace SimpleHash
{
@KaiserKatze
KaiserKatze / ReadFileI.cpp
Created February 5, 2020 09:51
Read a file interactively.
#include <fstream>
#include <iostream>
int interactive(const std::string& path)
{
int count{ 0 };
char buffer[1024]{ 0 };
std::ifstream ifs{ path };
if (!ifs)
return -1;
#!/bin/bash
# TODO check if there is enough memory
MemFree=$(cat /proc/meminfo | grep 'MemFree' | awk '{print $2}')
MemNeed=1048576
if [ $MemFree -lt $MemNeed ]; then
# memory is not enough
dd if=/dev/zero of=/swapfile bs=100M count=10
chmod 0600 /swapfile
mkswap /swapfile