From bb368f54ab0916628b31996cbdaf1e901272ad45 Mon Sep 17 00:00:00 2001 From: Eduardo Cardenas Date: Fri, 16 Dec 2022 06:16:22 -0600 Subject: [PATCH 1/5] Groovy solutions --- solutions/groovy/MedianSort.groovy | 78 +++++++++++++++++++ solutions/groovy/MoonsAndUmbrelas.groovy | 36 +++++++++ solutions/groovy/ParentingPartnering.groovy | 49 ++++++++++++ solutions/groovy/Reversort.groovy | 83 +++++++++++++++++++++ 4 files changed, 246 insertions(+) create mode 100644 solutions/groovy/MedianSort.groovy create mode 100644 solutions/groovy/MoonsAndUmbrelas.groovy create mode 100644 solutions/groovy/ParentingPartnering.groovy create mode 100644 solutions/groovy/Reversort.groovy diff --git a/solutions/groovy/MedianSort.groovy b/solutions/groovy/MedianSort.groovy new file mode 100644 index 00000000..c44bbd24 --- /dev/null +++ b/solutions/groovy/MedianSort.groovy @@ -0,0 +1,78 @@ +//Problem to solve: Median Sort + +//Function: getIndex +//Args: list: Actual list of ordered numbers +// a: Number to place in the list +def getIndex(List list, int a){ + //Define reader for the input + def reader = new Scanner(System.in) + int i = Math.floorDiv(list.size(), 3) //Define j as 2/3 from the length of the list + int j = Math.floorDiv(list.size() * 2, 3) //Define j as 2/3 from the length of the list + int inicio = 0, fin = list.size() - 1 //Define beginning and end of the subarray + while (true){ + println(list.get(i) + " " + a + " " + list.get(j)) //Print the 3 values to work with + def res = reader.nextInt() //Read the answer for the median of the 3 values given + if(res == -1) System.exit(-1) //Finish program if answer = -1 + if(res == a){ //Case [0].....[1/3].....[a].....[2/3].....[1] + if(j - i == 1){ + return j + }else{ + inicio = i + fin = j + j = inicio + Math.floorDiv((fin - inicio) * 2, 3) + i = inicio + Math.floorDiv((fin - inicio), 3) + } + }else if( res == list.get(i)){ //Case [0].....[a].....[1/3].....[2/3].....[1] + if(i == 0){ + return 0 + }else if(j - i == 1){ + j = i + i-- + }else{ + fin = i + j = inicio + Math.floorDiv((i - inicio) * 2, 3) + i = inicio + Math.floorDiv((i - inicio), 3) + } + }else{ //Case [0].....[1/3].....[2/3].....[a].....[1] + if(j == list.size() - 1){ + return list.size() + }else if(j - i == 1){ + i = j + j++ + }else{ + inicio = j + i = j + Math.floorDiv((fin - j), 3) + j = j + Math.floorDiv((fin - j) * 2, 3) + } + } + if(i == j) j++ + } +} + +def reader = new Scanner(System.in) //Define reader for the input +def T = reader.nextInt() //Read T = Number of cases +def N = reader.nextInt() //Read N = Length of the list +def Q = reader.nextInt() //Read Q = Max number of queries +//Iteration through all the cases +1.upto(T) {ti-> + println("1 2 3") //Initial question + def l = reader.nextInt() + if(l == -1)System.exit(-1) + def list = [] //Initial list + if(l == 1) list = [2, 1, 3] + else if(l == 2) list = [1, 2, 3] + else list = [1, 3, 2] + while(list.size() < N){ //Iterate every number until the length of the final list + def nextElm = list.size() + 1 + def indx = getIndex(list, nextElm) + list.add(indx, nextElm) + } + String res = "" + for(int i = 0; i < list.size(); i++){ + res = res + list.get(i) + if( i < list.size() -1)res = res + " " + } + println(res) //Print final result + def isOk = reader.nextInt() + if(isOk == -1) System.exit(-1) //If answer is -1, finish the program +} \ No newline at end of file diff --git a/solutions/groovy/MoonsAndUmbrelas.groovy b/solutions/groovy/MoonsAndUmbrelas.groovy new file mode 100644 index 00000000..4c7e99fc --- /dev/null +++ b/solutions/groovy/MoonsAndUmbrelas.groovy @@ -0,0 +1,36 @@ +//Problem to solve: Moons and Umbrellas + +class MoonsAndUmbrelas{ + static void main(String[] args) { + //Define reader for the input + def reader = new Scanner(System.in) + //T = Number of cases + def T = reader.nextInt() + //Iteration through all the cases + 1.upto(T){ti-> + def X = reader.nextInt() //X = CJ Cost + def Y = reader.nextInt() //Y = JC Cost + def S = reader.nextLine() + S = S.trim() //S = String to analyse + //With dynamic programming + def cCost = S.charAt(0) == "J"? Double.POSITIVE_INFINITY: 0 + def jCost = S.charAt(0) == "C"? Double.POSITIVE_INFINITY: 0 + for(int i = 1; i < S.length(); i++){ + def cCostNew = Double.POSITIVE_INFINITY + def jCostNew = Double.POSITIVE_INFINITY + if(S.charAt(i) == "C"){ + cCostNew = Math.min(cCost, jCost + Y) + }else if(S.charAt(i) =="J"){ + jCostNew = Math.min(jCost, cCost + X) + }else{ + cCostNew = Math.min(cCost, jCost + Y) + jCostNew = Math.min(jCost, cCost + X) + } + cCost = cCostNew + jCost = jCostNew + } + //Print the final result for the case + println("Case #" + ti + ": " + (Math.min(cCost, jCost) as int) ) + } + } +} \ No newline at end of file diff --git a/solutions/groovy/ParentingPartnering.groovy b/solutions/groovy/ParentingPartnering.groovy new file mode 100644 index 00000000..5d21961f --- /dev/null +++ b/solutions/groovy/ParentingPartnering.groovy @@ -0,0 +1,49 @@ +//Problem to solve: Parenting Partnering Returns + +//Function: isOverLap +//Args: a: List of activities from Jamie or Cameron +// b: List of activities to do +def isOverlap(List a, List b){ + if(a.get(0) == -1) return false + if ((b.get(0) >= a.get(0) && b.get(0) < a.get(1)) || (b.get(1) >= a.get(0) && b.get(1) < a.get(1))) return true + return false +} + +def reader = new Scanner(System.in) //Define reader for the input +def T = reader.nextInt() //Read T = Number of cases +//Iteration through all the cases +1.upto(T) {ti-> + def N = reader.nextInt() //Read N = Number of activities + reader.nextLine() + def list = [] //Define list of activities to sort + def activities = [] //Define list of activities unsorted + for(int i = 0; i < N; i++) activities.add(reader.nextLine().trim()) //Read all the activities + for(int i = 0; i < N; i++){ //Pass the activities from String to Integer list + String[] h = activities.get(i).split("\\s") + def tempList = [] + for(int j = 0; j < h.length; j++) tempList.add(h[j] as int) + list.add(tempList) + } + Collections.sort(list, (a, b)->{ //Sort the activities by start time + return a.get(0) - b.get(0) + }); + def cameron = [-1,-1] //Define list of activities for Cameron + def jamie = [-1,-1] //Define list of activities for Jamie + String res = "" + def fin = false + for(int i = 0; i < list.size(); i++){ + if(!isOverlap(cameron, list.get(i))){ + cameron = list.get(i) + activities.set(activities.indexOf(list.get(i).get(0) + " " +list.get(i).get(1)), "C") //Replace with "C" in the unsorted list + }else if(!isOverlap(jamie, list.get(i))){ + jamie = list.get(i) + activities.set(activities.indexOf(list.get(i).get(0) + " " +list.get(i).get(1)), "J") //Replace with "J" in the unsorted list + }else{ + println("Case #" + ti + ": IMPOSSIBLE") + fin = true + break; + } + } + for(int i = 0; i < activities.size(); i++) res = res + activities.get(i) + if(!fin) println("Case #" + ti + ": " + res) //Print the answer for the case +} \ No newline at end of file diff --git a/solutions/groovy/Reversort.groovy b/solutions/groovy/Reversort.groovy new file mode 100644 index 00000000..1feedefa --- /dev/null +++ b/solutions/groovy/Reversort.groovy @@ -0,0 +1,83 @@ +//Problem to solve: Reversort Engineering +class Reversort { + static void main(String[] args) { + //Define reader for the input + def reader = new Scanner(System.in) + //T = Number of cases + def T = reader.nextInt() + //Iteration through all the cases + 1.upto(T) { ti -> + def N = reader.nextInt() //N = Size of the list + def C = reader.nextInt() //C = Desired cost + def minValue = N - 1; //Minimum cost of a list with size N: N-1 + def maxValue = ((N * (N+1))/2)-1; + ////Maximum cost of a list with size N: (N * (N - 1)) / 2 - 1 + if(C < minValue || C > maxValue){ + println("Case #"+ti+": IMPOSSIBLE") //IMPOSSIBLE case when C is not in range [N-1, (N * (N - 1)) / 2 - 1] + }else{ + def arr = [] //List + for(int i = 0; i < N; i++){ + arr.add(i+1) + } + + def maxArr = [] //List of size N with Max value of cost + for(int i = 0; i < N/2; i++){ + maxArr.putAt(i, (i+1)*2); + } + for(int i = N-1, m = 1; i > N/2 - 1; i--,m+=2){ + maxArr.putAt(i,m); + } + String sArr = ""; //Final List + switch(C) { + case minValue: //When C = N - 1 + for(int i = 0; i < arr.size(); i++){ + sArr = sArr + (i+1); + if(i < arr.size() - 1) sArr = sArr + " "; + } + break + case maxValue: //When C = (N * (N - 1)) / 2 - 1 + for(int i = 0; i < maxArr.size(); i++){ + sArr = sArr + maxArr.get(i); + if(i < maxArr.size() - 1) sArr = sArr + " "; + } + break + default: + //When C is in range (N-1, (N * (N - 1)) / 2 - 1) + int j, k = N - 1, cost = minValue + 1, f = 0; + def tArr = arr; + def fArr = []; + while(k>0){ + j = k -1; + while(j > f-1){ + fArr = []; + fArr.addAll(tArr.subList(0, f)); + fArr.addAll(tArr.subList(f, j)); + fArr.addAll(tArr.subList(j,k+1).reverse()); + if (k + 1 < tArr.size())fArr.addAll(tArr.subList(k+1, tArr.size())) + if(cost == C){ + for(int i = 0; i < fArr.size(); i++){ + sArr = sArr + fArr.get(i); + if(i < fArr.size() - 1) sArr = sArr + " "; + } + j = f -1; + k = 0; + } + cost++; + j--; + } + if(fArr.get(f) == maxArr.get(f)){ + f++; + } + if(fArr.get(k) == maxArr.get(k)){ + k--; + } + tArr = fArr; + } + } + //Print final answer for the case + println("Case #"+ti+": " + sArr) + } + } + } + +} From bbdb78b9fc76b07662ff835cb7a1364120ee2905 Mon Sep 17 00:00:00 2001 From: David Cardenas <52186152+davidcardd1@users.noreply.github.com> Date: Fri, 23 Dec 2022 16:37:48 -0600 Subject: [PATCH 2/5] Rename MedianSort.groovy to median_sort.groovy --- solutions/groovy/{MedianSort.groovy => median_sort.groovy} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename solutions/groovy/{MedianSort.groovy => median_sort.groovy} (99%) diff --git a/solutions/groovy/MedianSort.groovy b/solutions/groovy/median_sort.groovy similarity index 99% rename from solutions/groovy/MedianSort.groovy rename to solutions/groovy/median_sort.groovy index c44bbd24..befdc37f 100644 --- a/solutions/groovy/MedianSort.groovy +++ b/solutions/groovy/median_sort.groovy @@ -75,4 +75,4 @@ def Q = reader.nextInt() //Read Q = Max number of queries println(res) //Print final result def isOk = reader.nextInt() if(isOk == -1) System.exit(-1) //If answer is -1, finish the program -} \ No newline at end of file +} From 77a289363fec26dab35482d310540e607fbc1949 Mon Sep 17 00:00:00 2001 From: David Cardenas <52186152+davidcardd1@users.noreply.github.com> Date: Fri, 23 Dec 2022 16:38:23 -0600 Subject: [PATCH 3/5] Rename MoonsAndUmbrelas.groovy to moons_and_umbrellas.groovy --- .../{MoonsAndUmbrelas.groovy => moons_and_umbrellas.groovy} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename solutions/groovy/{MoonsAndUmbrelas.groovy => moons_and_umbrellas.groovy} (99%) diff --git a/solutions/groovy/MoonsAndUmbrelas.groovy b/solutions/groovy/moons_and_umbrellas.groovy similarity index 99% rename from solutions/groovy/MoonsAndUmbrelas.groovy rename to solutions/groovy/moons_and_umbrellas.groovy index 4c7e99fc..e0098d30 100644 --- a/solutions/groovy/MoonsAndUmbrelas.groovy +++ b/solutions/groovy/moons_and_umbrellas.groovy @@ -33,4 +33,4 @@ class MoonsAndUmbrelas{ println("Case #" + ti + ": " + (Math.min(cCost, jCost) as int) ) } } -} \ No newline at end of file +} From d4ddfb00c847aa0e5910202fc27fea29df7f9289 Mon Sep 17 00:00:00 2001 From: David Cardenas <52186152+davidcardd1@users.noreply.github.com> Date: Fri, 23 Dec 2022 16:40:07 -0600 Subject: [PATCH 4/5] Rename ParentingPartnering.groovy to parenting_partnering_returns.groovy --- ...ingPartnering.groovy => parenting_partnering_returns.groovy} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename solutions/groovy/{ParentingPartnering.groovy => parenting_partnering_returns.groovy} (99%) diff --git a/solutions/groovy/ParentingPartnering.groovy b/solutions/groovy/parenting_partnering_returns.groovy similarity index 99% rename from solutions/groovy/ParentingPartnering.groovy rename to solutions/groovy/parenting_partnering_returns.groovy index 5d21961f..6e42b64d 100644 --- a/solutions/groovy/ParentingPartnering.groovy +++ b/solutions/groovy/parenting_partnering_returns.groovy @@ -46,4 +46,4 @@ def T = reader.nextInt() //Read T = Number of cases } for(int i = 0; i < activities.size(); i++) res = res + activities.get(i) if(!fin) println("Case #" + ti + ": " + res) //Print the answer for the case -} \ No newline at end of file +} From e4e8d05c52b17a1a89634d80040be628891bfda7 Mon Sep 17 00:00:00 2001 From: David Cardenas <52186152+davidcardd1@users.noreply.github.com> Date: Fri, 23 Dec 2022 16:40:41 -0600 Subject: [PATCH 5/5] Rename Reversort.groovy to reversort_engineering.groovy --- .../groovy/{Reversort.groovy => reversort_engineering.groovy} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename solutions/groovy/{Reversort.groovy => reversort_engineering.groovy} (100%) diff --git a/solutions/groovy/Reversort.groovy b/solutions/groovy/reversort_engineering.groovy similarity index 100% rename from solutions/groovy/Reversort.groovy rename to solutions/groovy/reversort_engineering.groovy