diff --git a/Assignment10/bin/MotzkinNumber.class b/Assignment10/bin/MotzkinNumber.class index 4eb83d9..9bdd7d5 100644 Binary files a/Assignment10/bin/MotzkinNumber.class and b/Assignment10/bin/MotzkinNumber.class differ diff --git a/Assignment10/bin/MyArrayUtil.class b/Assignment10/bin/MyArrayUtil.class new file mode 100644 index 0000000..a0bc65f Binary files /dev/null and b/Assignment10/bin/MyArrayUtil.class differ diff --git a/Assignment10/bin/RecursiveExercise.class b/Assignment10/bin/RecursiveExercise.class new file mode 100644 index 0000000..517ee7f Binary files /dev/null and b/Assignment10/bin/RecursiveExercise.class differ diff --git a/Assignment10/src/MotzkinNumber.java b/Assignment10/src/MotzkinNumber.java index ab3d3e7..d787059 100644 --- a/Assignment10/src/MotzkinNumber.java +++ b/Assignment10/src/MotzkinNumber.java @@ -2,7 +2,7 @@ public class MotzkinNumber { public static void main(String[] args) { - for(int i=0;i<=20;i++)System.out.format("%d:%d\n",i,M(i)); + for(int i=0;i<=20;i++)System.out.format("%d:%d%n",i,M(i)); } public static int M(int n) { diff --git a/Assignment10/src/MyArrayUtil.java b/Assignment10/src/MyArrayUtil.java new file mode 100644 index 0000000..46bc170 --- /dev/null +++ b/Assignment10/src/MyArrayUtil.java @@ -0,0 +1,53 @@ +import java.util.Arrays; + +public class MyArrayUtil { + // return the first data inside the array. + public static int head(int[] a) throws Exception { + if (a == null) + throw new Exception("Array is empty! No head."); + return a[0]; + } + + // return array similar to a, but without the first data. + public static int[] tail(int[] a) throws Exception { + if (a == null) + throw new Exception("Array is empty! No tail."); + if (a.length == 1) + return null; + return Arrays.copyOfRange(a, 1, a.length); + } + + // create a new array that is like array a, but with x inserted at + // the front. + public static int[] addAsFirst(int x, int[] a) { + int[] ans; + if (a == null) { + ans = new int[1]; + ans[0] = x; + } else { + ans = new int[a.length + 1]; + ans[0] = x; + for (int i = 0; i < a.length; i++) { + ans[i + 1] = a[i]; + } + } + return ans; + } + + // print array contents out. + public static void print(int[] a) { + if (a == null) { + System.out.println("Array is empty!"); + return; + } + for (int i = 0; i < a.length; i++) { + System.out.print(a[i]); + if (i != a.length - 1) { + System.out.print(", "); + } else { + System.out.println("."); + } + } + } + +} diff --git a/Assignment10/src/RecursiveExercise.java b/Assignment10/src/RecursiveExercise.java new file mode 100644 index 0000000..9ef8d7d --- /dev/null +++ b/Assignment10/src/RecursiveExercise.java @@ -0,0 +1,165 @@ +import java.util.Arrays; + +public class RecursiveExercise { + + // return an array that has all data of ht and n, + // just like concatenating ht and n. + public static int[] append(int[] ht, int[] n) throws Exception { + if (ht == null) + return n; + if (n == null) + return ht; + int[] l = append(MyArrayUtil.tail(ht), n); + int head = MyArrayUtil.head(ht); + return MyArrayUtil.addAsFirst(head, l); + } + public static int[] reverse(int[] ht) throws Exception { + // fill your code here + if(ht == null) return null; + if (ht.length == 1) + return ht.clone(); + int temp = ht[0]; + ht[0] = ht[ht.length - 1]; + ht[ht.length - 1] = temp; + return recursiveInnerSandwich(reverse(MyArrayUtil.tail(removeLast(ht, new int[ht.length-1], 0))),ht.clone()); + } + //This method make an array sandwich excluding the bread (index 0 and index last) + public static int[] recursiveInnerSandwich(int[] src,int[] dest) throws Exception{ + if (src==null)return dest; + dest[dest.length-src.length-1] = src[0]; + recursiveInnerSandwich(MyArrayUtil.tail(src),dest); + return dest; + } + //Remove the last element + public static int[] removeLast(int[] ia1,int[] ia2,int i) { + ia2[i]=ia1[i]; + if (i==ia2.length-1)return ia2; + return removeLast(ia1,ia2,i+1); + } + public static boolean isIn(int x, int[] a) throws Exception { + // fill your code here + if (a[0] == x) + return true; + if (a.length == 1) + return false; + return isIn(x, MyArrayUtil.tail(a.clone())); + } + + public static boolean subArray(int[] a1, int[] a2) throws Exception { + // fill your code here + if (a1 == null) + return true; + if (isIn(a1[0], a2)) + return subArray(MyArrayUtil.tail(a1.clone()), a2); + if (a1.length == 1) + return true; + return false; + } + + public static int[] shift(int[] ht, int n) throws Exception { + // fill your code here + if(ht==null)return null; + if(n==0)return ht; + int temp = ht[0]; + ht = insertLast(temp,MyArrayUtil.tail(ht)); + return shift(ht,n-1); + } + public static int[] insertLast(int val,int[] ia) throws Exception{ + return reverse(MyArrayUtil.addAsFirst(val, reverse(ia))); + } + public static void main(String[] args) throws Exception { + // test append, when appending empty array with {1,2,3} + int[] a = null; + int[] b = { 1, 2, 3 }; + int[] ans1 = append(a, b); + System.out.println("Append - Expected: 1, 2, 3."); + System.out.print("Get: "); + MyArrayUtil.print(ans1); + System.out.println(""); + + // test append, when appending 2 non-empty arrays. + int[] c = { 1, 2, 3 }; + int[] d = { 4, 5, 6 }; + ans1 = append(c, d); + System.out.println("Append - Expected: 1, 2, 3, 4, 5, 6."); + System.out.print("Get: "); + MyArrayUtil.print(ans1); + System.out.println(""); + + // test reverse + int[] e = { 1, 2, 3, 4, 5 }; + ans1 = reverse(e); + System.out.println("Reverse - Expected: 5, 4, 3, 2, 1."); + System.out.print("Get: "); + MyArrayUtil.print(ans1); + System.out.println(""); + + // test isIn + boolean b1 = isIn(3, e); + System.out.println("isIn - Expected: true."); + System.out.println("Get: " + b1 + "."); + + boolean b2 = isIn(7, e); + System.out.println("isIn - Expected: false."); + System.out.println("Get: " + b2 + "."); + + // test subArray + int[] f = { 1, 3, 5 }; + int[] g = { 1, 2, 3, 4, 5 }; + int[] h = { 1, 2, 3, 4, 5 }; + int[] i = null; + int[] j = { 1, 4, 5 }; + + b1 = subArray(f, g); + System.out.println("subArray - Expected: true."); + System.out.println("Get: " + b1 + "."); + + b1 = subArray(g, h); + System.out.println("subArray - Expected: true."); + System.out.println("Get: " + b1 + "."); + + b1 = subArray(i, h); + System.out.println("subArray - Expected: true."); + System.out.println("Get: " + b1 + "."); + + b1 = subArray(f, j); + System.out.println("subArray - Expected: false."); + System.out.println("Get: " + b1 + "."); + + b1 = subArray(h, j); + System.out.println("subArray - Expected: false."); + System.out.println("Get: " + b1 + "."); + + // test shift + int[] k = shift(g, 3); + System.out.println("shift - Expected: 4, 5, 1, 2, 3."); + System.out.print("Get: "); + MyArrayUtil.print(k); + System.out.println(""); + + k = shift(g, 2); + System.out.println("shift - Expected: 3, 4, 5, 1, 2."); + System.out.print("Get: "); + MyArrayUtil.print(k); + System.out.println(""); + + k = shift(g, 8); + System.out.println("shift - Expected: 4, 5, 1, 2, 3."); + System.out.print("Get: "); + MyArrayUtil.print(k); + System.out.println(""); + + k = shift(g, 0); + System.out.println("shift - Expected: 1, 2, 3, 4, 5."); + System.out.print("Get: "); + MyArrayUtil.print(k); + System.out.println(""); + + k = shift(null, 5); + System.out.println("shift - Expected: Array empty"); + System.out.print("Get: "); + MyArrayUtil.print(k); + System.out.println(""); + } + +} diff --git a/Assignment10/studentList.txt b/Assignment10/studentList.txt new file mode 100644 index 0000000..623c2c0 --- /dev/null +++ b/Assignment10/studentList.txt @@ -0,0 +1,153 @@ +Mr. Prem Pattanapitoon +Miss Praewpun Cheevamongkol +Mr. Nutthakom Trongjitraksa +Mr. Trivit Damrongrat +Mr. Pasutha Daichi Ide +Mr. Akarawut Krinukul +Mr. Athichat Chindaudom +Miss Claire Claude Prevost +Miss Nao Komada +Miss Clemence Muriel Berenice Jean-Louis Dit Montout +Miss Kim Zaatar +Mr. Jules Joseph Jean-Claude Victor Lestrade +Miss Noor Kardache +Miss Marie Charlotte Josephine Leygue +Mr. Marin Neyret +Miss Alice Francoise Lucienne Cabane +Mr. Adrien Manhieu Zychowski +Miss Aurore Marie Garrigoux-Desmoulins +Miss Charlotte Roppelt-Saint-Lebe +Mr. Pierre Antoine Claret +Mr. Louis Pierre Nouguerede +Miss Annika Ulrike Glaseke +Mr. Kornrapee Suthicharoenpanich +Mr. Krittanut Mongkolkulsat +Mr. Krittapat Onthuam +Mr. Krittapak Udompanich +Mr. Krittaphat Maiklad +Mr. Krittawat Archathawan +Mr. Kritasak Udompongsanont +Mr. Krittin Punpermcharoenkit +Mr. Krit Uthaisang +Mr. Gawin Lohaburananont +Miss Kavisara Jantarakolica +Mr. Kasidit Pratumtin +Mr. Kongpop Panya +Mr. Korpoj Piya-asdarat +Mr. Gunn Kanchanaraksa +Mr. Kan Jitpakdi +Mr. Karnthiti Thitikarn +Mr. Chris Vataniyapramote +Miss Kunanya Vuthilertdacha +Mr. Ken Kanate Wichiramala +Mr. Krit Sanguannapaporn +Mr. Chongchate Kumpeerichaya +Mr. Jomphat Itsarawisut +Mr. Jakkaphan Ekrojrattana +Mr. Jamorn Wacharasing +Miss Jinnipha Bhumtakwong +Mr. Jitrabhanu Simaraks +Mr. Jeerapat Thansuwanwong +Mr. Jade Udomkittivorakul +Mr. Jenwit Ratanapusdeekul +Mr. Chatr Tharachatr +Mr. Chonlasit Thangkasemsan +Mr. Chanon Wanasinchai +Mr. Chitipat Trachu +Mr. Chuwong Kulrattanarak +Mr. Athichat Chindaudom +Miss Claire Claude Prevost +Miss Nao Komada +Miss Clemence Muriel Berenice Jean-Louis Dit Montout +Miss Kim Zaatar +Mr. Jules Joseph Jean-Claude Victor Lestrade +Miss Noor Kardache +Miss Marie Charlotte Josephine Leygue +Mr. Marin Neyret +Miss Alice Francoise Lucienne Cabane +Miss Chotika Truengjitvilas +Mr. Chotipat Chaiyarat +Mr. Nakrit Panjasanka +Mr. Nachanon Rimsmutchai +Mr. Naphat Pattayanun +Mr. Napat Suwansujira +Mr. Napat Asavarojkul +Mr. Natthakit Kitrueangphatchara +Mr. Natchanon Auewetchanichkul +Mr. Natthorn Suwannapasri +Miss Nutthawadee Sungvonwattana +Mr. Nattawat Chongpita +Mr. Nattawat Pornthisan +Mr. Natthawat Piyanonthaya +Mr. Nuttawut Janwimaluang +Mr. Nat Sakdasak +Mr. Thinteerachot Hansnontchot +Mr. Tinnawish Tungphitukkul +Mr. Taksin Faikhao +Mr. Thanthai Udcharchon +Mr. Tanaseth Julerttrakul +Mr. Thanachot Sappakit +Mr. Thanatath Jiravanich +Mr. Dhanabordee Mekintharanggur +Mr. Tanabodee Wanitnunttada +Mr. Thanapat Pornsirianant +Mr. Tanawat Rattanagowin +Mr. Tanat Sangwong +Miss Tananyapat Wareechuensuk +Miss Thanaree Rongkavilit +Mr. Thatchapan Pianpucktr +Mr. Tatchapol Jetanachai +Mr. Thanyapat Khorporn +Mr. Tanmine Mesatien +Mr. Tissarut Suntreekasem +Mr. Teeratat Bunsantrakul +Mr. Noparuj Tantisoonthorn +Mr. Noppasin Ajchareeyasoontorn +Mr. Norrawee Charnpinyo +Mr. Nantanit Vittayaporn +Mr. Naris Limpapaswat +Mr. Nittun Lertwirojkul +Mr. Patiphol Vongsirisap +Mr. Panoj Kamolrattanawech +Mr. Panidh Mingmalairak +Mr. Pathanin Runggeratigul +Miss Papitchaya Iemtananon +Mr. Papatchol Thientong +Mr. Noppasin Ajchareeyasoontorn +Mr. Norrawee Charnpinyo +Mr. Nantanit Vittayapo +Miss Paphawee Rattanagosol +Miss Prapaluck Pugkhem +Mr. Prapas Arayasaeng +Mr. Prat Prasertsup +Mr. Punn Damrongratnuwong +Mr. Pannaton Kerdkaewfah +Mr. Pannavich Setawanna +Mr. Panud Chamnanratanakul +Miss Pinpinut Teerawesagapawanis +Miss Punyada Jantananont +Mr. Bhunaron Sornklin +Mr. Punnatt Punnotok +Mr. Purapat Rattanatraipop +Miss Premika Prachayamongkol +Mr. Pongpao Nanami Pongponrat +Mr. Pongsapak Sajjapong +Miss Prim Viriyasopon +Mr. Pasin Kanchanarusmeechoti +Miss Pattanun Sangvijitr +Mr. Pitchakorn Watcharodomprasert +Mr. Pirat Pothavorn +Mr. Phawat Borisuitsawat +Mr. Peemapol Tangamonsiri +Mr. Yodsapat Jiravipapan +Mr. Rattanapong Wongcharoensin +Mr. Waritnan Kositcharoensuk +Mr. Sattra Piyapunsutti +Mr. Supphaset Engphaiboon +Mr. Sirapop Rojanakul +Mr. Sukit Sukittanon +Mr. Athit Arsirakarn +Miss Oranich Jamkachornkiat +Miss Unna Talomsin +Miss Reena Lim +Mr. Eisen Lance Tamayao De Guzman \ No newline at end of file