rahulroshan96
BAN USER
from threading import Lock, Thread
zero_lock = Lock()
even_lock = Lock()
odd_lock = Lock()
# 010203040506
def zero_print():
odd = True
for n in range(6):
zero_lock.acquire()
print(0)
if odd:
odd = False
odd_lock.release()
else:
odd = True
even_lock.release()
def print_odd():
n = 1
while(n<=5):
odd_lock.acquire()
print(n)
n=n+2
zero_lock.release()
def print_even():
n = 2
while(n<=6):
even_lock.acquire()
print(n)
n=n+2
zero_lock.release()
t1 = Thread(target=zero_print)
t2 = Thread(target=print_odd)
t3 = Thread(target=print_even)
odd_lock.acquire()
even_lock.acquire()
threads = [t1,t2,t3]
for t in threads:
t.start()
for t in threads:
t.join()
package Basics;
class Node {
int data;
Node next;
Node(int data, Node next){
this.data = data;
this.next = next;
}
}
class Test {
public static void main(String args[]) {
Test tt = new Test();
int i = 2;
Node t = new Node(1,null);
Node x = t;
while(i<6){
x.next = new Node(i,null);
x = x.next;
i++;
}
tt.seperator(t);
}
void printList(Node n){
while(n!=null){
System.out.print(n.data+" ");
n=n.next;
}
System.out.println();
}
void seperator(Node root){
boolean flag =false;
Node temp1=null,t1=null;
Node temp2=null,t2=null;
while(root!=null){
if(flag){
if(t2!=null){
t2.next = new Node(root.data,null);
t2 = t2.next;
}else{
temp2 = new Node(root.data,null);
t2 = temp2;
}
root = root.next;
flag = false;
}else{
if(t1!=null){
t1.next = new Node(root.data,null);
t1 = t1.next;
}else{
temp1 = new Node(root.data,null);
t1 = temp1;
}
root = root.next;
flag=true;
}
}
printList(temp1);
printList(temp2);
merger(temp1,temp2);
}
void merger(Node t1, Node t2){
Node root = null,head=null;
while(t1!=null && t2!=null){
if(t1.data<t2.data){
if(root!=null){
root.next = new Node(t1.data, null);
root = root.next;
}else{
root = new Node(t1.data,null);
head = root;
}
t1 = t1.next;
}else{
if(root!=null){
root.next = new Node(t2.data, null);
root = root.next;
}else{
root = new Node(t2.data,null);
head = root;
}
t2 = t2.next;
}
}
if(t1==null){
while(t2!=null){
root.next = new Node(t2.data, null);
root = root.next;
t2 = t2.next;
}
}
if(t2==null){
while(t1!=null){
root.next = new Node(t1.data, null);
root = root.next;
t1 = t1.next;
}
}
printList(head);
}
}
package Careercup;
import javafx.util.Pair;
import java.util.Queue;
import java.util.LinkedList;
class Test {
public static void main(String args[]) {
Test t = new Test();
int a[][] = {
{1,0,0,0,0},
{0,1,0,0,1},
{0,1,0,0,0},
{0,1,0,0,0},
{0,0,0,0,0}
};
Pair<Integer,Integer> result = t.getShop(a,3,4);
System.out.println(result.getKey()+" "+result.getValue());
}
boolean isSafe(int x, int y, int a[][]){
return (x>=0 && x<a.length && y>=0 &&y<a[0].length);
}
// BFS
Pair<Integer,Integer> getShop(int a[][], int x, int y){
if(a[x][y] == 1) return new Pair(x,y);
Queue<Pair<Integer, Integer>> q = new LinkedList<>();
q.add(new Pair(x,y));
int l[] = {0,1,1,1,0,-1,-1,-1};
int r[] = {1,1,0,-1,-1,-1,0,1};
while(!q.isEmpty()){
Pair<Integer,Integer> temp = q.peek();
q.remove();
int xx = temp.getKey();
int yy = temp.getValue();
for(int i=0;i<l.length;i++){
if(isSafe(xx+l[i],yy+r[i],a)){
if(a[xx+l[i]][yy+r[i]]==1){
return new Pair<>(xx+l[i],yy+r[i]);
}else{
q.add(new Pair<>(xx+l[i],yy+r[i]));
}
}
}
}
return null;
}
}
void removeDuplicate(int lines[]){
int i=0;
int j=0;
HashSet<Integer> hs = new HashSet<>();
while(i<lines.length){
if(hs.contains(lines[i])){
i++;
}else{
lines[j] = lines[i];
hs.add(lines[i]);
i++;
j++;
}
}
for(int x=0;x<j;x++){
System.out.print(lines[x]+" ");
}
}
void printInColors(String colors[], String str){
int x = 0;
int size = colors.length;
for(int i=0;i<str.length();i++){
if(str.charAt(i)!=' '){
System.out.println(str.charAt(i)+" is "+colors[x%size]);
x++;
}
}
}
void printCombinations(StringBuilder s,int i){
if(i==s.length()){
System.out.println(s);
return;
}
if(s.charAt(i)=='?'){
s.replace(i,i+1,"0");
printCombinations(s,i+1);
s.replace(i,i+1,"1");
printCombinations(s,i+1);
s.replace(i,i+1,"?");
}else{
printCombinations(s,i+1);
}
}
e.g.
int files = {
{1,2,3,4},
{1,5,3},
{1,9,0}
};
Where files[0] is a file, and files[0] has 4 lines as 1,2,3,4.
ArrayList<Integer> getUnique(int files[][]){
ArrayList<Integer> result = new ArrayList<>();
HashMap<Integer,Integer> hm = new HashMap<>();
for(int i=0;i<files.length;i++){
for(int j=0;j<files[i].length;j++){
if(hm.containsKey(files[i][j])){
hm.put(files[i][j],hm.get(files[i][j])+1);
}else{
hm.put(files[i][j],1);
}
}
}
for(Map.Entry<Integer,Integer> e:hm.entrySet()){
if(e.getValue()==1){
result.add(e.getKey());
}
}
return result;
}
RepSilvia Devidson, maintenence engineer at Live Nation
Silvia Devidson working as a mechanical piping estimator performs process piping takeoffs from engineered drawings at various levels of design ...
- rahulroshan96 May 07, 2021