My programing World
A collection of programs and interview questions
Thursday, April 5, 2018
Saturday, June 10, 2017
Number of occurance
def count_substring(string, sub_string):
count=0
sublength = len(sub_string)
for i in range(len(string)):
try:
st = 0
st = string.index(sub_string,i,sublength+i)
if(st>0):
#print("Caught",i,sublength+i)
count = count+1
except:
#print("Error")
continue
return count
count=0
sublength = len(sub_string)
for i in range(len(string)):
try:
st = 0
st = string.index(sub_string,i,sublength+i)
if(st>0):
#print("Caught",i,sublength+i)
count = count+1
except:
#print("Error")
continue
return count
Tuesday, May 30, 2017
Sqoop : example to pull data from mysql
Example1:
sqoop import --connect jdbc:mysql:<localhost>/<databasename> --username=<username> --password-file <password file location> -table <tablename> -m 1 --compression-codec=snappy --as-avrodatafile --warehouse-dir=<hdfs location for creating datafiles>
Example 2:
sqoop import --connect jdbc:mysql:<localhost>/<databasename> --username=<username> --password=<password> -table <tablename> -m 1 --compression-codec=snappy --as-avrodatafile --warehouse-dir=<hdfs location for creating datafiles>
We can use this flag to overwrite the data pulled
--hive-overwrite flag
Cloudera hadoop scoop import
sqoop import --connect jdbc:mysql://quickstart:3306/retail_db --username=retail_dba --password=cloudera -table categories -m 1 --compression-codec=snappy --as-avrodatafile --warehouse-dir=/user/hive/warehouse
Monday, May 29, 2017
Graph : Shortest path
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; import java.util.Stack; import java.util.StringTokenizer; public class Solution { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader in=new FastReader(); int t1=in.nextInt(); for(int gj=0;gj<t1;gj++){ int n=in.nextInt(); int m=in.nextInt(); long w[][]=new long [n+1][n+1]; for (long[] row: w) Arrays.fill(row, 1000000l); for(int i=0;i<m;i++){ int x=in.nextInt(),y=in.nextInt(); long cmp=in.nextLong(); if(w[x][y]>cmp){ w[x][y]=cmp; w[y][x]=cmp; }} Stack <Integer> t=new Stack<Integer>(); int src=in.nextInt(); for(int i=1;i<=n;i++){ if(i!=src){t.push(i);}} Stack <Integer> p=new Stack<Integer>(); p.push(src); w[src][src]=0; while(!t.isEmpty()){int min=989997979,loc=-1; for(int i=0;i<t.size();i++){ w[src][t.elementAt(i)]=Math.min(w[src][t.elementAt(i)],w[src][p.peek()]+w[p.peek()][t.elementAt(i)]); if(w[src][t.elementAt(i)]<=min){ min=(int) w[src][t.elementAt(i)];loc=i;} } p.push(t.elementAt(loc));t.removeElementAt(loc);} for(int i=1;i<=n;i++){ if(i!=src && w[src][i]!=1000000l){System.out.print(w[src][i]+" ");} else if(i!=src){System.out.print("-1"+" ");} }System.out.println(); } }}
graph : shortest path2
#!/bin/python
import heapq
graph = {}
def shortestPath(start, end):
queue,seen = [(0, start, [])], set()
while True:
(cost, v, path) = heapq.heappop(queue)
if v not in seen:
path = path + [v]
seen.add(v)
if v == end:
return cost, path
for (next, c) in graph[v].iteritems():
heapq.heappush(queue, (cost + c, next, path))
t = int(raw_input().strip())
if t <1 or t>10:
exit(0)
for a0 in xrange(t):
n,m = raw_input().strip().split(' ')
n,m = [int(n),int(m)]
if n <2 or n>3000:
exit(0)
if m <1 or m>((n*(n-1))/2):
exit(0)
for a1 in xrange(m):
x,y,r = raw_input().strip().split(' ')
x,y,r = [int(x),int(y),float(r)]
if x <1 or x>n:
exit(0)
if y <1 or y>n:
exit(0)
if r <1 or r>10**5:
exit(0)
# print x,y,r
if x not in graph.keys():
graph[x] = {}
graph[x][y] = r
if y not in graph.keys():
graph[y] = {}
graph[y][x] = r
else:
graph[y][x] = r
# print "if"
print graph
else:
if y in graph[x] and graph[x][y] > r:
graph[x][y] = r
if y not in graph.keys():
graph[y] = {}
graph[y][x] = r
else:
graph[y][x] = r
else:
graph[x][y] = r
if y not in graph.keys():
graph[y] = {}
graph[y][x] = r
else:
graph[y][x] = r
# print "else"
print graph
s = int(raw_input().strip())
if s <1 or s>n:
exit(0)
for i in graph.keys():
if s !=i:
cost, path = shortestPath(s, i)
print int(cost),
#cost, path = shortestPath( x, y)
#print cost, path
import heapq
graph = {}
def shortestPath(start, end):
queue,seen = [(0, start, [])], set()
while True:
(cost, v, path) = heapq.heappop(queue)
if v not in seen:
path = path + [v]
seen.add(v)
if v == end:
return cost, path
for (next, c) in graph[v].iteritems():
heapq.heappush(queue, (cost + c, next, path))
t = int(raw_input().strip())
if t <1 or t>10:
exit(0)
for a0 in xrange(t):
n,m = raw_input().strip().split(' ')
n,m = [int(n),int(m)]
if n <2 or n>3000:
exit(0)
if m <1 or m>((n*(n-1))/2):
exit(0)
for a1 in xrange(m):
x,y,r = raw_input().strip().split(' ')
x,y,r = [int(x),int(y),float(r)]
if x <1 or x>n:
exit(0)
if y <1 or y>n:
exit(0)
if r <1 or r>10**5:
exit(0)
# print x,y,r
if x not in graph.keys():
graph[x] = {}
graph[x][y] = r
if y not in graph.keys():
graph[y] = {}
graph[y][x] = r
else:
graph[y][x] = r
# print "if"
print graph
else:
if y in graph[x] and graph[x][y] > r:
graph[x][y] = r
if y not in graph.keys():
graph[y] = {}
graph[y][x] = r
else:
graph[y][x] = r
else:
graph[x][y] = r
if y not in graph.keys():
graph[y] = {}
graph[y][x] = r
else:
graph[y][x] = r
# print "else"
print graph
s = int(raw_input().strip())
if s <1 or s>n:
exit(0)
for i in graph.keys():
if s !=i:
cost, path = shortestPath(s, i)
print int(cost),
#cost, path = shortestPath( x, y)
#print cost, path
graph shortest path
#!/bin/python
import heapq
graph = {}
def shortestPath(start, end):
queue,seen = [(0, start, [])], set()
while True:
(cost, v, path) = heapq.heappop(queue)
if v not in seen:
path = path + [v]
seen.add(v)
if v == end:
return cost, path
for (next, c) in graph[v].iteritems():
heapq.heappush(queue, (cost + c, next, path))
t = int(raw_input().strip())
for a0 in xrange(t):
n,m = raw_input().strip().split(' ')
n,m = [int(n),int(m)]
for a1 in xrange(m):
x,y,r = raw_input().strip().split(' ')
x,y,r = [int(x),int(y),int(r)]
# print x,y,r
if x not in graph.keys():
graph[x] = {}
graph[x][y] = r
if y not in graph.keys():
graph[y] = {}
graph[y][x] = r
else:
graph[y][x] = r
# print "if"
# print graph
else:
graph[x][y] = r
if y not in graph.keys():
graph[y] = {}
graph[y][x] = r
else:
graph[y][x] = r
# print "else"
# print graph
s = int(raw_input().strip())
print graph
print s
cost, path = shortestPath(s, 3)
print cost, path
#cost, path = shortestPath( x, y)
#print cost, path
Subscribe to:
Posts (Atom)