Groovy web console

subscribe to the feed Subscribe
to this
site

Homework

Published 4 months ago by Fabiola Jimenez
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts


// ---------------------------//
// HOMEWORK
//
// Use Groovy to write a code under "YOUR CODE GOES BELOW THIS LINE" comment.
// Make sure the code is working in some of the web Groovy consoles, e.g. https://groovyconsole.appspot.com
// Do not over-engineer the solution.
//
// Assume you got some data from a customer and your task is to 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.
//
// Plus points:
// - use Groovy closures (wherever it makes sense)
// - make the category look-up performance effective
 
// 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
// ranges are mutually exclusive and the last range has a null as upper limit.
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
//
// ---------------------------
category.sort()

def getProductCategory= { cost, increment=25 ->
    //Increments ocurr by 2, this can tell us the index of the category array
    int result = cost/increment
    int index = (result<= 4?result:4)
    return category[index][0]
}


def getPrice = { cost ->
    def cat = getProductCategory(cost)
    //This we do to figure if the margin is a percentage
    def finder = margins[cat].contains("%")
    def margin = 0.0

    //Since this is a string, we have to make it a number
    if(finder){
        margin = BigDecimal.valueOf(Double.valueOf((margins[cat] - "%")))/100
    }else{
        margin = BigDecimal.valueOf(Double.valueOf(margins[cat]))
    }
    return (cost * (1 + margin))
}

def productsByGroup = [:]

//Here i save the sum plus de ammount of numbers, this is more efficient than saving every number of every group
products.each { product ->
    if(productsByGroup.containsKey(product[1])){
        productsByGroup[product[1]][0]+= getPrice(product[2])
        productsByGroup[product[1]][1]+= 1
    }else{
        productsByGroup[product[1]] = [getPrice(product[2]), 1]
    }
}

//The result calculates the average for each found group, this means the number of groups can grow
def result = [:]
productsByGroup.each{ entry ->
    result[entry.key] = (entry.value[0] / entry.value[1]).round(1)
}


// ---------------------------
//
// 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!"