This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Check if two circles overlap | |
def is_overlap(circle1, circle2): | |
distance = ((circle1.x - circle2.x) ** 2 + (circle1.y - circle2.y) ** 2) ** 0.5 | |
return distance < circle1.r + circle2.r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/questions/691946/short-and-useful-python-snippets | |
def gcd(a, b): | |
while(b): | |
a, b = b, a % b | |
return a | |
print gcd (54, 24) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python -m SimpleHTTPServer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/questions/691946/short-and-useful-python-snippets | |
# Create 2-dimensional matrix | |
lst_2d = [[0] * 3 for i in xrange(3)] | |
print lst_2d | |
# Assign a value within the matrix | |
lst_2d[0][0] = 5 | |
print lst_2d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/questions/691946/short-and-useful-python-snippets | |
# Allows you to have access to the indexes of the elements within a for loop | |
l = ['a', 'b', 'c', 'd', 'e', 'f'] | |
for (index, value) in enumerate(l): | |
print index, value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/questions/691946/short-and-useful-python-snippets | |
a = [[1, 2, 3], [4, 5, 6]] | |
print zip(*a) | |
# Same with dicts | |
d = {"a":1, "b":2, "c":3} | |
print zip(*d.iteritems()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/questions/691946/short-and-useful-python-snippets | |
lol = [['a', 'b'], ['c'], ['d', 'e', 'f']] | |
for outer in lol: | |
for inner in outer: | |
print inner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/questions/691946/short-and-useful-python-snippets | |
line = "" | |
if not line.strip(): | |
print 'empty' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/questions/691946/short-and-useful-python-snippets | |
L = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] | |
print list(set(L)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stackoverflow.com/questions/691946/short-and-useful-python-snippets | |
S = "1 2 2 3 3 3 4 4 4 4" | |
print [int(x) for x in S.split()] |
OlderNewer