Skip to content

Commit 8e65c9c

Browse files
committed
Finished up until Chapter 19
1 parent 928a01d commit 8e65c9c

164 files changed

Lines changed: 8725 additions & 62 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.

.ideavimrc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
" Usage and undo
2+
set history=1000
3+
set undolevels=10000
4+
set rnu
5+
set nrformats+=alpha
6+
set formatoptions+=j
7+
8+
" Search
9+
set ignorecase
10+
set smartcase
11+
12+
" """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
13+
" Appearance: {{{1
14+
" -------------------------------------------------------------
15+
" Status
16+
set relativenumber
17+
set ruler
18+
set hlsearch
19+
set wildmenu
20+
21+
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
22+
" Key Mappings: {{{1
23+
" -------------------------------------------------------------
24+
set timeoutlen=500
25+
let mapleader = "\<space>"
26+
27+
" Insert-mode delete word/line with undo.
28+
inoremap <C-U> <C-G>u<C-U>
29+
inoremap <C-w> <C-g>u<C-w>
30+
31+
" New ones:
32+
nnoremap <silent> <Space> :nohlsearch<Bar>:echo<CR>
33+
nnoremap <leader>h <Esc>:call HardTimeToggle()<CR>
34+
35+
" Abbreviations
36+
abbr psvm public static void main(String[] args){<CR>}<esc>O
37+
abbr sop System.out.print("");<esc>2hi
38+
abbr sopl System.out.println("");<esc>2hi
39+
abbr sopf System.out.printf("");<esc>2hi

Andrej/Arrays.java

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
public class Arrays {
2+
public static void sum1(int[] a, int[] b) {
3+
assert (a.length == b.length);
4+
for (int i = 0; i < a.length; i++)
5+
for (int j = 0; j < a.length; j++)
6+
b[i] += j == i ? 0 : a[i];
7+
}
8+
9+
public static void sum2(int[] a, int[] b) {
10+
assert (a.length == b.length);
11+
int total = 0;
12+
for (int i = 0; i < a.length; i++)
13+
for (int j = 0; j < a.length; j++)
14+
total += b[i];
15+
16+
total /= (a.length-1);
17+
18+
for (int i = 0; i < a.length; i++)
19+
a[i] = total - b[i];
20+
}
21+
22+
public static void heapify(int[] a) {
23+
int heapsize = a.length;
24+
for (int i = a.length / 2; i >= 0; i--) {
25+
int j = i;
26+
while (true) {
27+
final int left = 2*j + 1;
28+
final int right = left + 1;
29+
int max = 0;
30+
31+
if (left <= heapsize && a[left] > a[right])
32+
max = left;
33+
else
34+
max = j;
35+
36+
if (right <= heapsize && a[right] > a[max])
37+
max = right;
38+
39+
if (max == j)
40+
break;
41+
// else
42+
int tmp = a[j];
43+
a[j] = a[max];
44+
a[max] = tmp;
45+
j = max;
46+
}
47+
}
48+
}
49+
50+
public static int stream(int[] a, long k) {
51+
assert (k >= 0);
52+
long len = a.length;
53+
k %= 2*len;
54+
if (k < len)
55+
return a[(int) k];
56+
else // k >= len
57+
return 1 - a[(int) (2*len-1 - k)];
58+
}
59+
60+
public static boolean isSubseq(int[] a, int[] b) {
61+
if (a.length > b.length)
62+
return false;
63+
64+
int current = 0;
65+
for (int i = 0; i < b.length ; i++)
66+
if (b[i] == a[current])
67+
current++;
68+
69+
return current >= a.length;
70+
}
71+
72+
public static boolean isConsecSubseq(int[] a, int[] b) {
73+
if (a.length > b.length)
74+
return false;
75+
76+
boolean match = false;
77+
for (int i = 0; i < b.length - a.length; i++) {
78+
match = true;
79+
for (int j = 0; j < a.length; j++)
80+
if (b[j+i] != a[j]) {
81+
match = false;
82+
break;
83+
}
84+
85+
if (match)
86+
return true;
87+
}
88+
return false;
89+
}
90+
91+
// Princeton Knuth-Morris-Pratt Algorithm: youtu.be/iZ93Unvxwtw
92+
public static boolean KMP(int[] a, int[] b) {
93+
if (a.length > b.length)
94+
return false;
95+
96+
final int RADIX = 10;
97+
final int[][] dfa = new int[RADIX][a.length];
98+
// assume all values are single digits
99+
dfa[a[0]][0] = 1;
100+
for (int x = 0, j = 1; j < a.length; j++) {
101+
for (int c = 0; c < RADIX; c++)
102+
dfa[c][j] = dfa[c][x];
103+
104+
dfa[a[j]][j] = j+1;
105+
x = dfa[a[j]][x];
106+
}
107+
108+
int i, j;
109+
for (j = 0, i = 0; i < b.length && j < a.length; i++)
110+
j = dfa[b[i]][j];
111+
112+
return j == a.length;
113+
}
114+
}

Andrej/Digits.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class Digits {
2+
public static int number(int n) {
3+
assert (n >= 0);
4+
int digits = 0;
5+
do {
6+
digits++;
7+
n /= 10;
8+
} while (n != 0);
9+
return digits;
10+
}
11+
12+
public static int sum(int n) {
13+
assert (n >= 0);
14+
int sum = 0;
15+
do {
16+
sum += n % 10;
17+
n /= 10;
18+
} while (n != 0);
19+
return sum;
20+
}
21+
22+
public static String binary(int n) {
23+
assert (n >= 0);
24+
String result = "";
25+
do {
26+
result += n % 2;
27+
n /= 2;
28+
} while (n != 0);
29+
return result;
30+
}
31+
}

