arunprasath9586
BAN USERMy solution for printing it both in clockwise and anti-clockwise spiral
def spiral(x, y):
"""
printing the matrix clockwise
"""
rows = [0, x]
columns = [0, y]
def _range(*args):
if args[0] >= args[1]:
raise StopIteration
return range(*args)
while True:
i = rows[0]
for j in _range(*columns):
yield (i, j)
j = columns[1]-1
rows[0] += 1
for i in _range(*rows):
yield (i, j)
columns[1] -= 1
i = rows[1]-1
for j in reversed(_range(*columns)):
yield (i, j)
j = columns[0]
rows[1] -= 1
for i in reversed(_range(*rows)):
yield (i, j)
columns[0] += 1
def anti_spiral(x, y):
"""
printing the matrix anti clockwise
"""
rows = [0, x]
columns = [0, y]
def _range(*args):
if args[0] >= args[1]:
raise StopIteration
return range(*args)
while True:
j = columns[0]
for i in _range(*rows):
yield (i, j)
columns[0] += 1
for j in _range(*columns):
yield (i, j)
rows[1] -= 1
for i in reversed(_range(*rows)):
yield (i, j)
columns[1] -= 1
for j in reversed(_range(*columns)):
yield (i, j)
rows[0] += 1
def iter_spiral(arr):
x = len(arr)
y = len(arr[0])
for i, j in spiral(x, y):
yield arr[i][j]
print
for i, j in anti_spiral(x, y):
yield arr[i][j]
if __name__ == '__main__':
m = [[3, 4, 2, 7], [9, 2, 5, 1], [14, 23, 44, 22], [5, 2, 8, 1]]
for n in iter_spiral(m):
print n,
My solution in Python.
Time complexity: O(n)
def solution(s):
# zm is a list of zeros to keep track of continuity
zm = [0 for _ in range(len(s))]
for i, c in enumerate(s):
if (i+1) < len(s):
if c == s[i+1]:
zm[i], zm[i+1] = 1, 1
seq = set()
seqs = []
for i, flag in enumerate(zm):
if flag == 1 and (i+1) < len(zm):
if (i+1) - i == 1:
seq.add(i)
if zm[i + 1] == 1:
seq.add(i+1)
else:
if len(tuple(seq)) > 0:
seqs.append(sorted(tuple(seq)))
seq = set()
lens = []
for seq in seqs:
lens.append(len(seq))
max_len = max(lens)
max_len_ind = lens.index(max_len)
start = seqs[max_len_ind][0]
end = seqs[max_len_ind][-1]
print s[start:end+1]
if __name__ == '__main__':
string = 'ehersdgfffghjhedeetyyyyy'
solution(string)
def find_lca(self, node1, node2):
root_node = self.root
def lca(n1, n2, root):
"""
Lowest Common Ancestor
"""
if n1 > n2:
n1, n2 = n2, n1
if n1 <= root.value <= n2:
return root
if root.value <= n1 and root.value <= n2:
return lca(n1, n2, root.right)
if root.value >= n1 and root.value >= n2:
return lca(n1, n2, root.left)
raise Exception("Elements not in tree")
return lca(node1, node2, root_node).__dict__['value']
one liner in Python
arr = "1,234,34,54"
print arr.replace(',','')
C++ Strings
#include <iostream>
using namespace std;
int main() {
string s = "1,204,342,544";
s.erase(remove(s.begin(), s.end(), ',' ), s.end());
cout << s;
}
cleaned version of solution in C++
#include <iostream>
using namespace std;
void maxProfit(int prices[], int len) {
int at_hand = 0;
int profit = 0;
cout << len << endl;
for (int i=0; i <= len-1; i++) {
if (at_hand == 0 && prices[i] < prices[i+1]) {
at_hand = 1;
cout << "Buy on day " << i+1 << endl;
}
else {
if (at_hand != 0 && prices[i] > prices[i+1]) {
profit += prices[i] - prices[i+1];
cout << "Sell on day " << i+1 << endl;
at_hand = 0;
}
}
}
cout << "Net profit = " << profit << endl;
}
int main() {
int stock_prices[] = {5,4,7,4,3,6,8,4,3,6};
int len = sizeof(stock_prices)/sizeof(int);
maxProfit(stock_prices, len);
return 0;
}
We can implement it using Trie and a queue. Will be posting the Python and Java implementation shortly.
- arunprasath9586 August 02, 2014