Merge remote-tracking branch 'upstream/master'

This commit is contained in:
sahilb2 2018-01-02 18:51:27 -08:00
commit d6a5572aa1
23 changed files with 128 additions and 121 deletions

View File

@ -3,7 +3,7 @@ import java.util.*;
import java.util.Scanner; import java.util.Scanner;
import javax.swing.*; import javax.swing.*;
public class HexaToBin { public class HexaDecimalToBinary {
private final int LONG_BITS = 8; private final int LONG_BITS = 8;
@ -28,10 +28,10 @@ public class HexaToBin {
//Testing Numbers: //Testing Numbers:
String[] hexNums = {"1", "A1", "ef", "BA", "AA", "BB", String[] hexNums = {"1", "A1", "ef", "BA", "AA", "BB",
"19", "01", "02", "03", "04"}; "19", "01", "02", "03", "04"};
Convert objConvert = new Convert(); HexaDecimalToBinary objConvert = new HexaDecimalToBinary();
for (String num : hexNums) { for (String num : hexNums) {
objConvert.convert(num); objConvert.convert(num);
} }
} }
} }

View File

@ -7,42 +7,41 @@ import java.util.Scanner;
* *
*/ */
public class OctalToDecimal { public class OctalToDecimal {
/** /**
* Main method * Main method
* *
* @param args Command line arguments * @param args
* Command line arguments
*/ */
public static void main(String args[]) { public static void main(String args[]) {
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
int o = sc.nextInt(); System.out.print("Octal Input: ");
System.out.println("Decimal equivalent: " + convertOctalToDecimal(o)); String inputOctal = sc.nextLine();
int result = convertOctalToDecimal(inputOctal);
if (result != -1)
System.out.println("Result convertOctalToDecimal : " + result);
sc.close(); sc.close();
} }
/** /**
* This method converts an octal number to * This method converts an octal number to a decimal number.
* a decimal number.
* *
* @param o The octal number * @param inputOctal
* The octal number
* @return The decimal number * @return The decimal number
*/ */
public static int convertOctalToDecimal(int o) { public static int convertOctalToDecimal(String inputOctal) {
System.out.print("Octal Input: ");
// Read the input from the console which we are expecting as an octal number: try {
Scanner s = new Scanner(System.in);
String inputHex = s.nextLine();
try{
// Actual conversion of Octal to Decimal: // Actual conversion of Octal to Decimal:
Integer outputDecimal = Integer.parseInt(inputHex, 8); Integer outputDecimal = Integer.parseInt(inputOctal, 8);
System.out.println("Decimal Equivalent : " + outputDecimal); return outputDecimal;
} } catch (NumberFormatException ne) {
catch(NumberFormatException ne){ // Printing a warning message if the input is not a valid octal
// Printing a warning message if the input is not a valid octal number: // number:
System.out.println("Invalid Input, Expecting octal number 0-7"); System.out.println("Invalid Input, Expecting octal number 0-7");
} return -1;
finally{
s.close();
} }
} }
} }

View File

