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):
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);
#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) {
@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
@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 / 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
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> {
#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));
}
@sasaki-shigeo
sasaki-shigeo / eratosthenes.scm
Created September 16, 2020 18:48
The Sieve of Eratosthenes / エラトステネスの篩
;;;;
;;;; Sieve of Eratosthenes
;;;;
(require srfi/2) ; and-let*
(require srfi/4) ; homogenous numeric vector
(require srfi/42) ; list comprehension
;;;;
;;;;