Skip to content

Instantly share code, notes, and snippets.

View andriyankov's full-sized avatar

Dmitry Andriyankov andriyankov

View GitHub Profile
@andriyankov
andriyankov / sorted_squares.py
Last active November 24, 2022 08:03
Algorithm puzzle: return sorted squares for sorted input numbers. Input value numbers without limitations
# Дан отсортированный массив чисел. Нужно вернуть отсортированный массив квадратов этих чисел.
# Ограничение по сложности O(n)
# Диапазон чисел не ограничен
from pytest import mark
def index_of_min_abs(numbers):
''' Return index of minimal abs element
'''
@andriyankov
andriyankov / csv_read_and_write.py
Created November 12, 2022 09:59
CSV file reading and writing
import csv
def write_csv(filename, lines):
with open(filename, 'wt', newline='') as f:
csvout = csv.DictWriter(f, ('author', 'book'))
csvout.writeheader()
csvout.writerows(lines)
@andriyankov
andriyankov / sorting_algorithms.py
Created September 16, 2022 07:42
The index, choise, buble and count sorting algorithms
# Четыре простых алгоритма для сортировки чисел.
#
# Инварианты для сортировок методами вставок и выбора является то, что после очередной итерации
# слева от текущего индекса массив уже отсортирован.
#
# Инвариантом для пузырькового метода является то, что после каждой итерации справа от текущего
# индекса массив уже отсортирован.
def insert_sort(array):
'''
@andriyankov
andriyankov / csv_write_example.py
Created September 2, 2022 09:44
Write to CSV file
def save_report_to_csv(content, filename='report.csv'):
with open(filename, 'w', newline='') as f:
fieldnames = ('filename', 'time')
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for item in content:
writer.writerow({'filename': item[0], 'time': item[1]})
@andriyankov
andriyankov / make_passwords.js
Created May 25, 2022 09:23
Generate passwords list
const MAX_PASSWORD_BLOCK_SIZE = 3* 1024;
const MAX_PASSWORD_LENGTH = 128;
const CHARSET_OPTIONS = 'upper,lower,numbers,special';
function randomInt(max, min = 0) {
return Math.floor(min + Math.random() * (max - min));
}
@andriyankov
andriyankov / storiesindex.py
Created November 20, 2021 14:38
Example for SQLite3 usage
import sqlite3
DB_SCHEME = '''CREATE TABLE IF NOT EXISTS stories
(author TEXT,
categories TEXT,
endpoint TEXT,
title TEXT)
'''
@andriyankov
andriyankov / csvexample.js
Created February 10, 2021 09:54
Example CSV reading with PapaParse and Promise using
const fs = require('fs');
const Papa = require('papaparse'); // https://www.papaparse.com/
async function readCSVFile(filepath) {
return new Promise((resolve, reject) => {
Papa.parse(fs.ReadStream(filepath), {
complete: (results) => resolve(results.data),
error: (err) => reject(err)
});
@andriyankov
andriyankov / csvexample.js
Last active February 10, 2021 09:55
Example CSV reading with PapaParse and callback functions using
const fs = require('fs');
const Papa = require('papaparse'); // https://www.papaparse.com/
function readCSVFile(filepath, cb) {
Papa.parse(fs.ReadStream(filepath), {
complete: (results, file) => cb(null, results.data, file),
error: (err, file) => cb(err, null, file)
});
}
@andriyankov
andriyankov / js
Created September 26, 2019 12:43
JS: Class for Site login method with HTTP Basic Auth and async-constructor
const urlresolve = require('url').resolve;
const needle = require('needle');
class Site {
constructor(mainPageUrl, cookies) {
const PHPSESSID = 'PHPSESSID';
if (!(PHPSESSID in cookies)) {
throw new Error('PHP Session ID not found');
}
this.phpsessid = cookies[PHPSESSID];