Skip to content

Instantly share code, notes, and snippets.

View sasaki-shigeo's full-sized avatar

SASAKI Shigeo sasaki-shigeo

View GitHub Profile
@sasaki-shigeo
sasaki-shigeo / LinTable.py
Created May 24, 2022 00:09
Linear Search Table in Python and Ruby
from collections.abc import Mapping, MutableMapping
class Table(MutableMapping):
def __init__(self):
self.__table = [] # the internal table as a list of key-value pairs
def __len__(self):
return len(self.__table)
def __str__(self):
@sasaki-shigeo
sasaki-shigeo / rainbow-and-sky.pde
Last active January 16, 2022 17:33
A sample code in Processing: a (translucent) rainbow in the sky / Processing で Perlin noise を使った雲と半透明な虹
size(800, 400);
colorMode(HSB, 360.0, 1.0, 1.0, 1.0);
// The sky by the perlin noise
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
stroke(210, noise(0.005 * i, 0.005 * j), 1.0);
point(i, j);
}
}
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int e_atoi(char *str) {
enum { ST_START, ST_ZERO, ST_DEC, ST_OCT, ST_X, ST_HEX } state = ST_START;
int result = 0;
for (int c = *(str++); c != '\0'; c = *(str++)) {
switch (state) {
import java.io.Reader;
import java.io.StringReader;
import java.io.IOException;
import static java.lang.Character.isDigit;
enum State { START, ZERO, DEC, OCT, X, HEX }
public class String2int {
public static int string2int(String digits) {
return (int)string2long(digits);
@sasaki-shigeo
sasaki-shigeo / HashTable.java
Last active October 27, 2021 14:39
Sample Implementation of Hash Table (Open Address Method) / ハッシュ表の実装例
import java.util.*;
public class HashTable<K, V> extends AbstractMap<K, V> {
protected SimpleEntry<K, V>[] table_;
protected int count_ = 0;
private HashTable<K, V> self = this;
public HashTable(int n) {
table_ = (SimpleEntry<K, V>[])new SimpleEntry[n];
}
@sasaki-shigeo
sasaki-shigeo / HashTable.scala
Created October 14, 2021 18:07
Sample Implementation of Hash Table (Chain Method) / ハッシュ表の実装例(チェーン法)
import scala.collection.mutable
class HashTable[K, V](n: Int) extends mutable.AbstractMap[K, V] {
protected var table = mutable.ArraySeq.fill(n)(Nil:List[(K, V)])
protected var count = 0
def this() = this(10)
override def size: Int = count
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;
import java.util.AbstractMap;
import java.util.AbstractSet;
public class LinearSearchTable<K, V> extends AbstractMap<K, V> {
class Entry extends SimpleEntry<K, V> {
@sasaki-shigeo
sasaki-shigeo / LinearSearchTable.scala
Created October 14, 2021 09:48
A sample code of linear search in Scala
import scala.collection.mutable
class LinearSearchTable[K, V] extends mutable.AbstractMap[K, V] {
protected val table = new mutable.ArrayBuffer[(K,V)]
private def lin_search(key: K): Int = {
var (ix, result) = (0, -1)
while (ix < table.size &&
{ if (key == table(ix)._1) { result = ix; false } else true })
ix += 1
#include <stdlib.h>
#include <stdio.h>
int gcd(int m, int n) {
int r = m % n;
if (r == 0) {
return n;
}
else {
return gcd(n, r);
ArrayList<Star> stars = new ArrayList<Star>();
ArrayList<Crystal> snow = new ArrayList<Crystal>();
void setup() {
size(800, 600);
colorMode(HSB, 360, 1.0, 1.0, 1.0);
for (int i = 0; i < 30; i++) {
stars.add(new Star(random(width), random(0.7 * height), 30));
}