From c88ac94cc14dd7d7d8347e9afed290c18d25ac0c Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Tue, 3 Nov 2020 14:20:20 +0700 Subject: [PATCH] Finished RecursiveExrcise --- Assignment10/bin/MotzkinNumber.class | Bin 919 -> 920 bytes Assignment10/bin/MyArrayUtil.class | Bin 0 -> 1344 bytes Assignment10/bin/RecursiveExercise.class | Bin 0 -> 4029 bytes Assignment10/src/MotzkinNumber.java | 2 +- Assignment10/src/MyArrayUtil.java | 53 ++++++++ Assignment10/src/RecursiveExercise.java | 165 +++++++++++++++++++++++ Assignment10/studentList.txt | 153 +++++++++++++++++++++ 7 files changed, 372 insertions(+), 1 deletion(-) create mode 100644 Assignment10/bin/MyArrayUtil.class create mode 100644 Assignment10/bin/RecursiveExercise.class create mode 100644 Assignment10/src/MyArrayUtil.java create mode 100644 Assignment10/src/RecursiveExercise.java create mode 100644 Assignment10/studentList.txt diff --git a/Assignment10/bin/MotzkinNumber.class b/Assignment10/bin/MotzkinNumber.class index 4eb83d9a4427c1387da6cf8bcad3a6e3dfe01f2d..9bdd7d527145757e741ab4319d797af3b490a3a1 100644 GIT binary patch delta 19 bcmbQvK7)P2Hx72y6f4yf)x3>=XE6Z)LQn?$ delta 18 acmbQiKAnBSH+DAF6f4yfu8sd@F#!NOO$KrR diff --git a/Assignment10/bin/MyArrayUtil.class b/Assignment10/bin/MyArrayUtil.class new file mode 100644 index 0000000000000000000000000000000000000000..a0bc65f9bf7843d52e952f46e0331e41fe20f097 GIT binary patch literal 1344 zcmZuxNlz0|9RIz}z?9Mjs;s3@T-#}#BH#+LxR5jfF`^`fi$go=U}>9nF!t{EP)}Su zhzBrg>e0jx;0N(&+EVRbAZ!8O3gJRa zo}GWZU|04@+HR4OC7YF;AA|n~@|Z=S1dbUP-ZO239%dD*S`f!^g6`(amHOg?+jNtz z_}Mfk+ft=nD9qO8+-i;f8p)hOBdw?WGpyMqIwf2KeRQTm+DuapEw>?yvj#@?K-Enx z)oY%!9>gfdSX|yhl(GPuI4OXgC`eoWKLOhN``WuV88Yb0+`g*-KP;NRlHhX6+z1`FCb26q!Uhq!A)7!hW5aIG`asoE?sS5`#LPYAt7 z=e_8W9dvy(HR}~mHPth$2(+@wU=RPH-cj_Tk9qwnSA+=?!@xfvt4)x>eUP{UY3MJ| zB3_%i*?5bg*ZY9o)CrQ-2`5P(CFyPwJ`go{2v~-z{GY1?@CB2t@`Y5@dCdSBQfIQ;d z4PuDD)0{_eOz|35oJV<
rTL%wG+O0kEJl>^Py02P_c)0A;x=#d|Y7)Xo2H+E^` z^ElH~@`^b=cAKGd!8fp?^(#sp9#U} z%U@sQ1KGVyOR+soT}f(6(d9IvM2r#hI4th697k}Tt7(j>?CxC-P0u5YO7=pr51=LV Qo!`Wm;p545;`ngmHz1qk82|tP literal 0 HcmV?d00001 diff --git a/Assignment10/bin/RecursiveExercise.class b/Assignment10/bin/RecursiveExercise.class new file mode 100644 index 0000000000000000000000000000000000000000..517ee7fb36504f8e4818874da9ea2fb805b92a99 GIT binary patch literal 4029 zcmb7GTTmRw6+PWM`(zMkB@oL)UL>5rE`sQ33q1^$kU|Vv8!Qoo9eLOtU?kYZI}4Kh zh~Ez>ekHOUy<*EszFbtS_ybXiiufx|@{>v>pZQ7hl~Seh;i^==oSdH7T^-){*bk??J%+i#_<+(7|PbIS<~upseqFS2k;$Kc2HDbZkNmk>(6*g1+gJ4nq*A)v*p0YEr$925hFyxM?t~SJ}{H zBceJQv4zo%RI1B9Vr6Yo;BPK~lH>f83Z6*wp?kr0`9RJ{p6N5D^11r6=8T!ONmCrH zel+RWhBih?PGr*LXJd0q?_x48&-)a}sLmEvvL~H3vjavtb=FFbtNhK$DK49xP4XjUDrX+c*zWu?im=#i**#3e=OlPi zx$V+%5D#$l93O)#Vo#4a%qkk3#c|Az>Nt#Ua-sZ5gYLZcxHamKHQZA`+o$80!={zC z&1_C3q+&d*<6C%$IA#noC@qH3CJl=~s+)(GP&1IrTIsQUEhmCFfp7aUxJ;k}=WS-F zV+bedDl^U4RqkXf)BZ4L<~ZLpCieyKm_S{ZBeCeVX!p4(3foL|M7Os_ceF-#wuSH= zJmJTwg2zVyP4ha_l(a0>bDBL$Y$pO> z>Ii`Tn%0Ka8r{_z-Q619(-r{Na!bD}tYuknm_|FDT(;9LOB&U(E;Gtyr_DA$$kC=n z>ACzqIBiX&%xnlYX4IY@QLS@otDG08Z$7zHtjlJQ*rzEH(r4pYW&d4)ZL4upLRUT{ zMQ`JI9WUTT3N4p$y|3ILZZCtqOz~(K_~&dv8olxUo<3`w^p^vdvR@P^^QP8X+&qUMY(lzq+c_#W2$PdvMb0u<)s;E zn>$c&iwo*cu)N3-)xkX>N7ODK=%ST3448JB!v&f}h2wDC9*HaqhIoE2oW5dX^9d*R8l=xlf2^q`2(L0}Nt?q8p|}-W&~BhC zHiy_}h~J8N+V;mhF?Ocf63(m6eccIp=F!eP=CLzxJdfQgvj`K#24dgH|0>3=Cf%Ee zsK&{n+Krpfq6V$FpNK^faXAsQ_Lm?YRrwHcL!Q(4LTn+WrHD0=XLk2sd=X06A*#&FX+(!`i zFM-v9Z{}HQK~E8u7am`Y!o?%C1lM`et5&Dc)o>j(&0XecJ5a9#F^}GdVs%<-9*MS3!9S(d)Yt8uDm~ACpy+CjxV^59oa@v952bEcvU9wnjFOI z@)Rz}6fVjMydiUVQ$B;Y&1JW6fWA~AlK;-v{IL87F0tfD)X2Y) zjV4yoZg~gqv87><@!n_4%gB@XAzMBy$V0e{D?C~5%4%|V1pyHif5tVA2E|_SYg}h5 pB>KfC_z_!W;<%XQG1mm0*R*T+fUR=o{V6_VYaI*g4TT6F{Rd-wDVzWR literal 0 HcmV?d00001 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