Skip to content

Instantly share code, notes, and snippets.

View fluency03's full-sized avatar

Chang Liu fluency03

  • London
View GitHub Profile
/**
* Class: Calculator
*/
class Calculator(var x: Int, var y: Int) {
var temp = 0
def add(): Int = {
return x + y
}
def subtract(): Int = {
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
# Find potential outliers in values array
# and visualize them on a plot
def is_outlier(value, p25, p75):
"""Check if value is an outlier
"""
lower = p25 - 1.5 * (p75 - p25)
upper = p75 + 1.5 * (p75 - p25)
return value <= lower or value >= upper
# Reference: http://stackoverflow.com/questions/22354094/pythonic-way-of-detecting-outliers-in-one-dimensional-observation-data/22357811#22357811
import numpy as np
def is_outlier(points, thresh=3.5):
"""
Returns a boolean array with True if points are outliers and False
otherwise.
Parameters:
-----------
points : An numobservations by numdimensions array of observations
def compress_seq(seq, limit=5):
'''
Compress a sequence to a short on by compress a long-continuously-apprearing
sub-sequence into limited length.
Args:
seq: the sequence to be compressed.
limit: the sub-sequence length limitation.
Returns:
void foo(int[] array) {
int sum = 6;
int product = 1;
for (int i = 6; i < array.length ; i++) {
sum += array[i];
}
for (int i = 6; i < array.length; i++) {
product *= array[i] ;
}
System.out.println(sum + ", " + product);
def KnuthMorrisPratt(text, pattern):
'''Yields all starting positions of copies of the pattern in the text.
Calling conventions are similar to string.find, but its arguments can be
lists or iterators, not just strings, it returns all matches, not just
the first one, and it does not need the whole text in memory at once.
Whenever it yields, it will have read the text exactly up to and including
the match that caused the yield.'''
# allow indexing into pattern and protect against change during yield
def seq_in_seq(subseq, seq):
"""Return an index of `subseq`uence in the `seq`uence.
Or `-1` if `subseq` is not a subsequence of the `seq`.
The time complexity of the algorithm is O(n*m), where
n, m = len(seq), len(subseq)
>>> index([1,2], range(5))
1
>>> index(range(1, 6), range(5))
print __file__
print os.curdir
print os.getcwd()
print os.chdir('.')
print os.getcwd()
print os.path.dirname(__file__)
print os.pardir
print os.path.abspath(os.path.join(os.path.dirname( __file__ ), os.pardir))
print os.path.join(os.path.dirname( __file__ ), os.pardir)
@fluency03
fluency03 / time_dict_merge.py
Created December 1, 2016 15:43 — forked from treyhunner/time_dict_merge.py
Test performance of different dictionary merging functions in Python
"""
Results:
multiple_update: 57 ms
copy_and_update: 46 ms
dict_constructor: 56 ms
kwargs_hack: 45 ms
dict_comprehension: 45 ms
concatenate_items: 166 ms
union_items: 163 ms