Skip to content

Instantly share code, notes, and snippets.

@habina
habina / CareerCup 1.7.py
Last active August 29, 2015 14:02
1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.
def getIndex(x, y, width):
return x + y * width
def printMatrix(matrix, m, n):
"""
Print Matrix row by row
"""
for i in range(m*n):
print(matrix[i], end="\t")
if((i + 1) % n == 0):
@habina
habina / CareerCup 1.6.py
Last active August 29, 2015 14:02
1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?
def getIndex(x, y, width):
"""
Return the index in 1D array
"""
index = x + y * width
return index
def printMatrix(matrix, width):
"""
Print Matrix row by row
@habina
habina / CareerCup 1.5.py
Last active August 29, 2015 14:02
1.5 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.
def stringCompression(originalString):
"""Compress string by using the counts of repeated characters
Input: aabcccccaaa
Output: a2b1c5a3
If the length of string bigger than the compressed string
Then return the original string
"""
if(originalString == ""):
return originalString
@habina
habina / CareerCup 1.4.py
Last active August 29, 2015 14:02
1.4 Write a method to replace all spaces in a string with'%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.)
def replaceSpace(string):
"""Replace all spaces in a string with '%20'"""
"Split string by space, save into a list"
newList = string.split()
"str.join concatenate list at the end"
newString = "%20".join(newList)
return newString
@habina
habina / CareerCup 1.3.py
Last active August 29, 2015 14:02
1.3 Given two strings, write a method to decide if one is a permutation of the other.
def permutationString(firstStr, secondStr):
"Return True if the second string is a permutation of the other"
"""If the length of two strings are different,
directly return false
"""
if(len(firstStr) != len(secondStr)):
return False
"Change all characters to lowercase"
@habina
habina / CareerCup 1.2.c
Last active February 28, 2022 14:58
1.2 Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string.
#include <stdio.h>
void swap(char* a, char* b);
void reverse(char* str);
int main(){
char strArray[] = "1point3acres";
printf("Before Reverse: %s\n", strArray);
reverse(strArray);
printf("After Reverse: %s\n", strArray);
}
@habina
habina / CareerCup 1.1.py
Last active August 29, 2015 14:02
1.1 Implement an algorithm to determine if a string has all unique characters. Whatif you cannot use additional data structures?
def uniqueString(str):
"Return True if the string has all unique characters."
allChar = []
for i in range(256):
allChar.append(True)
for char in str:
if(allChar[ord(char)]):
allChar[ord(char)] = False
else:
return False