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 @PPogodi
View GitHub Profile
@joshuashaffer
joshuashaffer / nthSqrt2.hs
Created October 15, 2017 16:38
Get the n-th digit on the sqrt(2)
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
nthSqrt2 n d=foldl1(++).map show.map((!!)$w 2 0)$[(1+n)..(d+n)]
@joshuashaffer
joshuashaffer / stringsRearrangement.hs
Created October 15, 2017 16:36
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a way that after the rearrangement the strings at consecutive positions would differ by exactly one character.
import Data.Char
import Data.List
stringsRearrangement_ inputArray =  zap
    where chars = map (map $ ord) inputArray
          zap   = foldl1 (&&) $  map (==1) .  map (sum). map (map (\x -> if (x == 0) then 0 else 1) ) . zipWith (\x y-> zipWith (-) x y) chars $ tail chars
stringsRearrangement xs = foldl1 (||) $ map stringsRearrangement_ $ permutations xs
@joshuashaffer
joshuashaffer / codeGolfReducedScore.hs
Created October 15, 2017 16:31
Tokenize javascript. Count Tokens.
import Data.List
import Data.Char
data Operator = Plus | Minus | Times | Div | LParen | RParen | Equals | Dot | XOR | Semi | LBracket | RBracket | GreaterThan | LessThan | LBrace | RBrace | Comma | Mod | QMark | Colon | And | Or | At | BitNeg | Hash | RDiv | LNot
   deriving (Show, Eq)
data Token = TokOp Operator
          | TokIdent String
          | TokString String
          | TokNum String
@joshuashaffer
joshuashaffer / clockRadian.py
Created October 15, 2017 16:21
Given a time, your mission is to calculate the angle between the hour hand and the minute hand on an analog clock and return it in radians in lowest terms.
import fractions as f
def clockRadian(time):
   h, m = tuple(map(int, time.split(':')))
   t = abs((h % 12) * 60 - m * 12 + m)
   t = 720 - t if t > 360 else t
   if t == 0:
       return "0"
   a = f.Fraction(t, 360)
   b = a.numerator
   c = a.denominator
@joshuashaffer
joshuashaffer / prevPalindrome.hs
Created October 15, 2017 16:18
Given a positive integer x, find the greatest number smaller than x that is a palindrome. A number is considered to be a palindrome if its string representation looks the same when written backwards as it does when written forwards. For example, 1991, 7 and 808 are all palindromes.
prevPalindrome x =  head$dropWhile e [x-1,x-2..]
s=show
e x = s x /= (reverse$s x)
@joshuashaffer
joshuashaffer / stringsRearrangement.hs
Created October 15, 2017 16:15
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a way that after the rearrangement the strings at consecutive positions would differ by exactly one character.
import Data.Char
import Data.List
stringsRearrangement_ inputArray =  zap
    where chars = map (map $ ord) inputArray
          zap   = foldl1 (&&) $  map (==1) .  map (sum). map (map (\x -> if (x == 0) then 0 else 1) ) . zipWith (\x y-> zipWith (-) x y) chars $ tail chars
stringsRearrangement xs = foldl1 (||) $ map stringsRearrangement_ $ permutations xs
@joshuashaffer
joshuashaffer / singkatan.cpp
Created October 15, 2017 16:12
Given two strings s1 and s2, merge them into one string by overlapping the suffix of the first string and the prefix of the second string. If there's nothing to concatenate, return the concatenation of the strings.
#define x substr
auto singkatan(auto w, auto q) {
int a=w.length(),b=q.length(),m=a<b?a:b;
while(m--)if(w.x(a-m-1,m+1)==q.x(0,m+1))q=q.x(m+1,b-m);
return w+q;
}
@joshuashaffer
joshuashaffer / thatsOdd.hs
Created October 15, 2017 16:11
Is the sum of the ascii character codes odd?
import Data.Char
thatsOdd = odd . sum . map (ord)
@joshuashaffer
joshuashaffer / steganographer.cpp
Created October 15, 2017 16:09
Recover Steganography Code embedded in RGB Values. e is the bit position.
auto steganographer(vector<vector<long long>> r, int e) {
bool f[r.size() * r[0].size()],*t;
int z = 0;
for (i:r)
for (j:i)
for(l:{3,2,1,0})
if (e&(1<<l)) f[z++] = j >> 8*l & 1;
string s;
for (t = f; t < f + z; t += 7)
s += accumulate(t, t + 7, 0, [](int x, int y) { return (x << 1) + y; });
@joshuashaffer
joshuashaffer / surviveIt.cpp
Created October 15, 2017 16:05
Extract the most significant binary digit from an integer.
int surviveIt(int n){
// Get the index of the most significant bit.
asm ("bsrl %1,%0" : "=r" (n) : "r" (n));
return 1<<n;
}
// movl $1, %eax
// bsrl %edi, %ecx
// sall %cl, %eax
// ret