Skip to content

Instantly share code, notes, and snippets.

@kernel1994
kernel1994 / introrx.md
Created March 20, 2020 04:26 — forked from staltz/introrx.md
The introduction to Reactive Programming you've been missing
package com.company;
public class MergeSort {
public void mergeSort(int[] nums) {
// 在排序前,先建好一个长度等于原数组长度的临时数组,避免递归中频繁开辟空间
int[] tmps = new int[nums.length];
sort(nums, 0, nums.length - 1, tmps);
}
package com.company;
public class QuickSort {
public void quickSort(int[] nums) {
sort(nums, 0, nums.length - 1);
}
public void sort(int[] nums, int left, int right) {
int pivotIndex;
@kernel1994
kernel1994 / top_k.py
Last active October 26, 2021 01:06
Top k element index in numpy array
import numpy as np
def largest_indices(array: np.ndarray, n: int) -> tuple:
"""Returns the n largest indices from a numpy array.
Arguments:
array {np.ndarray} -- data array
n {int} -- number of elements to select
@kernel1994
kernel1994 / mha_to_nii.py
Created April 18, 2019 08:02 — forked from jcreinhold/mha_to_nii.py
Convert mha file format to NIfTI
#/usr/bin/env python
# -*- coding: utf-8 -*-
"""
nii_to_tif
convert all mha files in a directory to nifti
Author: Jacob Reinhold (jacob.reinhold@jhu.edu)
"""
@kernel1994
kernel1994 / download_via_python.py
Created January 15, 2019 03:02
download image via requests or urllib
import shutil
import urllib
import pathlib
import requests
from PIL import Image
from io import BytesIO
def get_file_name(url: str) -> str:
return url.split('/')[-1].split('?')[0]
@kernel1994
kernel1994 / concat_images.py
Created August 21, 2018 16:00
concatenate images in vertical or horizontal.
import argparse
import numpy as np
from PIL import Image
def pad_image(image: Image, desired_size: tuple, pad_value: str='#fff') -> Image:
"""
pad image to make image in center.
Args:
@kernel1994
kernel1994 / quickSort.js
Created March 4, 2017 13:30
Quick sort with JavaScript
/* 快速排序 */
/** 待排序原始数据 */
let data = [4, 10, 14, 11, 17, 8, 13, 15, 12, 2, 6, 9];
let n = data.length;
/** 快速排序 */
function quickSort(data, low, hight) {
/** 枢轴 */
let pivot = 0;
@kernel1994
kernel1994 / regular.js
Created February 15, 2017 12:06
JavaScript 中正则表达式笔记
// NOTE: 在线学习正则表达式:http://regexr.com/
// NOTE: XMLDocument 对象可用 new XMLSerializer().serializeToString(obj); 的方式转换为 JS 中的字符串
let data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><schs><sch><dm><![CDATA[10593]]></dm><mc><![CDATA[广西大学]]></mc></sch><sch><dm><![CDATA[10608]]></dm><mc><![CDATA[广西民族大学]]></mc></sch><sch><dm><![CDATA[10603]]></dm><mc><![CDATA[广西师范学院]]></mc></sch><sch><dm><![CDATA[10596]]></dm><mc><![CDATA[桂林理工大学]]></mc></sch><sch><dm><![CDATA[10601]]></dm><mc><![CDATA[桂林医学院]]></mc></sch></schs>';
// 结尾 g 代表全局匹配
let reg = /(<mc><\!\[CDATA\[)(.*?)(\]\]\><\/mc>)/g;
let r;
while((r = reg.exec(data)) != null) {
console.log(r[2]);
}