Skip to content

Instantly share code, notes, and snippets.

View TuxCoding's full-sized avatar

Alex (TuxCoding) TuxCoding

  • [::1]
View GitHub Profile
@TuxCoding
TuxCoding / hasher.java
Created December 9, 2016 12:08
Hash a input string and return the hash as string
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class JavaHasher {
private static final String HASH_ALGO = "SHA512";
public String hashInput(String input) {
String hashed = null;
@TuxCoding
TuxCoding / FlightRecorder.java
Created December 9, 2016 12:06
Starts a new Java Flight Recording and dump the results to a file. It then can be analyzed by Java Mission Control.
import java.io.File;
import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
@TuxCoding
TuxCoding / HeapDump.java
Created December 9, 2016 12:04
Dumps the JVM heap stats to a single string or in a file which then can be analyzed by VisualVM or Java Mission Control
import java.io.File;
import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
@TuxCoding
TuxCoding / AgentLoadingRuntime.java
Created December 9, 2016 12:01
Loads a java agent at runtime
import com.github.games647.JarUtils;
import com.sun.tools.attach.VirtualMachine;
import java.lang.management.ManagementFactory;
public class AgentLoadingRuntime {
public static void loadAgent(String[] args) {
String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
int p = nameOfRunningVM.indexOf('@');
@TuxCoding
TuxCoding / settings.xml
Created December 4, 2016 13:09
Change the location of your local maven repository. Place this file into ~/.m2 and then maven use the specified folder for dependency management
<settings>
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ~/.m2/repository
-->
<localRepository>/path/to/local/repo</localRepository>
</settings>
@TuxCoding
TuxCoding / round-half-up.rkt
Created November 23, 2016 14:27
In racket the round method rounds to the next even number. This procedure rounds to the nearest neighbor.
;; round-half-up: number -> integer
;;
;; Rounds the number to the nearest neighbor
;;
;; Example: (round-half-up 2.5) = 3
(define (round-half-up number) (floor (+ number .5)))
;; Tests
(check-expect (round-half-up 2.5) 3)
(check-expect (round-half-up 3) 3)
@TuxCoding
TuxCoding / convert-base.rkt
Created November 23, 2016 14:09
Converts a number from base-10 to base-x
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname newton-method) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
;; length-representation: number number -> number
;;
;; Outputs the lowester exponent for b which is higher than x
;;
;; Example: (length-representation 10 2) = 4
(define (length-representation x b)
@TuxCoding
TuxCoding / newton.rkt
Created November 23, 2016 14:07
Newton method in racket
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname newton-method) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
;; newton-method: (X -> Y) (X -> Z) number number -> number
;;
;; Makes use of the newton method to calculate the f(x) = 0
;;
;; Example:
(define (newton-method fct fct-abl x delta)
@TuxCoding
TuxCoding / invert.rkt
Created November 22, 2016 16:24
Reimplementation of inverting a list
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-intermediate-reader.ss" "lang")((modname invert) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
(define (invert lst)
(cond [(empty? lst) empty]
[else (append (invert (rest lst)) (cons (first lst) empty))]))
(check-expect (invert empty) empty)
(check-expect (invert (list 1)) (list 1))
@TuxCoding
TuxCoding / int_to_binary.py
Created November 6, 2016 10:54
Converts an integer to a binary with two_complement and unsigned representation
#! /usr/bin/python3
# Example output:
# Enter a number:1024
# Number: 1024
# Needed bits: 11
# Bytes (unsigned) 100 0000 0000
# Bytes (two-complement) 0100 0000 0000