Skip to content

Instantly share code, notes, and snippets.

@dudepare
dudepare / rotation.py
Created August 26, 2016 05:14
String Rotation: Assumeyou have a method isSubstringwhich checks ifoneword isa substring of another. Given two strings, sl and s2, write code to check if s2 is a rotation of sl using only one call to isSubstring (e.g., "waterbottle" is a rotation of"erbottlewat").
View rotation.py
class Rotation:
@staticmethod
def is_substring(s1, s2):
"""Checks if s2 is a substring of s1
Args:
s1 (string) string input
s2 (string) rotation of s1
@dudepare
dudepare / zero_matrix.py
Created August 19, 2016 05:19
Zero Matrix: Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.
View zero_matrix.py
class Matrix:
@staticmethod
def zero(M, m, n):
"""Zeroes out the whole row and column if one elements is found to be zero.
Args:
M (array) of m rows and n columns
m (int) of rows
n (int) of columns
@dudepare
dudepare / matrix_rotate.py
Created August 17, 2016 11:54
Rotates an N x N matrix M 90 degrees clockwise.
View matrix_rotate.py
class Matrix:
@staticmethod
def rotate(m, n):
"""Rotates a matrix m of n x n size"""
# reverse each row of the matrix
for i in range(n):
m[i] = m[i][::-1]
@dudepare
dudepare / sample_crypto_aes.js
Created August 10, 2016 06:51
This is a sample of AES encryption + decryption using CryptoJS. This is a bit different from the examples out there because this one uses a COMPANY header + payload. There's a little bit more processing going on. I also used a CDN for CryptoJS. I hope someone finds this useful.
View sample_crypto_aes.js
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/core-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/sha256.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/components/enc-base64.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
<script>
function toWordArray(str){
return CryptoJS.enc.Utf8.parse(str);
}
function toString(words){
@dudepare
dudepare / compressed_string.py
Created August 10, 2016 04:07
String Compression: Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase an…
View compressed_string.py
"""
String Compression: Implement a method to perform basic string
compression using the counts of repeated characters. For example,
the string aabcccccaaa would become a2blc5a3. If the "compressed"
string would not become smaller than the original string, your
method should return the original string. You can assume the
string has only uppercase and lowercase letters (a -z).
"""
def compress(inputstr):
if len(inputstr) == 0:
@dudepare
dudepare / hired.py
Created April 20, 2016 06:39
This solution is from a recent ad by @Hired_HQ
View hired.py
def main(msg):
convert = map(chr, msg)
print("".join(list(convert)))
if __name__ == '__main__':
msg = [76, 101, 116, 116, 104, 101, 98, 101, 115, 116, 115, 116, 97, 114, 116,117, 112, 115, 97, 112, 112, 108, 121, 116, 111, 121, 111, 117, 0]
main(msg)
View love_rectangles.py
import unittest
def find_overlap(r1, r2, axis, result):
if axis is 'x':
start = 'x'
distance = 'width'
elif axis is 'y':
start = 'y'
distance = 'height'
p1 = r1[start]
View even_elements.py
import unittest
def even_elements(l):
"""
Given a list, this function returns a new list
with the with items found in the even indexes only
"""
if len(l) == 0:
return l
View find_mode.py
import unittest
def find_mode(l):
if len(l) == 0:
return "ZeroLength"
l.sort()
sums = {}
View next_bigger.py
import unittest
def next_bigger(l):
"""
This function returns a modified list
by appending a new element that is
bigger than the max element of the list
l = [], returns l[0]