Skip to content

Instantly share code, notes, and snippets.

View manhdaovan's full-sized avatar

Vanmanh Dao manhdaovan

View GitHub Profile
@manhdaovan
manhdaovan / SplitString.scala
Last active February 13, 2017 09:57
Split String into 2 strings with lower and upper case. Eg: split("HelloWorld") returns ["HW", "elloorld"]
/**
* `benchmark` function took:
* scala -nc SplitString.scala 14.71s user 0.50s system 274% cpu 5.532 total
* on MacbookPro Late 2012 (Core i5 2.5, DDR3 8GB @1600)
*/
object SplitString {
def split(string: String): (String, String) = {
def loop(chars: List[Char], upper: String, lower: String): (String, String) =
chars match {
case Nil => (upper, lower)
@manhdaovan
manhdaovan / remove_duplicate_rows.sql
Last active March 17, 2017 01:52
Remove duplicate rows on tables
/* Remove duplicate rows on table by simple query.
Repeate until no more query is removed */
delete from shop_product_price
where id in
(select tmp.id from
(select *, count(*) as num from shop_product_price
group by shop_id, product_id, price, min_quantity, shipping_fee
having num >= 2) as tmp);
@manhdaovan
manhdaovan / compare_vietnamese_string.rb
Last active February 27, 2017 01:48
Compare Vietnamese string
def string_2array(s)
s.split('')
end
# return 1 if s2 > s1
# return 0 if s2 == s1
# return -1 if s2 < s1
def my_compare(s1, s2)
downcase_letters = ' aàáảãạăằắẳẵặâầấẩẫậbcdđeèéẻẽẹêềếểễệfghiìíỉĩịjklmnoòóỏõọôồốổỗộơờớởỡợpqrstuùúủũụưừứửữựvwxyỳýỷỹỵz'.freeze
upcase_letters = 'AÀÁẢÃẠĂẰẮẲẴẶÂẦẤẨẪẬBCDĐEÈÉẺẼẸÊỀẾỂỄỆFGHIÌÍỈĨỊJKLMNOÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢPQRSTUÙÚỦŨỤƯỪỨỬỮỰVWXYỲÝỶỸỴZ'.freeze
@manhdaovan
manhdaovan / similar_numbers.rb
Last active March 16, 2017 01:44
Calculate number of similar numbers
# Calculate number of Similar Numbers
# 112 has 112, 121, 211 (3 similar numbers)
# 100 has 100 (1 similar number)
# Given integer a (1 <= a <= 2^32)
# Calculate number of similar numbers of a
def permute(n)
return 1 if n == 0
(1..n).reduce(:*)
end
@manhdaovan
manhdaovan / merge_cell.js
Created March 17, 2017 01:50
Merge table cell by key column
// Merge table cell by cell value,
// dependent or independent to other column
// jQuery is required.
// Eg: Table has 2 col: Date and Fullname
// |----------------------------|
// | Date | Fullname |
// |--------------|-------------|
// | 2012/11/01 | ManhDV |
// |--------------|-------------|
@manhdaovan
manhdaovan / nginx-tuning.md
Created March 17, 2017 01:53 — forked from denji/nginx-tuning.md
NGINX tuning for best performance

NGINX Tuning For Best Performance

For this configuration you can use web server you like, i decided, because i work mostly with it to use nginx.

Generally, properly configured nginx can handle up to 400K to 500K requests per second (clustered), most what i saw is 50K to 80K (non-clustered) requests per second and 30% CPU load, course, this was 2 x Intel Xeon with HyperThreading enabled, but it can work without problem on slower machines.

You must understand that this config is used in testing environment and not in production so you will need to find a way to implement most of those features best possible for your servers.

@manhdaovan
manhdaovan / laravel_and_wordpress_mixin.conf
Last active April 12, 2017 02:10
Nginx configuration for Laravel and wordpress mixin (wordpress as laravel module)
# You have 2 project as:
# /var/www/html/laravel_app, and
# /var/www/html/wordpress_app
# You want to access laravel app as default except blog in url.
# Eg: http://example.com or http://example.com/controller-name
# but when http://example.com/blog, it point to wordpress app.
server {
listen 80;
server_name example.com;
@manhdaovan
manhdaovan / init_db_to_ram_disk.sh
Last active March 12, 2018 01:20
Init RAM disk and run MySQL on it. (MacOS)
#!/bin/bash
# Create 2GB RAM disk
diskutil erasevolume HFS+ "ramdisk" `hdiutil attach -nomount ram://4194304`
# Install mysql to above ramdisk, with no password
mysqld --initialize-insecure --log-error-verbosity --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/Volumes/ramdisk/mysql --tmpdir=/tmp
# Stop current mysql if existing
mysql.server stop
@manhdaovan
manhdaovan / delete_local_branch
Created March 22, 2018 02:09
Bash: Delete local branch before time
# Usage: delete_local_branch number_of_weeks_ago
# Eg: delete_local_branch 4
function delete_local_branch() {
for k in $(git branch | sed /\*/d); do
if [ -n "$(git log -1 --before='$1 week ago' -s $k)" -a "$k" != "develop" -a "$k" != "master" -a "$k" != "staging" ]; then
echo "Delete $k"
git branch -D $k
fi
done
@manhdaovan
manhdaovan / .bashrc
Created March 29, 2018 03:56
scp via proxy
# Download file from target server via proxy server using scp command
# Usage: scp_download_file /path/to/file/in/target/server
function scp_download_file(){
scp -o "ProxyCommand ssh user@proxy_server_domain_or_ip -W %h:%p" user@target_server_domain_or_ip:$1 ~/Downloads/
}