Skip to content

Instantly share code, notes, and snippets.

View zjor's full-sized avatar
🏠
Working from home

Sergey Royz zjor

🏠
Working from home
View GitHub Profile
@zjor
zjor / huffman.py
Last active December 15, 2015 06:49
Huffman Codes Implementation
text = 'abdfbcbedbabcfefbdddedbbfababc'
def build_huffman_tree(text):
f = {}
for char in text:
if char in f:
f[char] = f[char] + 1
else:
f[char] = 1
@zjor
zjor / perms.scala
Created March 22, 2013 04:12
Generates permutations recursively
object Main {
def perms(a: List[Int]): List[List[Int]] = {
if (a.size <= 1) List(a)
else {
for {
p <- perms(a.tail)
i <- (0 to p.size).toList
} yield p.take(i) ::: List(a.head) ::: p.drop(i)
}
@zjor
zjor / resize_images.py
Created April 3, 2013 09:22
Script for resizing images
# make sure that you have libjpeg and PIL installed
import re
import os, sys
import Image
size = 60, 60 # max_width, max_height
files = [f for f in os.listdir('.') if re.match(r'view[\d]{3}_large', f)]
@zjor
zjor / center-image.html
Created May 4, 2013 12:11
Center <img> inside <div> preserving image aspect ratio without knowing image size
<html>
<head>
<style type="text/css">
.wide-container {
width: 200px;
height: 20px;
border: 1px solid red;
text-align: center;
vertical-align: middle;
display: table-cell;
@zjor
zjor / apache.httpclient.4.2.3.ignore.ssl.java
Created May 30, 2013 11:03
How to avoid "Exception : javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated" Compliant with Apache Http Client 4.2.3
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
@zjor
zjor / HttpClientBuilder.java
Last active December 18, 2015 12:09
Convenient class for building SSL careless multithreaded Apache httpclient. Compatible with version 4.2.3
import lombok.extern.slf4j.Slf4j;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import javax.net.ssl.SSLContext;
@zjor
zjor / HashString.java
Created January 4, 2014 05:26
Implementation of standard hashing algorithm in Java
package org.example;
public class Main {
private static long power(long x, long y) {
if (y == 0) {
return 1;
} else if (y == 1) {
return x;
}
@zjor
zjor / meta_parser.js
Created January 28, 2014 09:55
Parses values of meta-tags from html page with jsdom & node.js
var jsdom = require('jsdom');
jsdom.env({
url: 'http://www.youtube.com/watch?v=c_b1j6trfa8',
scripts: ["http://code.jquery.com/jquery.js"],
done: function(error, window) {
var $ = window.$;
$('meta').each(function(){
var name = $(this).attr('property');
@zjor
zjor / create-http-client-ignoring-ssl
Created July 27, 2014 15:15
Creates Apache HttpClient ignoring SSL. Version httpclient > 4.2.xx
SSLContextBuilder sslbuilder = new SSLContextBuilder();
sslbuilder.loadTrustMaterial(null,
new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
@zjor
zjor / download.js
Created December 25, 2014 11:35
Downloads mp3 files from URLs listed in files.txt
var fs = require('fs');
var http = require('http');
var urls = fs.readFileSync('files.txt', {encoding: 'utf8'}).split('\n');
next();
function next() {
if (urls.length > 0) {
download(urls.pop(), next);