From 49e9bfa622c520225a2096849f721abfe270aa53 Mon Sep 17 00:00:00 2001 From: David Trombello Date: Tue, 29 Oct 2019 13:43:13 -0400 Subject: [PATCH 1/2] test pass --- .idea/$PRODUCT_WORKSPACE_FILE$ | 19 ++++ .idea/compiler.xml | 16 ++++ .idea/misc.xml | 13 +++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ PlaylistChallenge.iml | 17 ++++ pom.xml | 6 ++ src/main/java/io/zipcoder/Music.java | 135 ++++++++++++++++++++++++++- 8 files changed, 217 insertions(+), 3 deletions(-) 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 diff --git a/.idea/$PRODUCT_WORKSPACE_FILE$ b/.idea/$PRODUCT_WORKSPACE_FILE$ new file mode 100644 index 0000000..3733e0d --- /dev/null +++ b/.idea/$PRODUCT_WORKSPACE_FILE$ @@ -0,0 +1,19 @@ + + + + + + + 1.8 + + + + + + + + \ 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..d30d09e --- /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..3241bc7 --- /dev/null +++ b/PlaylistChallenge.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index bbd82c1..7442fa6 100644 --- a/pom.xml +++ b/pom.xml @@ -16,5 +16,11 @@ 4.12 test + + + org.apache.commons + commons-lang3 + 3.9 + \ 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..22c31e7 100644 --- a/src/main/java/io/zipcoder/Music.java +++ b/src/main/java/io/zipcoder/Music.java @@ -1,14 +1,143 @@ package io.zipcoder; +import org.apache.commons.lang3.ArrayUtils; +import java.lang.Math; +import java.lang.reflect.Array; + public class Music { private String[] playList; - public Music(String[] playList){ + public Music(String[] playList) { this.playList = playList; } - public Integer selection(Integer startIndex, String selection){ - return null; + public Integer selection(Integer startIndex, String selection) { + + int fwdCount = 0; + int bwdCount = 0; + +// int newSongIndex = ArrayUtils.indexOf(playList, selection); +// +// if (newSongIndex > startIndex) +// { +// +// } + + + for (int i = startIndex; i < startIndex + playList.length; i++) { + + if (playList[i].equals(selection)) { + break; + } + fwdCount++; + if(i == (playList.length-1)) + { + i = -1; + } + return fwdCount; + + } + + for (int j = startIndex; j >= 0; j--) { + if (playList[j].equals(selection)) { + break;} + bwdCount++; + if (j == 0) { + j = playList.length - 1; + } + return bwdCount; + } + + if (bwdCount < fwdCount) { + return bwdCount; + } else { + return fwdCount; + } } } + +// for (int j = startIndex; j < startIndex + playList.length - 1; j--) { +// if (!playList[playList.length + i % playList.length].equals(selection)) { +// bwdCount++; +// break; + + +// +// int newSongIndex = ArrayUtils.indexOf(playList, selection); +// int check = checkPlaylist(playList, startIndex, newSongIndex); +// +// int newSongIndex2 = checkForDuplicateIndex(playList, selection); +// //int newSongIndex2 = ArrayUtils.indexOf(playList, selection, newSongIndex); +// int check2 = checkPlaylist(playList, startIndex, newSongIndex2); +// int result = shorterClicks(check, check2); +// +// +// return result; +// } +// +// public int checkPlaylist (String[] playList, Integer startIndex, int newSongIndex) +// { +// +// int clicksFwd=0; +// int clicksBwd=0; +// int loopsAround=0; +// if (newSongIndex > startIndex) +// { +// clicksFwd = newSongIndex - startIndex; +// loopsAround = (startIndex) + ((playList.length) - newSongIndex+1); +// +// if (clicksFwd < loopsAround) +// {return clicksFwd;} +// else if (loopsAround < clicksFwd) +// {return loopsAround;} +// +// } +// else if (newSongIndex < startIndex) +// { +// clicksBwd = startIndex - newSongIndex; +// loopsAround= (newSongIndex) + ((playList.length)-startIndex); +// if (clicksBwd < loopsAround) +// {return clicksBwd;} +// else if (loopsAround < clicksBwd) +// {return loopsAround;} +// } +// return 0; +// } +// public int checkForDuplicateIndex (String[] playList, String selection) +// { +// int newSongIndex2=0; +// +// for (int i=playList.length-1; i>= 0; i--) +// { +// +// if(playList[i].equals(selection)) +// { +// newSongIndex2 = i; +// +// return newSongIndex2; +// +// } +// } +// +// +// return newSongIndex2; +// } +// +// public int shorterClicks (int check, int check2) +// { +// if (check < check2) +// { +// return check; +// } +// else if (check2 < check) +// { +// return check2; +// } +// return check; +// } + + + + + From ce2abbb5c4b295323f3607939759780792b7e4de Mon Sep 17 00:00:00 2001 From: David Trombello Date: Tue, 29 Oct 2019 13:53:28 -0400 Subject: [PATCH 2/2] all tests pass --- src/main/java/io/zipcoder/Music.java | 33 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main/java/io/zipcoder/Music.java b/src/main/java/io/zipcoder/Music.java index 22c31e7..ad2ed46 100644 --- a/src/main/java/io/zipcoder/Music.java +++ b/src/main/java/io/zipcoder/Music.java @@ -14,39 +14,40 @@ public Music(String[] playList) { public Integer selection(Integer startIndex, String selection) { + + int fwdCount = 0; int bwdCount = 0; -// int newSongIndex = ArrayUtils.indexOf(playList, selection); -// -// if (newSongIndex > startIndex) -// { -// -// } + int newSongIndex = ArrayUtils.indexOf(playList, selection); + + if (newSongIndex > startIndex) + { + } - for (int i = startIndex; i < startIndex + playList.length; i++) { - if (playList[i].equals(selection)) { - break; - } + for (int i = startIndex; i < playList.length; i++) { + + if (playList[i].equals(selection)) + {break;} + fwdCount++; + if(i == (playList.length-1)) { - i = -1; + i = -1; //because we have to include another click to go from the end to the beginning } - return fwdCount; - } for (int j = startIndex; j >= 0; j--) { if (playList[j].equals(selection)) { break;} - bwdCount++; + bwdCount++; if (j == 0) { - j = playList.length - 1; + j = playList.length; //because we have to do another click from the beginning back to the end. } - return bwdCount; + } if (bwdCount < fwdCount) {