Skip to content

Commit a7691da

Browse files
author
Dassi Orleando
committed
Merge branch 'master' of https://github.com/eugenp/tutorials into local
2 parents 797f96c + 2a127b3 commit a7691da

48 files changed

Lines changed: 1398 additions & 167 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ private void printBoard() {
4040
}
4141

4242
private boolean solve(int[][] board) {
43-
for (int r = BOARD_START_INDEX; r < BOARD_SIZE; r++) {
44-
for (int c = BOARD_START_INDEX; c < BOARD_SIZE; c++) {
45-
if (board[r][c] == NO_VALUE) {
43+
for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
44+
for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
45+
if (board[row][column] == NO_VALUE) {
4646
for (int k = MIN_VALUE; k <= MAX_VALUE; k++) {
47-
board[r][c] = k;
48-
if (isValid(board, r, c) && solve(board)) {
47+
board[row][column] = k;
48+
if (isValid(board, row, column) && solve(board)) {
4949
return true;
5050
}
51-
board[r][c] = NO_VALUE;
51+
board[row][column] = NO_VALUE;
5252
}
5353
return false;
5454
}
@@ -57,44 +57,44 @@ private boolean solve(int[][] board) {
5757
return true;
5858
}
5959

60-
private boolean isValid(int[][] board, int r, int c) {
61-
return rowConstraint(board, r) &&
62-
columnConstraint(board, c) &&
63-
subsectionConstraint(board, r, c);
60+
private boolean isValid(int[][] board, int row, int column) {
61+
return rowConstraint(board, row) &&
62+
columnConstraint(board, column) &&
63+
subsectionConstraint(board, row, column);
6464
}
6565

66-
private boolean subsectionConstraint(int[][] board, int r, int c) {
66+
private boolean subsectionConstraint(int[][] board, int row, int column) {
6767
boolean[] constraint = new boolean[BOARD_SIZE];
68-
int subsectionRowStart = (r / SUBSECTION_SIZE) * SUBSECTION_SIZE;
68+
int subsectionRowStart = (row / SUBSECTION_SIZE) * SUBSECTION_SIZE;
6969
int subsectionRowEnd = subsectionRowStart + SUBSECTION_SIZE;
7070

71-
int subsectionColumnStart = (c / SUBSECTION_SIZE) * SUBSECTION_SIZE;
71+
int subsectionColumnStart = (column / SUBSECTION_SIZE) * SUBSECTION_SIZE;
7272
int subsectionColumnEnd = subsectionColumnStart + SUBSECTION_SIZE;
7373

74-
for (int i = subsectionRowStart; i < subsectionRowEnd; i++) {
75-
for (int j = subsectionColumnStart; j < subsectionColumnEnd; j++) {
76-
if (!checkConstraint(board, i, constraint, j)) return false;
74+
for (int r = subsectionRowStart; r < subsectionRowEnd; r++) {
75+
for (int c = subsectionColumnStart; c < subsectionColumnEnd; c++) {
76+
if (!checkConstraint(board, r, constraint, c)) return false;
7777
}
7878
}
7979
return true;
8080
}
8181

82-
private boolean columnConstraint(int[][] board, int c) {
82+
private boolean columnConstraint(int[][] board, int column) {
8383
boolean[] constraint = new boolean[BOARD_SIZE];
8484
return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
85-
.allMatch(i -> checkConstraint(board, i, constraint, c));
85+
.allMatch(row -> checkConstraint(board, row, constraint, column));
8686
}
8787

88-
private boolean rowConstraint(int[][] board, int r) {
88+
private boolean rowConstraint(int[][] board, int row) {
8989
boolean[] constraint = new boolean[BOARD_SIZE];
9090
return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
91-
.allMatch(i -> checkConstraint(board, r, constraint, i));
91+
.allMatch(column -> checkConstraint(board, row, constraint, column));
9292
}
9393

94-
private boolean checkConstraint(int[][] board, int r, boolean[] constraint, int c) {
95-
if (board[r][c] != NO_VALUE) {
96-
if (!constraint[board[r][c] - 1]) {
97-
constraint[board[r][c] - 1] = true;
94+
private boolean checkConstraint(int[][] board, int row, boolean[] constraint, int column) {
95+
if (board[row][column] != NO_VALUE) {
96+
if (!constraint[board[row][column] - 1]) {
97+
constraint[board[row][column] - 1] = true;
9898
} else {
9999
return false;
100100
}

‎algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java‎

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ private void solve(int[][] board) {
3434
dlx.runSolver();
3535
}
3636

37-
private int getIndex(int row, int col, int num) {
38-
return (row - 1) * BOARD_SIZE * BOARD_SIZE + (col - 1) * BOARD_SIZE + (num - 1);
37+
private int getIndex(int row, int column, int num) {
38+
return (row - 1) * BOARD_SIZE * BOARD_SIZE + (column - 1) * BOARD_SIZE + (num - 1);
3939
}
4040

4141
private boolean[][] createExactCoverBoard() {
@@ -51,12 +51,12 @@ private boolean[][] createExactCoverBoard() {
5151
}
5252

5353
private int checkSubsectionConstraint(boolean[][] coverBoard, int hBase) {
54-
for (int br = COVER_START_INDEX; br <= BOARD_SIZE; br += SUBSECTION_SIZE) {
55-
for (int bc = COVER_START_INDEX; bc <= BOARD_SIZE; bc += SUBSECTION_SIZE) {
54+
for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row += SUBSECTION_SIZE) {
55+
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column += SUBSECTION_SIZE) {
5656
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
57-
for (int rDelta = 0; rDelta < SUBSECTION_SIZE; rDelta++) {
58-
for (int cDelta = 0; cDelta < SUBSECTION_SIZE; cDelta++) {
59-
int index = getIndex(br + rDelta, bc + cDelta, n);
57+
for (int rowDelta = 0; rowDelta < SUBSECTION_SIZE; rowDelta++) {
58+
for (int columnDelta = 0; columnDelta < SUBSECTION_SIZE; columnDelta++) {
59+
int index = getIndex(row + rowDelta, column + columnDelta, n);
6060
coverBoard[index][hBase] = true;
6161
}
6262
}
@@ -67,10 +67,10 @@ private int checkSubsectionConstraint(boolean[][] coverBoard, int hBase) {
6767
}
6868

6969
private int checkColumnConstraint(boolean[][] coverBoard, int hBase) {
70-
for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++) {
70+
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) {
7171
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
72-
for (int r1 = COVER_START_INDEX; r1 <= BOARD_SIZE; r1++) {
73-
int index = getIndex(r1, c, n);
72+
for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) {
73+
int index = getIndex(row, column, n);
7474
coverBoard[index][hBase] = true;
7575
}
7676
}
@@ -79,10 +79,10 @@ private int checkColumnConstraint(boolean[][] coverBoard, int hBase) {
7979
}
8080

8181
private int checkRowConstraint(boolean[][] coverBoard, int hBase) {
82-
for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) {
82+
for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) {
8383
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
84-
for (int c1 = COVER_START_INDEX; c1 <= BOARD_SIZE; c1++) {
85-
int index = getIndex(r, c1, n);
84+
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) {
85+
int index = getIndex(row, column, n);
8686
coverBoard[index][hBase] = true;
8787
}
8888
}
@@ -91,10 +91,10 @@ private int checkRowConstraint(boolean[][] coverBoard, int hBase) {
9191
}
9292

9393
private int checkCellConstraint(boolean[][] coverBoard, int hBase) {
94-
for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) {
95-
for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++, hBase++) {
94+
for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) {
95+
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++, hBase++) {
9696
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) {
97-
int index = getIndex(r, c, n);
97+
int index = getIndex(row, column, n);
9898
coverBoard[index][hBase] = true;
9999
}
100100
}
@@ -104,13 +104,13 @@ private int checkCellConstraint(boolean[][] coverBoard, int hBase) {
104104

105105
private boolean[][] initializeExactCoverBoard(int[][] board) {
106106
boolean[][] coverBoard = createExactCoverBoard();
107-
for (int i = COVER_START_INDEX; i <= BOARD_SIZE; i++) {
108-
for (int j = COVER_START_INDEX; j <= BOARD_SIZE; j++) {
109-
int n = board[i - 1][j - 1];
107+
for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) {
108+
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) {
109+
int n = board[row - 1][column - 1];
110110
if (n != NO_VALUE) {
111111
for (int num = MIN_VALUE; num <= MAX_VALUE; num++) {
112112
if (num != n) {
113-
Arrays.fill(coverBoard[getIndex(i, j, num)], false);
113+
Arrays.fill(coverBoard[getIndex(row, column, num)], false);
114114
}
115115
}
116116
}

‎algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ class DancingNode {
44
DancingNode L, R, U, D;
55
ColumnNode C;
66

7-
DancingNode hookDown(DancingNode n1) {
8-
assert (this.C == n1.C);
9-
n1.D = this.D;
10-
n1.D.U = n1;
11-
n1.U = this;
12-
this.D = n1;
13-
return n1;
14-
}
15-
16-
DancingNode hookRight(DancingNode n1) {
17-
n1.R = this.R;
18-
n1.R.L = n1;
19-
n1.L = this;
20-
this.R = n1;
21-
return n1;
7+
DancingNode hookDown(DancingNode node) {
8+
assert (this.C == node.C);
9+
node.D = this.D;
10+
node.D.U = node;
11+
node.U = this;
12+
this.D = node;
13+
return node;
14+
}
15+
16+
DancingNode hookRight(DancingNode node) {
17+
node.R = this.R;
18+
node.R.L = node;
19+
node.L = this;
20+
this.R = node;
21+
return node;
2222
}
2323

2424
void unlinkLR() {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.shufflingcollections;
2+
3+
import org.junit.Test;
4+
5+
import java.util.*;
6+
import java.util.stream.Collectors;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
public class ShufflingCollectionsUnitTest {
11+
12+
@Test
13+
public void whenShufflingList_thenListIsShuffled() {
14+
List<String> students = Arrays.asList("Foo", "Bar", "Baz", "Qux");
15+
16+
System.out.println("List before shuffling:");
17+
System.out.println(students);
18+
19+
Collections.shuffle(students);
20+
System.out.println("List after shuffling:");
21+
System.out.println(students);
22+
}
23+
24+
@Test
25+
public void whenShufflingMapKeys_thenValuesAreShuffled() {
26+
Map<Integer, String> studentsById = new HashMap<>();
27+
studentsById.put(1, "Foo");
28+
studentsById.put(2, "Bar");
29+
studentsById.put(3, "Baz");
30+
studentsById.put(4, "Qux");
31+
32+
System.out.println("Students before shuffling:");
33+
System.out.println(studentsById.values());
34+
35+
List<Integer> shuffledStudentIds = new ArrayList<>(studentsById.keySet());
36+
Collections.shuffle(shuffledStudentIds);
37+
38+
List<String> shuffledStudents = shuffledStudentIds.stream()
39+
.map(id -> studentsById.get(id))
40+
.collect(Collectors.toList());
41+
42+
System.out.println("Students after shuffling");
43+
System.out.println(shuffledStudents);
44+
}
45+
46+
@Test
47+
public void whenShufflingSet_thenElementsAreShuffled() {
48+
Set<String> students = new HashSet<>(Arrays.asList("Foo", "Bar", "Baz", "Qux"));
49+
50+
System.out.println("Set before shuffling:");
51+
System.out.println(students);
52+
53+
List<String> studentList = new ArrayList<>(students);
54+
55+
Collections.shuffle(studentList);
56+
System.out.println("Shuffled set elements:");
57+
System.out.println(studentList);
58+
}
59+
60+
@Test
61+
public void whenShufflingWithSameRandomness_thenElementsAreShuffledDeterministically() {
62+
List<String> students_1 = Arrays.asList("Foo", "Bar", "Baz", "Qux");
63+
List<String> students_2 = Arrays.asList("Foo", "Bar", "Baz", "Qux");
64+
65+
Collections.shuffle(students_1, new Random(5));
66+
Collections.shuffle(students_2, new Random(5));
67+
68+
assertThat(students_1).isEqualTo(students_2);
69+
}
70+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.baeldung.decimalformat;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.math.BigDecimal;
6+
import java.text.DecimalFormat;
7+
import java.text.DecimalFormatSymbols;
8+
import java.text.NumberFormat;
9+
import java.text.ParseException;
10+
import java.util.Locale;
11+
12+
import org.junit.Test;
13+
14+
public class DecimalFormatExamplesTest {
15+
16+
double d = 1234567.89;
17+
18+
@Test
19+
public void givenSimpleDecimal_WhenFormatting_ThenCorrectOutput() {
20+
21+
assertThat(new DecimalFormat("#.##", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
22+
.isEqualTo("1234567.89");
23+
24+
assertThat(new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
25+
.isEqualTo("1234567.89");
26+
27+
assertThat(new DecimalFormat("#########.###", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
28+
.isEqualTo("1234567.89");
29+
30+
assertThat(new DecimalFormat("000000000.000", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
31+
.isEqualTo("001234567.890");
32+
33+
}
34+
35+
@Test
36+
public void givenSmallerDecimalPattern_WhenFormatting_ThenRounding() {
37+
38+
assertThat(new DecimalFormat("#.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)).isEqualTo("1234567.9");
39+
40+
assertThat(new DecimalFormat("#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d)).isEqualTo("1234568");
41+
42+
}
43+
44+
@Test
45+
public void givenGroupingSeparator_WhenFormatting_ThenGroupedOutput() {
46+
47+
assertThat(new DecimalFormat("#,###.#", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
48+
.isEqualTo("1,234,567.9");
49+
50+
assertThat(new DecimalFormat("#,###", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
51+
.isEqualTo("1,234,568");
52+
53+
}
54+
55+
@Test
56+
public void givenMixedPattern_WhenFormatting_ThenCorrectOutput() {
57+
58+
assertThat(new DecimalFormat("The # number", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
59+
.isEqualTo("The 1234568 number");
60+
61+
assertThat(new DecimalFormat("The '#' # number", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
62+
.isEqualTo("The # 1234568 number");
63+
64+
}
65+
66+
@Test
67+
public void givenLocales_WhenFormatting_ThenCorrectOutput() {
68+
69+
assertThat(new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
70+
.isEqualTo("1,234,567.89");
71+
72+
assertThat(new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ITALIAN)).format(d))
73+
.isEqualTo("1.234.567,89");
74+
75+
assertThat(new DecimalFormat("#,###.##", DecimalFormatSymbols.getInstance(new Locale("it", "IT"))).format(d))
76+
.isEqualTo("1.234.567,89");
77+
78+
}
79+
80+
@Test
81+
public void givenE_WhenFormatting_ThenScientificNotation() {
82+
83+
assertThat(new DecimalFormat("00.#######E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
84+
.isEqualTo("12.3456789E5");
85+
86+
assertThat(new DecimalFormat("000.000000E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
87+
.isEqualTo("123.456789E4");
88+
89+
assertThat(new DecimalFormat("##0.######E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
90+
.isEqualTo("1.23456789E6");
91+
92+
assertThat(new DecimalFormat("###.000000E0", new DecimalFormatSymbols(Locale.ENGLISH)).format(d))
93+
.isEqualTo("1.23456789E6");
94+
95+
}
96+
97+
@Test
98+
public void givenString_WhenParsing_ThenCorrectOutput() throws ParseException {
99+
100+
assertThat(new DecimalFormat("", new DecimalFormatSymbols(Locale.ENGLISH)).parse("1234567.89"))
101+
.isEqualTo(1234567.89);
102+
103+
assertThat(new DecimalFormat("", new DecimalFormatSymbols(Locale.ITALIAN)).parse("1.234.567,89"))
104+
.isEqualTo(1234567.89);
105+
106+
}
107+
108+
@Test
109+
public void givenStringAndBigDecimalFlag_WhenParsing_ThenCorrectOutput() throws ParseException {
110+
111+
NumberFormat nf = new DecimalFormat("", new DecimalFormatSymbols(Locale.ENGLISH));
112+
((DecimalFormat) nf).setParseBigDecimal(true);
113+
assertThat(nf.parse("1234567.89")).isEqualTo(BigDecimal.valueOf(1234567.89));
114+
}
115+
116+
}

0 commit comments

Comments
 (0)