Skip to content

Instantly share code, notes, and snippets.

View igorvolnyi's full-sized avatar

Igor Volnyi igorvolnyi

View GitHub Profile
/**
* Algirithm: reverse string using recursion.
* Complexity: O(n)
*/
public class ReverseStringRecurse {
public String reverse(String str) {
if(str == null || str.length() < 2)
return str;
return reverse(str.substring(1)) + str.charAt(0);
@igorvolnyi
igorvolnyi / AjaxFindPersonsSnippet.php
Last active March 13, 2018 09:02
AJAX find persons
/**
* Returns persons via AJAX.
* @return mixed JSON data
*/
public function actionFindAjax($q = null) {
if(Yii::$app->request->isAjax && !empty($q)) {
$out = ['results' => ['id' => '', 'text' => '']];
Yii::$app->response->format = Response::FORMAT_JSON;
$query = new Query();
$result = $query->select([
@igorvolnyi
igorvolnyi / index.pug
Last active March 14, 2018 10:26
Layout this!
.wrap
.container
.col-md-9
.main-content
p main content
.col-md-3
.sidebar
p sidebar
touch ~/.zsh_functions
vim ~/.zsh_functions
function jcurl() {
  curl -s "$@" | json | pygmentize -l json
}

function tojson(){
@igorvolnyi
igorvolnyi / urldecode
Last active October 20, 2018 00:22
urldecode
#! /usr/bin/python3
import sys
from urllib.parse import unquote
print(unquote(sys.argv[1]))
#! /usr/bin/python3
from urllib.parse import unquote
print(quote(sys.argv[1]))
@igorvolnyi
igorvolnyi / get_get_params.js
Created December 14, 2018 03:59
Get GET parameters in your browser as JS object.
function getGetParameters() {
var s1 = location.search.substring(1, location.search.length).split('&'),
r = {}, s2, i;
for (i = 0; i < s1.length; i += 1) {
s2 = s1[i].split('=');
r[decodeURIComponent(s2[0])] = decodeURIComponent(s2[1]);
}
return r;
}
window.ServerData = getGetParameters();
@igorvolnyi
igorvolnyi / is_prime.py
Last active January 4, 2019 08:35
Test if a number is prime in python
# Taken from
# @see https://www.daniweb.com/programming/software-development/code/216880/check-if-a-number-is-a-prime-number-python
def is_prime(n):
if n == 2 or n == 3:
return True
elif n < 2 or n % 2 == 0:
return False
elif n < 9:
return True
elif n % 3 == 0:
const PromisesBus = function() {
this.promises = {};
this.resolves = {};
this.rejects = {};
}
PromisesBus.prototype.when = function(what) {
return this.register(what);
},
PromisesBus.prototype.register = function(what) {
if (this.promises.hasOwnProperty(what)) {
@igorvolnyi
igorvolnyi / genrandompass.sh
Created February 26, 2019 23:56
Generate random passwords using core linux utils only
# Count is needed to limit amount of bytes read from /dev/urandom
# fold controls letgth of displayed character sequences
# head specifies number of passwords to generate
# tr defines characters to use
dd if=/dev/urandom bs=1 count=30000 status=none | tr -cd '[:graph:]' | fold -w 25 | head -n 40