From 376a9f33bab71a803b9f0ff479a88a2a3593369c Mon Sep 17 00:00:00 2001 From: Ron Duwell Date: Thu, 24 Oct 2019 22:23:36 -0400 Subject: [PATCH 1/3] mostly finished, but need to retool loops to avoid infinites --- .idea/$PRODUCT_WORKSPACE_FILE$ | 30 ++++++++++++++++ .idea/compiler.xml | 16 +++++++++ .idea/misc.xml | 13 +++++++ .idea/modules.xml | 8 +++++ .idea/vcs.xml | 6 ++++ PlaylistChallenge.iml | 16 +++++++++ src/main/java/io/zipcoder/Music.java | 33 +++++++++++++++++- src/test/java/io/zipcoder/MusicTest.java | 26 ++++++++++++++ target/classes/io/zipcoder/Music.class | Bin 0 -> 1029 bytes .../test-classes/io/zipcoder/MusicTest.class | Bin 0 -> 2269 bytes 10 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 .idea/$PRODUCT_WORKSPACE_FILE$ create mode 100644 .idea/compiler.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 PlaylistChallenge.iml create mode 100644 target/classes/io/zipcoder/Music.class create mode 100644 target/test-classes/io/zipcoder/MusicTest.class diff --git a/.idea/$PRODUCT_WORKSPACE_FILE$ b/.idea/$PRODUCT_WORKSPACE_FILE$ new file mode 100644 index 0000000..9fba8c6 --- /dev/null +++ b/.idea/$PRODUCT_WORKSPACE_FILE$ @@ -0,0 +1,30 @@ + + + + + + + 1.8.0_201 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..2bb69ef --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3e62a0b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5103107 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/PlaylistChallenge.iml b/PlaylistChallenge.iml new file mode 100644 index 0000000..0ddf51c --- /dev/null +++ b/PlaylistChallenge.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/io/zipcoder/Music.java b/src/main/java/io/zipcoder/Music.java index 180c65c..d0b845a 100644 --- a/src/main/java/io/zipcoder/Music.java +++ b/src/main/java/io/zipcoder/Music.java @@ -9,6 +9,37 @@ public Music(String[] playList){ } public Integer selection(Integer startIndex, String selection){ - return null; + int clickUp = 0; + int clickDown = 0; + + //move forward through the array + for(int i = startIndex ; i <= playList.length; i++){ + if(playList[i].equals(selection)){ + break; + } + clickUp++; + /*if we get to the end of the playlist and don't find our song, we'll have to + start at the other end and keep on counting, bewaring of an infinite loop + */ + if (i == (playList.length - 1)) { + i = 0; + } + } + + //move backwards through the array + for(int i = startIndex; i >= 0 ; i--){ + if(playList[i].equals(selection)){ + break; + } + clickDown++; + if (i == 0) { + i = playList.length - 1; + } + } + + //return whichever counter is smaller + //Math.min was auto-suggested from an if statement + //it will return the smaller number + return Math.min(clickUp, clickDown); } } diff --git a/src/test/java/io/zipcoder/MusicTest.java b/src/test/java/io/zipcoder/MusicTest.java index 03ede61..f6bfe66 100644 --- a/src/test/java/io/zipcoder/MusicTest.java +++ b/src/test/java/io/zipcoder/MusicTest.java @@ -28,4 +28,30 @@ public void selectionTest2(){ Integer actual = music.selection(startingIndex, selection); Assert.assertEquals(expected, actual); } + + @Test + public void selectionTest3(){ + + String[] playlist = {"heyjude","inmylife","letitbe","ijustseenaface", + "isawherstandingthere","tickettoride","help","paperbackwriter","harddaysnight"}; + Music music = new Music(playlist); + Integer startingIndex = 2; + String selection = "tickettoride"; + Integer expected = 3; + Integer actual = music.selection(startingIndex, selection); + Assert.assertEquals(expected, actual); + } + + @Test + public void selectionTest4(){ + + String[] playlist = {"bohemianrhapsody","killerqueen","fatbottomedgirls","underpressure", + "iwantobreakfree","somebodytolove","flashgordontheme","crazylittlethingcalledlove","wearethechampions"}; + Music music = new Music(playlist); + Integer startingIndex = 1; + String selection = "crazylittlethingcalledlove"; + Integer expected = 3; + Integer actual = music.selection(startingIndex, selection); + Assert.assertEquals(expected, actual); + } } diff --git a/target/classes/io/zipcoder/Music.class b/target/classes/io/zipcoder/Music.class new file mode 100644 index 0000000000000000000000000000000000000000..155c0819e67a1a33ec33d3a5ef19ad454703618a GIT binary patch literal 1029 zcmZuwO-~b16g{t<>Cj>LDh?_L@+I~IselqAhK+>Kq{srr1mj}bj__cZLOVr~Eq_AY zn7Cl8i6U{q4{+tmpCg{v7TPG8%$s}fyZ794?tAm|&yQ08lelN11^rPN7%*`YgC>SB zY+?kX%DrV^%)o7dmL1R8&&qNj(ETF2>Fhaa&+*sOD?v&6>$3upN79$UoIp?flKsT0 zfcCgpa0NQD(s!R!wsUUjsgv^eS7W2jmd7RiVAb&|?9U_$Y^tu2=ZU$y702U@ z?dA#AuxAr%0__dpB`4S*)V8ELJ)X%ZNMlbmSbn9BJagH2d6NuYEmuAc z2+xIQwA3-eGrFxOzan({3FGQ(>r>;$(B2_(gucVDsPAfpkBBh&P!~sMzs?IkAoXue z@1e$;_pqv{)_&RPT#dXwkHWwZkWs{tE1g>-sik${rA7`{v@KycJR&MJtR}8GX&@8%AYD4;fV$J!14Nqwg3!W>jVLgwgkm z))+ly^aG(+i6-w`C~x9g9Y1=hiGr-HSY7(L`B>qLQ@gLOtR2 z01>Agxjb0oN}BgxFr|GNTQpUcQSr5GyEv|2#7)F{KX8a{q!O$h z$n?<=5yYa!wH69`ZCAJ=kldolUCx+Fzu1I|b#k*9I?pDNr3N)o%j2PoWHQvg@VMH* zSST4GVIVg}#PM$XVtYL?Gs0xpf|U&{?LcU$>jq1%C$WawkTfGJfQW7XI+~>;k_;wu1rjMb?*CSuFBoCF(+i~P= zTeN>e27yp76BNaU2G{iniCe;JN)@0R;|YdEwUvnD#MGXUTO94HE5SD!N+6PpvAvFL zItrpq1oDl5$9^+XUSwL)Lg=Wg_-pWL4Or80T!%n--TjkWf-8iD>+@E-J2Z!Owcd;l zat}OyRA0v!T6B1JkGo*5YNO6*wU%;H)mqv~S8Ew3 zQ!W2ac5VI*v4^R*w6Ef1N~ts!rS$m=h172}R>~C8h0I&pKd3xI3>o)R0d;3+913zY zPZPKuc`DH)Es@hvwbHHJQ&oVrc{&euGjxhB&_yUbOVhx+M5FjJh4WXS_A)YDMJ$b6 zjILpoq3ft;cyDy*ZoZc;Yv{%eUCz+uhUi8+bh%Q>VE#Yd6zGmXX#pCKLftfU9fP9d zpgZwV>2CGu{^@!5BEPR|-h{!+8@x$_=TvL9bD&yd&cSLepK%W1X*-AUWX{wOy79B( z94w_^;|HLhMw`#jB8F@kpewMi0_&b)sGg${X56~QzUUcyqi^hD-`M58u`7LJD}7_1 zzQ@?l-;aL8pcf4KQG-5h(2p7P;~n%99rTkO^ixCV-I+f5q3D-@eg)`Pfqo5`*MWWm R=r@6W>!YHV(AE)J_zxkD;+Oyc literal 0 HcmV?d00001 From bb94c20db814a8070c29ab3a4ae40f644bcc656b Mon Sep 17 00:00:00 2001 From: Ron Duwell Date: Thu, 24 Oct 2019 23:16:19 -0400 Subject: [PATCH 2/3] still stuck on looping around to the beginning or end of the array --- src/main/java/io/zipcoder/Music.java | 12 +++--- src/test/java/io/zipcoder/MusicTest.java | 38 +++++++++++++----- target/classes/io/zipcoder/Music.class | Bin 1029 -> 1031 bytes .../test-classes/io/zipcoder/MusicTest.class | Bin 2269 -> 2638 bytes 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/src/main/java/io/zipcoder/Music.java b/src/main/java/io/zipcoder/Music.java index d0b845a..2366ca6 100644 --- a/src/main/java/io/zipcoder/Music.java +++ b/src/main/java/io/zipcoder/Music.java @@ -13,7 +13,8 @@ public Integer selection(Integer startIndex, String selection){ int clickDown = 0; //move forward through the array - for(int i = startIndex ; i <= playList.length; i++){ + for(int i = startIndex ; i <= playList.length - 1; i++){ + if(playList[i].equals(selection)){ break; } @@ -27,13 +28,14 @@ public Integer selection(Integer startIndex, String selection){ } //move backwards through the array - for(int i = startIndex; i >= 0 ; i--){ - if(playList[i].equals(selection)){ + for(int j = startIndex; j >= 0 ; j--){ + + if(playList[j].equals(selection)){ break; } clickDown++; - if (i == 0) { - i = playList.length - 1; + if (j == 0) { + j = playList.length - 1; } } diff --git a/src/test/java/io/zipcoder/MusicTest.java b/src/test/java/io/zipcoder/MusicTest.java index f6bfe66..7f72c60 100644 --- a/src/test/java/io/zipcoder/MusicTest.java +++ b/src/test/java/io/zipcoder/MusicTest.java @@ -6,9 +6,9 @@ public class MusicTest { @Test - public void selectionTest1(){ + public void selectionTest1() { - String[] playlist = {"wheniseeyouagain","borntorun","nothingelsematters","cecelia"}; + String[] playlist = {"wheniseeyouagain", "borntorun", "nothingelsematters", "cecelia"}; Music music = new Music(playlist); Integer startingIndex = 1; String selection = "cecelia"; @@ -18,9 +18,9 @@ public void selectionTest1(){ } @Test - public void selectionTest2(){ + public void selectionTest2() { - String[] playlist = {"dancinginthedark","rio","liveoak","liveoak"}; + String[] playlist = {"dancinginthedark", "rio", "liveoak", "liveoak"}; Music music = new Music(playlist); Integer startingIndex = 0; String selection = "liveoak"; @@ -30,10 +30,10 @@ public void selectionTest2(){ } @Test - public void selectionTest3(){ + public void selectionTest3() { - String[] playlist = {"heyjude","inmylife","letitbe","ijustseenaface", - "isawherstandingthere","tickettoride","help","paperbackwriter","harddaysnight"}; + String[] playlist = {"heyjude", "inmylife", "letitbe", "ijustseenaface", + "isawherstandingthere", "tickettoride", "help", "paperbackwriter", "harddaysnight"}; Music music = new Music(playlist); Integer startingIndex = 2; String selection = "tickettoride"; @@ -42,16 +42,32 @@ public void selectionTest3(){ Assert.assertEquals(expected, actual); } + //test to wrap around the bottom of the array @Test - public void selectionTest4(){ + public void selectionTest4() { - String[] playlist = {"bohemianrhapsody","killerqueen","fatbottomedgirls","underpressure", - "iwantobreakfree","somebodytolove","flashgordontheme","crazylittlethingcalledlove","wearethechampions"}; + String[] playlist = {"bohemianrhapsody", "killerqueen", "fatbottomedgirls", "underpressure", + "iwantobreakfree", "somebodytolove", "flashgordontheme", "crazylittlethingcalledlove", "wearethechampions"}; Music music = new Music(playlist); Integer startingIndex = 1; String selection = "crazylittlethingcalledlove"; - Integer expected = 3; + Integer expected = 4; + Integer actual = music.selection(startingIndex, selection); + Assert.assertEquals(expected, actual); + } + + + //test to wrap around the top of the array + @Test + public void selectionTes5() { + String[] playlist = {"tinydancer", "downtletthesungodownonme", "yoursong", "saturday", + "bennyandthejets", "crocodilerock", "flashgordontheme"}; + Music music = new Music(playlist); + Integer startingIndex = 5; + String selection = "tinydancer"; + Integer expected = 2; Integer actual = music.selection(startingIndex, selection); Assert.assertEquals(expected, actual); } } + diff --git a/target/classes/io/zipcoder/Music.class b/target/classes/io/zipcoder/Music.class index 155c0819e67a1a33ec33d3a5ef19ad454703618a..3221d90d5f63344491ea49e88fe6019e010b45e9 100644 GIT binary patch delta 379 zcmZ9IyG{Z@7=^#t-Ca?RTN?)m4!Zm35kud(U{nn7%Li#wW0D-m{{-t z8bU0104qydUqt+uXk(^1GjqQ4&zbAsO|bg@{Wb(Ps5=B%3}KUUSR(DPOvYh_tiR3K ztlH#nt6zpX?M|w*Ou;3DV^ee~QMReLR9UlGcO|UmvdPxVquNpJ6ea-~ab-5mhE?ig zHngXUuNXt?_?e=wJY6gIvF-@;h<8l&d;dg0%pI+IqmMuEfx>?x*0F@R#~FwEz^FI+ zOnTM;J?OT~L_11^EHmWs^K&Xp$-DRDOD3ew stgxD0;pfIm8cD*mID8c)h8nAgx(r?hFWvxo2C$$;Th~fd8LK^t3Sdc+*hb36RNf@|iOF zm3~{LcMUPA5vRx;B^KFXi9Kd$k>*gk9g(3;j$?71P@pUSXZZamWe%#s80?^d5HXp7 dnr8uPs^XFtl6N7QM!MRZlMtolo)kxjmq4Ws;~l$sW(2vh|&1Zo1C0(F6gfF|%s$l z1_bl~I?r=6S+ zH$$H0$kie_TB2E*{9GqTE9B-0b-YHx3=h1Pzq>6fQGV3&gwt|dQG@aYMU~`>ipu3n ziYnyGiW-&VF=gc=xSD(iu`1ppRF)^~jo{cZEXR&KffWd3p5iakXn}shMXX`z7?$D& eG35o3_JYWILCkqU6ucmo{|zE?h?S?S4~0K+AdwgV delta 709 zcmcIhyG{a86r5eyy%(~Dpizm&C@8ClPZaQpBI^3Wf&yv8_oFc353tw9>{nP=7&{aF z01H3B#2A0XI16O0>~xEhbI;Dbb7tnX=hUaQbkoZYvKvUQKE4A-D5sP{3=feRf4_HKUyAJUP{5HEO+vehl!WAV#c8 z?44FnqPC)&hYrf(qCmER3K2VqIMHErV+g~%VWI;g7=?#_+PKp=ysQBvIMXl Date: Fri, 25 Oct 2019 10:39:17 -0400 Subject: [PATCH 3/3] finished... again --- src/main/java/io/zipcoder/Music.java | 6 +++--- src/test/java/io/zipcoder/MusicTest.java | 8 ++++---- target/classes/io/zipcoder/Music.class | Bin 1031 -> 1031 bytes .../test-classes/io/zipcoder/MusicTest.class | Bin 2638 -> 2633 bytes 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/zipcoder/Music.java b/src/main/java/io/zipcoder/Music.java index 2366ca6..4f72e27 100644 --- a/src/main/java/io/zipcoder/Music.java +++ b/src/main/java/io/zipcoder/Music.java @@ -13,7 +13,7 @@ public Integer selection(Integer startIndex, String selection){ int clickDown = 0; //move forward through the array - for(int i = startIndex ; i <= playList.length - 1; i++){ + for(int i = startIndex ; i <= playList.length; i++){ if(playList[i].equals(selection)){ break; @@ -23,7 +23,7 @@ public Integer selection(Integer startIndex, String selection){ start at the other end and keep on counting, bewaring of an infinite loop */ if (i == (playList.length - 1)) { - i = 0; + i = -1; } } @@ -35,7 +35,7 @@ public Integer selection(Integer startIndex, String selection){ } clickDown++; if (j == 0) { - j = playList.length - 1; + j = playList.length; } } diff --git a/src/test/java/io/zipcoder/MusicTest.java b/src/test/java/io/zipcoder/MusicTest.java index 7f72c60..9a9ce71 100644 --- a/src/test/java/io/zipcoder/MusicTest.java +++ b/src/test/java/io/zipcoder/MusicTest.java @@ -51,7 +51,7 @@ public void selectionTest4() { Music music = new Music(playlist); Integer startingIndex = 1; String selection = "crazylittlethingcalledlove"; - Integer expected = 4; + Integer expected = 3; Integer actual = music.selection(startingIndex, selection); Assert.assertEquals(expected, actual); } @@ -59,13 +59,13 @@ public void selectionTest4() { //test to wrap around the top of the array @Test - public void selectionTes5() { + public void selectionTest5() { String[] playlist = {"tinydancer", "downtletthesungodownonme", "yoursong", "saturday", - "bennyandthejets", "crocodilerock", "flashgordontheme"}; + "bennyandthejets", "crocodilerock"}; Music music = new Music(playlist); Integer startingIndex = 5; String selection = "tinydancer"; - Integer expected = 2; + Integer expected = 1; Integer actual = music.selection(startingIndex, selection); Assert.assertEquals(expected, actual); } diff --git a/target/classes/io/zipcoder/Music.class b/target/classes/io/zipcoder/Music.class index 3221d90d5f63344491ea49e88fe6019e010b45e9..23c2df4412c9447d0b46a96d5115548b2576fce1 100644 GIT binary patch delta 66 zcmZqYXy@4QgGpsEgO1h~1}0HfBb{vwEHfF{mNQ7VFf)p>f<*VRq%2@yW11|(9K$Qh UAjF`|Ak3h~Aj+UQxs|yI01*KV_y7O^ delta 66 zcmZqYXy@4QgGq%YWif-6))odPQC1_JZ44|k8Q7LHNVYIDin4-4_bp&xW1cL-9K$Ql UAjF``Ak3h_Aj+UUxs|yI01xI3{Qv*} diff --git a/target/test-classes/io/zipcoder/MusicTest.class b/target/test-classes/io/zipcoder/MusicTest.class index baf2f7ab28e72d91d7c66dc4547c89cfa866a000..7e4446cea4b7237d9cfa9e8ed63a4b173e2c8077 100644 GIT binary patch delta 185 zcmX>na#Cc&Pewkz;?$hfI^P%R@+PJzLg!GwVc$dhI;1?pu6VsQp@1`D9gHVlFc VmOwr$gX?5RPH}cepk5{hCjga_AXfkY delta 195 zcmX>pa!zE!Pexwe;?$hf