Skip to content

Instantly share code, notes, and snippets.

View meagtan's full-sized avatar

Ata Deniz Aydın meagtan

  • Bilkent University
View GitHub Profile
@meagtan
meagtan / fuzzyclock.py
Last active May 15, 2017 22:40
Fuzzy clock script for Geektool
#!/usr/bin/python
from datetime import datetime
numwrds = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "quarter", "sixteen", "seventeen",
"eighteen", "nineteen", "twenty", "twenty-one", "twenty-two", "twenty-three",
"twenty-four", "twenty-five", "twenty-six", "twenty-seven", "twenty-eight",
"twenty-nine", "half"]
now = datetime.now()
@meagtan
meagtan / weather.sh
Created August 3, 2016 17:14
Geektool script to print current weather
curl --silent "http://wxdata.weather.com/wxdata/weather/local/TUXX0002?cc=*&unit=m&dayf=1" | \
grep -A2 '<tmp>' | tr '\n' ' ' | sed -E 's|.*<tmp>(.*)</tmp>.*<t>(.*)</t>.*|\2, \1°|'
@meagtan
meagtan / galois.c
Last active May 2, 2023 08:53
Quick implementation of Galois fields
/*
* The following is an implementation of the finite field GF(2^8) as bit vectors of length 8, where the nth bit represents the
* coefficient of the nth power of the generator in each element, and the generator satisfies the minimal polynomial
* x^8 + x^4 + x ^3 + x^2 + 1 in the prime field Z_2, in which addition is equivalent to XOR and multiplication to AND.
* The elements of GF(2^8) thus represent polynomials of degree < 8 in the generator x. Addition in this field is simply
* bitwise XOR, but multiplication requires the elimination of powers of x <= 8.
*/
#include <stdio.h>
#include <stdint.h>
@meagtan
meagtan / trie.js
Last active September 5, 2016 06:38
Trie implementation in JavaScript
// Tries are implemented as objects containing values and maps of letters to a value or subtrie.
// Construct trie from given list of strings (optional, defaults to 0)
function Trie(strings) {
this.value = null;
this.nodes = {};
if (strings !== undefined)
for (var str in strings)
this.addString(str);
@meagtan
meagtan / collatz.c
Last active October 21, 2016 19:18
Collatz conjecture
/*
* Verifies the Collatz conjecture up to N positive integers.
* The program searches backwards starting from 1 to form a tree, where each integer n has children 2n and (n-1)/3 if odd,
* and then searching this tree breadth-first without forming it.
*/
#include <stdio.h>
#include <stdlib.h>
#define N 1000
@meagtan
meagtan / search.c
Last active April 4, 2017 13:37
Newton's method for sorted array search
/*
* Search for integer a in sorted array arr[n] similarly to Newton's method
*/
int newtonsearch(int *arr, int n, int a)
{
int i = 0;
while (i >= 0 && i < n) {
if (arr[i] == a)
return i;
i -= (arr[i] - a) / (arr[i + 1] - arr[i]); // TODO check for boundary conditions, zero denominator, rounding errors
@meagtan
meagtan / hensel.py
Last active June 18, 2023 23:29
p-adic numbers implemented in Python
# Finding roots of polynomials in p-adic integers using Hensel's lemma
from padic import *
from poly import *
def roots(p, poly):
'Yield all roots of polynomial in the given p-adic integers.'
for root in xrange(p):
try:
yield PAdicPoly(p, poly, root)