Skip to content

Instantly share code, notes, and snippets.

View kwonghung-YIP's full-sized avatar

Kwong-Hung YIP kwonghung-YIP

View GitHub Profile
@kwonghung-YIP
kwonghung-YIP / knapsack.java
Created May 25, 2022 20:08
Knapsack problem
// "static void main" must be defined in a public class.
public class Knapsack {
private String[] name;
private int[] weight;
private int[] value;
private int[][] m;
private int capacity;
public static void main(String[] args) {
@kwonghung-YIP
kwonghung-YIP / list_all_combination.java
Last active May 17, 2022 01:19
Find the binary string of a long value with bitwise operator (Java)
// "static void main" must be defined in a public class.
public class Main {
public static void main(String[] args) {
listItem(new String[] {"A","B","C","D","E","F"});
}
static public void listItem(String[] names) {
int n = names.length;
for (int c=0;c<Math.pow(2,n);c++) {
System.out.print("{");
@kwonghung-YIP
kwonghung-YIP / jmeter-bsh-example.bsh
Last active February 24, 2022 05:03
JMeter BeanShell Example to integrate with Paho Java MQTTv5
import java.nio.charset.StandardCharsets;
import org.eclipse.paho.mqttv5.client.MqttClientPersistence;
import org.eclipse.paho.mqttv5.client.persist.MemoryPersistence;
import org.eclipse.paho.mqttv5.client.MqttAsyncClient;
import org.eclipse.paho.mqttv5.client.MqttConnectionOptions;
import org.eclipse.paho.mqttv5.client.IMqttToken;
import org.eclipse.paho.mqttv5.common.MqttSubscription;
import org.eclipse.paho.mqttv5.client.MqttCallback;
@kwonghung-YIP
kwonghung-YIP / App.js
Created November 22, 2021 07:19
Example App.js for including mqtt.js into local expo.dev project
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
// expo install mqtt
// fix refer to mqtt.js issue: https://github.com/mqttjs/MQTT.js/issues/573
const mqtt = require('mqtt/dist/mqtt')
export default function App() {
@kwonghung-YIP
kwonghung-YIP / extract-ca-crt-from-domain
Created January 7, 2021 03:45
Extract CA cert from domain and save into Java keystore
# Inspect the Root CA Cert of your domain, and it as ca.crt
openssl s_client -connect <host>:<port> -showcerts
# Verify the cert contexnt
openssl x509 -in ca.crt -text
# Create the Java Keystore file (JKS) and import the ca.crt into it
jdk/bin/keytool -import -file ca.crt -keystore trust.jks -storepass password
# Verify the JKS file
@kwonghung-YIP
kwonghung-YIP / backup-jenkins-home.sh
Created December 14, 2020 09:36
[docker-CLI] backup the jenkins_home volume for Jenkins service in docker swarm
#!/bin/bash
bkupdir="<your backup folder>/bkup_`date +%Y%m%d_%H%M%S`"
log="$bkupdir/backup.log"
mkdir -p $bkupdir
echo "backup folder : $bkupdir"
echo "Start time : `date`" >> $log
@kwonghung-YIP
kwonghung-YIP / save-img-to-tag.sh
Last active December 11, 2020 04:15
[docker -CLI] Backup all images on the docker daemon to tag file, and those images had been pull from your private registry and using by the running containers.
#!/bin/bash
bkupdir="bkup_`date +%Y%m%d_%H%M%S`"
restorefile="$bkupdir/restore.sh"
log="$bkupdir/backup.log"
mkdir $bkupdir
echo "#!/bin/bash" > $restorefile
chmod u+x $restorefile
@kwonghung-YIP
kwonghung-YIP / tag-and-push-images.sh
Created December 3, 2020 03:42
[docker-CLI] Re-tag and Re-push the images that used by running containers to your private image registry
#!/bin/bash
docker login -u <<registry login>> -p <<password>> <<user registry host>>
docker ps -a --format "{{.ID}} {{.Image}}" | grep <<your registry prefix>> | \
while read line
do
ctrid=`echo $line | awk '{print $1}'`
imgtag=`echo $line | awk '{print $2}'`
@kwonghung-YIP
kwonghung-YIP / index.js
Created June 21, 2020 19:47
example for react-redux-thunk
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import * as serviceWorker from './serviceWorker';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import { Provider, useSelector, useDispatch } from 'react-redux';
import { Card } from 'react-bootstrap';
import 'bootstrap/dist/css/bootstrap.min.css';
import thunk from 'redux-thunk';
@kwonghung-YIP
kwonghung-YIP / index.js
Created June 18, 2020 18:19
Counter sample for using react-redux
import React from 'react';
import ReactDOM from 'react-dom';
import * as serviceWorker from './serviceWorker';
import { createStore, combineReducers } from 'redux';
import { Provider, useSelector, useDispatch } from 'react-redux';
const reducer = combineReducers({
counter: (state = 0, action) => {
switch (action.type) {
case "INCR":