Skip to content

Instantly share code, notes, and snippets.

View joshuashaffer's full-sized avatar
🎯
练习关系

. joshuashaffer

🎯
练习关系
  • Shijiazhuang Donghua Jinlong Chemical Co., Ltd.
  • Shijiazhuang, Hebei, China
  • X @BertChintez
View GitHub Profile
@joshuashaffer
joshuashaffer / gist:b7da7cbb1d6b9bd77f4d
Created May 7, 2015 18:47
Convert Decimal to Fraction. Version without gotos.
!***********************************************************************************************************************************
!
! D E C 2 F R A C
!
! Module: DEC2FRAC
!
! Programmer: David G. Simpson
! NASA Goddard Space Flight Center
! Greenbelt, Maryland 20771
!
@joshuashaffer
joshuashaffer / possible_encodings.py
Created March 8, 2016 02:32
Figure out all possible alternative encodings for a unicode string.
# coding=utf-8
import pkgutil
import encodings
# List of possible codecs.
# Multibyte unicode is unlikely. Along with other...
unlikely_codecs = {"bz2_codec", "uu_codec", "base64_codec", "hex_codec", "zlib_codec", "unicode_escape",
"unicode_internal", "utf_16", "utf_16_le", "utf_16_be", "utf_32", "utf_32_be", "utf_32_le",
"raw_unicode_escape","punycode","palmos","idna"}
found = set(name for imp, name, ispkg in pkgutil.iter_modules(encodings.__path__) if not ispkg)
let empty_board = Array2D.init 8 8 (fun i j -> 0)
let check_board (board : int[,]) =
let queens = [for idx_i in 0..7 do
for idx_j in 0..7 do
if(board.[idx_i,idx_j] > 0) then
yield idx_i,idx_j]
//printfn "%A" queens
let z = List.length(queens)
@joshuashaffer
joshuashaffer / borland.c
Created May 13, 2016 02:45
Example of BorlandC inline ASM
//--------------------------------------------------------------------------
// FarRead()
//-------------------------------------------------------------------------
boolean FarRead (int handle, char far *dest, long length)
{
if (length>0xffffl)
TrashProg("FarRead doesn't support 64K reads yet!");
asm push ds
asm mov bx,[handle]
@joshuashaffer
joshuashaffer / const_cast_t.hpp
Created May 18, 2016 00:44
Const cast without specifing type...
template <typename T>
constexpr T& const_cast_t(const T& p) {
using pctype = std::remove_const_t<T>;
return const_cast<pctype&>(p);
}
@joshuashaffer
joshuashaffer / lprint.py
Created May 18, 2016 21:29
My linux printing solution...
#! /usr/bin/env python
import sys,os,subprocess
import climate
def printer(filename,printer_ip= "192.168.0.50"):
_rc0 = _rcr1, _rcw1 = os.pipe()
if os.fork():
os.close(_rcw1)
os.dup2(_rcr1, 0)
subprocess.call(["nc",printer_ip,"9100"],shell=False)
@joshuashaffer
joshuashaffer / sqrt_digits.hs
Last active February 19, 2017 22:24
Compute Sqrt, Decimal Digit at a Time, As List.
w x r=d:w(100*(x-20*r*d-d*d))(10*r+d)
where d=head(filter(\d -> 20*r*d+d*d>=x)[0..])-1
sqrt_digits n = w n 0
@joshuashaffer
joshuashaffer / news.ycombinator.com.css
Created March 9, 2017 22:19
stylebot css to change the color of [dead] and [flagged] on hackernews.
span.cdd {
color: #ff0000;
}
span.c88 {
color: #001aff;
}
span.cae p {
color: #7d107d;
@joshuashaffer
joshuashaffer / recover_lat_long_2.py
Created May 7, 2017 19:58
Recover Lat/Log from only two sets of (d,lat,log)
import numpy
import scipy
import scipy.optimize
def latlon_system(d1, my_phi1, my_lambda1, d2, my_phi2, my_lambda2):
"""
Non-linear objective function to solve for lat,long of unknown, given
known positions and distances...
@joshuashaffer
joshuashaffer / cauchyEstimator.m
Created October 15, 2017 15:12
Estimate Parameters of Cauchy Distribution.
function q = cauchyEstimator(x)
N = numel(x);
A = @(t) sum(1 ./ (t(1) - x + t(2) .^ 2) .^ 2);
B = @(t) 2 * t(2) * sum(1 ./ (t(1) - x + t(2) .^ 2) .^ 2);
D = @(t) -(N - 2 * sum(1 ./ (t(1) - x + (t(2) ^ 2)) .^ 2 .* ((t(2) .^ 2) - t(1) + x)) * t(2) .^ 2) ./ t(2) .^ 2;
J = @(t) [A(t) B(t); B(t) D(t)];
F = @(t)[-sum((2 * t(1) - 2 .* x) ./ ((t(1) - x) .* (t(1) - x) + t(2)*t(2))),N/t(2)-sum(2*t(2)./((t(1)-x).^2+t(2)^2))];