Groovy web console

subscribe to the feed Subscribe
to this
site

HomeWork

Published 6 months ago by Nguyen Ngoc Tuan
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
// HOMEWORK
//
// You can use either Groovy or Java.
//
// Design a routine that will calculate the average Product price per Group.
//
// The Price of each Product is calculated as:
// Cost * (1 + Margin)
//
// Assume there can be a large number of products and a large number of categories.
//
// Plus points:
// - use Groovy and its closures
// - make the category look-up performance effective
// - use method Collection.inject
 
// contains information about [Product, Group, Cost]
def products = [
    ["A", "G1", 20.1],
    ["B", "G2", 98.4],
    ["C", "G1", 49.7],
    ["D", "G3", 35.8],
    ["E", "G3", 105.5],
    ["F", "G1", 55.2],
    ["G", "G1", 12.7],
    ["H", "G3", 88.6],
    ["I", "G1", 5.2],
    ["J", "G2", 72.4]]
 
// contains information about Category classification based on product Cost
// [Category, Cost range from (inclusive), Cost range to (exclusive)]
// i.e. if a Product has Cost between 0 and 25, it belongs to category C1
def category = [
    ["C3", 50, 75],
    ["C4", 75, 100],
    ["C2", 25, 50],
    ["C5", 100, null],
    ["C1", 0, 25]]
 
// contains information about margins for each product Category
// [Category, Margin (either percentage or absolute value)]
def margins = [
    "C1" : "20%",
    "C2" : "30%",
    "C3" : "0.4",
    "C4" : "50%",
    "C5" : "0.6"]
 
// ---------------------------
//
// YOUR CODE GOES BELOW THIS LINE
//
// Assign the 'result' variable so the assertion at the end validates
//
// ---------------------------
 
def result1 = null
 
// ---------------------------

category = category.sort{ a,b -> a[1] <=> b[1] }

// ---------------------------

String categoryLookup(int min,int max, float cost, category){
    int i = (int)(min+max)/2;
    lowCost = category.get(i).get(1);
    highCost = category.get(i).get(2);
    if ((lowCost <= cost) && (cost < highCost)){
        return category.get(i).get(0);
    }
    if (cost >= highCost){
        if (i >= (max-1)){
            return category.get(max).get(0);
        }
        else{
            min = i;
            return categoryLookup(min, max, cost, category);
        }
    }
    if (cost < lowCost){
        if (i <= (min+1)){
            return category.get(min).get(0);
        }
        else{
            max = i;
            return categoryLookup(min, max, cost, category);
        }
    }
}
 
// ---------------------------

float calculateMargin(String c, margins){
    String stringMargin = margins.get(c);
    if (stringMargin.contains("%")){
        margin = (Float.parseFloat(stringMargin.minus("%")))/100;  
    } 
    else{
        margin = Float.parseFloat(stringMargin);
    }
    return margin;
}

// ---------------------------
def costMap = products.inject([:]){rs, product -> if(rs[product.get(1)]){ rs[product.get(1)] +=  product.get(2)*(1 + calculateMargin(categoryLookup(0,category.size()-1, product.get(2), category),margins)) } else {rs[product.get(1)] =  product.get(2)*(1+calculateMargin(categoryLookup(0,category.size()-1, product.get(2), category),margins))}
rs}
def countMap = products.inject([:]) { rs, product -> if (rs[product.get(1)]) {rs[product.get(1)] += 1} else {rs[product.get(1)] = 1} 
rs }
result = costMap.inject([:]){rs, cost ->rs[cost.getKey()]=(cost.getValue()/countMap[cost.getKey()]).round(1)
rs}



//
// IF YOUR CODE WORKS, YOU SHOULD GET "It works!" WRITTEN IN THE CONSOLE
//
// ---------------------------
assert result == [
    "G1" : 37.5,
    "G2" : 124.5,
    "G3" : 116.1
    ] : "It doesn't work"
 
println "It works!"

// ---------------------------