Skip to content

Commit 223c856

Browse files
committed
#Modification 90
1 parent 405cec7 commit 223c856

6 files changed

Lines changed: 271 additions & 171 deletions

File tree

backtracking/N_Queen.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
public class N_Queen {
44
final int N = 4;
55

6-
void printSolution(int board[][]) {
6+
void printSolution(int[][] board) {
77
for (int i = 0; i < N; i++) {
88
for (int j = 0; j < N; j++) {
99
System.out.print(" " + board[i][j] + " ");
@@ -12,7 +12,7 @@ void printSolution(int board[][]) {
1212
}
1313
}
1414

15-
boolean isSafe(int board[][], int row, int col) {
15+
boolean isSafe(int[][] board, int row, int col) {
1616
int i, j;
1717

1818
for (i = 0; i < col; i++) {
@@ -21,9 +21,8 @@ boolean isSafe(int board[][], int row, int col) {
2121
}
2222

2323
for (i = row, j = col; i >= 0 && j >= 0; i--, j--) {
24-
if (board[i][j] == 1) {
24+
if (board[i][j] == 1)
2525
return false;
26-
}
2726
}
2827

2928
for (i = row, j = col; j >= 0 && i < N; i++, j--) {
@@ -34,52 +33,47 @@ boolean isSafe(int board[][], int row, int col) {
3433
return true;
3534
}
3635

37-
boolean solveNQUtil(int board[][], int col) {
36+
boolean solveNQUtil(int[][] board, int col) {
3837
if (col >= N)
3938
return true;
4039

4140
for (int i = 0; i < N; i++) {
4241
if (isSafe(board, i, col)) {
4342
board[i][col] = 1;
4443

45-
if (solveNQUtil(board, col + 1) == true)
44+
if (solveNQUtil(board, col + 1))
4645
return true;
4746

48-
/*
49-
If placing queen in board[i][col] doesn't lead to a solution then rempve queen from board[i][col]
50-
*/
47+
//If placing queen in board[i][col] doesn't lead to a solution then remove queen from board[i][col]
5148
board[i][col] = 0; //"BACKTRACK"
5249
}
5350
}
5451

55-
/*
56-
* If the queen can not be palced in any row in this col, then return false
57-
*/
52+
// If the queen can not be placed in any row in this col, then return false
5853
return false;
5954
}
6055

6156
/*
6257
* This function solves the N-Queen problem using "BACKTRACKING".
6358
* It mainly uses solveNQUtil() to solve the problem.
64-
* It return false if queens cannot placed, otherwise,
59+
* It returns false if queens cannot place, otherwise,
6560
* return true & prints placement of queens in the form of 1's.
66-
* Please note that there may be more than one solutions,
67-
* this functionprints one of the feasible solutions.
61+
* Please note that there may be more than one solution,
62+
* this function prints one of the feasible solutions.
6863
*/
69-
boolean solveNQ() {
70-
int board[][] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
64+
void solveNQ() {
65+
int[][] board = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
7166

72-
if (solveNQUtil(board, 0) == false) {
67+
if (!solveNQUtil(board, 0)) {
7368
System.out.println("Solution does not exist");
74-
return false;
69+
return;
7570
}
7671

7772
printSolution(board);
78-
return true;
7973
}
8074

81-
//Driver program to test above functions
82-
public static void main(String[] args) {
75+
//Driver program to test above functions
76+
public static void main(String[] args) {
8377
N_Queen nq = new N_Queen();
8478
nq.solveNQ();
8579
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package compiler_lab_java;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Count_Operators_in_File {
7+
8+
public static void main(String[] args) throws Exception {
9+
10+
int count = 0;
11+
12+
String[] operators = {
13+
"+", "*", "-", "/", "%", "&",
14+
"&&", "++", "--", "=", "==",
15+
"!=", "^", "||", ".", "?", ":",
16+
";", "<<" , ">>", ">>>", ",", "+=",
17+
"-=", "*=", "/=", "%=", "&=", "^=",
18+
"|=", "<<=", ">>=", ">>>=", "<", ">",
19+
"<=", ">=", "instanceof", "++expr", "--expr",
20+
"+expr", "-expr", "~", "!", "expr++", "expr--"
21+
};
22+
23+
FileReader file = new FileReader("src/compiler_lab_java/data.txt");
24+
Scanner scanner = new Scanner(file);
25+
26+
while(scanner.hasNext()){
27+
String current = scanner.next();
28+
for (String operator : operators) {
29+
if (current.equals(operator))
30+
count++;
31+
}
32+
}
33+
34+
System.out.println();
35+
System.out.println("Number of operators present in given file: " + count);
36+
scanner.close();
37+
}
38+
}

compiler_lab_java/data.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Aman is a default keyword such that break extends .
1+
Aman is a default keyword such that break extends ++ minus 30 + 6 $ % ^ ~ ` 22 % > >>> >> += =+`

stack_and_queue/P15.JAVA

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,80 @@
1-
package stack_and_queue;
2-
import java.util.Stack;
3-
import java.util.*;
4-
5-
// Program Title => Sort a stack using Recursion
6-
public class P15 {
7-
8-
// Recursive Method to insert an item x in sorted way
9-
static void sortedInsert(Stack<Integer> s, int x){
10-
11-
// Base case: Either stack is empty or newly inserted item is greater than top (more than all existing)
12-
if(s.isEmpty() || x > s.peek()){
13-
s.push(x);
14-
return;
15-
}
16-
17-
// if top is greater, remove the top item and recur
18-
int temp = s.pop();
19-
sortedInsert(s, x);
20-
21-
// put back the top item removed earlier
22-
s.push(temp);
23-
}
24-
25-
// function to sort the stack
26-
static void sortStack(Stack<Integer> s){
27-
28-
// checking if stack is not empty
29-
if(!s.isEmpty()){
30-
31-
// Remove the top item
32-
int x = s.pop();
33-
34-
// Sort remainig stack
35-
sortStack(s);
36-
37-
// Push the top item back in sorted stack
38-
sortedInsert(s,x);
39-
}
40-
}
41-
42-
// function to sort the array
43-
static void printStack(Stack<Integer> s){
44-
45-
ListIterator<Integer> lt = s.listIterator();
46-
47-
// forwarding
48-
while(lt.hasNext())
49-
lt.next();
50-
51-
// printing from top to bottom
52-
while (lt.hasPrevious())
53-
System.out.println(lt.previous() + " ");
54-
}
55-
56-
public static void main(String[] args) {
57-
Scanner sc = new Scanner(System.in);
58-
59-
int n = sc.nextInt();
60-
int[] array = new int[n];
61-
62-
Stack<Integer> s = new Stack<>();
63-
for(int i= 0; i < array.length; i++){
64-
System.out.println("Enter your number: ");
65-
int value = sc.nextInt();
66-
s.push(value);
67-
}
68-
69-
while (!(s.isEmpty()))
70-
System.out.println(s.pop());
71-
72-
System.out.println("Stack elements before sorting: ");
73-
printStack(s);
74-
75-
sortStack(s);
76-
77-
System.out.println(" \n\nStack elements after sorting:");
78-
printStack(s);
79-
}
80-
}
1+
//package stack_and_queue;
2+
//import java.util.Stack;
3+
//import java.util.*;
4+
//
5+
//// Program Title => Sort a stack using Recursion
6+
//public class P15 {
7+
//
8+
// // Recursive Method to insert an item x in sorted way
9+
// static void sortedInsert(Stack<Integer> s, int x){
10+
//
11+
// // Base case: Either stack is empty or newly inserted item is greater than top (more than all existing)
12+
// if(s.isEmpty() || x > s.peek()){
13+
// s.push(x);
14+
// return;
15+
// }
16+
//
17+
// // if top is greater, remove the top item and recur
18+
// int temp = s.pop();
19+
// sortedInsert(s, x);
20+
//
21+
// // put back the top item removed earlier
22+
// s.push(temp);
23+
// }
24+
//
25+
// // function to sort the stack
26+
// static void sortStack(Stack<Integer> s){
27+
//
28+
// // checking if stack is not empty
29+
// if(!s.isEmpty()){
30+
//
31+
// // Remove the top item
32+
// int x = s.pop();
33+
//
34+
// // Sort remainig stack
35+
// sortStack(s);
36+
//
37+
// // Push the top item back in sorted stack
38+
// sortedInsert(s,x);
39+
// }
40+
// }
41+
//
42+
// // function to sort the array
43+
// static void printStack(Stack<Integer> s){
44+
//
45+
// ListIterator<Integer> lt = s.listIterator();
46+
//
47+
// // forwarding
48+
// while(lt.hasNext())
49+
// lt.next();
50+
//
51+
// // printing from top to bottom
52+
// while (lt.hasPrevious())
53+
// System.out.println(lt.previous() + " ");
54+
// }
55+
//
56+
// public static void main(String[] args) {
57+
// Scanner sc = new Scanner(System.in);
58+
//
59+
// int n = sc.nextInt();
60+
// int[] array = new int[n];
61+
//
62+
// Stack<Integer> s = new Stack<>();
63+
// for(int i= 0; i < array.length; i++){
64+
// System.out.println("Enter your number: ");
65+
// int value = sc.nextInt();
66+
// s.push(value);
67+
// }
68+
//
69+
// while (!(s.isEmpty()))
70+
// System.out.println(s.pop());
71+
//
72+
// System.out.println("Stack elements before sorting: ");
73+
// printStack(s);
74+
//
75+
// sortStack(s);
76+
//
77+
// System.out.println(" \n\nStack elements after sorting:");
78+
// printStack(s);
79+
// }
80+
//}

0 commit comments

Comments
 (0)