Andrej/Grid.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import java.lang.Math;
2+
3+
public class Grid {
4+
public static void main(String[] args){
5+
final int m = Integer.parseInt(args[0])/2;
6+
for (int i = -m; i <= m; i++) {
7+
for (int j = -m; j <= m; j++)
8+
System.out.print((m+1-Math.max(Math.abs(i), Math.abs(j))));
9+
System.out.println();
10+
}
11+
}
12+
}

Andrej/Line.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.lang.Math.*;
2+
3+
public class Line {
4+
public static void main(String[] args){
5+
// java Line x1 y1 x2 y2
6+
final int x = Math.abs(Integer.parseInt(args[0])-Integer.parseInt(args[2]));
7+
final int y = Math.abs(Integer.parseInt(args[1])-Integer.parseInt(args[3]));
8+
9+
int total = 0;
10+
for (int m = 1; m < (int) Math.sqrt(x); m++)
11+
for (int n = 1; n < m; n++)
12+
if (2*m*n*x == y*(m*m - n*n)) {
13+
total++;
14+
break;
15+
}
16+
System.out.println(total);
17+
}
18+
}
19+
20+
/*
21+
* Write a program that takes as its input four integers, representing two
22+
* points A and B in a plane with integer coordinates, and outputs the number
23+
* of points with integer coordinates that are on the line segment AB.
24+
*
25+
* Find how many sets of integers (a,b,c) satisfy,
26+
*
27+
* a <= x = |x2-x1| and b <= y = |y2-y1| where
28+
* k(a^2 + b^2) = k*c^2
29+
*
30+
* Use Pythogorean triples: a = m^2 - n^2, b = 2mn, (c = m^2 + n^2) where m > n.
31+
*
32+
* b/a = y/x => 2mn /(m^2 - n^2) = y/x => 2mx = y(m^2 - n^2).
33+
*
34+
* Since m^2 - n^2 <= x, m < floor (Math.sqrt (x)).
35+
* */

Andrej/NumberTheory.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.lang.Math.*;
4+
5+
public class NumberTheory {
6+
public static boolean isPrime(int n) {
7+
assert (n >= 2);
8+
if (n % 2 == 0 || n % 3 == 0)
9+
return false;
10+
for (int i = 1; i < (int) Math.sqrt(n); i++)
11+
if (n % (6*i-1) == 0 || n % (6*i+1) == 0)
12+
return false;
13+
return true;
14+
}
15+
16+
public static int gcd(int m, int n) {
17+
while (m != 0) {
18+
int m_ = m;
19+
m = n % m;
20+
n = m_;
21+
}
22+
return n;
23+
}
24+
25+
// Sieve of Sundaram: generates all ODD primes below 2*LIM + 2.
26+
// 2 is added manually.
27+
public static ArrayList<Integer> sundaram(int n) {
28+
final int LIM = (int) (Math.sqrt(n) / 2.0);
29+
final boolean[] primes = new boolean[LIM];
30+
Arrays.fill(primes, true);
31+
32+
// Eliminate
33+
for (int j = 1; j < LIM; j++)
34+
for (int i = 1; i <= j; i++) {
35+
int compound = i + j + 2*i*j;
36+
if (compound <= LIM)
37+
primes[compound-1] = false;
38+
}
39+
40+
// Collate. n / ln (n) ~ number of primes below n
41+
final ArrayList<Integer> result =
42+
new ArrayList<Integer>((int) (n / Math.log (n)));
43+
result.add(2);
44+
for (int i = 0; i < LIM; i++)
45+
if (primes[i])
46+
result.add(2*i + 3);
47+
return result;
48+
}
49+
50+
public static String FTA(int n) {
51+
final ArrayList<Integer> primes = sundaram(n);
52+
String result = "";
53+
for (int p : primes) {
54+
int exp = 0;
55+
while (n % p == 0) {
56+
exp++;
57+
n /= p;
58+
}
59+
result += p+"^"+exp+".";
60+
}
61+
return result.substring(0, result.length()-2);
62+
}
63+
}

deitel-10th/ch07/Dupes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static void main(String[] args) {
2525
Arrays.toString(vals2));
2626

2727
// Method 2
28-
final ArrayList<Integer> vals = new ArrayList<Integer>();
28+
final ArrayList<Integer> vals = new ArrayList<>();
2929
for (int i = 0; i < LIMIT; i++) {
3030
System.out.print("Enter number: ");
3131
int x = getInt();

deitel-10th/ch10/GUI/DrawPanel.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ else if (x1 % 3 == 1)
4444

4545
}
4646

47-
4847
public void paintComponent(Graphics g) {
4948
super.paintComponent(g);
5049

deitel-10th/ch10/q12-16/PayableInterfaceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Ex 10.14/15
2-
2+
import java.awt.
33
public class PayableInterfaceTest {
44
public static void main(String[] args) {
55
Payable[] payableObjects = {

deitel-10th/ch10/q17/Greenhouse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public class Greenhouse {
66
public static void main(String[] args){
7-
ArrayList<CarbonFootprint> emissions = new ArrayList<CarbonFootprint>();
7+
ArrayList<CarbonFootprint> emissions = new ArrayList<>();
88
emissions.add(new Bicycle(2000));
99
emissions.add(new Building(2340000));
1010
emissions.add(new Car(17000));

0 commit comments

Comments
 (0)