@ -6,7 +6,7 @@ import java.util.*;
* @author Unknown * @author Unknown
* *
*/ */
public class bfs{ public class BFS{
/** /**
* The BFS implemented in code to use. * The BFS implemented in code to use.

View File

@ -7,7 +7,7 @@ import java.util.*;
* *
*/ */
public class dfs{ public class DFS{
/** /**
* Implementation in code of a DFS * Implementation in code of a DFS

View File

@ -120,7 +120,7 @@ class AdjacencyMatrixGraph {
} }
/** /**
* this gives a list of verticies in the graph and their adjacencies * this gives a list of vertices in the graph and their adjacencies
* *
* @return returns a string describing this graph * @return returns a string describing this graph
*/ */

View File

@ -100,7 +100,7 @@ class PrimMST
| / \ | | / \ |
(3)-------(4) (3)-------(4)
9 */ 9 */
MST t = new MST(); PrimMST t = new PrimMST();
int graph[][] = new int[][] {{0, 2, 0, 6, 0}, int graph[][] = new int[][] {{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5}, {2, 0, 3, 8, 5},
{0, 3, 0, 0, 7}, {0, 3, 0, 0, 7},

View File

@ -1,4 +1,4 @@
public class AVLtree { public class AVLTree {
private Node root; private Node root;
@ -200,7 +200,7 @@ public class AVLtree {
} }
public static void main(String[] args) { public static void main(String[] args) {
AVLtree tree = new AVLtree(); AVLTree tree = new AVLTree();
System.out.println("Inserting values 1 to 10"); System.out.println("Inserting values 1 to 10");
for (int i = 1; i < 10; i++) for (int i = 1; i < 10; i++)

View File

@ -9,12 +9,12 @@ class Node
} }
} }
class BinaryTree public class LevelOrderTraversal
{ {
// Root of the Binary Tree // Root of the Binary Tree
Node root; Node root;
public BinaryTree() public LevelOrderTraversal()
{ {
root = null; root = null;
} }
@ -65,7 +65,7 @@ class BinaryTree
/* Driver program to test above functions */ /* Driver program to test above functions */
public static void main(String args[]) public static void main(String args[])
{ {
BinaryTree tree = new BinaryTree(); LevelOrderTraversal tree = new LevelOrderTraversal();
tree.root= new Node(1); tree.root= new Node(1);
tree.root.left= new Node(2); tree.root.left= new Node(2);
tree.root.right= new Node(3); tree.root.right= new Node(3);

View File

@ -14,7 +14,7 @@ class Node {
} }
/* Class to print Level Order Traversal */ /* Class to print Level Order Traversal */
class BinaryTree { public class LevelOrderTraversalQueue {
Node root; Node root;
@ -49,7 +49,7 @@ class BinaryTree {
{ {
/* creating a binary tree and entering /* creating a binary tree and entering
the nodes */ the nodes */
BinaryTree tree_level = new BinaryTree(); LevelOrderTraversalQueue tree_level = new LevelOrderTraversalQueue();
tree_level.root = new Node(1); tree_level.root = new Node(1);
tree_level.root.left = new Node(2); tree_level.root.left = new Node(2);
tree_level.root.right = new Node(3); tree_level.root.right = new Node(3);

View File

@ -78,7 +78,7 @@ class Tree
} }
// Driver class to test above methods // Driver class to test above methods
public class Main public class PrintTopViewofTree
{ {
public static void main(String[] args) public static void main(String[] args)
{ {

View File

@ -10,7 +10,7 @@ class Node
} }
} }
public class BinaryTree public class ValidBSTOrNot
{ {
//Root of the Binary Tree //Root of the Binary Tree
Node root; Node root;
@ -47,7 +47,7 @@ public class BinaryTree
/* Driver program to test above functions */ /* Driver program to test above functions */
public static void main(String args[]) public static void main(String args[])
{ {
BinaryTree tree = new BinaryTree(); ValidBSTOrNot tree = new ValidBSTOrNot();
tree.root = new Node(4); tree.root = new Node(4);
tree.root.left = new Node(2); tree.root.left = new Node(2);
tree.root.right = new Node(5); tree.root.right = new Node(5);

View File

@ -6,7 +6,7 @@
* *
*/ */
public class Levenshtein_distance{ public class LevenshteinDistance{
private static int minimum(int a, int b, int c){ private static int minimum(int a, int b, int c){
if(a < b && a < c){ if(a < b && a < c){
return a; return a;

View File

@ -1,4 +1,4 @@
public class HeapSort public class heap_sort
{ {
public void sort(int arr[]) public void sort(int arr[])
{ {
@ -64,7 +64,7 @@ public class HeapSort
int arr[] = {12, 11, 13, 5, 6, 7}; int arr[] = {12, 11, 13, 5, 6, 7};
int n = arr.length; int n = arr.length;
HeapSort ob = new HeapSort(); heap_sort ob = new heap_sort();
ob.sort(arr); ob.sort(arr);
System.out.println("Sorted array is"); System.out.println("Sorted array is");

View File

@ -0,0 +1,55 @@
import java.util.Scanner;
/**
*
* @author Nishita Aggarwal
*
* Brian Kernighans Algorithm
*
* algorithm to count the number of set bits in a given number
*
* Subtraction of 1 from a number toggles all the bits (from right to left) till the rightmost set bit(including the
* rightmost set bit).
* So if we subtract a number by 1 and do bitwise & with itself i.e. (n & (n-1)), we unset the rightmost set bit.
*
* If we do n & (n-1) in a loop and count the no of times loop executes we get the set bit count.
*
*
* Time Complexity: O(logn)
*
*/
public class BrianKernighanAlgorithm {
/**
* @param num: number in which we count the set bits
*
* @return int: Number of set bits
* */
static int countSetBits(int num)
{
int cnt = 0;
while(num != 0)
{
num = num & (num-1);
cnt++;
}
return cnt;
}
/**
*
* @param args : command line arguments
*
*/
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
int setBitCount = countSetBits(num);
System.out.println(setBitCount);
sc.close();
}
}

View File

@ -9,7 +9,7 @@ import java.util.Arrays;
import java.util.Scanner; import java.util.Scanner;
import java.util.Stack; import java.util.Stack;
public class Solution { public class Dijkshtra {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
Scanner in =new Scanner(System.in); Scanner in =new Scanner(System.in);

View File

@ -9,7 +9,7 @@ public class FibToN {
// print fibonacci sequence less than N // print fibonacci sequence less than N
int first = 0, second = 1; int first = 0, second = 1;
//first fibo and second fibonacci are 0 and 1 respectively //first fibo and second fibonacci are 0 and 1 respectively
scn.close();
while(first <= N){ while(first <= N){
//print first fibo 0 then add second fibo into it while updating second as well //print first fibo 0 then add second fibo into it while updating second as well

View File

@ -1,45 +0,0 @@
/**
* The Sieve of Eratosthenes is an algorithm use to find prime numbers,
* up to a given value.
* Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
* (This illustration is also in the github repository)
*
* @author Unknown
*
*/
public class FindingPrimes{
/**
* The Main method
*
* @param args Command line arguments
*/
public static void main(String args[]){
SOE(20); //Example: Finds all the primes up to 20
}
/**
* The method implementing the Sieve of Eratosthenes
*
* @param n Number to perform SOE on
*/
public static void SOE(int n){
boolean sieve[] = new boolean[n];
int check = (int)Math.round(Math.sqrt(n)); //No need to check for multiples past the square root of n
sieve[0] = false;
sieve[1] = false;
for(int i = 2; i < n; i++)
sieve[i] = true; //Set every index to true except index 0 and 1
for(int i = 2; i< check; i++){
if(sieve[i]==true) //If i is a prime
for(int j = i+i; j < n; j+=i) //Step through the array in increments of i(the multiples of the prime)
sieve[j] = false; //Set every multiple of i to false
}
for(int i = 0; i< n; i++){
if(sieve[i]==true)
System.out.print(i+" "); //In this example it will print 2 3 5 7 11 13 17 19
}
}
}

View File

@ -6,7 +6,7 @@ class FloydTriangle {
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows which you want in your Floyd Triangle: "); System.out.println("Enter the number of rows which you want in your Floyd Triangle: ");
int r = sc.nextInt(), n = 0; int r = sc.nextInt(), n = 0;
sc.close();
for(int i=0; i < r; i++) { for(int i=0; i < r; i++) {
for(int j=0; j <= i; j++) { for(int j=0; j <= i; j++) {
System.out.print(++n + " "); System.out.print(++n + " ");

View File

@ -1,5 +1,5 @@
import java.util.*; import java.util.*;
public class Array { public class InsertDeleteInArray {
public static void main(String[] args) { public static void main(String[] args) {
Scanner s = new Scanner(System.in); // Input statement Scanner s = new Scanner(System.in); // Input statement

View File

@ -4,7 +4,7 @@ import java.text.*;
import java.math.*; import java.math.*;
import java.util.regex.*; import java.util.regex.*;
public class Solution { public class RootPrecision {
public static void main(String[] args) { public static void main(String[] args) {
//take input //take input

View File

@ -1,6 +1,6 @@
import java.util.*; import java.util.*;
public class Postfix { public class StackPostfixNotation {
public static void main(String[] args) { public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
String post = scanner.nextLine(); // Takes input with spaces in between eg. "1 21 +" String post = scanner.nextLine(); // Takes input with spaces in between eg. "1 21 +"

View File

@ -7,7 +7,7 @@ import java.util.Scanner;
* @author Marcus * @author Marcus
* *
*/ */
class CountTheWords{ public class countwords{
public static void main(String[] args){ public static void main(String[] args){
Scanner input = new Scanner(System.in); Scanner input = new Scanner(System.in);

View File

@ -1,30 +1,28 @@
import java.util.Scanner; import java.util.Scanner;
class krishnamurthy class krishnamurthy {
{ static int fact(int n) {
int fact(int n) int i, p = 1;
{ for (i = n; i >= 1; i--)
int i,p=1; p = p * i;
for(i=n;i>=1;i--) return p;
p=p*i; }
return p;
} public static void main(String args[]) {
public static void main(String args[]) Scanner sc = new Scanner(System.in);
{ int a, b, s = 0;
Scanner sc=new Scanner(System.in); System.out.print("Enter the number : ");
int a,b,s=0; a = sc.nextInt();
System.out.print("Enter the number : "); int n = a;
a=sc.nextInt(); while (a > 0) {
int n=a; b = a % 10;
while(a>0) s = s + fact(b);
{ a = a / 10;
b=a%10; }
s=s+fact(b); if (s == n)
a=a/10; System.out.print(n + " is a krishnamurthy number");
} else
if(s==n) System.out.print(n + " is not a krishnamurthy number");
System.out.print(n+" is a krishnamurthy number"); sc.close();
else }
System.out.print(n+" is not a krishnamurthy number");
}
} }