From 701d5d1855d9e371e718ea7442f34b688332e7c9 Mon Sep 17 00:00:00 2001 From: Varun Upadhyay Date: Thu, 28 Sep 2017 11:38:20 -0700 Subject: [PATCH 01/34] Added SieveOfEratosthenes.java --- Others/SieveOfEratosthenes.java | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Others/SieveOfEratosthenes.java diff --git a/Others/SieveOfEratosthenes.java b/Others/SieveOfEratosthenes.java new file mode 100644 index 00000000..4b6fd5b7 --- /dev/null +++ b/Others/SieveOfEratosthenes.java @@ -0,0 +1,49 @@ +/** + * + * @author Varun Upadhyay (https://github.com/varunu28) + * + */ +public class SieveOfEratosthenes { + + /** + * This method implements the Sieve of Eratosthenes Algorithm + * + * @param n The number till which we have to check for prime + * Prints all the prime numbers till n + **/ + + public static void findPrimesTillN(int n) { + int[] arr = new int[n+1]; + + for (int i=0;i<=n;i++) { + arr[i] = 1; + } + + arr[0] = arr[1] = 0; + + for (int i=2;i<=Math.sqrt(n);i++) { + if (arr[i] == 1) { + for (int j=2;i*j <= n;j++) { + arr[i*j] = 0; + } + } + } + + for (int i=0;i Date: Thu, 28 Sep 2017 21:29:36 +0100 Subject: [PATCH 02/34] added interpolationSearch.java --- Searches/interpolationSearch.java | 53 +++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Searches/interpolationSearch.java diff --git a/Searches/interpolationSearch.java b/Searches/interpolationSearch.java new file mode 100644 index 00000000..b8041ae6 --- /dev/null +++ b/Searches/interpolationSearch.java @@ -0,0 +1,53 @@ + +class Test +{ + // Array of items on which search will + // be conducted. + static int arr[] = new int[]{10, 12, 13, 16, 18, 19, 20, 21, 22, 23, + 24, 33, 35, 42, 47}; + + // If x is present in arr[0..n-1], then returns + // index of it, else returns -1. + static int interpolationSearch(int x) + { + // Find indexes of two corners + int lo = 0, hi = (arr.length - 1); + + // Since array is sorted, an element present + // in array must be in range defined by corner + while (lo <= hi && x >= arr[lo] && x <= arr[hi]) + { + // Probing the position with keeping + // uniform distribution in mind. + int pos = lo + (((hi-lo) / + (arr[hi]-arr[lo]))*(x - arr[lo])); + + // Condition of target found + if (arr[pos] == x) + return pos; + + // If x is larger, x is in upper part + if (arr[pos] < x) + lo = pos + 1; + + // If x is smaller, x is in lower part + else + hi = pos - 1; + } + return -1; + } + + // Driver method + public static void main(String[] args) + { + int x = 18; // Element to be searched + int index = interpolationSearch(x); + + // If element was found + if (index != -1) + System.out.println("Element found at index " + index); + else + System.out.println("Element not found."); + } +} + From e416be63bb7d684d4bb9d6e443addbf33c3d4b69 Mon Sep 17 00:00:00 2001 From: Oskar Enmalm Date: Fri, 29 Sep 2017 00:49:28 +0200 Subject: [PATCH 03/34] Create Abecedarian.java --- Misc/Abecedarian.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Misc/Abecedarian.java diff --git a/Misc/Abecedarian.java b/Misc/Abecedarian.java new file mode 100644 index 00000000..8e4a1793 --- /dev/null +++ b/Misc/Abecedarian.java @@ -0,0 +1,21 @@ +//Oskar Enmalm 29/9/17 +//An Abecadrian is a word where each letter is in alphabetical order + +class Abecedarian{ + + public static boolean isAbecedarian(String s){ + int index = s.length() - 1; + + for(int i =0; i Date: Fri, 29 Sep 2017 01:04:24 +0200 Subject: [PATCH 04/34] Update Abecedarian.java Removed unnecessary println and compacted it --- Misc/Abecedarian.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/Misc/Abecedarian.java b/Misc/Abecedarian.java index 8e4a1793..6507411b 100644 --- a/Misc/Abecedarian.java +++ b/Misc/Abecedarian.java @@ -10,12 +10,8 @@ class Abecedarian{ 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 - else{ - System.out.println("That is not abecedarian"); - return false; + else{return false;} } - } - System.out.println("Wow, that number is abecedarian"); return true; - } +} From 658ed90553f40fd5df53edcea2503131bc73e282 Mon Sep 17 00:00:00 2001 From: Michael Rolland Date: Thu, 28 Sep 2017 21:32:55 -0400 Subject: [PATCH 05/34] Create LowestBasePalindrome.java Algorithm for determining the lowest base in which a given integer is a palindrome. NOTE: Has room for error, see note at line 63. --- Misc/LowestBasePalindrome.java | 144 +++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 Misc/LowestBasePalindrome.java diff --git a/Misc/LowestBasePalindrome.java b/Misc/LowestBasePalindrome.java new file mode 100644 index 00000000..fce2d216 --- /dev/null +++ b/Misc/LowestBasePalindrome.java @@ -0,0 +1,144 @@ +import java.util.InputMismatchException; +import java.util.Scanner; + +/** + * Class for finding the lowest base in which a given integer is a palindrome. + * Includes auxiliary methods for converting between bases and reversing strings. + * + * NOTE: There is potential for error, see note at line 63. + * + * @author RollandMichael + * @version 2017.09.28 + * + */ +public class LowestBasePalindrome { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + int n=0; + while (true) { + try { + System.out.print("Enter number: "); + n = in.nextInt(); + break; + } catch (InputMismatchException e) { + System.out.println("Invalid input!"); + in.next(); + } + } + System.out.println(n+" is a palindrome in base "+lowestBasePalindrome(n)); + System.out.println(base2base(Integer.toString(n),10, lowestBasePalindrome(n))); + } + + /** + * Given a number in base 10, returns the lowest base in which the + * number is represented by a palindrome (read the same left-to-right + * and right-to-left). + * @param num A number in base 10. + * @return The lowest base in which num is a palindrome. + */ + public static int lowestBasePalindrome(int num) { + int base, num2=num; + int digit; + char digitC; + boolean foundBase=false; + String newNum = ""; + String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + while (!foundBase) { + // Try from bases 2 to num (any number n in base n is 1) + for (base=2; base0) { + // Obtain the first digit of n in the current base, + // which is equivalent to the integer remainder of (n/base). + // The next digit is obtained by dividing n by the base and + // continuing the process of getting the remainder. This is done + // until n is <=0 and the number in the new base is obtained. + digit = (num % base); + num/=base; + // If the digit isn't in the set of [0-9][A-Z] (beyond base 36), its character + // form is just its value in ASCII. + + // NOTE: This may cause problems, as the capital letters are ASCII values + // 65-90. It may cause false positives when one digit is, for instance 10 and assigned + // 'A' from the character array and the other is 65 and also assigned 'A'. + + // Regardless, the character is added to the representation of n + // in the current base. + if (digit>=digits.length()) { + digitC=(char)(digit); + newNum+=digitC; + continue; + } + newNum+=digits.charAt(digit); + } + // Num is assigned back its original value for the next iteration. + num=num2; + // Auxiliary method reverses the number. + String reverse = reverse(newNum); + // If the number is read the same as its reverse, then it is a palindrome. + // The current base is returned. + if (reverse.equals(newNum)) { + foundBase=true; + return base; + } + } + } + // If all else fails, n is always a palindrome in base n-1. ("11") + return num-1; + } + + private static String reverse(String str) { + String reverse = ""; + for(int i=str.length()-1; i>=0; i--) { + reverse += str.charAt(i); + } + return reverse; + } + + private static String base2base(String n, int b1, int b2) { + // Declare variables: decimal value of n, + // character of base b1, character of base b2, + // and the string that will be returned. + int decimalValue = 0, charB2; + char charB1; + String output=""; + // Go through every character of n + for (int i=0; i9 and store it in charB2 + if (charB1 >= 'A' && charB1 <= 'Z') + charB2 = 10 + (charB1 - 'A'); + // Else, store the integer value in charB2 + else + charB2 = charB1 - '0'; + // Convert the digit to decimal and add it to the + // decimalValue of n + decimalValue = decimalValue * b1 + charB2; + } + + // Converting the decimal value to base b2: + // A number is converted from decimal to another base + // by continuously dividing by the base and recording + // the remainder until the quotient is zero. The number in the + // new base is the remainders, with the last remainder + // being the left-most digit. + + // While the quotient is NOT zero: + while (decimalValue != 0) { + // If the remainder is a digit < 10, simply add it to + // the left side of the new number. + if (decimalValue % b2 < 10) + output = Integer.toString(decimalValue % b2) + output; + // If the remainder is >= 10, add a character with the + // corresponding value to the new number. (A = 10, B = 11, C = 12, ...) + else + output = (char)((decimalValue % b2)+55) + output; + // Divide by the new base again + decimalValue /= b2; + } + return output; + } +} From dc8114c17d0664f459d0c59e59a7e6f562530395 Mon Sep 17 00:00:00 2001 From: icalF Date: Fri, 29 Sep 2017 10:47:05 +0700 Subject: [PATCH 06/34] Add longest increasing subsequence --- .../LongestIncreasingSubsequence.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Dynamic Programming/LongestIncreasingSubsequence.java diff --git a/Dynamic Programming/LongestIncreasingSubsequence.java b/Dynamic Programming/LongestIncreasingSubsequence.java new file mode 100644 index 00000000..1616c246 --- /dev/null +++ b/Dynamic Programming/LongestIncreasingSubsequence.java @@ -0,0 +1,62 @@ +import java.util.Scanner; + +/** + * + * @author Afrizal Fikri (https://github.com/icalF) + * + */ +public class LongestIncreasingSubsequence { + public static void main(String[] args) throws Exception { + + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + + int ar[] = new int[n]; + for (int i = 0; i < n; i++) { + ar[i] = sc.nextInt(); + } + + System.out.println(LIS(ar)); + } + + private static int upperBound(int[] ar, int l, int r, int key) { + while (l < r-1) { + int m = (l + r) / 2; + if (ar[m] >= key) + r = m; + else + l = m; + } + + return r; + } + + public static int LIS(int[] array) { + int N = array.length; + if (N == 0) + return 0; + + int[] tail = new int[N]; + int length = 1; // always points empty slot in tail + + tail[0] = array[0]; + for (int i = 1; i < N; i++) { + + // new smallest value + if (array[i] < tail[0]) + tail[0] = array[i]; + + // array[i] extends largest subsequence + else if (array[i] > tail[length-1]) + tail[length++] = array[i]; + + // array[i] will become end candidate of an existing subsequence or + // Throw away larger elements in all LIS, to make room for upcoming grater elements than array[i] + // (and also, array[i] would have already appeared in one of LIS, identify the location and replace it) + else + tail[upperBound(tail, -1, length-1, array[i])] = array[i]; + } + + return length; + } +} \ No newline at end of file From b64b92b378860855a08eb827807ae7ac0ccef7e7 Mon Sep 17 00:00:00 2001 From: Fahri YARDIMCI Date: Fri, 29 Sep 2017 12:33:32 +0300 Subject: [PATCH 07/34] Creating ciphers and adding Caesar Cipher --- ciphers/Caesar.java | 117 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 ciphers/Caesar.java diff --git a/ciphers/Caesar.java b/ciphers/Caesar.java new file mode 100644 index 00000000..32bc87b8 --- /dev/null +++ b/ciphers/Caesar.java @@ -0,0 +1,117 @@ +/** +Author : FAHRI YARDIMCI + +A Java implementation of Caesar Cipher. +/It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. / +**/ +import java.util.Scanner; +public class Caesar { +public static String encode (String message,int shift) +{ + String encoded = ""; + for(int i = 0 ; i=65 && current<= 90) + { + int numAlphabet = message.charAt(i); + if(shift + numAlphabet > 90) + { + int j = 90 - numAlphabet; + char nextKey = (char)(65 + (shift - j - 1)); + encoded += nextKey; + + } + else + { + char nextKey = (char)(current + shift); + encoded += nextKey; + } + } + else if (current>=97 && current <= 122) + { + int numAlphabet = message.charAt(i); + if(shift + numAlphabet > 122) + { + int j = 122 - numAlphabet; + char nextKey = (char)(97 + (shift - j - 1)); + encoded += nextKey; + } + else + { + char nextKey = (char)(current + shift); + encoded += nextKey; + } + } + } + return encoded; +} +public static String decode (String message,int shift) +{ + String decoded = ""; + for(int i = 0 ; i=65 && current<= 90) + { + int numAlphabet = message.charAt(i); + if(numAlphabet - shift < 65) + { + int j = numAlphabet - 65; + char nextKey = (char)(90 - (shift - j - 1)); + decoded += nextKey; + + } + else + { + char nextKey = (char)(current - shift); + decoded += nextKey; + } + } + else if (current>=97 && current <= 122) + { + int numAlphabet = message.charAt(i); + if(numAlphabet - shift < 97) + { + int j = numAlphabet - 97; + char nextKey = (char)(122 - (shift - j - 1)); + decoded += nextKey; + } + else + { + char nextKey = (char)(current - shift); + decoded += nextKey; + } + } + } + return decoded; +} +public static void main(String[] args) +{ + Scanner input = new Scanner(System.in); + System.out.println("Please enter the message (Latin Alphabet)"); + String message = input.nextLine(); + System.out.println(message); + System.out.println("Please enter the shift number"); + int shift = input.nextInt() % 26; + System.out.println("(E)ncode or (D)ecode ?"); + char choice = input.next().charAt(0); + if(choice == 'E' || choice=='e') + System.out.println("ENCODED MESSAGE IS \n" + encode(message,shift)); //send our function to handle + if(choice =='D' || choice =='d') + System.out.println("DECODED MESSAGE IS \n" + decode(message,shift)); +} + +} \ No newline at end of file From e5381585a5329af6a1ad2d0b5ababef11cba4062 Mon Sep 17 00:00:00 2001 From: mpokryva Date: Fri, 29 Sep 2017 11:21:47 -0400 Subject: [PATCH 08/34] Changed find(int key) method to return null when node is not found, and updated docs accordingly. Issue #104. --- data_structures/Trees/BinaryTree.java | 284 +++++++++++++------------- 1 file changed, 141 insertions(+), 143 deletions(-) diff --git a/data_structures/Trees/BinaryTree.java b/data_structures/Trees/BinaryTree.java index 3d05f176..a20d24ee 100644 --- a/data_structures/Trees/BinaryTree.java +++ b/data_structures/Trees/BinaryTree.java @@ -1,20 +1,20 @@ /** - * This entire class is used to build a Binary Tree data structure. - * There is the Node Class and the Tree Class, both explained below. - * - * @author Unknown - * - */ +* This entire class is used to build a Binary Tree data structure. +* There is the Node Class and the Tree Class, both explained below. +* +* @author Unknown +* +*/ /** - * This class implements the nodes that will go on the Binary Tree. - * They consist of the data in them, the node to the left, the node - * to the right, and the parent from which they came from. - * - * @author Unknown - * - */ +* This class implements the nodes that will go on the Binary Tree. +* They consist of the data in them, the node to the left, the node +* to the right, and the parent from which they came from. +* +* @author Unknown +* +*/ class Node{ /** Data for the node */ public int data; @@ -26,10 +26,10 @@ class Node{ public Node parent; /** - * Constructor of Node - * - * @param value Value to put in the node - */ + * Constructor of Node + * + * @param value Value to put in the node + */ public Node(int value){ data = value; left = null; @@ -40,56 +40,54 @@ class Node{ /** - * A binary tree is a data structure in which an element - * has two successors(children). The left child is usually - * smaller than the parent, and the right child is usually - * bigger. - * - * @author Unknown - * - */ +* A binary tree is a data structure in which an element +* has two successors(children). The left child is usually +* smaller than the parent, and the right child is usually +* bigger. +* +* @author Unknown +* +*/ class Tree{ /** The root of the Binary Tree */ private Node root; /** - * Constructor - */ + * Constructor + */ public Tree(){ root = null; } - + /** - * Method to find a Node with a certain value - * - * @param key Value being looked for - * @return The node if it finds it, otherwise returns the parent - */ - public Node find(int key){ + * Method to find a Node with a certain value + * + * @param key Value being looked for + * @return The node if it finds it, otherwise returns the parent + */ + public Node find(int key) { Node current = root; - Node last = root; - while(current != null){ - last = current; - if(key < current.data) + while (current != null) { + if(key < current.data) { current = current.left; - else if(key > current.data) + } else if(key > current.data) { current = current.right; - //If you find the value return it - else + } else { // If you find the value return it return current; + } } - return last; + return null; } /** - * Inserts certain value into the Binary Tree - * - * @param value Value to be inserted - */ + * Inserts certain value into the Binary Tree + * + * @param value Value to be inserted + */ public void put(int value){ Node newNode = new Node(value); if(root == null) - root = newNode; + root = newNode; else{ //This will return the soon to be parent of the value you're inserting Node parent = find(value); @@ -109,29 +107,29 @@ class Tree{ } /** - * Deletes a given value from the Binary Tree - * - * @param value Value to be deleted - * @return If the value was deleted - */ + * Deletes a given value from the Binary Tree + * + * @param value Value to be deleted + * @return If the value was deleted + */ public boolean remove(int value){ //temp is the node to be deleted Node temp = find(value); //If the value doesn't exist if(temp.data != value) - return false; + return false; //No children if(temp.right == null && temp.left == null){ if(temp == root) - root = null; + root = null; //This if/else assigns the new node to be either the left or right child of the parent else if(temp.parent.data < temp.data) - temp.parent.right = null; + temp.parent.right = null; else - temp.parent.left = null; + temp.parent.left = null; return true; } @@ -162,9 +160,9 @@ class Tree{ //This if/else assigns the new node to be either the left or right child of the parent if(temp.parent.data < temp.data) - temp.parent.right = successor; + temp.parent.right = successor; else - temp.parent.left = successor; + temp.parent.left = successor; return true; } } @@ -175,96 +173,96 @@ class Tree{ if(temp == root){ root = temp.right; return true;} - temp.right.parent = temp.parent; + temp.right.parent = temp.parent; - //Assigns temp to left or right child - if(temp.data < temp.parent.data) + //Assigns temp to left or right child + if(temp.data < temp.parent.data) temp.parent.left = temp.right; - else + else temp.parent.right = temp.right; - return true; + return true; + } + //If it has a left child + else{ + if(temp == root){ + root = temp.left; return true;} + + temp.left.parent = temp.parent; + + //Assigns temp to left or right side + if(temp.data < temp.parent.data) + temp.parent.left = temp.left; + else + temp.parent.right = temp.left; + return true; + } + } } - //If it has a left child - else{ - if(temp == root){ - root = temp.left; return true;} - temp.left.parent = temp.parent; + /** + * This method finds the Successor to the Node given. + * Move right once and go left down the tree as far as you can + * + * @param n Node that you want to find the Successor of + * @return The Successor of the node + */ + public Node findSuccessor(Node n){ + if(n.right == null) + return n; + Node current = n.right; + Node parent = n.right; + while(current != null){ + parent = current; + current = current.left; + } + return parent; + } - //Assigns temp to left or right side - if(temp.data < temp.parent.data) - temp.parent.left = temp.left; - else - temp.parent.right = temp.left; - return true; + /** + * Returns the root of the Binary Tree + * + * @return the root of the Binary Tree + */ + public Node getRoot(){ + return root; + } + + /** + * Prints leftChild - root - rightChild + * + * @param localRoot The local root of the binary tree + */ + public void inOrder(Node localRoot){ + if(localRoot != null){ + inOrder(localRoot.left); + System.out.print(localRoot.data + " "); + inOrder(localRoot.right); + } + } + + /** + * Prints root - leftChild - rightChild + * + * @param localRoot The local root of the binary tree + */ + public void preOrder(Node localRoot){ + if(localRoot != null){ + System.out.print(localRoot.data + " "); + preOrder(localRoot.left); + preOrder(localRoot.right); + } + } + + /** + * Prints rightChild - leftChild - root + * + * @param localRoot The local root of the binary tree + */ + public void postOrder(Node localRoot){ + if(localRoot != null){ + postOrder(localRoot.left); + postOrder(localRoot.right); + System.out.print(localRoot.data + " "); + } } } - } - - /** - * This method finds the Successor to the Node given. - * Move right once and go left down the tree as far as you can - * - * @param n Node that you want to find the Successor of - * @return The Successor of the node - */ - public Node findSuccessor(Node n){ - if(n.right == null) - return n; - Node current = n.right; - Node parent = n.right; - while(current != null){ - parent = current; - current = current.left; - } - return parent; - } - - /** - * Returns the root of the Binary Tree - * - * @return the root of the Binary Tree - */ - public Node getRoot(){ - return root; - } - - /** - * Prints leftChild - root - rightChild - * - * @param localRoot The local root of the binary tree - */ - public void inOrder(Node localRoot){ - if(localRoot != null){ - inOrder(localRoot.left); - System.out.print(localRoot.data + " "); - inOrder(localRoot.right); - } - } - - /** - * Prints root - leftChild - rightChild - * - * @param localRoot The local root of the binary tree - */ - public void preOrder(Node localRoot){ - if(localRoot != null){ - System.out.print(localRoot.data + " "); - preOrder(localRoot.left); - preOrder(localRoot.right); - } - } - - /** - * Prints rightChild - leftChild - root - * - * @param localRoot The local root of the binary tree - */ - public void postOrder(Node localRoot){ - if(localRoot != null){ - postOrder(localRoot.left); - postOrder(localRoot.right); - System.out.print(localRoot.data + " "); - } - } -} From 22c48e09253e43e499e94324e3baf0d8fddf0c58 Mon Sep 17 00:00:00 2001 From: Michael Rolland Date: Fri, 29 Sep 2017 14:31:26 -0400 Subject: [PATCH 09/34] Create AnyBaseToAnyBase Class for converting a number between any two bases, by going through decimal. --- Conversions/AnyBaseToAnyBase | 87 ++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 Conversions/AnyBaseToAnyBase diff --git a/Conversions/AnyBaseToAnyBase b/Conversions/AnyBaseToAnyBase new file mode 100644 index 00000000..9f3f3704 --- /dev/null +++ b/Conversions/AnyBaseToAnyBase @@ -0,0 +1,87 @@ +import java.util.InputMismatchException; +import java.util.Scanner; + +/** + * Class for converting from any base to any other base, though it's unclear how digits greater than + * 36 would be represented in bases >36. + * + * @author Michael Rolland + * @version 2017.09.29 + * + */ +public class AnyBaseToAnyBase { + + // Driver + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + System.out.print("Enter number: "); + String n = in.nextLine(); + int b1=0,b2=0; + while (true) { + try { + System.out.print("Enter beginning base: "); + b1 = in.nextInt(); + System.out.print("Enter end base: "); + b2 = in.nextInt(); + break; + } catch (InputMismatchException e) { + System.out.println("Invalid input."); + in.next(); + } + } + System.out.println(base2base(n, b1, b2)); + } + + /** + * Method to convert any integer from base b1 to base b2. Works by converting from b1 to decimal, + * then decimal to b2. + * @param n The integer to be converted. + * @param b1 Beginning base. + * @param b2 End base. + * @return n in base b2. + */ + public static String base2base(String n, int b1, int b2) { + // Declare variables: decimal value of n, + // character of base b1, character of base b2, + // and the string that will be returned. + int decimalValue = 0, charB2; + char charB1; + String output=""; + // Go through every character of n + for (int i=0; i9 and store it in charB2 + if (charB1 >= 'A' && charB1 <= 'Z') + charB2 = 10 + (charB1 - 'A'); + // Else, store the integer value in charB2 + else + charB2 = charB1 - '0'; + // Convert the digit to decimal and add it to the + // decimalValue of n + decimalValue = decimalValue * b1 + charB2; + } + + // Converting the decimal value to base b2: + // A number is converted from decimal to another base + // by continuously dividing by the base and recording + // the remainder until the quotient is zero. The number in the + // new base is the remainders, with the last remainder + // being the left-most digit. + + // While the quotient is NOT zero: + while (decimalValue != 0) { + // If the remainder is a digit < 10, simply add it to + // the left side of the new number. + if (decimalValue % b2 < 10) + output = Integer.toString(decimalValue % b2) + output; + // If the remainder is >= 10, add a character with the + // corresponding value to the new number. (A = 10, B = 11, C = 12, ...) + else + output = (char)((decimalValue % b2)+55) + output; + // Divide by the new base again + decimalValue /= b2; + } + return output; + } +} From 2d300b340a531988b55d00a71fd1ec9eda1d9e56 Mon Sep 17 00:00:00 2001 From: Michael Rolland Date: Fri, 29 Sep 2017 14:32:47 -0400 Subject: [PATCH 10/34] Update LowestBasePalindrome.java Very, very minor documentation edit --- Misc/LowestBasePalindrome.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/LowestBasePalindrome.java b/Misc/LowestBasePalindrome.java index fce2d216..d0df5c30 100644 --- a/Misc/LowestBasePalindrome.java +++ b/Misc/LowestBasePalindrome.java @@ -46,7 +46,7 @@ public class LowestBasePalindrome { String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; while (!foundBase) { - // Try from bases 2 to num (any number n in base n is 1) + // Try from bases 2 to num-1 for (base=2; base0) { From 1e99c48d4a3346476d7d48b8b03ae8ddea27569c Mon Sep 17 00:00:00 2001 From: Manmeet Singh Date: Sun, 1 Oct 2017 00:17:23 +0530 Subject: [PATCH 11/34] Tower of Hanoi using Recursion --- Misc/TowerOfHanoiUsingRecursion | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Misc/TowerOfHanoiUsingRecursion diff --git a/Misc/TowerOfHanoiUsingRecursion b/Misc/TowerOfHanoiUsingRecursion new file mode 100644 index 00000000..60778fc9 --- /dev/null +++ b/Misc/TowerOfHanoiUsingRecursion @@ -0,0 +1,26 @@ +package com.manmeet; + +import java.util.Scanner; + +public class TowerOfHanoi +{ + public static void shift(int n, String startPole, String intermediatePole, String endPole) + { + if (n == 0) // if n becomes zero the program returns thus ending the loop. + { + return; + } + // Shift function is called in recursion for swapping the n-1 disc from the startPole to the intermediatePole + shift(n - 1, startPole, endPole, intermediatePole); + System.out.println("\nMove \"" + n + "\" from " + startPole + " --> " + endPole); // Result Printing + // Shift function is called in recursion for swapping the n-1 disc from the intermediatePole to the endPole + shift(n - 1, intermediatePole, startPole, endPole); + } + public static void main(String[] args) + { + System.out.print("Enter number of discs on Pole 1: "); + Scanner scanner = new Scanner(System.in); + int numberOfDiscs = scanner.nextInt(); //input of number of discs on pole 1 + shift(numberOfDiscs, "Pole1", "Pole2", "Pole3"); //Shift function called + } +} From dad4c69c98629a4ceb2dc8e1515400a8851cdd84 Mon Sep 17 00:00:00 2001 From: Nimit Arora Date: Sun, 1 Oct 2017 12:39:38 +0530 Subject: [PATCH 12/34] Added algorithm to reverse a stack using recursion --- Misc/ReverseStackUsingRecursion.java | 74 ++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 Misc/ReverseStackUsingRecursion.java diff --git a/Misc/ReverseStackUsingRecursion.java b/Misc/ReverseStackUsingRecursion.java new file mode 100644 index 00000000..4bcdeb25 --- /dev/null +++ b/Misc/ReverseStackUsingRecursion.java @@ -0,0 +1,74 @@ +package stacks_and_queues; + + +import java.util.Stack; + +public class ReverseStackUsingRecursion { + + //Stack + private static Stack stack=new Stack<>(); + + //Main function + public static void main(String[] args) { + //To Create a Dummy Stack containing integers from 0-9 + for(int i=0;i<10;i++) + { + stack.push(i); + } + System.out.println("STACK"); + + //To print that dummy Stack + for(int k=9;k>=0;k--) + { + System.out.println(k); + } + + //Reverse Function called + reverseUsingRecursion(stack); + + System.out.println("REVERSED STACK : "); + //To print reversed stack + while (!stack.isEmpty()) + { + System.out.println(stack.pop()); + } + + + } + + //Function Used to reverse Stack Using Recursion + private static void reverseUsingRecursion(Stack stack) { + if(stack.isEmpty()) + { + return; + } + /* All items are stored in call stack until we reach the end*/ + + int temptop=stack.peek(); + stack.pop(); + reverseUsingRecursion(stack); //Recursion call + insertAtEnd(temptop); // Insert items held in call stack one by one into stack + } + + //Function used to insert element at the end of stack + private static void insertAtEnd(int temptop) { + if(stack.isEmpty()) + { + stack.push(temptop); // If stack is empty push the element + } + else { + int temp = stack.peek(); /* All the items are stored in call stack until we reach end*/ + stack.pop(); + + insertAtEnd(temptop); + + stack.push(temp); + } + + } + + + + + +} From 727769c06f45bf007cf2d229deb3fde46559bb87 Mon Sep 17 00:00:00 2001 From: Nimit Arora Date: Sun, 1 Oct 2017 12:45:13 +0530 Subject: [PATCH 13/34] Added algorithm to reverse a stack using recursion --- Misc/ReverseStackUsingRecursion.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Misc/ReverseStackUsingRecursion.java b/Misc/ReverseStackUsingRecursion.java index 4bcdeb25..bb9f606a 100644 --- a/Misc/ReverseStackUsingRecursion.java +++ b/Misc/ReverseStackUsingRecursion.java @@ -1,4 +1,4 @@ -package stacks_and_queues; +/* Program to reverse a Stack using Recursion*/ import java.util.Stack; @@ -38,7 +38,7 @@ public class ReverseStackUsingRecursion { //Function Used to reverse Stack Using Recursion private static void reverseUsingRecursion(Stack stack) { - if(stack.isEmpty()) + if(stack.isEmpty()) // If stack is empty then return { return; } @@ -60,7 +60,7 @@ public class ReverseStackUsingRecursion { int temp = stack.peek(); /* All the items are stored in call stack until we reach end*/ stack.pop(); - insertAtEnd(temptop); + insertAtEnd(temptop); //Recursive call stack.push(temp); } From f6c64409c0f794e0dd5c3a8787a6dcacddcce636 Mon Sep 17 00:00:00 2001 From: Dheeraj Kumar Barnwal Date: Sun, 1 Oct 2017 22:55:25 +0530 Subject: [PATCH 14/34] Corrected method call --- Dynamic Programming/Fibonacci.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dynamic Programming/Fibonacci.java b/Dynamic Programming/Fibonacci.java index 72048153..17c11d8d 100644 --- a/Dynamic Programming/Fibonacci.java +++ b/Dynamic Programming/Fibonacci.java @@ -40,7 +40,7 @@ public class Fibonacci { f = 1; } else { - f = fib(n-1) + fib(n-2); + f = fibMemo(n-1) + fibMemo(n-2); map.put(n,f); } From 84b2b8ad9411ffeded1ba81168fcc9202a0a2794 Mon Sep 17 00:00:00 2001 From: Nimit Arora Date: Sun, 1 Oct 2017 23:31:01 +0530 Subject: [PATCH 15/34] Added Algorithm to Return Subsequences --- Misc/ReturnSubsequence.java | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Misc/ReturnSubsequence.java diff --git a/Misc/ReturnSubsequence.java b/Misc/ReturnSubsequence.java new file mode 100644 index 00000000..ef3aaed0 --- /dev/null +++ b/Misc/ReturnSubsequence.java @@ -0,0 +1,59 @@ +/* +This program will return all the subsequences of the input string in a string array; +Sample Input: +abc +Sample Output: +"" ( Empty String ) +c +b +bc +a +ac +ab +abc + + */ + +import java.util.Scanner; + +public class ReturnSubsequence { + /* + Main function will accept the given string and implement return subsequences function + */ + public static void main(String[] args) { + System.out.println("Enter String: "); + Scanner s=new Scanner(System.in); + String givenString=s.next(); //given string + String[] subsequence=returnSubsequence(givenString); //calling returnSubsequence() function + System.out.println("Subsequences : "); + for(int i=0;i Date: Mon, 2 Oct 2017 00:42:20 +0530 Subject: [PATCH 16/34] Postfix Notation using Stack Note- Give input in the form like "1 21 + 45 13 + *" #96 --- Misc/StackPostfixNotation | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Misc/StackPostfixNotation diff --git a/Misc/StackPostfixNotation b/Misc/StackPostfixNotation new file mode 100644 index 00000000..c04b0ac6 --- /dev/null +++ b/Misc/StackPostfixNotation @@ -0,0 +1,38 @@ +import java.util.*; + +public class Postfix { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + String post = scanner.nextLine(); // Takes input with spaces in between eg. "1 21 +" + System.out.println(postfixEvaluate(post)); + } + + // Evaluates the given postfix expression string and returns the result. + public static int postfixEvaluate(String exp) { + Stack s = new Stack (); + Scanner tokens = new Scanner(exp); + + while (tokens.hasNext()) { + if (tokens.hasNextInt()) { + s.push(tokens.nextInt()); // If int then push to stack + } else { // else pop top two values and perform the operation + int num2 = s.pop(); + int num1 = s.pop(); + String op = tokens.next(); + + if (op.equals("+")) { + s.push(num1 + num2); + } else if (op.equals("-")) { + s.push(num1 - num2); + } else if (op.equals("*")) { + s.push(num1 * num2); + } else { + s.push(num1 / num2); + } + + // "+", "-", "*", "/" + } + } + return s.pop(); + } +} From 85771ea74fdeaf7756b87c7fadcb7884ac6a7a32 Mon Sep 17 00:00:00 2001 From: feng liu Date: Sun, 1 Oct 2017 17:26:05 -0600 Subject: [PATCH 17/34] prevent (ub+lb) overflow --- Searches/BinarySearch.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Searches/BinarySearch.java b/Searches/BinarySearch.java index 3949f6a8..535442c8 100644 --- a/Searches/BinarySearch.java +++ b/Searches/BinarySearch.java @@ -23,7 +23,7 @@ class BinarySearch if ( lb > ub) return -1; - int mid = (ub+lb)/2; + int mid = (ub+lb) >>> 1; int comp = key.compareTo(array[mid]); if (comp < 0) From 79a73aebb41df045c8aed286171b8051cd886554 Mon Sep 17 00:00:00 2001 From: feng liu Date: Sun, 1 Oct 2017 17:45:16 -0600 Subject: [PATCH 18/34] KMP algorithm --- Misc/KMP.java | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Misc/KMP.java diff --git a/Misc/KMP.java b/Misc/KMP.java new file mode 100644 index 00000000..c97c248b --- /dev/null +++ b/Misc/KMP.java @@ -0,0 +1,55 @@ + +/* +Implementation of Knuth–Morris–Pratt algorithm +Usage: +final String T = "AAAAABAAABA"; +final String P = "AAAA"; +KMPmatcher(T, P); +*/ +public class KMP { + + // find the starting index in string T[] that matches the search word P[] + public void KMPmatcher(final String T, final String P) { + final int m = T.length(); + final int n = P.length(); + final int[] pi = computePrefixFunction(P); + int q = 0; + for (int i = 0; i < m; i++) { + while (q > 0 && T.charAt(i) != P.charAt(q)) { + q = pi[q - 1]; + } + + if (T.charAt(i) == P.charAt(q)) { + q++; + } + + if (q == n) { + System.out.println("Pattern starts: " + (i + 1 - n)); + q = pi[q - 1]; + } + } + + } + + // return the prefix function + private int[] computePrefixFunction(final String P) { + final int n = P.length(); + final int[] pi = new int[n]; + pi[0] = 0; + int q = 0; + for (int i = 1; i < n; i++) { + while (q > 0 && P.charAt(q) != P.charAt(i)) { + q = pi[q - 1]; + } + + if (P.charAt(q) == P.charAt(i)) { + q++; + } + + pi[i] = q; + + } + + return pi; + } +} From 12b86774ad06857051e687fe85521de1fd29c70f Mon Sep 17 00:00:00 2001 From: Deepak Date: Mon, 2 Oct 2017 12:53:43 +0530 Subject: [PATCH 19/34] add FibToN print all fibonacci till N in O(n) --- Misc/FibToN | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Misc/FibToN diff --git a/Misc/FibToN b/Misc/FibToN new file mode 100644 index 00000000..4d002b8f --- /dev/null +++ b/Misc/FibToN @@ -0,0 +1,21 @@ +import java.util.Scanner; + +public class FibToN { + + public static void main(String[] args) { + Scanner scn = new Scanner(System.in); + + int n = scn.nextInt(); + + int fn = 0, sn = 1; + + while(fn <= n){ + System.out.println(fn); + + int next = fn + sn; + fn = sn; + sn = next; + } + } + +} From 355d4c1fee55b176f8309b1558b69f858329b8f5 Mon Sep 17 00:00:00 2001 From: Deepak Date: Mon, 2 Oct 2017 13:00:39 +0530 Subject: [PATCH 20/34] added precision_root_algo --- Misc/root_precision | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Misc/root_precision diff --git a/Misc/root_precision b/Misc/root_precision new file mode 100644 index 00000000..1b5e9547 --- /dev/null +++ b/Misc/root_precision @@ -0,0 +1,28 @@ +import java.io.*; +import java.util.*; +import java.text.*; +import java.math.*; +import java.util.regex.*; + +public class Solution { + + public static void main(String[] args) { + Scanner scn = new Scanner(System.in); + + int N = scn.nextInt(); + int P = scn.nextInt(); + + System.out.println(squareRoot(N, P)); + } + + public static double squareRoot(int N, int P) { + double sqrt = 0;; + + // Write your code here + double root = Math.pow(N, 0.5); + int pre = (int) Math.pow(10, P); + root = root * pre; + sqrt = (int)root; + return (double)sqrt/pre; + } +} From f2bfa6a1be8c028e732e01add8ce735bbacd3f56 Mon Sep 17 00:00:00 2001 From: Pusty Date: Tue, 3 Oct 2017 03:57:28 +0200 Subject: [PATCH 21/34] crc32 implementation --- Misc/crc32.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Misc/crc32.java diff --git a/Misc/crc32.java b/Misc/crc32.java new file mode 100644 index 00000000..443b70ba --- /dev/null +++ b/Misc/crc32.java @@ -0,0 +1,28 @@ +import java.util.BitSet; + +//Generates a crc32 checksum for a given string or byte array +public class crc32 { + + public static void main(String[] args) { + System.out.println(Integer.toHexString(crc32("Hello World"))); + } + + public static int crc32(String str) { + return crc32(str.getBytes()); + } + + public static int crc32(byte[] data) { + BitSet bitSet = BitSet.valueOf(data); + int crc32 = 0xFFFFFFFF; //initial value + for(int i=0;i>>31)&1) != (bitSet.get(i)?1:0)) + crc32 = (crc32 << 1) ^ 0x04C11DB7; //xoring with polynomial + else + crc32 = (crc32 << 1); + } + crc32 = crc32 ^ 0; + crc32 = Integer.reverse(crc32); //result reflect + return crc32 ^ 0xFFFFFFFF; //final xor value + } + +} From db7300f19189d5ee4757fdba9f860be64d7ff5be Mon Sep 17 00:00:00 2001 From: Pusty Date: Tue, 3 Oct 2017 04:27:09 +0200 Subject: [PATCH 22/34] fixed a small mistake --- Misc/crc32.java | 1 - 1 file changed, 1 deletion(-) diff --git a/Misc/crc32.java b/Misc/crc32.java index 443b70ba..e27c2470 100644 --- a/Misc/crc32.java +++ b/Misc/crc32.java @@ -20,7 +20,6 @@ public class crc32 { else crc32 = (crc32 << 1); } - crc32 = crc32 ^ 0; crc32 = Integer.reverse(crc32); //result reflect return crc32 ^ 0xFFFFFFFF; //final xor value } From b7a9d0de4435594e40fad18a6db1b406830430fa Mon Sep 17 00:00:00 2001 From: Deepak Date: Tue, 3 Oct 2017 08:28:04 +0530 Subject: [PATCH 23/34] rename --- Misc/{root_precision => root_precision.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/{root_precision => root_precision.java} (100%) diff --git a/Misc/root_precision b/Misc/root_precision.java similarity index 100% rename from Misc/root_precision rename to Misc/root_precision.java From 0afa2da3cef1778d88d24645cdf6009519846131 Mon Sep 17 00:00:00 2001 From: Deepak Date: Tue, 3 Oct 2017 08:32:14 +0530 Subject: [PATCH 24/34] rename .java extension added --- Misc/{FibToN => FibToN.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/{FibToN => FibToN.java} (100%) diff --git a/Misc/FibToN b/Misc/FibToN.java similarity index 100% rename from Misc/FibToN rename to Misc/FibToN.java From 455876723ba30fcec77898ef2cb918494ada1b88 Mon Sep 17 00:00:00 2001 From: Deepak Date: Tue, 3 Oct 2017 09:38:34 +0530 Subject: [PATCH 25/34] add 0-1 knapsack Very frequently asked DP problem --- Dynamic Programming/Knapsack.java | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Dynamic Programming/Knapsack.java diff --git a/Dynamic Programming/Knapsack.java b/Dynamic Programming/Knapsack.java new file mode 100644 index 00000000..ea84167b --- /dev/null +++ b/Dynamic Programming/Knapsack.java @@ -0,0 +1,38 @@ +// A Dynamic Programming based solution for 0-1 Knapsack problem + +public class Knapsack +{ + + private static int knapSack(int W, int wt[], int val[], int n) + { + int i, w; + int rv[][] = new int[n+1][W+1]; //rv means return value + + // Build table rv[][] in bottom up manner + for (i = 0; i <= n; i++) + { + for (w = 0; w <= W; w++) + { + if (i==0 || w==0) + rv[i][w] = 0; + else if (wt[i-1] <= w) + rv[i][w] = Math.max(val[i-1] + rv[i-1][w-wt[i-1]], rv[i-1][w]); + else + rv[i][w] = rv[i-1][w]; + } + } + + return rv[n][W]; + } + + + // Driver program to test above function + public static void main(String args[]) + { + int val[] = new int[]{50, 100, 130}; + int wt[] = new int[]{10, 20, 40}; + int W = 50; + int n = val.length; + System.out.println(knapSack(W, wt, val, n)); + } +} From b67efdf79e40e606a10fd11eea0238a5975e71da Mon Sep 17 00:00:00 2001 From: Deepak Date: Tue, 3 Oct 2017 10:16:32 +0530 Subject: [PATCH 26/34] rodcuting dp approach A Dynamic Programming solution for Rod cutting problem --- Dynamic Programming/rod_cutting.java | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Dynamic Programming/rod_cutting.java diff --git a/Dynamic Programming/rod_cutting.java b/Dynamic Programming/rod_cutting.java new file mode 100644 index 00000000..3424afac --- /dev/null +++ b/Dynamic Programming/rod_cutting.java @@ -0,0 +1,33 @@ +/* A Dynamic Programming solution for Rod cutting problem + Returns the best obtainable price for a rod of + length n and price[] as prices of different pieces */ + +public class RodCutting +{ + + private static int cutRod(int price[],int n) + { + int val[] = new int[n+1]; + val[0] = 0; + + for (int i = 1; i<=n; i++) + { + int max_val = Integer.MIN_VALUE; + for (int j = 0; j < i; j++) + max_val = Math.max(max_val,price[j] + val[i-j-1]); + + val[i] = max_val; + } + + return val[n]; + } + + //main function to test + public static void main(String args[]) + { + int arr[] = new int[] {2, 5, 13, 19, 20}; + int size = arr.length; + System.out.println("Maximum Obtainable Value is " + + cutRod(arr, size)); + } +} From 79e2eb2826c8819f80bb006f05edbe927cdd69bd Mon Sep 17 00:00:00 2001 From: Balance-Breaker Date: Tue, 3 Oct 2017 12:38:23 +0530 Subject: [PATCH 27/34] Removed unrequired Package --- ...fHanoiUsingRecursion => TowerOfHanoiUsingRecursion.java} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename Misc/{TowerOfHanoiUsingRecursion => TowerOfHanoiUsingRecursion.java} (95%) diff --git a/Misc/TowerOfHanoiUsingRecursion b/Misc/TowerOfHanoiUsingRecursion.java similarity index 95% rename from Misc/TowerOfHanoiUsingRecursion rename to Misc/TowerOfHanoiUsingRecursion.java index 60778fc9..b5ee7ad7 100644 --- a/Misc/TowerOfHanoiUsingRecursion +++ b/Misc/TowerOfHanoiUsingRecursion.java @@ -1,8 +1,6 @@ -package com.manmeet; - import java.util.Scanner; -public class TowerOfHanoi +class TowerOfHanoi { public static void shift(int n, String startPole, String intermediatePole, String endPole) { @@ -10,6 +8,8 @@ public class TowerOfHanoi { return; } + + // Shift function is called in recursion for swapping the n-1 disc from the startPole to the intermediatePole shift(n - 1, startPole, endPole, intermediatePole); System.out.println("\nMove \"" + n + "\" from " + startPole + " --> " + endPole); // Result Printing From 32d03516fe647d47655e16676bc114a9428083cd Mon Sep 17 00:00:00 2001 From: Deepak Date: Tue, 3 Oct 2017 13:12:50 +0530 Subject: [PATCH 28/34] update added comments and variable names are more simpler now --- Misc/root_precision.java | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Misc/root_precision.java b/Misc/root_precision.java index 1b5e9547..0ae00de0 100644 --- a/Misc/root_precision.java +++ b/Misc/root_precision.java @@ -7,22 +7,27 @@ import java.util.regex.*; public class Solution { public static void main(String[] args) { + //take input Scanner scn = new Scanner(System.in); - int N = scn.nextInt(); - int P = scn.nextInt(); + int N = scn.nextInt(); //N is the input number + int P = scn.nextInt(); //P is precision value for eg - P is 3 in 2.564 and 5 in 3.80870. System.out.println(squareRoot(N, P)); } public static double squareRoot(int N, int P) { - double sqrt = 0;; - - // Write your code here - double root = Math.pow(N, 0.5); - int pre = (int) Math.pow(10, P); - root = root * pre; - sqrt = (int)root; - return (double)sqrt/pre; + double rv = 0; //rv means return value + + double root = Math.pow(N, 0.5); + + //calculate precision to power of 10 and then multiply it with root value. + int precision = (int) Math.pow(10, P); + root = root * precision; + /*typecast it into integer then divide by precision and again typecast into double + so as to have decimal points upto P precision */ + + rv = (int)root; + return (double)rv/precision; } } From 9b4ae39291b63cb465748406e34b5de241beaf39 Mon Sep 17 00:00:00 2001 From: Deepak Date: Tue, 3 Oct 2017 13:25:06 +0530 Subject: [PATCH 29/34] updated added comments and changed variable names for better understanding --- Misc/FibToN.java | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Misc/FibToN.java b/Misc/FibToN.java index 4d002b8f..e7b7a9a2 100644 --- a/Misc/FibToN.java +++ b/Misc/FibToN.java @@ -3,18 +3,21 @@ import java.util.Scanner; public class FibToN { public static void main(String[] args) { + //take input Scanner scn = new Scanner(System.in); + int N = scn.nextInt(); + // print fibonacci sequence less than N + int first = 0, second = 1; + //first fibo and second fibonacci are 0 and 1 respectively - int n = scn.nextInt(); - - int fn = 0, sn = 1; - - while(fn <= n){ - System.out.println(fn); + while(first <= N){ + //print first fibo 0 then add second fibo into it while updating second as well - int next = fn + sn; - fn = sn; - sn = next; + System.out.println(first); + + int next = first+ second; + first = second; + second = next; } } From edde956625de87c6426796d87364c30808cf8a27 Mon Sep 17 00:00:00 2001 From: Oskar Enmalm Date: Tue, 3 Oct 2017 10:02:42 +0200 Subject: [PATCH 30/34] Create GCD.java --- Misc/GCD.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Misc/GCD.java diff --git a/Misc/GCD.java b/Misc/GCD.java new file mode 100644 index 00000000..dba60c68 --- /dev/null +++ b/Misc/GCD.java @@ -0,0 +1,15 @@ +//Oskar Enmalm 3/10/17 +//This is Euclid's algorithm which is used to find the greatest common denominator + +public class GCD{ + +public static int gcd(int a, int b) { + + int r = a % b; + while (r != 0) { + b = r; + r = b % r; + } + return b; + } +} From ae3342fb50eb5f4a2b973356e8685fb3ac57543a Mon Sep 17 00:00:00 2001 From: Manmeet Singh Date: Tue, 3 Oct 2017 16:51:42 +0530 Subject: [PATCH 31/34] Added Extension --- Misc/{StackPostfixNotation => StackPostfixNotation.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Misc/{StackPostfixNotation => StackPostfixNotation.java} (100%) diff --git a/Misc/StackPostfixNotation b/Misc/StackPostfixNotation.java similarity index 100% rename from Misc/StackPostfixNotation rename to Misc/StackPostfixNotation.java From 7bb187bca15e942c1f1ef899ca729bdb90da8a5b Mon Sep 17 00:00:00 2001 From: Michael Rolland Date: Tue, 3 Oct 2017 15:59:12 -0400 Subject: [PATCH 32/34] Make it actually a java file Oops --- Conversions/{AnyBaseToAnyBase => AnyBaseToAnyBase.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Conversions/{AnyBaseToAnyBase => AnyBaseToAnyBase.java} (100%) diff --git a/Conversions/AnyBaseToAnyBase b/Conversions/AnyBaseToAnyBase.java similarity index 100% rename from Conversions/AnyBaseToAnyBase rename to Conversions/AnyBaseToAnyBase.java From 2128c7a15dc757c20af73dfd54f64da6febe789d Mon Sep 17 00:00:00 2001 From: "DESKTOP-0VAEMFL\\joaom" <1160907@isep.ipp.pt> Date: Thu, 5 Oct 2017 22:37:13 +0100 Subject: [PATCH 33/34] Columnar Transposition Cipher with example usage --- ciphers/ColumnarTranspositionCipher.java | 169 +++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 ciphers/ColumnarTranspositionCipher.java diff --git a/ciphers/ColumnarTranspositionCipher.java b/ciphers/ColumnarTranspositionCipher.java new file mode 100644 index 00000000..b1274371 --- /dev/null +++ b/ciphers/ColumnarTranspositionCipher.java @@ -0,0 +1,169 @@ +/** + * Columnar Transposition Cipher Encryption and Decryption. + * @author freitzzz + */ +public class ColumnarTranspositionCipher { + private static String keyword; + private static Object[][] table; + private static String abecedarium; + public static final String ABECEDARIUM="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.;:-@"; + private static final String ENCRYPTION_FIELD="≈"; + private static final char ENCRYPTION_FIELD_CHAR='≈'; + /** + * Encrypts a certain String with the Columnar Transposition Cipher Rule + * @param word Word being encrypted + * @param keyword String with keyword being used + * @return a String with the word encrypted by the Columnar Transposition Cipher Rule + */ + public static String encrpyter(String word,String keyword){ + ColumnarTranspositionCipher.keyword=keyword; + abecedariumBuilder(500); + table=tableBuilder(word); + Object[][] sortedTable=sortTable(table); + String wordEncrypted=""; + for(int i=0;iword.length()/keyword.length()){ + return (word.length()/keyword.length())+1; + }else{ + return word.length()/keyword.length(); + } + } + private static Object[] findElements(){ + Object[] charValues=new Object[keyword.length()]; + for(int i=0;i(int)table[0][j]){ + Object[] column=getColumn(tableSorted,tableSorted.length,i); + switchColumns(tableSorted,j,i,column); + } + } + } + return tableSorted; + } + private static Object[] getColumn(Object[][] table,int rows,int column){ + Object[] columnArray=new Object[rows]; + for(int i=0;i>> "+wordBeingEncrypted); + System.out.println("Word encrypted ->>> "+ColumnarTranspositionCipher.encrpyter(wordBeingEncrypted,keywordForExample)); + System.out.println("Word decryped ->>> "+ColumnarTranspositionCipher.decrypter()); + System.out.println("\n### Encrypted Table ###"); + showTable(); + } +} From a9e8b6b1001832861c063b18ad1165b1bbe4be99 Mon Sep 17 00:00:00 2001 From: KyleScharnhorst Date: Sat, 7 Oct 2017 20:18:35 -0700 Subject: [PATCH 34/34] Add: level order traversal. Prints each level of the bst. I.e., Root -> its children -> its children's children -> and so on... --- data_structures/Trees/TreeTraversal.java | 30 +++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/data_structures/Trees/TreeTraversal.java b/data_structures/Trees/TreeTraversal.java index c4ff92d0..8dd0555a 100644 --- a/data_structures/Trees/TreeTraversal.java +++ b/data_structures/Trees/TreeTraversal.java @@ -3,6 +3,7 @@ * @author Varun Upadhyay (https://github.com/varunu28) * */ +import java.util.LinkedList; // Driver Program public class TreeTraversal { @@ -10,18 +11,25 @@ public class TreeTraversal { Node tree = new Node(5); tree.insert(3); tree.insert(7); + tree.insert(1); + tree.insert(9); - // Prints 3 5 7 + // Prints 1 3 5 7 9 tree.printInOrder(); System.out.println(); - // Prints 5 3 7 + // Prints 5 3 1 7 9 tree.printPreOrder(); System.out.println(); - // Prints 3 7 5 + // Prints 1 3 9 7 5 tree.printPostOrder(); System.out.println(); + + // Add a couple more nodes for print level test + // Print 5 3 7 1 9 + tree.printLevelOrder(); + System.out.println(); } } @@ -31,6 +39,7 @@ public class TreeTraversal { * printInOrder: LEFT -> ROOT -> RIGHT * printPreOrder: ROOT -> LEFT -> RIGHT * printPostOrder: LEFT -> RIGHT -> ROOT + * printLevelOrder: ROOT -> ROOT's CHILDREN -> ROOT's CHILDREN's CHILDREN -> etc */ class Node { Node left, right; @@ -88,5 +97,20 @@ class Node { } System.out.print(data + " "); } + + public void printLevelOrder() { + LinkedList queue = new LinkedList<>(); + queue.add(this); + while(!queue.isEmpty()) { + Node n = queue.poll(); + System.out.print(n.data + " "); + if (n.left != null) { + queue.add(n.left); + } + if (n.right != null) { + queue.add(n.right); + } + } + } }