Skip to content

Commit bb6f827

Browse files
authored
Merge branch 'master' into level-order
2 parents 6823278 + 7dacb2f commit bb6f827

23 files changed

+1261
-142
lines changed

Conversions/AnyBaseToAnyBase.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import java.util.InputMismatchException;
2+
import java.util.Scanner;
3+
4+
/**
5+
* Class for converting from any base to any other base, though it's unclear how digits greater than
6+
* 36 would be represented in bases >36.
7+
*
8+
* @author Michael Rolland
9+
* @version 2017.09.29
10+
*
11+
*/
12+
public class AnyBaseToAnyBase {
13+
14+
// Driver
15+
public static void main(String[] args) {
16+
Scanner in = new Scanner(System.in);
17+
System.out.print("Enter number: ");
18+
String n = in.nextLine();
19+
int b1=0,b2=0;
20+
while (true) {
21+
try {
22+
System.out.print("Enter beginning base: ");
23+
b1 = in.nextInt();
24+
System.out.print("Enter end base: ");
25+
b2 = in.nextInt();
26+
break;
27+
} catch (InputMismatchException e) {
28+
System.out.println("Invalid input.");
29+
in.next();
30+
}
31+
}
32+
System.out.println(base2base(n, b1, b2));
33+
}
34+
35+
/**
36+
* Method to convert any integer from base b1 to base b2. Works by converting from b1 to decimal,
37+
* then decimal to b2.
38+
* @param n The integer to be converted.
39+
* @param b1 Beginning base.
40+
* @param b2 End base.
41+
* @return n in base b2.
42+
*/
43+
public static String base2base(String n, int b1, int b2) {
44+
// Declare variables: decimal value of n,
45+
// character of base b1, character of base b2,
46+
// and the string that will be returned.
47+
int decimalValue = 0, charB2;
48+
char charB1;
49+
String output="";
50+
// Go through every character of n
51+
for (int i=0; i<n.length(); i++) {
52+
// store the character in charB1
53+
charB1 = n.charAt(i);
54+
// if it is a non-number, convert it to a decimal value >9 and store it in charB2
55+
if (charB1 >= 'A' && charB1 <= 'Z')
56+
charB2 = 10 + (charB1 - 'A');
57+
// Else, store the integer value in charB2
58+
else
59+
charB2 = charB1 - '0';
60+
// Convert the digit to decimal and add it to the
61+
// decimalValue of n
62+
decimalValue = decimalValue * b1 + charB2;
63+
}
64+
65+
// Converting the decimal value to base b2:
66+
// A number is converted from decimal to another base
67+
// by continuously dividing by the base and recording
68+
// the remainder until the quotient is zero. The number in the
69+
// new base is the remainders, with the last remainder
70+
// being the left-most digit.
71+
72+
// While the quotient is NOT zero:
73+
while (decimalValue != 0) {
74+
// If the remainder is a digit < 10, simply add it to
75+
// the left side of the new number.
76+
if (decimalValue % b2 < 10)
77+
output = Integer.toString(decimalValue % b2) + output;
78+
// If the remainder is >= 10, add a character with the
79+
// corresponding value to the new number. (A = 10, B = 11, C = 12, ...)
80+
else
81+
output = (char)((decimalValue % b2)+55) + output;
82+
// Divide by the new base again
83+
decimalValue /= b2;
84+
}
85+
return output;
86+
}
87+
}

Dynamic Programming/Fibonacci.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static int fibMemo(int n) {
4040
f = 1;
4141
}
4242
else {
43-
f = fib(n-1) + fib(n-2);
43+
f = fibMemo(n-1) + fibMemo(n-2);
4444
map.put(n,f);
4545
}
4646

