From eda59f4223acfb1a60ec50f15ebcf22b5589ea83 Mon Sep 17 00:00:00 2001 From: Het Patel <96062726+IamHetPatel@users.noreply.github.com> Date: Sat, 15 Oct 2022 00:28:00 +0530 Subject: [PATCH 1/3] Create numtoword --- Java/numtoword | 138 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 Java/numtoword diff --git a/Java/numtoword b/Java/numtoword new file mode 100644 index 0000000..dd7b698 --- /dev/null +++ b/Java/numtoword @@ -0,0 +1,138 @@ +package com.Java; + +import java.text.DecimalFormat; + +public class EnglishNumberToWords { + + private static final String[] tensNames = { + "", + " ten", + " twenty", + " thirty", + " forty", + " fifty", + " sixty", + " seventy", + " eighty", + " ninety" + }; + + private static final String[] numNames = { + "", + " one", + " two", + " three", + " four", + " five", + " six", + " seven", + " eight", + " nine", + " ten", + " eleven", + " twelve", + " thirteen", + " fourteen", + " fifteen", + " sixteen", + " seventeen", + " eighteen", + " nineteen" + }; + + private EnglishNumberToWords() {} + + private static String convertLessThanOneThousand(int number) { + String soFar; + + if (number % 100 < 20){ + soFar = numNames[number % 100]; + number /= 100; + } + else { + soFar = numNames[number % 10]; + number /= 10; + + soFar = tensNames[number % 10] + soFar; + number /= 10; + } + if (number == 0) return soFar; + return numNames[number] + " hundred" + soFar; + } + + + public static String convert(long number) { + if (number == 0) { return "zero"; } + + String snumber; + + String mask = "000000000000"; + DecimalFormat df = new DecimalFormat(mask); + snumber = df.format(number); + + int billions = Integer.parseInt(snumber.substring(0,3)); + + int millions = Integer.parseInt(snumber.substring(3,6)); + + int hundredThousands = Integer.parseInt(snumber.substring(6,9)); + + int thousands = Integer.parseInt(snumber.substring(9,12)); + + String tradBillions; + switch (billions) { + case 0: + tradBillions = ""; + break; + case 1 : + tradBillions = convertLessThanOneThousand(billions) + + " billion "; + break; + default : + tradBillions = convertLessThanOneThousand(billions) + + " billion "; + } + String result = tradBillions; + + String tradMillions; + switch (millions) { + case 0: + tradMillions = ""; + break; + default : + tradMillions = convertLessThanOneThousand(millions) + + " million "; + } + result = result + tradMillions; + + String tradHundredThousands; + switch (hundredThousands) { + case 0: + tradHundredThousands = ""; + break; + case 1 : + tradHundredThousands = "one thousand "; + break; + default : + tradHundredThousands = convertLessThanOneThousand(hundredThousands) + + " thousand "; + } + result = result + tradHundredThousands; + + String tradThousand; + tradThousand = convertLessThanOneThousand(thousands); + result = result + tradThousand; + return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " "); + } + + /** + * testing + * @param args + */ + public static void main(String[] args) { + System.out.println(EnglishNumberToWords.convert(7)); + System.out.println(EnglishNumberToWords.convert(297)); + System.out.println(EnglishNumberToWords.convert(2147483647)); + System.out.println(EnglishNumberToWords.convert(3000000010L)); + + } +} From 44cbda26529f76588af8b0c018f017234f73fb0a Mon Sep 17 00:00:00 2001 From: Het Patel <96062726+IamHetPatel@users.noreply.github.com> Date: Sat, 15 Oct 2022 00:41:09 +0530 Subject: [PATCH 2/3] Create maximumpathsum.java Added a very important all-in-one DP question with explanation. Kindly accept --- Java/maximumpathsum.java | 95 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 Java/maximumpathsum.java diff --git a/Java/maximumpathsum.java b/Java/maximumpathsum.java new file mode 100644 index 0000000..fa5477c --- /dev/null +++ b/Java/maximumpathsum.java @@ -0,0 +1,95 @@ +//question is to find maximum path to travel only in downward or right direction in a 2D array and reach at any one of the right or bottom edge of the array. +//using this code, maximum path from top to any point bottom can also be implemented +//also only 1D dynamic array is required to store a data in this optimized solution + +import java.util.Arrays; + +public class maximumpathsum { + public static int maximum(int a,int b){ + int maxint; + if(a>b){maxint = a; + } + else{maxint = b; + } + return maxint; + } + + public static void main(String[] args) { + int[][] arr = { + {-1,7,-2,10,-5}, + {8,-4,3,-6,0}, + {5,1,5,6,-5}, + {-7,-4,1,-4,8}, + {7,1,-9,4,0} + }; + int max = Integer.MIN_VALUE; + int n = arr.length-1; + int[] arr2 = new int[n+1]; + + //we have taken only a 1-D array for storing the max values of each index since + //since we require only the right and the down element and then it becomes useless + //so the previous elements of a 2D DP array would be a garbage stored instead of which + //we take 1D array and keep on replacing with new required elements each time + + //we traverse doing a bottom up approach since + //I found it easy as the end is fixed at any of the right or bottom edge of array given + for (int j = n; j >= 0; j--) { + for (int i = n; i >= 0; i--) { + //condition for the last most element of the 2D array + if(j==n && i==n){ + arr2[i]=arr[j][i]; + max = maximum(arr2[i],max); + continue; + } + + //condition for the elements of the last row + if(j == n && i arr[j][i]){ + arr2[i]= arr2[i+1]+arr[j][i]; + } + else{ + arr2[i] = arr[j][i]; + } + max = maximum(arr2[i],max); + continue; + } + + //condition for the elements of the last column + if(i == n && j arr[j][i]){ + arr2[i]= arr2[i]+arr[j][i]; + } + else{ + arr2[i] = arr[j][i]; + } + max = maximum(arr2[i],max); + continue; + } + + //condition for elements NOT at any right or bottom edges of the 2D array + if(i arr2[i+1]+arr[j][i]){ + arr2[i] = arr2[i]+arr[j][i]; + } + //the element would have to compulsorily go in any one of right or down direction + //since the program ends when it reaches the edge + else{ + arr2[i] = arr2[i+1]+arr[j][i]; + } + + max = maximum(arr2[i],max); + continue; + } + } + } + + System.out.println("The Inputted array was : "); + for (int i = 0; i < arr.length; i++) { + System.out.println(Arrays.toString(arr[i])); + } + + System.out.println("\n"); + System.out.println("The maximum length path is: "+max); + //we stored the max value of the maximum length of the path throughout the array given + } +} From 79a0a71755af7528c0fe1ae5bb89e745d127af48 Mon Sep 17 00:00:00 2001 From: Het Patel <96062726+IamHetPatel@users.noreply.github.com> Date: Fri, 21 Oct 2022 21:46:19 +0530 Subject: [PATCH 3/3] Create huffmanEncoder.java huffman encoding algorithm in java --- Java/huffmanEncoder.java | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Java/huffmanEncoder.java diff --git a/Java/huffmanEncoder.java b/Java/huffmanEncoder.java new file mode 100644 index 0000000..a54d22a --- /dev/null +++ b/Java/huffmanEncoder.java @@ -0,0 +1,65 @@ +import java.util.Comparator; +import java.util.PriorityQueue; + +public class huffmanAlgo { + + static class Node { + int data; + char ch; + Node left; + Node right; + + public Node(int data) { + this.data = data; + } + + public Node(int data, char ch) { + this.data = data; + this.ch = ch; + } + } + + public static void printcode(Node n){ + printcode(n,""); + } + public static void printcode(Node n, String str) { //printing the encoded huffman code + if (n.left == null && n.right == null)//always only the leaf node will have a character stored + { + System.out.println(n.ch + ":\t" + str); + return; + } + printcode(n.left, str + "0"); + //recursing ustil reaching the leaf + printcode(n.right, str + "1"); + } + static class comp implements Comparator { + public int compare(Node a, Node b) { + return a.data - b.data; + } + } + public static void main(String[] args) { + char[] arr = { 'h', 'e', 't', 'r', 'p', 'a' ,'l'}; + int[] freq = {1,2,5,8,11,20,21}; + //taking priorly sorted elements with their frequencies + + PriorityQueue q= new PriorityQueue<>(freq.length,new comp()); + int i = 0; + while(i