From 5fe44b10c81bfbd0765cf132d6783db62c64172d Mon Sep 17 00:00:00 2001 From: alicee06 Date: Thu, 22 Jun 2017 11:18:56 +0100 Subject: [PATCH 01/21] Cleaning the repo --- Assignment1/Q1/{src => }/Main.java | 6 +++++- Assignment1/Q1/src/Main.class | Bin 1394 -> 0 bytes 2 files changed, 5 insertions(+), 1 deletion(-) rename Assignment1/Q1/{src => }/Main.java (90%) delete mode 100644 Assignment1/Q1/src/Main.class diff --git a/Assignment1/Q1/src/Main.java b/Assignment1/Q1/Main.java similarity index 90% rename from Assignment1/Q1/src/Main.java rename to Assignment1/Q1/Main.java index 3685359..38d8283 100644 --- a/Assignment1/Q1/src/Main.java +++ b/Assignment1/Q1/Main.java @@ -34,7 +34,7 @@ public static void main(String[] args) }//if else { - if(checkPermutation(string1.toLowerCase(), string2.toLowerCase())) + if(checkPermutation(string1, string2)) { System.out.println("\" " + string1 + " \" is a permutation of \" " + string2 + " \"!"); }//if @@ -49,6 +49,10 @@ public static void main(String[] args) // Check if the number of characters of each type are the same for both strings public static boolean checkPermutation(String s1, String s2) { + // Transform s1 and s2 into low cases + s1 = s1.toLowerCase(); + s2 = s2.toLowerCase(); + char[] charS1 = s1.toCharArray(); char[] charS2 = s2.toCharArray(); diff --git a/Assignment1/Q1/src/Main.class b/Assignment1/Q1/src/Main.class deleted file mode 100644 index fd0866f7448e9239e9af6672ccfbc9bbcf461398..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1394 zcmZuxTUQfT7~O}EOc+LlL;u@+3VMMH>&UVvWKA)JztWG0=Nc=;Rc zQ=j_Sm%ivjaMiW?1N-~tV6WxhGHzuW#UC$HcB`yYTS_{GFYI0kY<$m61k z0nF%AA&prRMO@OzWfPxbE{!W1xvD>|nK*k+}0w;~n_4`H|^J(1F$1l>jrR}~n zv4BM_ZW|~W_(~vk)AiiwmO#SEeJ3zj^0$=0(X#8QN~g7{f>pWMBr@2Nt|u_=Y?L3# z$FkUz-gdDT1+KR}uN{xnB3a*AmhIS{1sJ#^Fj{Y@`c72^txhB(*Jp$aPJe)YB)2YL z*8EOTS9e{Ve`uNcXLV`|AK{e1SWKf6xy@p&EMj3 z(C<+dnCh8TR#p!)weSPh46Iw&z>gMw!p|I-ZWt%9&tYZrk*Y`ULA?RK4?PPb)gndh zeY`gr*Do%4?M_5}vwbm(>f7D%?W5xoxCjd&|P z^_1#kPOg7bxWc`e#4EfcV8p?iWIr7FW&eo^N;2dIrDVIUye)ykVOjbD@6C$ErX&Ad zpBS;!kilXQ$Y*S^liMi0YhJvGu_B}<^aB!3IvXX;Kc#-_$R=@v0HJl9=7l}U8xJHP z^g>fk@oS22!FMvh3-O!>fsZLuF_^^=K7q}%Hhn|aLB9WZg@LsfNbF*;`~t~Lste;{ z7wKg!Dl=US75Myx5v?@482%fZL@lD$OC0$V1u9-)WNjBmGoxLMQ8|{$P|#v5lcnh5 zSnQWS%<_1@r6xWQ6VJosc>=?%H_Iv~NqveXIPAb2&R~%@We4Z+dyD|1NW8_IfoTJ0 z_`80dCG#%rmAt^Hy5z}x7vs9V@yg6gOceOcP)z Date: Thu, 22 Jun 2017 11:19:41 +0100 Subject: [PATCH 02/21] Delete compiler.xml --- Assignment1/Q1/.idea/compiler.xml | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 Assignment1/Q1/.idea/compiler.xml diff --git a/Assignment1/Q1/.idea/compiler.xml b/Assignment1/Q1/.idea/compiler.xml deleted file mode 100644 index 96cc43e..0000000 --- a/Assignment1/Q1/.idea/compiler.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From 58d0b9726721e415318150970ec441d1fc87842e Mon Sep 17 00:00:00 2001 From: Alice Colt Date: Thu, 22 Jun 2017 11:19:52 +0100 Subject: [PATCH 03/21] Delete description.html --- Assignment1/Q1/.idea/description.html | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Assignment1/Q1/.idea/description.html diff --git a/Assignment1/Q1/.idea/description.html b/Assignment1/Q1/.idea/description.html deleted file mode 100644 index db5f129..0000000 --- a/Assignment1/Q1/.idea/description.html +++ /dev/null @@ -1 +0,0 @@ -Simple Java application that includes a class with main() method \ No newline at end of file From d01d84ecaebda93df6a20cce2960676ea3771c4f Mon Sep 17 00:00:00 2001 From: Alice Colt Date: Thu, 22 Jun 2017 11:20:31 +0100 Subject: [PATCH 04/21] Delete profiles_settings.xml --- Assignment1/Q1/.idea/copyright/profiles_settings.xml | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 Assignment1/Q1/.idea/copyright/profiles_settings.xml diff --git a/Assignment1/Q1/.idea/copyright/profiles_settings.xml b/Assignment1/Q1/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf3..0000000 --- a/Assignment1/Q1/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file From cdfd76d191d763d706a0b6f8e3c852e75804fa2f Mon Sep 17 00:00:00 2001 From: Alice Colt Date: Thu, 22 Jun 2017 11:20:38 +0100 Subject: [PATCH 05/21] Delete misc.xml --- Assignment1/Q1/.idea/misc.xml | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 Assignment1/Q1/.idea/misc.xml diff --git a/Assignment1/Q1/.idea/misc.xml b/Assignment1/Q1/.idea/misc.xml deleted file mode 100644 index e29b6f0..0000000 --- a/Assignment1/Q1/.idea/misc.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - \ No newline at end of file From 098c9275ae5de07097c37a068fdc8d8ad5564bcf Mon Sep 17 00:00:00 2001 From: Alice Colt Date: Thu, 22 Jun 2017 11:20:46 +0100 Subject: [PATCH 06/21] Delete modules.xml --- Assignment1/Q1/.idea/modules.xml | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 Assignment1/Q1/.idea/modules.xml diff --git a/Assignment1/Q1/.idea/modules.xml b/Assignment1/Q1/.idea/modules.xml deleted file mode 100644 index dba418f..0000000 --- a/Assignment1/Q1/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file From ebcf714529f2a9a797fef1ad3093000b838a840b Mon Sep 17 00:00:00 2001 From: Alice Colt Date: Thu, 22 Jun 2017 11:20:54 +0100 Subject: [PATCH 07/21] Delete project-template.xml --- Assignment1/Q1/.idea/project-template.xml | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 Assignment1/Q1/.idea/project-template.xml diff --git a/Assignment1/Q1/.idea/project-template.xml b/Assignment1/Q1/.idea/project-template.xml deleted file mode 100644 index 1f08b88..0000000 --- a/Assignment1/Q1/.idea/project-template.xml +++ /dev/null @@ -1,3 +0,0 @@ - \ No newline at end of file From 3bd8e4bde6530f30ad9cb637ac220ce26fad25f6 Mon Sep 17 00:00:00 2001 From: Alice Colt Date: Thu, 22 Jun 2017 11:21:05 +0100 Subject: [PATCH 08/21] Delete uiDesigner.xml --- Assignment1/Q1/.idea/uiDesigner.xml | 124 ---------------------------- 1 file changed, 124 deletions(-) delete mode 100644 Assignment1/Q1/.idea/uiDesigner.xml diff --git a/Assignment1/Q1/.idea/uiDesigner.xml b/Assignment1/Q1/.idea/uiDesigner.xml deleted file mode 100644 index e96534f..0000000 --- a/Assignment1/Q1/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From 5b887e3bbd31add8cdff8b69a0a6e27a1e9b2a06 Mon Sep 17 00:00:00 2001 From: Alice Colt Date: Thu, 22 Jun 2017 11:21:14 +0100 Subject: [PATCH 09/21] Delete workspace.xml --- Assignment1/Q1/.idea/workspace.xml | 440 ----------------------------- 1 file changed, 440 deletions(-) delete mode 100644 Assignment1/Q1/.idea/workspace.xml diff --git a/Assignment1/Q1/.idea/workspace.xml b/Assignment1/Q1/.idea/workspace.xml deleted file mode 100644 index 0c9f617..0000000 --- a/Assignment1/Q1/.idea/workspace.xml +++ /dev/null @@ -1,440 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1495115303135 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From d82ac16eaed8e8a692ebd640d282447596470769 Mon Sep 17 00:00:00 2001 From: Alice Colt Date: Thu, 22 Jun 2017 11:21:56 +0100 Subject: [PATCH 10/21] Delete profiles_settings.xml --- Assignment1/Q2/.idea/copyright/profiles_settings.xml | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 Assignment1/Q2/.idea/copyright/profiles_settings.xml diff --git a/Assignment1/Q2/.idea/copyright/profiles_settings.xml b/Assignment1/Q2/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf3..0000000 --- a/Assignment1/Q2/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file From b5a6aa3a648b55c6cf64846515f78b80391c7814 Mon Sep 17 00:00:00 2001 From: Alice Colt Date: Thu, 22 Jun 2017 11:22:05 +0100 Subject: [PATCH 11/21] Delete compiler.xml --- Assignment1/Q2/.idea/compiler.xml | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 Assignment1/Q2/.idea/compiler.xml diff --git a/Assignment1/Q2/.idea/compiler.xml b/Assignment1/Q2/.idea/compiler.xml deleted file mode 100644 index 96cc43e..0000000 --- a/Assignment1/Q2/.idea/compiler.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From 90fc6b21ae69547ae047c25eb7ca5d1120834ba2 Mon Sep 17 00:00:00 2001 From: Alice Colt Date: Thu, 22 Jun 2017 11:22:13 +0100 Subject: [PATCH 12/21] Delete misc.xml --- Assignment1/Q2/.idea/misc.xml | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 Assignment1/Q2/.idea/misc.xml diff --git a/Assignment1/Q2/.idea/misc.xml b/Assignment1/Q2/.idea/misc.xml deleted file mode 100644 index c6d8fb7..0000000 --- a/Assignment1/Q2/.idea/misc.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From b0502d181e2894a2332113718e1a23e6cc1c2bdf Mon Sep 17 00:00:00 2001 From: Alice Colt Date: Thu, 22 Jun 2017 11:22:24 +0100 Subject: [PATCH 13/21] Delete modules.xml --- Assignment1/Q2/.idea/modules.xml | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 Assignment1/Q2/.idea/modules.xml diff --git a/Assignment1/Q2/.idea/modules.xml b/Assignment1/Q2/.idea/modules.xml deleted file mode 100644 index a22f264..0000000 --- a/Assignment1/Q2/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file From 7edb564c936d1ea13e0643ccf9952a03489c4832 Mon Sep 17 00:00:00 2001 From: Alice Colt Date: Thu, 22 Jun 2017 11:22:33 +0100 Subject: [PATCH 14/21] Delete workspace.xml --- Assignment1/Q2/.idea/workspace.xml | 548 ----------------------------- 1 file changed, 548 deletions(-) delete mode 100644 Assignment1/Q2/.idea/workspace.xml diff --git a/Assignment1/Q2/.idea/workspace.xml b/Assignment1/Q2/.idea/workspace.xml deleted file mode 100644 index 5b50305..0000000 --- a/Assignment1/Q2/.idea/workspace.xml +++ /dev/null @@ -1,548 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1495140747076 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - No facets are configured - - - - - - - - - - - - - - - 1.8 - - - - - - - - Q2 - - - - - - - - 1.8 - - - - - - - - - - - - - - - \ No newline at end of file From 9e2846d2dd78ddbf166da47b8af22454681b5c0f Mon Sep 17 00:00:00 2001 From: Alice Colt Date: Thu, 22 Jun 2017 11:23:06 +0100 Subject: [PATCH 15/21] Delete Q2.iml --- Assignment1/Q2/Q2.iml | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 Assignment1/Q2/Q2.iml diff --git a/Assignment1/Q2/Q2.iml b/Assignment1/Q2/Q2.iml deleted file mode 100644 index c90834f..0000000 --- a/Assignment1/Q2/Q2.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file From 10ee14e9c7e2a4e6b49d466aa9af9d6a71e9ea19 Mon Sep 17 00:00:00 2001 From: alicee06 Date: Thu, 22 Jun 2017 11:25:56 +0100 Subject: [PATCH 16/21] Cleanings --- Assignment1/Q2/{src => }/Main.java | 0 Assignment1/Q2/{src => }/Singly_Linked_List.java | 0 Assignment1/Q2/src/Main.class | Bin 1232 -> 0 bytes Assignment1/Q2/src/Node.class | Bin 269 -> 0 bytes Assignment1/Q2/src/Singly_Linked_List.class | Bin 683 -> 0 bytes 5 files changed, 0 insertions(+), 0 deletions(-) rename Assignment1/Q2/{src => }/Main.java (100%) rename Assignment1/Q2/{src => }/Singly_Linked_List.java (100%) delete mode 100644 Assignment1/Q2/src/Main.class delete mode 100644 Assignment1/Q2/src/Node.class delete mode 100644 Assignment1/Q2/src/Singly_Linked_List.class diff --git a/Assignment1/Q2/src/Main.java b/Assignment1/Q2/Main.java similarity index 100% rename from Assignment1/Q2/src/Main.java rename to Assignment1/Q2/Main.java diff --git a/Assignment1/Q2/src/Singly_Linked_List.java b/Assignment1/Q2/Singly_Linked_List.java similarity index 100% rename from Assignment1/Q2/src/Singly_Linked_List.java rename to Assignment1/Q2/Singly_Linked_List.java diff --git a/Assignment1/Q2/src/Main.class b/Assignment1/Q2/src/Main.class deleted file mode 100644 index 5830728101bdfa04961d6a9be1d3f5e1addc1c1c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1232 zcmaJ>OHUI~6#i}>%yb-}lon8mPLW5U(5fJ6i&a3-O3T9%4RK+H_67!~)25voaP6;9 z7sd^1h$)FA{s4cDOC##JElCLwySV4{obNl&d*{cWZ{Gn-VokwS3`!VM;J~0bh9!hO z7{RClHzGnClQFI!ikPt6@(|{>j61?IAqbNa?kezNP{x#y_r!5u#seYaG7=J|B|Kzk znKmrLPBJ*dBQF_TbHzMoXipm!U#b>1czI3VFv;R7=!V766@H!G*5B$eQ@1u_S-Wgl zn+ZW|&DwfyC!?3@c&eQ$W>B)lYB|Rj3=zVUp%78QR&foz48AO}&Am62Y=`GbRBRPV zJYwjm=cw9-8O!E$%i?7Tk5$ZIR#4|uJi$EWT(q||Zt?=R?6@XjLB&%%Q<1`R6^lqS zw27FSQPFszWbcJkWQ2bS%M9J;+nB8yW}cU+)wM0Ii9%MyvC1&hOe11hMG9$YHlalL zsFpR}ajjC&O_P_&tT%i+O;%LJ3uGBWO;a<1=>4?JxJKE7^34 z@&pN|#K_M;vU?Sq7Z@CZ8fa|DD8^D&scO@bxL!yw^n@>Uh)~XAl@c_GE40@pv9n$a z#XDnWaF+y#Sf^pn-w4U#yLL(-=xU{?>I#jCfsrcJsV3E`Y;t?PvB5@P)*9LlBEsOx_vLG7pK=9Uj?Wk%=P|q#xx?qHp|h_Bzb`;a4P8Xp$IxYf zZUO{c1Z|*12ab9NNwkx$LPi^X4ZH}D>_sPr;HU3L02Am&oJKM$-WoAhsnb``u!{kF h#4u@~k-1NxNC>fC@ScbdR6qNTN!CvjxmnLN@DD}+8ngfa diff --git a/Assignment1/Q2/src/Node.class b/Assignment1/Q2/src/Node.class deleted file mode 100644 index 9e28ff402d98d3b706592c3cc00275d27f3acbd9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 269 zcmW+w!D_-l6r7FG)HF4*7Cdq*fg$SW2v9zp$I+n1Nu?o ztcRV)3^TLyfBpRf@P-#3F2Xu0h&(hsJPWu+eU}2Y*?ga>A$clGDIliQM%w7|omy|& zU-OJ<5eR46sQGc1sKZJo1r>j}KOVMfqAh7JwaJRphOQ5l@_d%Bx%`p=>SzV<@W3Yf znLuRqK_N{xSR}dHaz~}tU!%)`E(be&Rn&;EIU;OXcL`*c9cp`VuxB*jjEqVX| diff --git a/Assignment1/Q2/src/Singly_Linked_List.class b/Assignment1/Q2/src/Singly_Linked_List.class deleted file mode 100644 index 2c386d5cb64f4ad7e09a03b60964d7d2c9b827f3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 683 zcmZWlO=}ZT6g_Wdn(0g?X>2sLrW2d0H4f3>*Fr5R_*Du`T||Vg^d))3Y13(Fr=soz z!M`DHy2+-HQc!TI;BRs1QmyAriY_F~x!m*aJ?G?p`~C4VfE&2r!oe8_<0xvFvEjS0 zP;v-oHO$&5+n5uuo-5fANUf}Hb<`d9JK<|3AePB3gi#pR1SE-PJCruzK|ZWnDF2sy z?NMu8wY7DmUh2}3u|%1dad<9+AGL|lMnn@r`x0~P<~bo)-QW-+Fb`ntvlBR>xn#S* zi%yf7ME-c^9T}7Fi^19uhV_g~!4T$Gbxkhz})EA_6Y3v>SsQw#B zX(J;snopwoG@NBW2cQ4Ggp0%q0{*VyGU}+|WD?3O{e)%XRF+)Y7e64I#CU6py`%4b@jb@b+XG|pH%Pz;lz@$!jC6~4y3H#sV21W(EHctv aRB<0yv6O`IU^%~#Vi-|y802)KH2nu?k8y Date: Thu, 22 Jun 2017 11:32:30 +0100 Subject: [PATCH 17/21] Removed commented code --- Assignment1/Q2/Singly_Linked_List.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Assignment1/Q2/Singly_Linked_List.java b/Assignment1/Q2/Singly_Linked_List.java index 78b693c..5880b27 100644 --- a/Assignment1/Q2/Singly_Linked_List.java +++ b/Assignment1/Q2/Singly_Linked_List.java @@ -70,8 +70,4 @@ int getElement(int k) return -1; }//getElement -/* int getSize() - { - return size; - }//getSize*/ }//Singly_List_List \ No newline at end of file From 59d5e610924e55377f68e9afaea6df57c0642fb7 Mon Sep 17 00:00:00 2001 From: Alice Colt Date: Thu, 22 Jun 2017 11:48:03 +0100 Subject: [PATCH 18/21] Assignment Tasks --- README.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/README.md b/README.md index b1dcf57..b6ee02c 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,56 @@ This repository is for the completed assignments given by Google as part of Code There will be a total of six assignments completed in Java. + +****************************** +Assignment 1 - Getting Started + Q1 Task: + "Given two strings, write a method to decide if one is a permutation of the other." + e.g. Listen, Silent ⇒ True Triangle, Integral ⇒ True Apple, Pabble ⇒ False + Q2 Task: + "Implement an algorithm to find the kth to last element of a singly linked list." + e.g. k=1 +****************************** + +****************************** +Assignment 2 - Ancestors + Q1 - Print Ancestors: + "Given a Binary Tree and a key, write a function that prints all the ancestors of the key in the given binary tree." + + Q2 - Common Ancestor + "Design an algorithm and write code to find the lowest common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure." +****************************** + +****************************** +Assignment 3 - Word Search + + "Given a grid of letters and a dictionary, find all the words from the dictionary that can be formed in the grid. + The rules for forming a word: + ● You can start at any position. + ● You can move to one of the 8 adjacent cells (horizontally/vertically/diagonally). + ● You can't visit the same cell twice in the same word. + Assume you have a dictionary class with these two methods: + ● isWord(string): Returns whether the given string is a valid word. + ● isPrefix(string): Returns whether the given string is a prefix of at least one word in the + dictionary. + + Visual example with 2 rows, 3 columns: + The dictionary is made up of entries [CAR, CARD, CART, CAT], the list of prefixes is [C, CA, CAR, CARD, CART, CAT]. The correct output in this case would be [CAR, CARD, CAT]." +****************************** + +****************************** +Assignment 4 - Counting Islands + + Given a 2-dimensional map of tiles. Each tile is either land or water. Write a function that counts the number of islands. + Two tiles belong to the same island if they are both land and are adjacent horizontally or vertically, but not diagonally. + The input to your function is the number of rows, number of columns, and a 2-dimensional array of booleans, where false means water and true means land. The function should simply return the number of islands. +****************************** + +****************************** +Assignment 5 +****************************** + +****************************** +Assignment 6 +****************************** + From 3055b059673d51c793c354327f1b80db35e9b7e6 Mon Sep 17 00:00:00 2001 From: alicee06 Date: Thu, 22 Jun 2017 12:00:52 +0100 Subject: [PATCH 19/21] Assignment3 --- Assignment2/Q1/src/BinaryTree.java | 93 -------------- Assignment2/Q1/src/Main.java | 27 ---- Assignment2/Q1/src/NodeBinaryTree.java | 93 -------------- Assignment2/Q2/src/BinaryTree.java | 167 ------------------------- Assignment2/Q2/src/Main.java | 29 ----- Assignment2/Q2/src/NodeBinaryTree.java | 93 -------------- Assignment3/Dictionary.java | 18 +++ Assignment3/WordSearch.java | 113 +++++++++++++++++ 8 files changed, 131 insertions(+), 502 deletions(-) delete mode 100644 Assignment2/Q1/src/BinaryTree.java delete mode 100644 Assignment2/Q1/src/Main.java delete mode 100644 Assignment2/Q1/src/NodeBinaryTree.java delete mode 100644 Assignment2/Q2/src/BinaryTree.java delete mode 100644 Assignment2/Q2/src/Main.java delete mode 100644 Assignment2/Q2/src/NodeBinaryTree.java create mode 100644 Assignment3/Dictionary.java create mode 100644 Assignment3/WordSearch.java diff --git a/Assignment2/Q1/src/BinaryTree.java b/Assignment2/Q1/src/BinaryTree.java deleted file mode 100644 index 80175f3..0000000 --- a/Assignment2/Q1/src/BinaryTree.java +++ /dev/null @@ -1,93 +0,0 @@ -/** - * Created by Alice Colt. - * - * Implementaiton of a Binary Tree with functions for insert, serach and takeAncestors - * - */ -public class BinaryTree { - private NodeBinaryTree root; - - //Constructor - public BinaryTree(NodeBinaryTree new_root){ - root = new_root; - } - - - public void insert(NodeBinaryTree new_node) { - - NodeBinaryTree actualNode = root; - - if (new_node != null) { - - while (actualNode != null) { - if (new_node.compareTo(actualNode) == 1 && actualNode.getLeft() == null) { - actualNode.setLeft(new_node); - new_node.setParent(actualNode); - break; - }//if - else if (new_node.compareTo(actualNode) == -1 && actualNode.getRight() == null) { - actualNode.setRight(new_node); - new_node.setParent(actualNode); - break; - }//else if - else if (new_node.compareTo(actualNode) == 0) { - System.out.println(" The node added is a duplicate "); - break; - }//else if - else { - if (new_node.compareTo(actualNode) == 1) { - actualNode = actualNode.getLeft(); - }//if - else if(new_node.compareTo(actualNode) == -1) { - actualNode = actualNode.getRight(); - }//else if - }//else - }//while - }//if - }//insert - - - // Get ancestors - public void takeAncestors(int value) - { - NodeBinaryTree actualNode = root; - - // Check if child is null - if (actualNode == null) { - System.out.println("Node null"); - }//if - - // Search Call - actualNode = search(root, value); - - // Print ancestors - while (actualNode != root) - { - System.out.print(actualNode.getParent().getValue() + " "); - actualNode = actualNode.getParent(); - }//while - }//takeAncestors - - - // Function for search - private NodeBinaryTree search(NodeBinaryTree node, int value) - { - while ((node != null)) - { - if (node.getValue() > value) - node = node.getLeft(); - else - if (node.getValue() < value) - node = node.getRight(); - else - return node; - - // Recursive search - return search(node, value); - }//while - - // Return node - return node; - }// search - -}//BinaryTree class \ No newline at end of file diff --git a/Assignment2/Q1/src/Main.java b/Assignment2/Q1/src/Main.java deleted file mode 100644 index 676d9dd..0000000 --- a/Assignment2/Q1/src/Main.java +++ /dev/null @@ -1,27 +0,0 @@ -import java.util.*; -/** - * Created by Alice Colt. - * - */ -public class Main { - - public static void main(String[] args) { - - BinaryTree bst = new BinaryTree(new NodeBinaryTree(16)); - // Hardcoded Test - - bst.insert(new NodeBinaryTree(9)); - bst.insert(new NodeBinaryTree(18)); - bst.insert(new NodeBinaryTree(3)); - bst.insert(new NodeBinaryTree(14)); - bst.insert(new NodeBinaryTree(19)); - bst.insert(new NodeBinaryTree(5)); - bst.insert(new NodeBinaryTree(1)); - - int value = Integer.parseInt(args[0]); - System.out.print("Ancestors for ' " + value + "' are: "); - bst.takeAncestors(value); - System.out.println(); - - } -} \ No newline at end of file diff --git a/Assignment2/Q1/src/NodeBinaryTree.java b/Assignment2/Q1/src/NodeBinaryTree.java deleted file mode 100644 index 60ccd74..0000000 --- a/Assignment2/Q1/src/NodeBinaryTree.java +++ /dev/null @@ -1,93 +0,0 @@ -import javax.xml.soap.Node; - -/** - * Created by Alice Colt. - * - * Implementation of Node for BinaryTree with constructor, getters, setters and compareTo - * - */ -public class NodeBinaryTree -{ - // Value of node - private int value; - - // References to root, left and right - - private NodeBinaryTree root; - private NodeBinaryTree left; - private NodeBinaryTree right; - - - // Constructor - public NodeBinaryTree(int data) - { - value = data; - }// - - public NodeBinaryTree(int data, NodeBinaryTree valueL, NodeBinaryTree valueR) - { - value = data; - left = valueL; - right = valueR; - } - - - // Getters - public int getValue() - { - return value; - }//getData - - - public NodeBinaryTree getParent() - { - return root; - }//getRoot - - - public void setParent(NodeBinaryTree parent) - { - root = parent; - }//setParent - - - public NodeBinaryTree getLeft() - { - return left; - }//getLeft - public NodeBinaryTree getRight() - { - return right; - }//getRight - - - // Setters - public void setData(int data) - { - value = data; - }//setData - - public void setLeft(NodeBinaryTree leftChild) - { - left = leftChild; - }//setLeft - public void setRight(NodeBinaryTree rightChild) - { - right = rightChild; - }//setRight - - - // Compare two integers; - public int compareTo(NodeBinaryTree node2) - { - int value1 = this.getValue(); - int value2 = node2.getValue(); - - if (value1 > value2) - return -1; - else if (value1 < value2) - return 1; - else - return 0; - }// compareTo -}//NodeBinaryTree \ No newline at end of file diff --git a/Assignment2/Q2/src/BinaryTree.java b/Assignment2/Q2/src/BinaryTree.java deleted file mode 100644 index 03d40d9..0000000 --- a/Assignment2/Q2/src/BinaryTree.java +++ /dev/null @@ -1,167 +0,0 @@ -/** - * Created by Alice Colt. - * - * Implementaiton of a Binary Tree with functions for insert, search and takeAncestors and commonAncestors - * - */ -public class BinaryTree { - private NodeBinaryTree root; - - //Constructor - public BinaryTree(NodeBinaryTree new_root){ - root = new_root; - } - - - public void insert(NodeBinaryTree new_node) { - - NodeBinaryTree actualNode = root; - - if (new_node != null) { - - while (actualNode != null) { - if (new_node.compareTo(actualNode) == 1 && actualNode.getLeft() == null) { - actualNode.setLeft(new_node); - new_node.setParent(actualNode); - break; - }//if - else if (new_node.compareTo(actualNode) == -1 && actualNode.getRight() == null) { - actualNode.setRight(new_node); - new_node.setParent(actualNode); - break; - }//else if - else if (new_node.compareTo(actualNode) == 0) { - System.out.println(" The node added is a duplicate "); - break; - }//else if - else { - if (new_node.compareTo(actualNode) == 1) { - actualNode = actualNode.getLeft(); - }//if - else if(new_node.compareTo(actualNode) == -1) { - actualNode = actualNode.getRight(); - }//else if - }//else - }//while - }//if - }//insert - - - // Get ancestors - public void takeAncestors(int value) - { - NodeBinaryTree actualNode = root; - - // Check if child is null - if (actualNode == null) { - System.out.println("Node null"); - }//if - - // Search Call - actualNode = search(root, value); - - // Print ancestors - while (actualNode != root) - { - System.out.print(actualNode.getParent().getValue() + " "); - actualNode = actualNode.getParent(); - }//while - }//takeAncestors - - - // Function for search - private NodeBinaryTree search(NodeBinaryTree node, int value) - { - while ((node != null)) - { - if (node.getValue() > value) - node = node.getLeft(); - else - if (node.getValue() < value) - node = node.getRight(); - else - return node; - - // Recursive search - return search(node, value); - }//while - - // Return node - return node; - }// search - - -/* -* -* A more complicated method -* Takes the node with the smallest value -* Work the way up, searching for the second node -* It keeps track of the common ancestor -* - public int commonAncestor1(int value1, int value2) - { - NodeBinaryTree currentNode; - NodeBinaryTree currentParent; - - if(value1 < value2) - { - currentNode = search(root, value1); - }//if - else - { - currentNode = search(root, value2); - }//else - - NodeBinaryTree copyParent = currentNode.getParent(); - currentNode = currentNode.getParent(); - - if(currentNode.getValue() == value2) - { - return currentNode.getParent().getValue(); - }//if - - copyParent = currentNode.getParent(); - - while(currentNode != root) - { - if(currentNode.getValue() == value2) - { - return currentNode.getParent().getValue(); - } - else - if(currentNode == root) - return root.getValue(); - else - { - if(currentNode.getValue() > value2) - { - copyParent = currentNode; - currentNode = currentNode.getRight(); - } - else - { - copyParent = currentNode; - currentNode = currentNode.getLeft(); - } - } - } - return copyParent.getValue(); - }//commonAncestor -*/ - - // Lowest Common Ancestor - public int commonAncestor(int value1, int value2) - { - NodeBinaryTree currentNode = root; - - while(true) - if(value1 < currentNode.getValue() && value2 < currentNode.getValue()) - currentNode = currentNode.getLeft(); - else - if(value1 > currentNode.getValue() && value2 > currentNode.getValue()) - currentNode = currentNode.getRight(); - else - return currentNode.getValue(); - }//commonAncestor - -}//BinaryTree class \ No newline at end of file diff --git a/Assignment2/Q2/src/Main.java b/Assignment2/Q2/src/Main.java deleted file mode 100644 index 0babb90..0000000 --- a/Assignment2/Q2/src/Main.java +++ /dev/null @@ -1,29 +0,0 @@ -import java.util.*; -/** - * Created by Alice Colt. - * - */ -public class Main { - - public static void main(String[] args) { - - BinaryTree bst = new BinaryTree(new NodeBinaryTree(16)); - // Hardcoded Test - - bst.insert(new NodeBinaryTree(9)); - bst.insert(new NodeBinaryTree(18)); - bst.insert(new NodeBinaryTree(3)); - bst.insert(new NodeBinaryTree(14)); - bst.insert(new NodeBinaryTree(19)); - bst.insert(new NodeBinaryTree(5)); - bst.insert(new NodeBinaryTree(1)); - - - int value1 = Integer.parseInt(args[0]); - int value2 = Integer.parseInt(args[1]); - - System.out.print("Lowest Common Ancestors for ' " + value1 + "' and '" + value2 + "' is: "); - int x = bst.commonAncestor(value1, value2); - System.out.println(x); - } -} \ No newline at end of file diff --git a/Assignment2/Q2/src/NodeBinaryTree.java b/Assignment2/Q2/src/NodeBinaryTree.java deleted file mode 100644 index 60ccd74..0000000 --- a/Assignment2/Q2/src/NodeBinaryTree.java +++ /dev/null @@ -1,93 +0,0 @@ -import javax.xml.soap.Node; - -/** - * Created by Alice Colt. - * - * Implementation of Node for BinaryTree with constructor, getters, setters and compareTo - * - */ -public class NodeBinaryTree -{ - // Value of node - private int value; - - // References to root, left and right - - private NodeBinaryTree root; - private NodeBinaryTree left; - private NodeBinaryTree right; - - - // Constructor - public NodeBinaryTree(int data) - { - value = data; - }// - - public NodeBinaryTree(int data, NodeBinaryTree valueL, NodeBinaryTree valueR) - { - value = data; - left = valueL; - right = valueR; - } - - - // Getters - public int getValue() - { - return value; - }//getData - - - public NodeBinaryTree getParent() - { - return root; - }//getRoot - - - public void setParent(NodeBinaryTree parent) - { - root = parent; - }//setParent - - - public NodeBinaryTree getLeft() - { - return left; - }//getLeft - public NodeBinaryTree getRight() - { - return right; - }//getRight - - - // Setters - public void setData(int data) - { - value = data; - }//setData - - public void setLeft(NodeBinaryTree leftChild) - { - left = leftChild; - }//setLeft - public void setRight(NodeBinaryTree rightChild) - { - right = rightChild; - }//setRight - - - // Compare two integers; - public int compareTo(NodeBinaryTree node2) - { - int value1 = this.getValue(); - int value2 = node2.getValue(); - - if (value1 > value2) - return -1; - else if (value1 < value2) - return 1; - else - return 0; - }// compareTo -}//NodeBinaryTree \ No newline at end of file diff --git a/Assignment3/Dictionary.java b/Assignment3/Dictionary.java new file mode 100644 index 0000000..e7ba0c8 --- /dev/null +++ b/Assignment3/Dictionary.java @@ -0,0 +1,18 @@ +/** + * Created by Alice Colt. + * + * + * Interface Word Dictionary + */ + +public interface Dictionary +{ + + // Returns if the given 'str' is a valid word + boolean isWord(String str); + + // Returns if the given 'str' is a prefix of at least one word in the dictionary + boolean isPrefix(String str); + + +}// Dictionary interface diff --git a/Assignment3/WordSearch.java b/Assignment3/WordSearch.java new file mode 100644 index 0000000..84f3017 --- /dev/null +++ b/Assignment3/WordSearch.java @@ -0,0 +1,113 @@ +import java.util.*; + +/** + * Created by Alice Colt. + */ +public class WordSearch { + + + // Search for a WORD - using list + + static List wordSearchGrid(char[][] characters, + int row, + int column, + Dictionary dictionary){ + + + // List for keeping all the solutions + List solution = new LinkedList<>(); + + boolean[][] visited = new boolean[row][column]; + + + for (int index = 0; index < row; index++) + { + for (int indexj = 0; indexj < column; indexj++) + { + Set validWord = wordGetter(Character.toString(characters[index][indexj]), + index, indexj, + row, column, + characters,visited,dictionary); + + // Add to solution list + solution.addAll(validWord); + }//for + }//for + + + // Return solution list + return new ArrayList<>(solution); + + }// List wordSearchInGrid + + + + // Get word - using hashes + + static Set wordGetter(String prefix, + int indexRow, int row, + int indexColumn, int column, + char[][] characters, + boolean[][] visited, + Dictionary dictionary) + { + + // Set for keeping all solutions + Set solution = new HashSet<>(); + + // If prefix is part of a dictionary word, add to solutions. + if(dictionary.isWord((prefix))) + { + solution.add(prefix); + }// if + // Else return solution word without adding anything + else + { + return solution; + }// else + + + + int newRow = 0; + int newCol = 0; + + for(int index = -1; index < 2; index++) + { + for (int indexj = -1; indexj < 2; indexj++) + { + newRow = indexRow + index; + newCol = indexColumn + indexj; + + //if not out of grid + if((newRow >= 0) && (newRow < indexRow ) && + newCol >= 0 && (newCol < indexColumn) && + !visited[newRow][newCol]) + { + + String newSol = prefix + characters[newRow][newCol]; + + /* + Mark the visited one after getting the word in words set + Does not need to be taken again in the same wordsSolution set + */ + visited[indexRow][indexColumn] = true; + + + Set wordsSolution = wordGetter(newSol, newRow, indexRow, + newCol, indexColumn, + characters, visited, dictionary); + + + visited[newRow][newCol] = false; + solution.addAll(wordsSolution); + + }// if + }//for + }//for + + return solution; + + }// wordGetter + + +}//WordSearch \ No newline at end of file From 4ab86d12057ea5f6423e0376ee97a2f002668a46 Mon Sep 17 00:00:00 2001 From: alicee06 Date: Thu, 22 Jun 2017 12:01:43 +0100 Subject: [PATCH 20/21] Assignment2 --- Assignment2/Q1/BinaryTree.java | 93 ++++++++++++++++ Assignment2/Q1/Main.java | 27 +++++ Assignment2/Q1/NodeBinaryTree.java | 93 ++++++++++++++++ Assignment2/Q2/BinaryTree.java | 167 +++++++++++++++++++++++++++++ Assignment2/Q2/Main.java | 29 +++++ Assignment2/Q2/NodeBinaryTree.java | 93 ++++++++++++++++ 6 files changed, 502 insertions(+) create mode 100644 Assignment2/Q1/BinaryTree.java create mode 100644 Assignment2/Q1/Main.java create mode 100644 Assignment2/Q1/NodeBinaryTree.java create mode 100644 Assignment2/Q2/BinaryTree.java create mode 100644 Assignment2/Q2/Main.java create mode 100644 Assignment2/Q2/NodeBinaryTree.java diff --git a/Assignment2/Q1/BinaryTree.java b/Assignment2/Q1/BinaryTree.java new file mode 100644 index 0000000..80175f3 --- /dev/null +++ b/Assignment2/Q1/BinaryTree.java @@ -0,0 +1,93 @@ +/** + * Created by Alice Colt. + * + * Implementaiton of a Binary Tree with functions for insert, serach and takeAncestors + * + */ +public class BinaryTree { + private NodeBinaryTree root; + + //Constructor + public BinaryTree(NodeBinaryTree new_root){ + root = new_root; + } + + + public void insert(NodeBinaryTree new_node) { + + NodeBinaryTree actualNode = root; + + if (new_node != null) { + + while (actualNode != null) { + if (new_node.compareTo(actualNode) == 1 && actualNode.getLeft() == null) { + actualNode.setLeft(new_node); + new_node.setParent(actualNode); + break; + }//if + else if (new_node.compareTo(actualNode) == -1 && actualNode.getRight() == null) { + actualNode.setRight(new_node); + new_node.setParent(actualNode); + break; + }//else if + else if (new_node.compareTo(actualNode) == 0) { + System.out.println(" The node added is a duplicate "); + break; + }//else if + else { + if (new_node.compareTo(actualNode) == 1) { + actualNode = actualNode.getLeft(); + }//if + else if(new_node.compareTo(actualNode) == -1) { + actualNode = actualNode.getRight(); + }//else if + }//else + }//while + }//if + }//insert + + + // Get ancestors + public void takeAncestors(int value) + { + NodeBinaryTree actualNode = root; + + // Check if child is null + if (actualNode == null) { + System.out.println("Node null"); + }//if + + // Search Call + actualNode = search(root, value); + + // Print ancestors + while (actualNode != root) + { + System.out.print(actualNode.getParent().getValue() + " "); + actualNode = actualNode.getParent(); + }//while + }//takeAncestors + + + // Function for search + private NodeBinaryTree search(NodeBinaryTree node, int value) + { + while ((node != null)) + { + if (node.getValue() > value) + node = node.getLeft(); + else + if (node.getValue() < value) + node = node.getRight(); + else + return node; + + // Recursive search + return search(node, value); + }//while + + // Return node + return node; + }// search + +}//BinaryTree class \ No newline at end of file diff --git a/Assignment2/Q1/Main.java b/Assignment2/Q1/Main.java new file mode 100644 index 0000000..676d9dd --- /dev/null +++ b/Assignment2/Q1/Main.java @@ -0,0 +1,27 @@ +import java.util.*; +/** + * Created by Alice Colt. + * + */ +public class Main { + + public static void main(String[] args) { + + BinaryTree bst = new BinaryTree(new NodeBinaryTree(16)); + // Hardcoded Test + + bst.insert(new NodeBinaryTree(9)); + bst.insert(new NodeBinaryTree(18)); + bst.insert(new NodeBinaryTree(3)); + bst.insert(new NodeBinaryTree(14)); + bst.insert(new NodeBinaryTree(19)); + bst.insert(new NodeBinaryTree(5)); + bst.insert(new NodeBinaryTree(1)); + + int value = Integer.parseInt(args[0]); + System.out.print("Ancestors for ' " + value + "' are: "); + bst.takeAncestors(value); + System.out.println(); + + } +} \ No newline at end of file diff --git a/Assignment2/Q1/NodeBinaryTree.java b/Assignment2/Q1/NodeBinaryTree.java new file mode 100644 index 0000000..60ccd74 --- /dev/null +++ b/Assignment2/Q1/NodeBinaryTree.java @@ -0,0 +1,93 @@ +import javax.xml.soap.Node; + +/** + * Created by Alice Colt. + * + * Implementation of Node for BinaryTree with constructor, getters, setters and compareTo + * + */ +public class NodeBinaryTree +{ + // Value of node + private int value; + + // References to root, left and right + + private NodeBinaryTree root; + private NodeBinaryTree left; + private NodeBinaryTree right; + + + // Constructor + public NodeBinaryTree(int data) + { + value = data; + }// + + public NodeBinaryTree(int data, NodeBinaryTree valueL, NodeBinaryTree valueR) + { + value = data; + left = valueL; + right = valueR; + } + + + // Getters + public int getValue() + { + return value; + }//getData + + + public NodeBinaryTree getParent() + { + return root; + }//getRoot + + + public void setParent(NodeBinaryTree parent) + { + root = parent; + }//setParent + + + public NodeBinaryTree getLeft() + { + return left; + }//getLeft + public NodeBinaryTree getRight() + { + return right; + }//getRight + + + // Setters + public void setData(int data) + { + value = data; + }//setData + + public void setLeft(NodeBinaryTree leftChild) + { + left = leftChild; + }//setLeft + public void setRight(NodeBinaryTree rightChild) + { + right = rightChild; + }//setRight + + + // Compare two integers; + public int compareTo(NodeBinaryTree node2) + { + int value1 = this.getValue(); + int value2 = node2.getValue(); + + if (value1 > value2) + return -1; + else if (value1 < value2) + return 1; + else + return 0; + }// compareTo +}//NodeBinaryTree \ No newline at end of file diff --git a/Assignment2/Q2/BinaryTree.java b/Assignment2/Q2/BinaryTree.java new file mode 100644 index 0000000..03d40d9 --- /dev/null +++ b/Assignment2/Q2/BinaryTree.java @@ -0,0 +1,167 @@ +/** + * Created by Alice Colt. + * + * Implementaiton of a Binary Tree with functions for insert, search and takeAncestors and commonAncestors + * + */ +public class BinaryTree { + private NodeBinaryTree root; + + //Constructor + public BinaryTree(NodeBinaryTree new_root){ + root = new_root; + } + + + public void insert(NodeBinaryTree new_node) { + + NodeBinaryTree actualNode = root; + + if (new_node != null) { + + while (actualNode != null) { + if (new_node.compareTo(actualNode) == 1 && actualNode.getLeft() == null) { + actualNode.setLeft(new_node); + new_node.setParent(actualNode); + break; + }//if + else if (new_node.compareTo(actualNode) == -1 && actualNode.getRight() == null) { + actualNode.setRight(new_node); + new_node.setParent(actualNode); + break; + }//else if + else if (new_node.compareTo(actualNode) == 0) { + System.out.println(" The node added is a duplicate "); + break; + }//else if + else { + if (new_node.compareTo(actualNode) == 1) { + actualNode = actualNode.getLeft(); + }//if + else if(new_node.compareTo(actualNode) == -1) { + actualNode = actualNode.getRight(); + }//else if + }//else + }//while + }//if + }//insert + + + // Get ancestors + public void takeAncestors(int value) + { + NodeBinaryTree actualNode = root; + + // Check if child is null + if (actualNode == null) { + System.out.println("Node null"); + }//if + + // Search Call + actualNode = search(root, value); + + // Print ancestors + while (actualNode != root) + { + System.out.print(actualNode.getParent().getValue() + " "); + actualNode = actualNode.getParent(); + }//while + }//takeAncestors + + + // Function for search + private NodeBinaryTree search(NodeBinaryTree node, int value) + { + while ((node != null)) + { + if (node.getValue() > value) + node = node.getLeft(); + else + if (node.getValue() < value) + node = node.getRight(); + else + return node; + + // Recursive search + return search(node, value); + }//while + + // Return node + return node; + }// search + + +/* +* +* A more complicated method +* Takes the node with the smallest value +* Work the way up, searching for the second node +* It keeps track of the common ancestor +* + public int commonAncestor1(int value1, int value2) + { + NodeBinaryTree currentNode; + NodeBinaryTree currentParent; + + if(value1 < value2) + { + currentNode = search(root, value1); + }//if + else + { + currentNode = search(root, value2); + }//else + + NodeBinaryTree copyParent = currentNode.getParent(); + currentNode = currentNode.getParent(); + + if(currentNode.getValue() == value2) + { + return currentNode.getParent().getValue(); + }//if + + copyParent = currentNode.getParent(); + + while(currentNode != root) + { + if(currentNode.getValue() == value2) + { + return currentNode.getParent().getValue(); + } + else + if(currentNode == root) + return root.getValue(); + else + { + if(currentNode.getValue() > value2) + { + copyParent = currentNode; + currentNode = currentNode.getRight(); + } + else + { + copyParent = currentNode; + currentNode = currentNode.getLeft(); + } + } + } + return copyParent.getValue(); + }//commonAncestor +*/ + + // Lowest Common Ancestor + public int commonAncestor(int value1, int value2) + { + NodeBinaryTree currentNode = root; + + while(true) + if(value1 < currentNode.getValue() && value2 < currentNode.getValue()) + currentNode = currentNode.getLeft(); + else + if(value1 > currentNode.getValue() && value2 > currentNode.getValue()) + currentNode = currentNode.getRight(); + else + return currentNode.getValue(); + }//commonAncestor + +}//BinaryTree class \ No newline at end of file diff --git a/Assignment2/Q2/Main.java b/Assignment2/Q2/Main.java new file mode 100644 index 0000000..0babb90 --- /dev/null +++ b/Assignment2/Q2/Main.java @@ -0,0 +1,29 @@ +import java.util.*; +/** + * Created by Alice Colt. + * + */ +public class Main { + + public static void main(String[] args) { + + BinaryTree bst = new BinaryTree(new NodeBinaryTree(16)); + // Hardcoded Test + + bst.insert(new NodeBinaryTree(9)); + bst.insert(new NodeBinaryTree(18)); + bst.insert(new NodeBinaryTree(3)); + bst.insert(new NodeBinaryTree(14)); + bst.insert(new NodeBinaryTree(19)); + bst.insert(new NodeBinaryTree(5)); + bst.insert(new NodeBinaryTree(1)); + + + int value1 = Integer.parseInt(args[0]); + int value2 = Integer.parseInt(args[1]); + + System.out.print("Lowest Common Ancestors for ' " + value1 + "' and '" + value2 + "' is: "); + int x = bst.commonAncestor(value1, value2); + System.out.println(x); + } +} \ No newline at end of file diff --git a/Assignment2/Q2/NodeBinaryTree.java b/Assignment2/Q2/NodeBinaryTree.java new file mode 100644 index 0000000..60ccd74 --- /dev/null +++ b/Assignment2/Q2/NodeBinaryTree.java @@ -0,0 +1,93 @@ +import javax.xml.soap.Node; + +/** + * Created by Alice Colt. + * + * Implementation of Node for BinaryTree with constructor, getters, setters and compareTo + * + */ +public class NodeBinaryTree +{ + // Value of node + private int value; + + // References to root, left and right + + private NodeBinaryTree root; + private NodeBinaryTree left; + private NodeBinaryTree right; + + + // Constructor + public NodeBinaryTree(int data) + { + value = data; + }// + + public NodeBinaryTree(int data, NodeBinaryTree valueL, NodeBinaryTree valueR) + { + value = data; + left = valueL; + right = valueR; + } + + + // Getters + public int getValue() + { + return value; + }//getData + + + public NodeBinaryTree getParent() + { + return root; + }//getRoot + + + public void setParent(NodeBinaryTree parent) + { + root = parent; + }//setParent + + + public NodeBinaryTree getLeft() + { + return left; + }//getLeft + public NodeBinaryTree getRight() + { + return right; + }//getRight + + + // Setters + public void setData(int data) + { + value = data; + }//setData + + public void setLeft(NodeBinaryTree leftChild) + { + left = leftChild; + }//setLeft + public void setRight(NodeBinaryTree rightChild) + { + right = rightChild; + }//setRight + + + // Compare two integers; + public int compareTo(NodeBinaryTree node2) + { + int value1 = this.getValue(); + int value2 = node2.getValue(); + + if (value1 > value2) + return -1; + else if (value1 < value2) + return 1; + else + return 0; + }// compareTo +}//NodeBinaryTree \ No newline at end of file From 5c2543431a693a3e6b332bba109f34d9bde045e5 Mon Sep 17 00:00:00 2001 From: alicee06 Date: Mon, 26 Jun 2017 23:42:36 +0100 Subject: [PATCH 21/21] Assignment4 --- Assignment4/CountingIslands.java | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Assignment4/CountingIslands.java diff --git a/Assignment4/CountingIslands.java b/Assignment4/CountingIslands.java new file mode 100644 index 0000000..33a3cd3 --- /dev/null +++ b/Assignment4/CountingIslands.java @@ -0,0 +1,58 @@ +import java.util.*; +import java.lang.*; +import java.io.*; + +public class CountingIslands +{ + int CountingIslands(int noRows, int noCols, boolean[][] landOrWater) + { + int index, indexj; + // Make a bool array to mark visited cells. + boolean visited[][] = new boolean[noRows][noCols]; + + // Count the islands + int count = 0; + + for (index = 0; index < noRows; index++) + for (indexj = 0; indexj < noCols; indexj++) + if (landOrWater[index][indexj] == true && !visited[index][indexj]) + { + DFS(index, indexj,landOrWater, visited); + count = count++; + } + + return count; + } + + + // Check if cell: (indexRow, indexCol) can be made + boolean isSafe(int indexRow, int indexCol, boolean landOrWater[][], + boolean visited[][]) + { + // Check if indexRow and indexCol numbers are in the landOrWater range + // Check if current position is land or water + return (indexRow < noRows) && (indexRow >= 0) && + (indexCol < noCols) && (indexCol >= 0) && + (landOrWater[indexRow][indexCol] == true) && (!visited[indexRow][indexCol]); + } + + + // DFS taking the 4 neighbors + void DFS(int indexRow, int indexCol, boolean landOrWater[][], boolean visited[][]) + { + // Mark cell - visited + visited[indexRow][indexCol] = true; + + + // Array with neighbors + int rowNeighb[] = new int[] {0, -1, 0, 1}; + int colNeighb[] = new int[] {-1, 0, 1, 0}; + + + // DFS for all neighbours + for (int index = 0; index < 4; index++) + if (isSafe(indexRow + rowNeighb[index], indexCol + colNeighb[index], landOrWater, visited) ) + DFS(indexRow + rowNeighb[index], indexCol + colNeighb[index],landOrWater, visited); + } + +} \ No newline at end of file