From ed976b30f0b1dca13bf32da8779acd6ca71e3950 Mon Sep 17 00:00:00 2001 From: Peter McCormick Date: Thu, 22 Feb 2018 06:14:40 -0500 Subject: [PATCH 1/2] taking a break --- .idea/misc.xml | 2 +- PlaylistChallenge.iml | 2 + src/main/java/io/zipcoder/Music.java | 59 +++++++++++++++++- target/classes/io/zipcoder/Music.class | Bin 0 -> 1811 bytes .../test-classes/io/zipcoder/MusicTest.class | Bin 0 -> 1381 bytes 5 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 target/classes/io/zipcoder/Music.class create mode 100644 target/test-classes/io/zipcoder/MusicTest.class diff --git a/.idea/misc.xml b/.idea/misc.xml index e8942bd..c5f67dd 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -7,7 +7,7 @@ - + \ No newline at end of file diff --git a/PlaylistChallenge.iml b/PlaylistChallenge.iml index 54ddd53..e0c2965 100644 --- a/PlaylistChallenge.iml +++ b/PlaylistChallenge.iml @@ -13,5 +13,7 @@ + + \ 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..6623658 100644 --- a/src/main/java/io/zipcoder/Music.java +++ b/src/main/java/io/zipcoder/Music.java @@ -1,5 +1,7 @@ package io.zipcoder; +import java.util.List; + public class Music { private String[] playList; @@ -8,7 +10,60 @@ public Music(String[] playList){ this.playList = playList; } - public Integer selection(Integer startIndex, String selection){ - return null; + public Integer selection(Integer startIndex, String selection) { + Integer[] occurrenceIndex = selectionOccurrences(selection); + int lowestPushesPressDown = pressDown(startIndex, occurrenceIndex); + int lowestPushesPressUp = pressUp(startIndex, occurrenceIndex); + + if (lowestPushesPressUp <= lowestPushesPressDown) { + return lowestPushesPressUp; + } else return lowestPushesPressDown; + } + + + public Integer[] selectionOccurrences(String selection) { + Integer[] occurrenceIndex; + StringBuilder sb = new StringBuilder(); + + for (int i =0; i < playList.length; i++) { + if (playList[i].equals(selection)) { + sb.append(i + " "; + } + } + return occurrenceIndex; + + } + + public int pressUp (Integer startIndex, Integer[] occurrenceIndex) { + int lowestPushes = 0; + for (int i = 0; i startIndex) { + if (lowestPushes <= (occurrenceIndex[i] - startIndex)) { + lowestPushes = (occurrenceIndex[i] - startIndex); + } + } + if (occurrenceIndex[i] < startIndex) { + if ((Math.abs(0 - occurrenceIndex[i])) + (playList.length - startIndex) < lowestPushes) { + lowestPushes = occurrenceIndex[i] - startIndex; + } + } + } + return lowestPushes; + } + public int pressDown (Integer startIndex, Integer[] occurrenceIndex) { + int lowestPushes = 0; + for (int i = 0; i startIndex) { + if (lowestPushes <= (occurrenceIndex[i] - startIndex)) { + lowestPushes = (occurrenceIndex[i] - startIndex); + } + } + if (occurrenceIndex[i] < startIndex) { + if ((Math.abs(0 - occurrenceIndex[i])) + (playList.length - startIndex) < lowestPushes) { + lowestPushes = occurrenceIndex[i] - startIndex; + } + } + } + return lowestPushes; } } diff --git a/target/classes/io/zipcoder/Music.class b/target/classes/io/zipcoder/Music.class new file mode 100644 index 0000000000000000000000000000000000000000..70df45e134e9ff06347e2758266b82694b75bd24 GIT binary patch literal 1811 zcmd5+O>^5+6g|(DqFNDlnns}wIBoeTw(P_uZJoABUD`rtJmWN-31k7ADzZqFaiyS- zwy@?eu;>h{lo>WGUI}UMjU$n_KLv7s^@!gM_{&g%z1rNK)Dn2U4cr^^WA&N!!0-b)ZQ9!XSx?S_F&Tv zJ?UqgO1$Gm1nYT0>l<&+VQ$!3OCrx9SSfM`t`mEKFL1q~~sa$XwqDlF)JQdUAAnI*~=$Hv<)1HSh+?2F~HUfe-M$f%l}jhU)?+k2F`qih)(! zFwntG4Ide}h1(1~GI4>aaq{}sE=Mm=tjSBIJ+cRhyZ)uTcirP>oXZ{djhEH^DzOLb zs+n)fu<}a%b6$7Pk2huI6o(Bdj|r^V@ea4`E#@l(?czKikiqvQP>}k$Qn{{RiuNSE zyWB7Et6RTASdHIM_=R2p)3jw2X-iO`(@x?Pz1d+jW?<4|URsrxh9_@kHJ>tXf@gKB zd4S?QtFdqZrLCl2wXK?}sXRl;RG(wwXH$`u!9&QoG_g!uA^a4JPNPcTcL^xrJHx^w z_&QGG3}Fez*g002VC-9Xn-t3YRxwZdv&=oqm6Xo;XEeWOjLOWV;-Apgik)IxF%|1q z6khD(to)kFQsV$8o?^0DO?hs7tB4<8{nZ@vo#)>OWS|mTBi;;=-yy2S^+jSy&X;+r z;{x|37vhcBFQuIMBCwEhF0e*}{O8F`$orWw{X1itWF(tpxZ5PX1C+?H^aIXSb8i0` zrt)ql=j@|V%-sHSPF>2CCHfDl`>$oCV!x~YM9RbASd;;hDo~7kLvyH6XdTOF;uA`E zfJHpySwxA4!d3Vq;mJsNG8$Ww2`@0~LMEKA^ZzLP3Wc{Re3`iWOmpJNgL literal 0 HcmV?d00001 diff --git a/target/test-classes/io/zipcoder/MusicTest.class b/target/test-classes/io/zipcoder/MusicTest.class new file mode 100644 index 0000000000000000000000000000000000000000..3a5845d28504d1de96f858cf44e777177bdaa963 GIT binary patch literal 1381 zcmcIjSx*yD6#j-zJ3}c21_3LJiVKvjf_p&}j7E|wi6tbBPnYSnTpVU_7F_<7FG@7} zu4d1>$qX#rj1)RZdNAPeu9iS+ct&!ws?JG2>JM zS9qHuRQldbn(?-Z3=CCNPZ%sRnjg!C3|ZBA%&K*_ zDvn8P`F4-8WS#zn8z;guP1?v~n8ktps7Cw3BdBrBj_P7|2$G zBy{CtrKgoY9>Sca&!GiZ)S>{xQHHpPWqPu&A{QapSKcEgJ*i` z6O0cGI&g;1%waNv0miwb0t{k^Hv?zc+;Q0a9VS0PVAcAHj#|n|RcoeWR%>Y|U9DxD zOtta_R&Dwd?5E}dI-fb2vYBR3PS4C1%#X;HGlg^^bAYZE^EhlGcA-Gtadcw>c}!FM zJmr@ue1+^<)srLUrm6yp>^Mhtfj?w8Q zqG`XEc^bI#S9dw>u3NjyYj^oJcd0{n`Ld~P{@dLUwVow=ff~DFXli literal 0 HcmV?d00001 From c411adaacd2c535c2443a3cb1654d23dfc65e6c4 Mon Sep 17 00:00:00 2001 From: Peter McCormick Date: Thu, 22 Feb 2018 16:56:38 -0500 Subject: [PATCH 2/2] Complete --- src/main/java/io/zipcoder/Music.java | 79 ++++++++++--------------- target/classes/io/zipcoder/Music.class | Bin 1811 -> 1447 bytes 2 files changed, 31 insertions(+), 48 deletions(-) diff --git a/src/main/java/io/zipcoder/Music.java b/src/main/java/io/zipcoder/Music.java index 6623658..645c526 100644 --- a/src/main/java/io/zipcoder/Music.java +++ b/src/main/java/io/zipcoder/Music.java @@ -11,59 +11,42 @@ public Music(String[] playList){ } public Integer selection(Integer startIndex, String selection) { - Integer[] occurrenceIndex = selectionOccurrences(selection); - int lowestPushesPressDown = pressDown(startIndex, occurrenceIndex); - int lowestPushesPressUp = pressUp(startIndex, occurrenceIndex); - - if (lowestPushesPressUp <= lowestPushesPressDown) { - return lowestPushesPressUp; - } else return lowestPushesPressDown; - } - - - public Integer[] selectionOccurrences(String selection) { - Integer[] occurrenceIndex; - StringBuilder sb = new StringBuilder(); - - for (int i =0; i < playList.length; i++) { - if (playList[i].equals(selection)) { - sb.append(i + " "; - } + return pressCounts(startIndex, selection); + } + + public Integer pressCounts (Integer startIndex, String selection) { + int pressNext = pressForwards(startIndex, selection); + int pressPrevious = pressBackwards(startIndex ,selection); + if (pressNext > pressPrevious) { + return pressPrevious; } - return occurrenceIndex; - + return pressNext; } - public int pressUp (Integer startIndex, Integer[] occurrenceIndex) { - int lowestPushes = 0; - for (int i = 0; i startIndex) { - if (lowestPushes <= (occurrenceIndex[i] - startIndex)) { - lowestPushes = (occurrenceIndex[i] - startIndex); - } + public Integer pressForwards(Integer startIndex, String selection) { + int skipCount = 0; + int length = playList.length -1; + while (!playList[startIndex].equals(selection)) { + skipCount ++; + if (startIndex ==0) { + startIndex = length; + } else { + startIndex --; } - if (occurrenceIndex[i] < startIndex) { - if ((Math.abs(0 - occurrenceIndex[i])) + (playList.length - startIndex) < lowestPushes) { - lowestPushes = occurrenceIndex[i] - startIndex; - } - } - } - return lowestPushes; + } return skipCount; } - public int pressDown (Integer startIndex, Integer[] occurrenceIndex) { - int lowestPushes = 0; - for (int i = 0; i startIndex) { - if (lowestPushes <= (occurrenceIndex[i] - startIndex)) { - lowestPushes = (occurrenceIndex[i] - startIndex); - } - } - if (occurrenceIndex[i] < startIndex) { - if ((Math.abs(0 - occurrenceIndex[i])) + (playList.length - startIndex) < lowestPushes) { - lowestPushes = occurrenceIndex[i] - startIndex; - } + + + public int pressBackwards (Integer startIndex, String selection) { + int skipCount = 0; + int length = playList.length -1; + while (!playList[startIndex].equals(selection)) { + skipCount ++; + if (startIndex ==length) { + startIndex = 0; + } else { + startIndex ++; } - } - return lowestPushes; + } return skipCount; } } diff --git a/target/classes/io/zipcoder/Music.class b/target/classes/io/zipcoder/Music.class index 70df45e134e9ff06347e2758266b82694b75bd24..983aa21628467b96a937f90b156042db35a0b93e 100644 GIT binary patch literal 1447 zcma)6O;giQ6g{sEgoXeD7DYsXiuMC2Aff`pLWNFeDmtTsqizgstbv+hn&QV={{$-+ z&aiQzGYXCi_xdOFm#F6@F^!|nxOsW^zI)F-=e*Z``Tp?}fEirWpy5;k35;u8PHQ-W zw1y0_a+j023As$Fm{O4!=-93qPfJ$A73jZP+B9~Ie9f?{`5Ufd+0{jX_9e@<++~4* z^k2?1>jKKLwOwYf zlfT+*SY-}NG|ZY=cCETCu#k?>DcY`CHJ!ysDqxt2q<&QiXbsnJ+@f7EpU7D)U9FP7 zNH$d4we6UV#%kSpY&aFtOf>OK(Y_6jc<<*lGH{hxFR(&_bj zsB(lCt(i|a!XJO#F?XzblV{qEdebSJtCqA=^IV#eT6A<_T1O{z9Vd}gF{5J^XI0GU zIEQ&1=W#*DAd)(UaYCT?53>ahwM@CTu}Rkj;%RxGy{&`5Ej|;=cGrzslQdXF4Uul& zJbGh3Y8o}NPDZWpu@^IU1k@dgaP7W)OU1w48%FswH9lm4m=wod8~0u4=Gwz(mH8OI zTJ``Un|qJgTShS);@XWk*8~(IB-q`{Xuz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