Skip to content

Instantly share code, notes, and snippets.

View hungtuchen's full-sized avatar

Hung-Tu Chen hungtuchen

View GitHub Profile
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
# for auto-reloading external modules
# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython
%load_ext autoreload
%autoreload 2
@hungtuchen
hungtuchen / getSubStringIndex.js
Created September 26, 2015 02:34
index all occurrences of subString
function getSubStringIndex(str, pattern) {
var indexArray = [];
for (var i = -1; (i = str.indexOf(pattern, i + 1)) !== -1; ) {
indexArray.push(i);
}
return indexArray;
}
function getSubStringIndexRecursion(lastIndex, str, pattern) {
var arr = [];
@hungtuchen
hungtuchen / a10.js
Created September 14, 2015 12:41
find sub string in nested array recursively
let list= ['abc', 'efg', ['abctest', ['aodb', 'kdka', ['dkkd', 'ad']]]];
/*
1. find out string which contains 'a' output: [[0], [2, 0], [2, 1, 0], [2, 1, 1], [2, 1, 2, 1]]
2. regex
*/
@hungtuchen
hungtuchen / fbJsonToCsv.js
Last active September 14, 2015 10:21
script for transforming facebook response json to csv
import fs from 'fs';
import fbData from './facebook-data.json';
let result = "id,message,likes\n";
fbData.posts.data.map((post) => {
result += `${post.id},${post.message || ''},${post.likes.summary.total_count}\n`;
});
fs.writeFileSync('facebook-ma.csv', result);
@hungtuchen
hungtuchen / snippet.js
Last active August 31, 2015 02:56
snippet for demonstrating to interview
// I add the source where the snippet coming from at the begining.
// And the comment about what certain part of snippet is about at the end.
// https://github.com/g0v/ppt/blob/master/common/views/Governor.jsx
if (isLoading || errorMessage || !governor) {
return (
<div style={styles.root}>
<section>
{ isLoading ? <Loading /> :
errorMessage ? errorMessage :
@hungtuchen
hungtuchen / FbInitUtils.js
Created May 2, 2015 07:39
Parse.FacebookUtils.logIn only work in incognito mode
'use strict';
import * as FbApiActionCreators from '../actions/FbApiActionCreators.js';
import {Parse} from 'parse';
// convert messages in thread
function mapToMessages(message) {
return ({
id: message.id,
created_time: message.created_time,
require 'yaml'
def file_reader_by_line(in_filename) # method for reading file line by line.
f = File.open(in_filename, 'r')
f.each_line do |line|
yield line
end
f.close
end
require 'yaml'
def yaml_to_arr(in_filename = ARGV[0], out_filename = ARGV[1])
complete_array = []
arr_of_hash = YAML::load(File.read(in_filename))
arr_of_hash.each do |hash|
temp_arr = []
temp_arr.push (hash[:date] << "\t")
temp_arr.push (hash[:student_id] << "\t")
temp_arr.push (hash[:languages] << "\t")
def fizzbuzz(size)
(1..size).each do |num|
result = ''
result << 'Fizz' if num % 3 == 0
result << 'Buzz' if num % 5 == 0
result = num.to_s if result == ''
yield result
end
end