Skip to content

Instantly share code, notes, and snippets.

View daGrevis's full-sized avatar
⌨️
Keyboard operator

Raitis Stengrevics daGrevis

⌨️
Keyboard operator
View GitHub Profile
@daGrevis
daGrevis / gist:1389717
Created November 23, 2011 19:56
Solution to problem #1 (ProjectEuler.net) in Python
numbers = range(1, 999 + 1)
result = 0
for that in numbers:
if that % 3 == 0 or that % 5 ==0:
result += that
print result # Prints '233168'.
@daGrevis
daGrevis / gist:1409716
Created November 30, 2011 16:33
No white-spaces
/^\S+$/
@daGrevis
daGrevis / full_name.py
Created December 31, 2011 20:06
Makes full name more anonymous (`John Smith` -> `John S.`)
full_name = raw_input('What\'s your full name? ')
parts_of_name = full_name.split()
first_name = parts_of_name[0]
last_name = parts_of_name[1]
initial_letter_of_last_name = last_name[0]
print '{0} {1}.'.format(first_name, initial_letter_of_last_name)
@daGrevis
daGrevis / ls.py
Created December 31, 2011 20:28
Lists files and directories (like `ls` command does)
import sys, os
try:
root = sys.argv[1];
except IndexError:
root = '.'
for dirname, dirnames, filenames in os.walk(root):
for subdirname in dirnames:
print os.path.join(dirname, subdirname)
@daGrevis
daGrevis / gist:1625091
Created January 17, 2012 06:04
Timestamp to datetime
Y-m-d H:i:s
@daGrevis
daGrevis / sql_where.php
Created January 24, 2012 13:45
Assoc array to SQL where
<?php
/**
* Makes SQL compatible WHERE statement from assoc array where array's keys are table's columns, but values - rows.
*
* @param array `array('a' => 'foo', 'b' => 123, 'c' => 'bar',)`
* @return string `WHERE `a` = 'foo' AND `b` = 123 AND `c` = 'bar'`
*
* @author daGrevis
*/
@daGrevis
daGrevis / empty_param.php
Created January 24, 2012 17:46
Request::empty_param() (Kohana 3.x)
<?php
class Request extends Kohana_Request {
/**
* Helper to reduce LOCs.
*
* @param string Key of the value
* @return boolean
*
@daGrevis
daGrevis / string1.py
Created February 6, 2012 06:04
PyQuick: String #1
# A. donuts
# Given an int count of a number of donuts, return a string
# of the form 'Number of donuts: <count>', where <count> is the number
# passed in. However, if the count is 10 or more, then use the word 'many'
# instead of the actual count.
# So donuts(5) returns 'Number of donuts: 5'
# and donuts(23) returns 'Number of donuts: many'
def donuts(count):
if count < 10:
return 'Number of donuts: ' + str(count)
@daGrevis
daGrevis / string2.py
Created February 6, 2012 13:28
PyQuick: String #2
# D. verbing
# Given a string, if its length is at least 3,
# add 'ing' to its end.
# Unless it already ends in 'ing', in which case
# add 'ly' instead.
# If the string length is less than 3, leave it unchanged.
# Return the resulting string.
def verbing(s):
if len(s) < 3:
return s
@daGrevis
daGrevis / gist:1790459
Created February 10, 2012 15:58
[bench] String concatenation versus printing string
<?php
echo '<div style="display: none;">';
$s1 = microtime(true);
for ($i = 0; $i < 1000000; ++$i) {
echo 'a'.'b';