Dynamic Programming/Knapsack.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// A Dynamic Programming based solution for 0-1 Knapsack problem
2+
3+
public class Knapsack
4+
{
5+
6+
private static int knapSack(int W, int wt[], int val[], int n)
7+
{
8+
int i, w;
9+
int rv[][] = new int[n+1][W+1]; //rv means return value
10+
11+
// Build table rv[][] in bottom up manner
12+
for (i = 0; i <= n; i++)
13+
{
14+
for (w = 0; w <= W; w++)
15+
{
16+
if (i==0 || w==0)
17+
rv[i][w] = 0;
18+
else if (wt[i-1] <= w)
19+
rv[i][w] = Math.max(val[i-1] + rv[i-1][w-wt[i-1]], rv[i-1][w]);
20+
else
21+
rv[i][w] = rv[i-1][w];
22+
}
23+
}
24+
25+
return rv[n][W];
26+
}
27+
28+
29+
// Driver program to test above function
30+
public static void main(String args[])
31+
{
32+
int val[] = new int[]{50, 100, 130};
33+
int wt[] = new int[]{10, 20, 40};
34+
int W = 50;
35+
int n = val.length;
36+
System.out.println(knapSack(W, wt, val, n));
37+
}
38+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
*
5+
* @author Afrizal Fikri (https://github.com/icalF)
6+
*
7+
*/
8+
public class LongestIncreasingSubsequence {
9+
public static void main(String[] args) throws Exception {
10+
11+
Scanner sc = new Scanner(System.in);
12+
int n = sc.nextInt();
13+
14+
int ar[] = new int[n];
15+
for (int i = 0; i < n; i++) {
16+
ar[i] = sc.nextInt();
17+
}
18+
19+
System.out.println(LIS(ar));
20+
}
21+
22+
private static int upperBound(int[] ar, int l, int r, int key) {
23+
while (l < r-1) {
24+
int m = (l + r) / 2;
25+
if (ar[m] >= key)
26+
r = m;
27+
else
28+
l = m;
29+
}
30+
31+
return r;
32+
}
33+
34+
public static int LIS(int[] array) {
35+
int N = array.length;
36+
if (N == 0)
37+
return 0;
38+
39+
int[] tail = new int[N];
40+
int length = 1; // always points empty slot in tail
41+
42+
tail[0] = array[0];
43+
for (int i = 1; i < N; i++) {
44+
45+
// new smallest value
46+
if (array[i] < tail[0])
47+
tail[0] = array[i];
48+
49+
// array[i] extends largest subsequence
50+
else if (array[i] > tail[length-1])
51+
tail[length++] = array[i];
52+
53+
// array[i] will become end candidate of an existing subsequence or
54+
// Throw away larger elements in all LIS, to make room for upcoming grater elements than array[i]
55+
// (and also, array[i] would have already appeared in one of LIS, identify the location and replace it)
56+
else
57+
tail[upperBound(tail, -1, length-1, array[i])] = array[i];
58+
}
59+
60+
return length;
61+
}
62+
}

Dynamic Programming/rod_cutting.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* A Dynamic Programming solution for Rod cutting problem
2+
Returns the best obtainable price for a rod of
3+
length n and price[] as prices of different pieces */
4+
5+
public class RodCutting
6+
{
7+
8+
private static int cutRod(int price[],int n)
9+
{
10+
int val[] = new int[n+1];
11+
val[0] = 0;
12+
13+
for (int i = 1; i<=n; i++)
14+
{
15+
int max_val = Integer.MIN_VALUE;
16+
for (int j = 0; j < i; j++)
17+
max_val = Math.max(max_val,price[j] + val[i-j-1]);
18+
19+
val[i] = max_val;
20+
}
21+
22+
return val[n];
23+
}
24+
25+
//main function to test
26+
public static void main(String args[])
27+
{
28+
int arr[] = new int[] {2, 5, 13, 19, 20};
29+
int size = arr.length;
30+
System.out.println("Maximum Obtainable Value is " +
31+
cutRod(arr, size));
32+
}
33+
}

Misc/Abecedarian.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//Oskar Enmalm 29/9/17
2+
//An Abecadrian is a word where each letter is in alphabetical order
3+
4+
class Abecedarian{
5+
6+
public static boolean isAbecedarian(String s){
7+
int index = s.length() - 1;
8+
9+
for(int i =0; i <index; i++){
10+
11+
if(s.charAt(i)<=s.charAt(i + 1)){} //Need to check if each letter for the whole word is less than the one before it
12+
13+
else{return false;}
14+
}
15+
}
16+
return true;
17+
}

Misc/FibToN.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Scanner;
2+
3+
public class FibToN {
4+
5+
public static void main(String[] args) {
6+
//take input
7+
Scanner scn = new Scanner(System.in);
8+
int N = scn.nextInt();
9+
// print fibonacci sequence less than N
10+
int first = 0, second = 1;
11+
//first fibo and second fibonacci are 0 and 1 respectively
12+
13+
while(first <= N){
14+
//print first fibo 0 then add second fibo into it while updating second as well
15+
16+
System.out.println(first);
17+
18+
int next = first+ second;
19+
first = second;
20+
second = next;
21+
}
22+
}
23+
24+
}

Misc/GCD.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//Oskar Enmalm 3/10/17
2+
//This is Euclid's algorithm which is used to find the greatest common denominator
3+
4+
public class GCD{
5+
6+
public static int gcd(int a, int b) {
7+
8+
int r = a % b;
9+
while (r != 0) {
10+
b = r;
11+
r = b % r;
12+
}
13+
return b;
14+
}
15+
}

Misc/KMP.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
/*
3+
Implementation of Knuth–Morris–Pratt algorithm
4+
Usage:
5+
final String T = "AAAAABAAABA";
6+
final String P = "AAAA";
7+
KMPmatcher(T, P);
8+
*/
9+
public class KMP {
10+
11+
// find the starting index in string T[] that matches the search word P[]
12+
public void KMPmatcher(final String T, final String P) {
13+
final int m = T.length();
14+
final int n = P.length();
15+
final int[] pi = computePrefixFunction(P);
16+
int q = 0;
17+
for (int i = 0; i < m; i++) {
18+
while (q > 0 && T.charAt(i) != P.charAt(q)) {
19+
q = pi[q - 1];
20+
}
21+
22+
if (T.charAt(i) == P.charAt(q)) {
23+
q++;
24+
}
25+
26+
if (q == n) {
27+
System.out.println("Pattern starts: " + (i + 1 - n));
28+
q = pi[q - 1];
29+
}
30+
}
31+
32+
}
33+
34+
// return the prefix function
35+
private int[] computePrefixFunction(final String P) {
36+
final int n = P.length();
37+
final int[] pi = new int[n];
38+
pi[0] = 0;
39+
int q = 0;
40+
for (int i = 1; i < n; i++) {
41+
while (q > 0 && P.charAt(q) != P.charAt(i)) {
42+
q = pi[q - 1];
43+
}
44+
45+
if (P.charAt(q) == P.charAt(i)) {
46+
q++;
47+
}
48+
49+
pi[i] = q;
50+
51+
}
52+
53+
return pi;
54+
}
55+
}

0 commit comments

Comments
 (0)