Off line Minimum Coderbyte Solution

0

Have the function OffLineMinimum(strArr) take the strArr parameter being passed which will be an array of integers ranging from 1...n and the letter "E" and return the correct subset based on the following rules.The input will be in the following format: ["I","I","E","I",...,"E",...,"I"] where the I's stand for integers and the E means take out the smallest integer currently in the whole set. When finished, your program should return that new set with integers separated by commas.


For example:
 if strArr is ["5","4","6","E","1","7","E","E","3","2"] then your program should return 4,1,5.

Off Line Minimum Coderbyte Java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;


public class OffLineMinimum {
String OffLineMinimum(String[] strArr) {
List<Integer> allNumbers = new ArrayList<Integer>();
List<Integer> numbers = new ArrayList<Integer>();

for (String str : strArr) {
if (str.equals("E")) {
moveSmallestNum(allNumbers, numbers);
} else {
allNumbers.add(Integer.parseInt(str));
}
}

StringBuilder builder = new StringBuilder("");
for (int i = 0; i < numbers.size(); i++) {
builder.append(numbers.get(i));
if (i < numbers.size() - 1) builder.append(",");
}
return builder.toString();
}

void moveSmallestNum(List<Integer> allNumbers, List<Integer> numbers) {
Collections.sort(allNumbers);
Integer smallestNum = allNumbers.get(0);
numbers.add(smallestNum);
allNumbers.remove(smallestNum);
}

public static void main (String[] args) {
Scanner s = new Scanner(System.in);
OffLineMinimum c = new OffLineMinimum();
System.out.print(c.OffLineMinimum(new String[] {"5","4","6","E","1","7","E","E","3","2"}));
}
}

Off Line Minimum Coderbyte Python


def OffLineMinimum(strArr):
currentNumbers = list()
result = list()
for idx,ch in enumerate(strArr):
if unicode(ch).isnumeric():
currentNumbers.append(int(ch))
elif ch == "E":
currentNumbers = sorted(currentNumbers)
result.append(currentNumbers[0])
currentNumbers.pop(0)
return result

Off Line Minimum JavaScript Solution

Using the JavaScript language, have the function OffLineMinimum(strArr) take the strArr parameter being passed which will be an array of integers ranging from 1...n and the letter "E" and return the correct subset based on the following rules. The input will be in the following format: ["I","I","E","I",...,"E",...,"I"] where the I's stand for integers and the E means take out the smallest integer currently in the whole set. When finished, your program should return that new set with integers separated by commas. 

For example: if strArr is ["5","4","6","E","1","7","E","E","3","2"] then your program should return 4,1,5.  


function OffLineMinimum(strArr) {

var subset = [], min = 0;
function findMin (arr) { return arr.sort(function (a,b) {return Number(a)-Number(b);})[0]; }
for (var i=0;i<strArr.length;i++) {
if (strArr[i] == "E") {
min = findMin(strArr.slice(0,i));
minIndex = strArr.slice(0,i).indexOf(min)
subset.push(Number(min));
strArr = strArr.slice(0,minIndex).concat(strArr.slice(minIndex+1,i)).concat(strArr.slice(i+1));
i-=2;
}
}
return subset.join(",");
}

Explanation

This challenge requires you to repeatedly find the lowerst value in a subset of an array every time the letter E is found. So to handle this repettiion I am creating a function that will sort an array in ascending order and return the lowest value. I will loop through each entry in the array and when I encounter an E then I will pass a subset of my array starting from position 0 to the location of the E.  I will send this subset to my function to get the lowest value. I will collect all of the lowest values in an array. When finished I will convert array to string.

Disclaimer: The above Problem is generated by Coderbyte but the Solution is provided by ShouterFolk.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !