Skip to content

Instantly share code, notes, and snippets.

@tymofij
tymofij / vortex.py
Created July 24, 2019 20:50
Creates a spiral of numbers in a matrix NxN
# creates a spiral of numbers in a matrix NxN
from copy import copy
def vortex(n):
i = 1
row = [0] * n
matrix = [copy(row) for _ in range(n)]
pos_row = 0
@tymofij
tymofij / terorium.txt
Last active September 5, 2015 15:08
GoogleTranslate fixed up by Yaroslav :)
President of Ukraine Avtodiy Dormidontovych Kromyeshnyy was crazy.
That means he wasn`t inadequate fool - but persistent paranoid.
It is as nothing had harmed if a potential fad of variatsko`s hospital A.D. Kromyeshnoho not suffered all Ukraine.
Immediatly after the inauguration of President-elect, then more or less mentally healthy person, according to tradition, decided to find out about his future.
For this he turned not to the astrologers, not to diviners, not the wizards, as did his predecessors, and - to fashion a professor of occult sciences, a magician and mystic, linguist and Bible scholars, Playboy and Player A. Cop-Aum.
The professor at that time was relatively young, irresponsible and for laughing predicted newly elected for A.D.Kromyeshnom long and happy Presidency, but warned of jewelry made of gold, they hiding death for Kromyeshniy.
Since that moment the President went crazy.
First, he ordered to everyone of his surroundings, begining with office chief and ending with maid, immediately to ge
@tymofij
tymofij / div_wo_div.py
Created July 1, 2015 12:22
div w/o div
from math import log, e, copysign
def div(x, y):
""" divide x by y without using / operator
"""
sign = x * y
res = e ** (log(abs(x)) - log(abs(y)))
res = copysign(res, sign)
rounded = int(round(res))
if rounded * y == x:
@tymofij
tymofij / edx-notes-api.sh
Last active August 29, 2015 14:12
edx-notes-api service at devstack
# make port available for your main machine
vagrant halt
# edit Vagrantfile, to line 72 (near other port forwarding stuff) add:
` config.vm.network :forwarded_port, guest: 8120, host: 8120`
vagrant up
# installing and starting API service
sudo su edxapp
pip install virtualenvwrapper
source virtualenvwrapper.sh
import math
def cube_root(x):
# negative number cannot be raised to a fractional power
res = math.copysign(abs(x) ** (1.0/3), x)
# 64 ** (1/3.0) gives us 3.9999999999999996
# and it breaks things up pretty bad. let's try finding int one
rounded_res = int(round(res))
if rounded_res ** 3 == x:
res = rounded_res
@tymofij
tymofij / inversions.py
Last active August 29, 2015 13:56
Solution to Array-Inversion-Count by codility
# https://codility.com/demo/take-sample-test/array_inversion_count
import bisect
def solution(A):
""" computes the number of inversions in A, or returns -1 if it exceeds 1,000,000,000
"""
# the idea is to store elements left of n-th sorted,
# which would allow us to easily find the number of them greater than n-th.
sorted_left = []
res = 0
@tymofij
tymofij / sinfest.py
Created November 17, 2013 14:29
Archive all available sinfest.net strips. You know, just in case.
#!/usr/bin/env python
from datetime import date, timedelta
import urllib, os
day = date(2000,1,17) # the first day
while day <= date.today():
name = day.strftime("%Y-%m-%d") + '.gif'
if not os.path.isfile(name):
@tymofij
tymofij / diligent_queryset.py
Last active December 17, 2015 03:09
QuerySet that lets you sort on object fields, and filter with lambdas. Of course, those actions will go though all the recordset. Use wisely.
from operator import attrgetter
from django.db import models
class DiligentQuerySet(models.query.QuerySet):
"""
Represents a QuerySet that allows filtering with lambdas,
and sorting on object properties.
"""
def __init__(self, *args, **kwargs):
self._custom_filters = []