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
''' | |
Coding Problem Statement | |
Given a list of file paths (e.g. as output by grep), pretty print all the paths in a tree-like format. | |
Write your code as the prettyPrint function (do not assume the input is sorted): | |
String[] prettyPrint(String[] files); | |
Example Input: | |
/a/b/c/d | |
/b/b/f |
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
class Trie(object): | |
def __init__(self): | |
self.data = [None for _ in range(26)] | |
self.count = 0 | |
self.is_end = False | |
def __repr__(self): | |
node_data = (lambda x : (1, x.count, x.is_end) if x is not None else 0) | |
return str([node_data(x) for x in self.data]) |
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
def pretty(maze): | |
for i in maze: | |
print(*i) | |
print() | |
def trace_ways(maze, i, j, ex, ey, visited): | |
maze[i][j] = '+' | |
visited[i][j] = True | |
if i == ex and j == ey: |
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
d = [] | |
def isprime(n): | |
if n<=1: return False | |
if n<=3: return True | |
if n%2 == 0 or n%3 == 0: return False | |
i = 5 | |
while i*i <= n: | |
if n % i ==0 or n%(i+2) == 0: return False | |
i+=1 |
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
/* | |
Following is the algorithm for finding the next greater number. | |
I) Traverse the given number from rightmost digit, keep traversing till you find a digit which is smaller than the previously traversed digit. For example, if the input number is “53(4)976”, we stop at 4 because 4 is smaller than next digit 9. If we do not find such a digit, then output is “Not Possible”. | |
II) Now search the right side of above found digit ‘d’ for the smallest digit greater than ‘d’. For “534976″, the right side of 4 contains “97(6)”. The smallest digit greater than 4 is 6. | |
III) Swap the above found two digits, we get 536974 in above example. | |
IV) Now sort all digits from position next to ‘d’ to the end of number. The number that we get after sorting is the output. For above example, we sort digits in 536(974). We get “536479” which is the next greater number for input 534976. | |
*/ |
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
# sort dates using Objects, and also by lists | |
# first year wise, then month and finally day wise | |
class Date: | |
def __init__(self, d, m, y): | |
self.d = d; | |
self.m = m; | |
self.y = y; | |
def __repr__(self): |
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
N = int(input()) | |
d = {} | |
for _ in range(N): | |
cmd = input().strip().split() | |
if cmd[0] == 'add': | |
s = cmd[1] | |
for i in range(len(s)): | |
prefix = s[:i+1] | |
d[prefix] = d.get(prefix, 0) + 1 | |
elif cmd[0] == 'find': |
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
#include <iostream> | |
#include <string.h> | |
using namespace std; | |
int compare(const void *a, const void *b){ | |
return (*(int *)a- *(int*)b); | |
} | |
int str_compare(const void *a, const void *b) { |
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
#include <iostream> | |
using namespace std; | |
void merge_em_all(int *a, int *b, int *c, int x, int y, int z) { | |
int i=0, j=0, k= 0; | |
for(i=0, j=0; i<x && j<y; ) { | |
if(a[i]< b[j]){ |
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
a= [ | |
[1,2,3,4], | |
[5,6,7,8], | |
[9,10,11,12], | |
[13,14,15,16] | |
] | |
for _ in a: | |
print(*_) | |
print() | |
ab= [x[::-1] for x in zip(*a)] |
NewerOlder