Skip to content

Instantly share code, notes, and snippets.

View hoyangtsai's full-sized avatar
⌨️
Prompting

Hoyang Tsai hoyangtsai

⌨️
Prompting
View GitHub Profile
@hoyangtsai
hoyangtsai / ip.sh
Created January 1, 2022 05:14
#shell get local ip address #command
#!/bin/bash
IP=`ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\ -f2`
echo "$IP"
@hoyangtsai
hoyangtsai / unshift.js
Created December 27, 2021 06:49
Add an element ahead of array #snippet #array
// use ES6 unshift
TheArray.unshift(TheNewElement);
// use splice
TheArray.splice(0, 0, TheNewElement);
@hoyangtsai
hoyangtsai / diff.js
Created December 27, 2021 06:45
Get differences of two arrays #snippet #array
Array.prototype.diff = function(a) {
return this.filter(function(i) {return a.indexOf(i) < 0;});
};
@hoyangtsai
hoyangtsai / mergeArray.js
Last active December 27, 2021 06:38
Merge two sorted arrays #snippet #array
const arr1 = [3, 5, 6, 10, 11, 20];
const arr2 = [1, 2, 7, 8, 15, 19];
mergeTwo(arr1, arr2); // [1, 2, 3, 5, 6, 7, 8, 10, 11, 15, 19, 20]
// First Approach
// O(n log n) time & O(n) space
function mergeTwo(arr1, arr2) {
let result = [...arr1, ...arr2];
return result.sort((a,b) => a-b);
@hoyangtsai
hoyangtsai / duplcArray.js
Created December 27, 2021 06:28
Duplicate an array (make a copy, not reference)
const arr = [1,2,3,4,5];
// Spread Operator
const arr1 = [...arr];
// Slice Operator
const arr2 = arr.slice();
// Concat
const arr3 = [].concat(arr)
@hoyangtsai
hoyangtsai / ArrayToString.js
Created December 27, 2021 06:24
Convert array to a string
// 1. Use .toString()
['a', 'b', 'c'].toString(); // 'a,b,c'
// 2. Coerce to string
['a', 'b', 'c'] + []; // 'a,b,c'
['a', 'b', 'c'] + ''; // 'a,b,c'
// 3. Use .join()
['a', 'b', 'c'].join(); // 'a,b,c' (defaults to ',' separator)
['a', 'b', 'c'].join(''); // 'abc'
@hoyangtsai
hoyangtsai / uniq.js
Created December 27, 2021 06:20
Remove duplicates from an array
// use Set
const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
let unique = [...new Set(names)];
console.log(unique); // 'John', 'Paul', 'George', 'Ringo'
// use Filter
const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
let x = (names) => names.filter((v,i) => names.indexOf(v) === i)
@hoyangtsai
hoyangtsai / range.js
Last active December 27, 2021 06:41
javascript range array function #snippet #jsUtil
function range(start, edge, step) {
// If only one number was passed in make it the edge and 0 the start.
if (arguments.length == 1) {
edge = start;
start = 0;
}
// Validate the edge and step numbers.
edge = edge || 0;
step = step || 1;
@hoyangtsai
hoyangtsai / index.js
Created August 28, 2021 09:33
nodejs imgur upload
// register an application
// https://api.imgur.com/oauth2/addclient
// get registered apps info
// https://imgur.com/account/settings/apps
const imgur = require('imgur');
imgur.setClientId(`${CLIENT_ID}`);
@hoyangtsai
hoyangtsai / litemine.sh
Created May 1, 2021 09:40
lite mining via litecoinpool with bfgminer
#!/bin/sh
export DISPLAY=:0
export GPU_MAX_ALLOC_PERCENT=100
export GPU_USE_SYNC_OBJECTS=1
./bfgminer --scrypt --url=stratum+tcp://litecoinpool.org:3333 --userpass=h0yang.1:1 --url=stratum+tcp://us.litecoinpool.org:3333 --userpass=h0yang.1:1 --failover-only