Skip to content

Instantly share code, notes, and snippets.

@OzTamir
OzTamir / bot.py
Created December 31, 2013 14:12
sudo's winning code, cleaned up and documented properly - now in a public gist. Written by Oz Tamir, Tomer Schlesinger and Eden Rozen.
def DoTurn(pw):
Overtake(pw)
def Attack(pw, source, target, ships):
'''
A custom controller for pw built-in IssueOrder() method
'''
if source.NumShips() < ships:
send = source.NumShips()
else:
for cnt in contours:
approx = cv2.approxPolyDP(cnt,cv2.arcLength(cnt,True)*0.02,True)
if len(approx)==4:
cv2.drawContours(im,[approx],0,(0,0,255),2)
x,y,w,h = cv2.boundingRect(cnt)
@OzTamir
OzTamir / dict_parser.py
Last active August 29, 2015 13:56
Parse socket-recived dictionaries in the following format = {Key:Value,Key:Value} (sorry for lack of standards)
#!/usr/bin/env python
import socket
DATA = {}
def parser(*args):
'''Parse key-value pairs recived from client'''
new_data = {}
dict_data = args[0]
for pair in dict_data:
@OzTamir
OzTamir / git_sync.py
Created February 10, 2014 13:49
Little Python script to mimic GitHub's GUI 'Sync' button - The code doesn't work, but it does demonstrate some nice concepts
import subprocess
#TES
# -- Global Variables Declarations -- #
USER = 'Oz Tamir'
EMAIL = 'TheOzTamir@gmail.com'
#REPO = 'https://' + USER + '@github.com/OzTamir/git-fix.git'
# Changed files
modified = []
# Deleted files to remove from remote
deleted = []
@OzTamir
OzTamir / Title_Comments.py
Last active August 29, 2015 13:56
Make code comments prettier (Title format) - USE WITH CAUTION!
#! /usr/bin/env python
import sys
def commentsTitler(filename):
'''WARNING: This will overwrite the file content, make sure you have a backup'''
# Get The Appropriate Comment Prefix For The File Type
ext = {'py' : '#', 'cs' : '//', 'cpp' : '//', 'h' : '//', 'asm' : ';', 'gcode' : ';', 'java' : '//', 'js' : '//'}
COMMENTS_PREFIX = ext[filename[filename.find('.') + 1:].lower()]
# Get All The Lines In The File
with open(filename, 'r') as file:
@OzTamir
OzTamir / Hanoi.py
Created February 16, 2014 18:09
Towers of Hanoi in Python (using recursion)
def Hanoi(ndisks, start_rod=1, end_rod=3):
if ndisks:
Hanoi(ndisks - 1, start_rod, 6 - start_rod - end_rod)
print('Move disk {DISK} from {START} to {END}'.format(DISK = ndisks, START = start_rod, END = end_rod))
Hanoi(ndisks - 1, 6 - start_rod - end_rod, end_rod)
if __name__ == '__main__':
print('Towers Of Hanoi - Recursion!')
disks = input('Enter number of disks >>>')
Hanoi(int(disks))
@OzTamir
OzTamir / Sort.py
Created February 17, 2014 12:14
All sorts of sorts, sort of...
from heapq import merge
import random
def merge_sort(m):
if len(m) <= 1:
return m
middle = len(m) / 2
left = m[:middle]
right = m[middle:]
@OzTamir
OzTamir / Question_4.py
Last active August 29, 2015 13:56
Find the number of options to paint m (or more) pixels in a pixel row with n pixels
def number_of_paint_options(n, m):
def find_num_options(n, m):
'''Find the number of options to paint m (or more) pixels in a pixel row with n pixels'''
return 1 + sum((k - m + 1) * find_num_options(n - k - 1, m) for k in range(m, n + 1))
if m > n or m == 0:
return 0
elif n == m:
return 1
return find_num_options(n, m)
@OzTamir
OzTamir / fizzbuzz.py
Last active August 29, 2015 13:56
One line 'FizzBuzz' - Without a single if!
# 3 - Fizz
# 5 - Buzz
# 15 - FizzBuzz
# With empty strings
a = ['Fizz' * (not (i % 3)) + 'Buzz' * (not (i % 5)) for i in xrange(100)]
# Without empty strings
b = filter(None, (['Fizz' * (not (i % 3)) + 'Buzz' * (not (i % 5)) for i in xrange(100)]))
@OzTamir
OzTamir / spacer.py
Last active August 29, 2015 13:57
Give Me Some Space - Find unconventional criminals
#! /usr/bin/env python
import sys
import re
def space_standert(filename, copy=True, no_regex=True):
'''
filename, [copy (True creates a copy, False overwrites (True is recomanded!)), no_regex (Can we risk ruined regex?)]
Format the file 'filename' to match the spacing standert (commas and equality marks)
'''
# Regex for these cases: X(OP)Y || X (OP)Y || X(OP) Y (Works for !<>*+-= operators as well as '(OP)=' operators)