Skip to content

Instantly share code, notes, and snippets.

View alopatindev's full-sized avatar

Alexander Lopatin alopatindev

View GitHub Profile
@alopatindev
alopatindev / run_jad.sh
Created June 17, 2015 16:36
Decompile recursively directory with *.class files
#!/bin/bash
P=$(pwd)
for d in $(find . -type d); do
cd "$P/$d"
for f in *.class; do
yes n | jad -s '.java' "$f"
done
done
@alopatindev
alopatindev / Perm.scala
Last active December 23, 2015 22:58
Permutations (dynamic programming)
object Perm extends App {
def time[A](f: => A) = {
val start = System.nanoTime
val ret = f
val dt = (System.nanoTime - start) / 1e6
dt
}
class Permutations[T](implicit ord: Ordering[T]) {
@alopatindev
alopatindev / stack_array_as_argument.c
Last active January 9, 2016 01:10
Passing multidimensional array created on stack as function argument without dimention specifying
// "we can not declare that function as accepting a pointer-to-pointer" (c) https://www.eskimo.com/~scs/cclass/int/sx9a.html
// Instead we can do this ugly thing
#include <stdio.h>
#include <stdlib.h>
void printStackArray(const int* const arr, const size_t n, const size_t m)
{
size_t i, j;
for (i = 0; i < n; i++)
@alopatindev
alopatindev / primes_benchmark.cc
Last active January 20, 2016 12:18
primes benchmark
// clang++ -Wall -std=c++11 primes_benchmark.cc -o primes_benchmark -O3 && ./primes_benchmark
#include <cassert>
#include <chrono>
#include <functional>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
#include <utility>
@alopatindev
alopatindev / CommonRectsArea.scala
Last active February 10, 2016 00:56
CommonRectsArea.scala
// find a common area of two (probably intersected) rectangles
object CommonRectsArea extends App {
type Coord = (Int, Int)
class Rect(val bottomLeft: Coord, val topRight: Coord) {
def width: Int = topRight._1 - bottomLeft._1
def height: Int = topRight._2 - bottomLeft._2
def area: Int = width * height
@alopatindev
alopatindev / cargo_silent_watch.sh
Last active February 19, 2016 13:24
Filtered cargo + inotifywait
#!/bin/bash
# pip2 install toml
# wget https://raw.githubusercontent.com/rust-lang/rust/master/src/etc/ctags.rust -O ~/.config/ctags.rust
SRCDIR=src
RUSTFLAGS="-C target-cpu=core-avx2"
#PROJECT="${PWD##*/}"
PROJECT=$(python2 -c 'import toml; print toml.loads(file("Cargo.toml").read())["package"]["name"]')
// find common area of given list of rectangles
object CommonAreaRects extends App {
type Coord = (Int, Int)
class Rect(val bottomLeft: Coord, val topRight: Coord) {
override def toString: String = s"Rect($bottomLeft, $topRight)"
def ==(other: Rect): Boolean = bottomLeft == other.bottomLeft && topRight == other.topRight
@alopatindev
alopatindev / red_blue_buttons.py
Last active February 23, 2016 12:52
red_blue_buttons.py
'''
You have two positive integers: the first one is a, the second one is b.
You also have a red button and a blue button.
Whenever you push the red button, both your numbers are incremented by 1.
Whenever you push the blue button, both your numbers are multiplied by 2.
Your goal is to change the pair (a, b) into the pair (newA, newB).
You are given the ints a, b, newA, and newB.
@alopatindev
alopatindev / LongestAirPath.scala
Last active February 23, 2016 13:25
LongestAirPath.scala
// https://leetcode.com/problems/reconstruct-itinerary/
// Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the (longest) itinerary in order.
import scala.collection.immutable.{HashMap, HashSet}
object LongestAirPath extends App {
type Ticket = (String, String)
type TicketsFromMap = HashMap[String, List[String]]
@alopatindev
alopatindev / caesar.rb
Last active March 15, 2016 17:36
caesar.rb
def is_letter (x)
return x.ord >= "a".ord && x.ord <= "z".ord
end
def encrypt (text, key, n, offset)
encrypted_text = ""
text.each_char do |x|
if is_letter(x)
code = (x.ord + key - offset) % n + offset
encrypted_text += code.chr