Skip to content

Instantly share code, notes, and snippets.

View khan5v's full-sized avatar

Vladimir Orekhov khan5v

  • Zurich, Switzerland
View GitHub Profile
import java.util.*;
public class Scheduler {
public static void main(String[] args) {
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
final Runnable ticker = new Runnable() {
public void run() {
System.out.println("Ran");
}
};
@khan5v
khan5v / IO.java
Created March 12, 2018 08:05
Basic file I/O
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("PATH1"));
int T = Integer.parseInt(br.readLine());
PrintWriter writer = new PrintWriter("PATH2", "UTF-8");
writer.println("Line");
writer.close();
}
@khan5v
khan5v / binpow.java
Created September 25, 2017 09:58
Binpow - a^n % m in sublinear time
public class Utils {
public static long mulmod(long a, long b, long mod) {
return (a * b) % mod;
}
public static long binpow(long a, long pow, long mod) {
if (pow == 0)
return 1;
long res = binpow(a, pow / 2, mod);
@khan5v
khan5v / stringUtils.java
Created September 18, 2017 08:39
String utils
// To join list of srings on a delimiter
List<String> l = new ArrayList<>();
String.join("+", l);
// To split based on control character
String[] strings = br.readLine().split("\\+");
@khan5v
khan5v / ascii
Created August 11, 2017 19:08
Java conversion from ASCII to int and back
int ascii_int = (int) 'a';
char ascii_char = (char) 97;
@khan5v
khan5v / pipe_ipc.pl
Created February 6, 2017 15:41
Parent - child IPC through pipes
#/usr/bin/perl
use strict;
use warnings;
use AnyEvent;
use AnyEvent::Handle;
use Parallel::SubFork;
my $manager = Parallel::SubFork->new();
@khan5v
khan5v / PairCompare.java
Created December 15, 2016 14:46
Java comparator usage
Collections.sort(list, new Comparator<Pair>() {
public int compare(Pair p1, Pair p2) {
//Reverse order of i (integers) and lexicographic order of s (strings)
return p1.i < p2.i ? 1 : p1.i > p2.i ? -1 : p1.s.compareTo(p2.s);
}
});
use strict;
use warnings;
use Parallel::SubFork;
use Data::Dumper;
my $manager = Parallel::SubFork->new();
for(1..10) {
$manager->start(\&cb, [$_]);
use strict;
use warnings;
use Data::Dumper;
use AnyEvent::Handle;
use AnyEvent;
my($pipe_w, $pipe_r);
pipe $pipe_r, $pipe_w;
my $cv = AnyEvent->condvar;
@khan5v
khan5v / nohang_forking.pl
Created November 3, 2016 14:36
Perl fork() example with multiple child processes and a way to stop the process in case child processes are stuck
use strict;
use warnings;
use POSIX qw( WNOHANG );
my $spawn_processes = 5;
my $forked = 0;
my ($err, $success) = (0, 0);
my $local_id = 0;
print "($$) parent has started\n";