Skip to content

Instantly share code, notes, and snippets.

@TApicella
TApicella / RosettaCode- Knapsack problem.py
Created May 11, 2017 17:19
RosettaCode- Knapsack problem created by tapicella - https://repl.it/HtMj/28
'''
http://rosettacode.org/wiki/Knapsack_problem/0-1
Knapsack problem/0-1
A tourist wants to make a good trip at the weekend with his friends.
They will go to the mountains to see the wonders of nature, so he needs to pack well for the trip.
He has a good knapsack for carrying things, but knows that he can carry a maximum of only 4kg in it, and it will have to last the whole day.
@TApicella
TApicella / RosettaCode- Hash Join.py
Created May 11, 2017 15:37
RosettaCode- Hash Join created by tapicella - https://repl.it/HtIS/31
'''
http://rosettacode.org/wiki/Hash_join
Implement the "hash join" algorithm, and demonstrate that it passes the test-case listed below.
You should represent the tables as data structures that feel natural in your programming language.
Guidance
The "hash join" algorithm consists of two steps:
Hash phase: Create a multimap from one of the two tables, mapping from each join column value to all the rows that contain it.
@TApicella
TApicella / Daily Programmer 5-11-2017: Shorthand Range.py
Created May 11, 2017 13:58
Daily Programmer 5-11-2017: Shorthand Range created by tapicella - https://repl.it/HsuW/15
'''
Take the following inputs in shorthand range, and translate them to longhand range.
1,6,4,9,1,6 => [1, 6, 14, 19, 21, 26]
2,9..9,9 => [2, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 29]
5,7,0,1,9,4 => [5, 7, 10, 11, 19, 24]
'''
@TApicella
TApicella / RosettaCode JaroDistance.py
Created May 9, 2017 18:29
RosettaCode JaroDistance created by tapicella - https://repl.it/Hous/36
'''
http://rosettacode.org/wiki/Jaro_distance
d = 0 if no matching characters
otherwise d = (1/3) * (m/len(s1) + m/len(s2) + (m-(transpose/2))/m )
characters match if distance >= (max(len(s1), len(s2))/2)-1
if distance != 0, counts as a transpose
'''
def JaroDistance(s1, s2):
@TApicella
TApicella / Daily Programmer: 5-9-2017.py
Last active May 9, 2017 15:27
Daily Programmer: 5-9-2017 created by tapicella - https://repl.it/Hoet/27
'''
Following up on yesterday... Given a sorted list of distinct integers, write a function that returns whether there *any subset combinations* in the list that add up to 0. For example, you would return true if `[-8, 3, 5, 11]` are in the list, because -8 + 3 + 5 = 0. Also return true if 0 appears in the list.
'''
from itertools import combinations
def findSubset(mylist):
if len(mylist)==0: return False
elif len(mylist)==1: return(mylist[0]==0)
else:
@TApicella
TApicella / RosettaCode_validateISIN.py
Created May 8, 2017 21:11
RosettaCode_validateISIN created by tapicella - https://repl.it/HnEm/27
'''
An International Securities Identification Number (ISIN) is a unique international identifier for a financial security such as a stock or bond.
Replace letters with digits, by converting each character from base 36 to base 10, e.g. AU0000XVGZA3 →1030000033311635103.
Perform the Luhn test on this base-10 number.
US0378331005:TRUE
US0373831005:FALSE
U50378331005:FALSE
AU0000XVGZA3:TRUE
@TApicella
TApicella / RosettaCode Rep-string.py
Created May 8, 2017 20:09
RosettaCode Rep-string created by tapicella - https://repl.it/Hmz8/30
'''
rosettacode.org/wiki/Rep-string
Given a series of ones and zeroes in a string, define a repeated string or rep-string as a string which is created by repeating a substring of the first N characters of the string truncated on the right to the length of the input string, and in which the substring appears repeated at least twice in the original.
For example, the string 10011001100 is a rep-string as the leftmost four characters of 1001 are repeated three times and truncated on the right to give the original string.
Note that the requirement for having the repeat occur two or more times means that the repeating unit is never longer than half the length of the input string.
10011 10011
@TApicella
TApicella / Hmhz-18.py
Last active May 8, 2017 18:12
RosettaCode: Sum to 100
'''
rosettacode.org/wiki/Sum_to_100
brute force solution
https://repl.it/Hmhz/20
'''
# http://stackoverflow.com/questions/34559663/convert-decimal-to-ternarybase3-in-python
def decimalToTernary (n):
if n == 0:
return '0'
@TApicella
TApicella / HmSh-10.py
Last active May 8, 2017 15:09
Philly Daily Programmer: 5-8-2016
'''
Given a sorted list of distinct integers, write a function that returns whether there are two integers in the list that add up to 0. For example, you would return true if both -14435 and 14435 are in the list, because -14435 + 14435 = 0. Also return true if 0 appears in the list.
[1, 2, 3] -> false
[-5, -3, -1, 2, 4, 6] -> false
https://repl.it/HmSh/10
'''
test1 = [] #False
@TApicella
TApicella / watermark.py
Last active July 23, 2016 01:45
A script that adds a watermark to the bottom right of each picture in a directory of pictures
from PIL import Image, ImageDraw
import os, math
#SETTINGS
WIDTH_RATIO = 3 #Ratio of image min width to watermark width
MIN_WIDTH = 200 #Minimum width of watermark. In my watermark, it is 1/3 of the normal width
MIN_HEIGHT = 40 #Minimum height of watermark. In my watermark ,it is 1/3 of the normal height
WATERMARK_LOCATION = "watermark.png"
#Most helpful source: http://stackoverflow.com/questions/13662184/python-pil-lighten-transparent-image-and-paste-to-another-one