From 8f76df34bc743c34e36c2d09e361f114d1e10dd2 Mon Sep 17 00:00:00 2001 From: Shikhar Tyagi Date: Tue, 3 Oct 2017 21:10:08 +0530 Subject: [PATCH 1/6] Add Compression Algorithm --- .../bin/HEncoder$Node$Nodecomparator.class | Bin 0 -> 859 bytes Compression/bin/HEncoder$Node.class | Bin 0 -> 683 bytes Compression/bin/HEncoder.class | Bin 0 -> 3674 bytes Compression/bin/compressclient.class | Bin 0 -> 756 bytes Compression/bin/genericheap.class | Bin 0 -> 2858 bytes Compression/src/HEncoder.java | 105 ++++++++++++++++++ Compression/src/compressclient.java | 11 ++ Compression/src/genericheap.java | 93 ++++++++++++++++ 8 files changed, 209 insertions(+) create mode 100644 Compression/bin/HEncoder$Node$Nodecomparator.class create mode 100644 Compression/bin/HEncoder$Node.class create mode 100644 Compression/bin/HEncoder.class create mode 100644 Compression/bin/compressclient.class create mode 100644 Compression/bin/genericheap.class create mode 100644 Compression/src/HEncoder.java create mode 100644 Compression/src/compressclient.java create mode 100644 Compression/src/genericheap.java diff --git a/Compression/bin/HEncoder$Node$Nodecomparator.class b/Compression/bin/HEncoder$Node$Nodecomparator.class new file mode 100644 index 0000000000000000000000000000000000000000..132ac19ff6cac8f89e2f9c0e4fbdab3088d97268 GIT binary patch literal 859 zcmaJfgc5PjpMapO9z6Bo)yDJ?BDUnK%@L8&UC2!y35Jrw1>ahKGUYs1FoS8=IS z6^R4l%#T9M+Wkr*aoBk?v$JpC?9QLRKYsyugFO=lVg2L#$d8Yu+8MCTj31wU5lSSn zGEpQrr{bIFgd!Su4o9cbPfU~ut64Tpg0R!OsV9{7gD6N22!(oMNGSH0)ItGEHcTuM zR{B9C2kF^Js!w7RNi@v&8($*;Tr$oy>cV7a8uN S&RAm^E*fY-f0K(g%KrfHwzif4 literal 0 HcmV?d00001 diff --git a/Compression/bin/HEncoder$Node.class b/Compression/bin/HEncoder$Node.class new file mode 100644 index 0000000000000000000000000000000000000000..9fcce4c5042a2435e9f72adbb12ab07fb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zJhtTiNU!J}nUC4y`i4ddh*M^V%SN*A{vey?O20Ao&FV{(#b-FNbx|HW!XE>axZ-Qm zx{NyXf`0qx0^Yazu}^D{ZR#c3V39z7nNbBh*yWDBtO!}6Klq92r&{d-SHthPc3itX w!`&GU=IhoBM_jL7gKtp(*OVf literal 0 HcmV?d00001 diff --git a/Compression/bin/genericheap.class b/Compression/bin/genericheap.class new file mode 100644 index 0000000000000000000000000000000000000000..16c2d03aa1029eb85be2e4cf7fee29187320d1b2 GIT binary patch literal 2858 zcma)8TT@e46#h2nyd zPQfX=nQh1BfoY&;$9`tdqNU?8PTyHz(gbGcl$_fl@9x?mub zcDD+4b+_yoIML2$acym3iH>HbS}YrgH2GX9=1X?j<~i-rwuaj6jzzcNR+kJ6#oGEU z#McdswzugR;$U7WW}Pnham<2+UIYGEd_9PM1AT4Sb)gT(LNG9BA%G594Os}G%fQi; zTX1ge<~N=4n!TAL_uf=7W9QcGvMYP<(5!B|6$3|7O?4*K$YI~TQq8}&*|A35thiq? z0imu6qBksz;go@(TS?|i)jef-D3%EeXK`-B|L*s2H^WHx8ZVITGCHfT;qKFlH! z-nVcCR}FN!m6Tmp{(;!)YFvfCX5l(k7_#!YT{55y4DHLf28>lMN?aUCSMAKxn|4Vl zDY$Q0xQ#o^vE@k3h^G}n827LiMA|@qqn{dGZs9(JL`L;JHJxZvwAQF`f-(f$4zs(} zZGLyE+k)vM3m@YX?kj?6BDUJ*a_bQ}lfw$R^vBwekdnk%NS3ecG>IN`}prb;s8B~-_ZBq z00S=(Nld=P@fS4saf07onEZzM?c_BJ{R|Z4Nkm`9Fh;mz7%%DQA=}BiO+Xkjv;0c> zt7;E)CVoa_AE%%5OlK4-K#$5|sdcB9MPfHjB(@>5&TpHD(cztn^S*;BX66Inhur@E511!JL9z^fVZh=#%?+XCvX2!^|FFzLBh( zj$oh=s~y2;lLCWYLSPU}c#n7+3W!ZBAhu=NeDbDv?7n79i8>bj7UqknnGNJFC8EOz zxcm*e)gqbTPN6VWFN~@NI>lZ%&6YdEt~kqn7^iK5dEyGyn2uRTHHKAuz>Hyb$PKQ1 z3ZGVS%)^%=3jPuY{>G3%%JEqWidcOBTai%$r;tpBgfM!CgPTL%@9Q z7r`~qh`M!-<(lHmP805V!ktwBXLZCnz;SFSfXspq6+kKS1D4XyyM1`bm8n2$YU3Vg z?X_I@TBer}VWa encoder = new HashMap<>(); + public HashMap decoder = new HashMap<>(); + + private static class Node { + + Character ch; + Integer freq; + Node left; + Node right; + + public static final Nodecomparator Ctor = new Nodecomparator(); + + public static class Nodecomparator implements Comparator { + + @Override + public int compare(Node o1, Node o2) { + return o2.freq - o1.freq; + } + + } + } + + public HEncoder(String feeder) { + // 1. freq map + HashMap freqmap = new HashMap<>(); + for (int i = 0; i < feeder.length(); ++i) { + char ch = feeder.charAt(i); + if (freqmap.containsKey(ch)) { + freqmap.put(ch, freqmap.get(ch) + 1); + } else { + freqmap.put(ch, 1); + } + } + + // 2. prepare the heap from keyset + genericheap heap = new genericheap(Node.Ctor); + ArrayList k = new ArrayList<>(freqmap.keySet()); + for (Character c : k) { + Node n = new Node(); + n.ch = c; + n.left = null; + n.right = null; + n.freq = freqmap.get(c); + heap.add(n); + } + + // 3.Prepare tree, remove two , merge, add it back + Node fn = new Node(); + while (heap.size() != 1) { + Node n1 = heap.removeHP(); + Node n2 = heap.removeHP(); + fn = new Node(); + + fn.freq = n1.freq + n2.freq; + fn.left = n1; + fn.right = n2; + + heap.add(fn); + } + + // 4. traverse + + traverse(heap.removeHP(), ""); + } + + private void traverse(Node node, String osf) { + + if (node.left == null && node.right == null) { + encoder.put(node.ch, osf); + decoder.put(osf, node.ch); + return; + } + traverse(node.left, osf + "0"); + traverse(node.right, osf + "1"); + + } + + public String compress(String str) { + String rv = ""; + for (int i = 0; i < str.length(); ++i) { + rv += encoder.get(str.charAt(i)); + } + return rv; + } + + public String decompress(String str) { + String s = ""; + String code = ""; + for (int i = 0; i < str.length(); ++i) { + code += str.charAt(i); + if (decoder.containsKey(code)) { + s += decoder.get(code); + code = ""; + } + } + + return s; + } +} diff --git a/Compression/src/compressclient.java b/Compression/src/compressclient.java new file mode 100644 index 00000000..496c8a84 --- /dev/null +++ b/Compression/src/compressclient.java @@ -0,0 +1,11 @@ + +public class compressclient { + + public static void main(String[] args) { + + HEncoder h= new HEncoder("aaaabbbcccccccccccdddd"); + System.out.println(h.compress("aabccd")); + System.out.println(h.decompress("101011000111")); + } + +} diff --git a/Compression/src/genericheap.java b/Compression/src/genericheap.java new file mode 100644 index 00000000..ce4b491a --- /dev/null +++ b/Compression/src/genericheap.java @@ -0,0 +1,93 @@ + + +import java.util.ArrayList; +import java.util.Comparator; + +public class genericheap { + + private ArrayList data = new ArrayList<>(); + private Comparator ctor; + + public genericheap(Comparator ctor) { + this.ctor=ctor; + } + + public int size() { + return data.size(); + } + + public boolean isEmpty() { + return data.isEmpty(); + } + + public void display() { + System.out.println(this.data); + } + + public void add(T integer) { + data.add(integer); + upheapify(data.size() - 1); + } + + private void upheapify(int ci) { + if (ci == 0) { + return; + } + int pi = (ci - 1) / 2; + if (isLarger(ci,pi) == true) { + swap(ci, pi); + upheapify(pi); + } + } + + private boolean isLarger(int i, int j) { + T ith = data.get(i); + T jth = data.get(j); + if(ctor.compare(ith,jth)>0) + { + return true; + } + else + { + return false; + } + } + + private void swap(int ci, int pi) { + T ith = data.get(ci); + T jth=data.get(pi); + data.set(ci, jth); + data.set(pi, ith); + } + + public T getHP() { + return data.get(0); + } + + public T removeHP() { + + swap(0, data.size() - 1); + T rv=data.remove(data.size()-1); + downheapify(0); + return rv; + } + + private void downheapify(int pi) { + int lci = 2 * pi + 1; + int rci = 2 * pi + 2; + + int max = pi; + + if (lci < data.size() && isLarger(lci, max) == true) { + max = lci; + } + if (rci < data.size() && isLarger(rci, max) == true) { + max = rci; + } + if (max != pi) { + swap(pi, max); + downheapify(max); + } + } + +} From 27ccd4298b1d2eb2b7d8af17ac21756c552bbeae Mon Sep 17 00:00:00 2001 From: Shikhar Tyagi Date: Tue, 3 Oct 2017 22:07:30 +0530 Subject: [PATCH 2/6] Create Any_to_Any Input : Source base , Source number , Destination base Output : Destination Number --- Conversions/AnytoAny | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Conversions/AnytoAny diff --git a/Conversions/AnytoAny b/Conversions/AnytoAny new file mode 100644 index 00000000..2e43fb0e --- /dev/null +++ b/Conversions/AnytoAny @@ -0,0 +1,28 @@ +import java.util.Scanner; + +//sn ,sb,db ---> ()dn +public class anytoany { + + public static void main(String[] args) { + Scanner scn = new Scanner(System.in); + int sn = scn.nextInt(); + int sb = scn.nextInt(); + int db = scn.nextInt(); + int m=1,dec=0,dn=0; + while(sn!=0) + { + dec=dec+ (sn%10)*m; + m*=sb; + sn/=10; + } + m=1; + while(dec!=0) + { + dn=dn+ (dec%db)*m; + m*=10; + dec/=db; + } + System.out.println(dn); + } + +} From e1149eb4b9da2a190538070dca9cb55dc09af183 Mon Sep 17 00:00:00 2001 From: Shikhar Tyagi Date: Mon, 23 Oct 2017 11:46:44 +0530 Subject: [PATCH 3/6] Rename AnytoAny to AnytoAny.java --- Conversions/{AnytoAny => AnytoAny.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Conversions/{AnytoAny => AnytoAny.java} (100%) diff --git a/Conversions/AnytoAny b/Conversions/AnytoAny.java similarity index 100% rename from Conversions/AnytoAny rename to Conversions/AnytoAny.java From 979be41b218cfcfd8022e7bbd968a0356553cbca Mon Sep 17 00:00:00 2001 From: Shikhar Tyagi Date: Mon, 23 Oct 2017 11:48:18 +0530 Subject: [PATCH 4/6] Update AnytoAny.java --- Conversions/AnytoAny.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Conversions/AnytoAny.java b/Conversions/AnytoAny.java index 2e43fb0e..a1acdb1a 100644 --- a/Conversions/AnytoAny.java +++ b/Conversions/AnytoAny.java @@ -1,6 +1,6 @@ import java.util.Scanner; - -//sn ,sb,db ---> ()dn +//given a source number , source base, destination base, this code can give you the destination number. +//sn ,sb,db ---> ()dn . this is what we have to do . public class anytoany { public static void main(String[] args) { From 4d2972658eaaaabd982fd5a0e66e3bdd3e6a0ab3 Mon Sep 17 00:00:00 2001 From: Shikhar Tyagi Date: Mon, 8 Jan 2018 19:16:32 +0530 Subject: [PATCH 5/6] added comments for some functions --- Compression/src/HEncoder.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Compression/src/HEncoder.java b/Compression/src/HEncoder.java index e0ebaa1d..37bd8fc0 100644 --- a/Compression/src/HEncoder.java +++ b/Compression/src/HEncoder.java @@ -4,8 +4,8 @@ import java.util.HashMap; public class HEncoder { - public HashMap encoder = new HashMap<>(); - public HashMap decoder = new HashMap<>(); + public HashMap encoder = new HashMap<>(); // in order to encode + public HashMap decoder = new HashMap<>(); // in order to decode private static class Node { @@ -81,6 +81,7 @@ public class HEncoder { } + // compression work done here public String compress(String str) { String rv = ""; for (int i = 0; i < str.length(); ++i) { @@ -88,7 +89,9 @@ public class HEncoder { } return rv; } + + //in order to decompress public String decompress(String str) { String s = ""; String code = ""; From c0c72ca2036cdd17a88916a04badd9b241e2ea39 Mon Sep 17 00:00:00 2001 From: Shikhar Tyagi Date: Mon, 8 Jan 2018 19:21:37 +0530 Subject: [PATCH 6/6] added comments --- Compression/src/genericheap.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Compression/src/genericheap.java b/Compression/src/genericheap.java index ce4b491a..de00316f 100644 --- a/Compression/src/genericheap.java +++ b/Compression/src/genericheap.java @@ -3,28 +3,28 @@ import java.util.ArrayList; import java.util.Comparator; -public class genericheap { +public class genericheap { // create a generic heap class , where T can be of any type. private ArrayList data = new ArrayList<>(); private Comparator ctor; - public genericheap(Comparator ctor) { + public genericheap(Comparator ctor) { // constructor to initialize the generic comparator this.ctor=ctor; } - public int size() { + public int size() { // returns the size of the arraylist data return data.size(); } - public boolean isEmpty() { + public boolean isEmpty() { // checks whether the list is empty or not :: return true or false for the same return data.isEmpty(); } - public void display() { + public void display() { //displays the list System.out.println(this.data); } - public void add(T integer) { + public void add(T integer) { // in this function we have added the type object into the arraylist and called upheapify data.add(integer); upheapify(data.size() - 1); } @@ -53,7 +53,7 @@ public class genericheap { } } - private void swap(int ci, int pi) { + private void swap(int ci, int pi) { // swap function written like this because of the generic property T ith = data.get(ci); T jth=data.get(pi); data.set(ci, jth);