Skip to content

Instantly share code, notes, and snippets.

@drincruz
drincruz / python2_round
Created September 14, 2014 03:00
Python 2 round() vs. Python 3 round()
>>> round(0.5)
1.0
>>> round(2.5)
3.0
>>> round(0.4)
0.0
>>> round(2.6)
3.0
@drincruz
drincruz / string_appending.py
Created August 22, 2014 16:31
Appending to a Python string
"""
There are multiple ways to append to a string in Python
"""
str0 = "This is my string"
# Use the + operator
@drincruz
drincruz / db_row_generator.py
Created August 22, 2014 16:17
Simple example of using a Python generator to fetch a row from a MySQL database
#!/usr/bin/env python
"""
Here is a simple example of how you can
use a generator to fetch a row from a database
"""
import MySQLdb
@drincruz
drincruz / anagram.py
Created August 17, 2014 15:53
Simple example to find out if two strings are anagrams of each other
#!/usr/bin/env python
"""
Example of how you can test if strings are anagrams
"""
def is_anagram(str1, str2):
"""
Check if str1 is an anagram of str2
@drincruz
drincruz / .mailcap
Last active August 29, 2015 14:04
.mailcap and .muttrc entries
text/html; w3m -I %{charset} -T text/html; copiousoutput;
@drincruz
drincruz / SecretSanta.java
Last active December 25, 2015 07:49
An input String array of names and returns a two dimensional array to see who has who in secret Santa. So basically, {"user1":"user2"}, meaning "user1" needs to get "user2" a gift.
import static java.util.Arrays.deepToString;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Queue;
class SecretSanta {
public String[][] go(String[] s) {
@drincruz
drincruz / hex_to_ascii.py
Created July 25, 2013 13:58
Reads stdin hex and outputs ascii. Youtube posted on google+, so I wrote this quickly. https://plus.google.com/u/0/+youtube/posts/TWvnTtEgz8L
#!/usr/bin/env python
import sys
def main():
for line in sys.stdin:
print("%s\n" % bytes.fromhex(line.replace(' ', '').strip()).decode('utf-8'))
if '__main__' == __name__:
main()
@drincruz
drincruz / csvreader.py
Created December 8, 2011 16:27
I needed a quick and easy way to print specific csv columns for my life on the command line...
#!/usr/bin/env python
import csv
import sys
if 3 > len(sys.argv):
sys.exit("Usage: %s <csv_file> <column_to_print>" % sys.argv[0])
fileparse = csv.reader(open(sys.argv[1]))
for f in fileparse: