Skip to content

Instantly share code, notes, and snippets.

View hokamoto's full-sized avatar

Hideki Okamoto hokamoto

View GitHub Profile
@hokamoto
hokamoto / tdrly.nim
Last active April 7, 2021 16:47
URL Shortener in Nim
import jester, tables, md5
var urls {.threadvar.}: Table[string, string]
routes:
get "/":
if @"url" != "":
let id = ($(@"url".toMD5)).substr(0, 5)
urls[id] = @"url"
resp id
@hokamoto
hokamoto / rdrand.c
Created November 9, 2019 13:37
Generate a random number by RDRAND operation
#include <immintrin.h>
#include <stdio.h>
unsigned int rnd() {
unsigned int r = 0;
_rdrand32_step(&r);
return r;
}
@hokamoto
hokamoto / number_converter.rb
Last active December 24, 2019 09:06
convert to Japanese Numerals
class BigDecimal
SUFFIX = [['京', 16], ['兆', 12], ['億', 8], ['万', 4]]
def convertNumber2JapaneseNumerals
number ||= self
number_string ||= ''
SUFFIX.each do |suffix|
if number.exponent >= suffix[1]
number_string += number.div(10 ** suffix[1]).to_s + suffix[0]
import Data.List
-- http://d.hatena.ne.jp/guccyon/20090510/p1
combn [] n=[]
combn lst 1=map (\x -> [x]) lst
combn (x:xs) n=[x:y| y <- combn xs (n-1)] ++ combn xs n
ss = combn [1..10] 4
ans = [s | sp <- ss, s <- (permutations sp), let x1 = abs(s !! 0 - s !! 1); x2 = abs(s !! 1 - s !! 2); x3 = abs(s !! 2 - s !! 3); x4 = abs(x1 - x2); x5 = abs(x2 - x3); x6 = abs(x4 - x5) in length (nub (x1:x2:x3:x4:x5:x6:s)) == 10]
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
@hokamoto
hokamoto / sign.rb
Created November 28, 2013 01:03
Create a signature (Akamai OPEN API)
SIGNING_ALGORITHM = 'EG1-HMAC-SHA256'
def create_auth_header(method, scheme, host, path, headers, payload = nil, _timestamp = nil, _nonce = nil)
timestamp = _timestamp.nil? ? Time.now.gmtime.strftime('%Y%m%dT%H:%M:%S%z') : _timestamp
nonce = _nonce.nil? ? UUIDTools::UUID.random_create.to_s : _nonce
signing_key = Base64.encode64(OpenSSL::HMAC::digest(OpenSSL::Digest::SHA256.new, @secret, timestamp)).strip
authorization_header = SIGNING_ALGORITHM + ' '
authorization_header << "client_token=#{@client_token};"
@hokamoto
hokamoto / Temp.java
Created March 9, 2013 11:46
lexer test
import java.util.regex.Pattern;
public class Temp {
public static void main(String[] args) {
System.out.println(Pattern.compile("^0$|^0^[0-9]+|^[1-9][0-9]*").matcher("0^1").find());
}
}
@hokamoto
hokamoto / gist:4162106
Last active October 13, 2015 07:38
Rotate images by 90 degrees in place.
package questions;
public class Questions1_6 {
public static void main(String[] args) {
// initialize matrix
final int N = 10;
int[][] matrix = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
@hokamoto
hokamoto / gist:4103564
Last active October 12, 2015 23:28
Eliminating duplicate characters from strings
public static String deleteDuplicateCharacters(String val) {
char[] str = val.toCharArray();
int len = str.length;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
if (str[i] == str[j] && str[j] != 0) {
for (int k = j; k < len - 1; k++) {
str[k] = str[k + 1];
}
@hokamoto
hokamoto / gist:4103527
Last active October 12, 2015 23:28
A *wrong* code for eliminating duplicate characters from strings
public static String deleteDuplicateCharacters(String val) {
char[] str = val.toCharArray();
int len = str.length;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
if (str[i] == str[j]) {
for (int k = j; k < len - 1; k++) {
str[k] = str[k + 1];
}