Skip to content

Instantly share code, notes, and snippets.

View rdtr's full-sized avatar

Norio Akagi rdtr

View GitHub Profile
@rdtr
rdtr / gist:1812d889fa34a2058782
Last active August 29, 2015 14:10
C++: Formatting float to string with arbitrary decimal digits
std::stringstream ss;
// set a flag to formatting with decimal digits
ss.setf(std::ios_base::fixed, std::ios_base::floatfield);
float f = 1.555555;
// set a nuber of digits
ss.precision(2);
ss << "Float: " <<f;
@rdtr
rdtr / sequential_timeout.js
Created August 30, 2015 21:53
setTimeout sequeitial
# node --harmony sequential_timeout.js
# need to install co
var processor = function(timer) {
return new Promise(function(resolve, reject) {
setTimeout(resolve, timer * 1000);
});
}
var serial_timeout = function(timer_arr) {
@rdtr
rdtr / algorithms_dp_longest_common_substring.java
Last active November 3, 2015 01:20
algorithms_dp_longest_common_substring.java
public class Solution
{
public static int LCS(String s1, String s2) {
int l1 = s1.length();
int l2 = s2.length();
if (l1 == 0 || l2 == 0) return 0; // no common strings
// memorization
int[][] m = new int[l1][l2];
int maxLen = 0;
@rdtr
rdtr / algorithms_dp_longest_common_substring.py
Created November 3, 2015 02:05
algorithms_dp_longest_common_substring.py
def LCS(s1, s2):
if not s1 or not s2:
return 0
len1, len2 = len(s1), len(s2)
# memorization
m = []
for i in range(len1):
m.append([0]*len2) #m[len1][len2]
@rdtr
rdtr / algorithms_dp_longest_common_subsequence.java
Last active November 4, 2015 08:49
algorithms_dp_longest_common_subsequence.java
import java.lang.Math;
public class Solution {
public static String LCS(String s1, String s2) {
if (s1.isEmpty() || s2.isEmpty()) {
return "";
}
int l1 = s1.length();
int l2 = s2.length();
@rdtr
rdtr / algorithms_dp_longest_common_subsequence.py
Last active November 4, 2015 08:50
algorithms_dp_longest_common_subsequence.py
def LCS(s1, s2):
l1, l2 = len(s1), len(s2)
if l1 == 0 or l2 == 0: return ''
# memorization of m[l1][l2]
m = []
for x in range(l1):
m.append([0]*(l2))
# Fill 0th row
@rdtr
rdtr / algorithms_string_matching_brute_force.py
Created November 7, 2015 07:57
algorithms_string_matching_brute_force.py
def index_of(string, target):
leng_s = len(string)
leng_t = len(target)
if leng_s == 0 or leng_t == 0:
return -1
for i in range(leng_s):
j = 0
if i + leng_t > leng_s:
return -1
@rdtr
rdtr / algorithms_string_matching_brute_force.java
Created November 7, 2015 08:15
algorithms_string_matching_brute_force.java
class Solution
{
public static int indexOf(String s, String t) {
int s_length = s.length();
int t_length = t.length();
if (s_length == 0 || t_length == 0) return -1;
for (int i = 0; i < s_length; i++) {
if (i + t_length > s_length) return -1;
@rdtr
rdtr / algorithm_linkedlist_find_nth_from_tail_1.py
Last active November 8, 2015 20:13
algorithm_linkedlist_find_nth_from_tail_1.py
# Class of linked list Node.
class Node():
def __init__(self, val):
self.next = None
self.value = val
def next(self):
return self.next
def setNext(self, nextNode):
@rdtr
rdtr / algorithm_linkedlist_find_nth_from_tail_1.java
Created November 8, 2015 20:13
algorithm_linkedlist_find_nth_from_tail_1.java
// Class of linked list node
class Node {
public int value;
public Node next;
public Node(int value) {
this.value = value;
this.next = null;
}