Menu
Topics Index
...
`

Weekend Hack - 18 May 2013 - Results

Thanks to Ramana T, Pramod Jain and Soumya Tata for spending so much time, coming up with original and innovative answers and for submitting them. Thanks to many others who have tried to get the solution.
We have reviewed the solutions as per the criteria mentioned at Weekend Hack and included the comments below. Given the experience of the participants, they were really innovative and clean. Few minor comments found are included below in the order of the submission. These comments are not intended to point out the mistakes in the solutions but to learn from them.

The winner of the Weekend Hack 18 May 2013 - 1 is Pramod Jain. He will get a recharge or gift voucher worth Rs. 150.

To convert amount from one currency to another

Write a program to convert amount from one currency to another. The exchange rates between various currencies are given.

Input (List, String, double, String) Output (double)
[CNY-USD:6.16, JPY-USD:99.079, EGP-EUR:8.96, LKR-USD:126.87, USD-GBP:1.55], EGP, 1264.00, EUR 141.07143
[CNY-USD:6.16, JPY-USD:99.079, EGP-EUR:8.96, LKR-USD:126.87, USD-GBP:1.55], CNY, 499.00, LKR 10277.29383
[CNY-USD:6.16, JPY-USD:99.079, EGP-EUR:8.96, LKR-USD:126.87, USD-GBP:1.55], JPY, 2500.00, GBP 16.27896
[INR-USD:54.18, JPY-USD:99.079, CNY-USD:6.16, EGP-USD:6.88, EGP-EUR:8.96, LKR-USD:126.87, USD-GBP:1.55], INR, 5000.00, USD 92.28498
[INR-USD:54.18, JPY-USD:99.079, CNY-USD:6.16, EGP-USD:6.88, EGP-EUR:8.96, LKR-USD:126.87, USD-GBP:1.55], INR, 5000.00, EUR 70.86168
INR-USD:54.18, JPY-USD:99.079, EGP-USD:6.88, EGP-EUR:8.96, LKR-USD:126.87, USD-GBP:1.55], EGP, 1264.00, JPY 18202.88605
INR-USD:54.18, JPY-USD:99.079, EGP-USD:6.88, EGP-EUR:8.96, LKR-USD:126.87, USD-GBP:1.55], GBP, 2500.00, JPY 383931.125

Solution 1 - Submitted on Sat May 18 20:18:17 2013

import java.util.ArrayList;
import java.util.List;

class AdvancedExchangeCurrency {

    public static void main(String s[]) {
        List exchangeRates = new ArrayList();
        exchangeRates.add(new ExchangeRate("INR", "USD", 54.18));
        exchangeRates.add(new ExchangeRate("JPY", "USD", 99.079));
        exchangeRates.add(new ExchangeRate("CNY", "USD", 6.16));
        exchangeRates.add(new ExchangeRate("USD", "EGP", 0.15));
        exchangeRates.add(new ExchangeRate("EGP", "EUR", 8.96));
        exchangeRates.add(new ExchangeRate("LKR", "USD", 126.87));
        exchangeRates.add(new ExchangeRate("USD", "GBP", 1.55));
        double pounds = exchange(exchangeRates, "INR", 500.0, "GBP");
        System.out.println("500 INR is same as " + pounds + " GBP");
    }

    public static Double exchange(List exchangeRates, String sourceCurrency, double amount, String targetCurrency) {
        double answer = 0;
        for (ExchangeRate eR : exchangeRates) {
            //in the below code eR and eR1 are used for more than 1 level checking and eR2 is used for 2nd level calculation
            //------------------------------------------------------------------------------------------------------------
            if (eR.targetCurrency == sourceCurrency && eR.baseCurrency == targetCurrency)
                answer = amount / eR.rate;
            //here target and base currencies are checked directly in same order as it is.
            if (eR.baseCurrency == sourceCurrency && eR.targetCurrency == targetCurrency)
                answer = amount * eR.rate;
            //here target and base currencies are checked directly in reverse order as it before.
            //------------------------------------------------------------------------------------------------------------
            //here eR object is used for checking with target currency
            if (eR.targetCurrency == sourceCurrency) {
                for (ExchangeRate eR1 : exchangeRates) {
                    if (eR1.baseCurrency == targetCurrency) {
                        if (eR1.targetCurrency == eR.baseCurrency)
                            answer = amount / (eR.rate * eR1.rate);
                        else {
                            for (ExchangeRate eR2 : exchangeRates) {
                                if (eR2.targetCurrency == eR1.targetCurrency && eR2.baseCurrency == eR.baseCurrency)
                                    answer = (amount * eR2.rate) / (eR1.rate * eR.rate);
                            }
                        }
                    }
                    if (eR1.targetCurrency == targetCurrency && eR.baseCurrency == eR1.baseCurrency) {
                        answer = (amount * eR1.rate) / eR.rate;
                    }
                }
            }
            //------------------------------------------------------------------------------------------------------------------
            //here eR object is used for checking with base currency
            if (eR.baseCurrency == sourceCurrency) {
                for (ExchangeRate eR1 : exchangeRates) {
                    if (eR1.targetCurrency == targetCurrency) {
                        if (eR.targetCurrency == eR1.baseCurrency)
                            answer = amount * eR.rate * eR1.rate;
                        else {
                            for (ExchangeRate eR2 : exchangeRates) {
                                if (eR2.targetCurrency == eR.targetCurrency && eR2.baseCurrency == eR1.baseCurrency)
                                    answer = (amount * eR.rate * eR1.rate) / eR2.rate;
                            }
                        }
                    }
                }
            }
        }
        return answer;
    }
}

class ExchangeRate {

    String targetCurrency;
    String baseCurrency;
    double rate;

    public ExchangeRate(String targetCurrency, String baseCurrency, double rate) {
        this.targetCurrency = targetCurrency;
        this.baseCurrency = baseCurrency;
        this.rate = rate;
    }

    @Override
    public String toString() {
        return targetCurrency + "-" + baseCurrency + ":" + rate;
    }
}

Review Comments

Overall Rating (out of 10): 5

Solution 2 - Submitted on Sun May 19 09:14:09 2013

import java.util.ArrayList;
import java.util.List;

class AdvancedExchangeCurrency {

    public static void main(String s[]) {
        List exchangeRates = new ArrayList();
        exchangeRates.add(new ExchangeRate("INR", "USD", 54.18));
        exchangeRates.add(new ExchangeRate("JPY", "USD", 99.079));
        exchangeRates.add(new ExchangeRate("CNY", "USD", 6.16));
        exchangeRates.add(new ExchangeRate("USD", "EGP", 0.15));
        exchangeRates.add(new ExchangeRate("EGP", "EUR", 8.96));
        exchangeRates.add(new ExchangeRate("LKR", "USD", 126.87));
        exchangeRates.add(new ExchangeRate("USD", "GBP", 1.55));
        double pounds = exchange(exchangeRates, "INR", 500.0, "GBP");
        System.out.println("500 INR is same as " + pounds + " GBP");
    }

    public static Double exchange(List exchangeRates, String sourceCurrency, double amount, String targetCurrency) {
        for (ExchangeRate rate : exchangeRates) {
            if (targetCurrency.equals(rate.targetCurrency) && sourceCurrency.equals(rate.baseCurrency)) {
                return amount * rate.rate; // both strings matches and are in same order so amount multiplied with conversion factor(rate) and returned
            } else if (targetCurrency.equals(rate.baseCurrency) && sourceCurrency.equals(rate.targetCurrency)) {
                return amount / rate.rate; //both strings matches but are in reverse order so amount is divided with conversion factor(rate) and returned
            }
        }
        List copiedlist = new ArrayList(exchangeRates); // creating a copy of exchangeRates list
        double returnedvalue = 0.0;
        for (ExchangeRate rate : exchangeRates) {
            if (sourceCurrency.equals(rate.targetCurrency)) { // if the sourceCurrency matches to one of the targetCurrencies in the list 
                copiedlist.remove(rate); //remove the record where the match occurs from copiedlist
                returnedvalue = exchange(copiedlist, rate.baseCurrency, amount / rate.rate, targetCurrency);//recall the function with same list(but not having recent record used for conversion to avoid infinite function calls) with rate.baseCurrency as the new sourceCurrency and also convert amount accordingly)
                if (returnedvalue != 0.0) { // if above call returns 0.0 it means no further solution exists so do not enter if loop
                    return returnedvalue;
                }
            }
            if (sourceCurrency.equals(rate.baseCurrency)) { // if the sourceCurrency matches to one of the baseCurrencies in the list 
                copiedlist.remove(rate); //remove the record where the match occurs from copiedlist
                returnedvalue = exchange(copiedlist, rate.targetCurrency, amount * rate.rate, targetCurrency);//recall the function with same list(but not having recent record used for conversion to avoid infinite function calls) with rate.targetCurrency as the new sourceCurrency and also convert amount accordingly)
                if (returnedvalue != 0.0) { // if above call returns 0.0 it means no further solution exists so do not enter if loop
                    return returnedvalue;
                }
            }
        }
        return 0.0; //returns 0.0 if no further solution exists for the given parameters.
    }
}

class ExchangeRate {

    String targetCurrency;
    String baseCurrency;
    double rate;

    public ExchangeRate(String targetCurrency, String baseCurrency, double rate) {
        this.targetCurrency = targetCurrency;
        this.baseCurrency = baseCurrency;
        this.rate = rate;
    }

    @Override
    public String toString() {
        return targetCurrency + "-" + baseCurrency + ":" + rate;
    }
}

Review Comments

Overall Rating (out of 10): 7

Solution 3 - Submitted on Sun May 19 13:55:47 2013

import java.util.ArrayList;
import java.util.List;

class AdvancedExchangeCurrency {

    public static void main(String s[]) {
        List exchangeRates = new ArrayList();
        exchangeRates.add(new ExchangeRate("INR", "USD", 54.18));
        exchangeRates.add(new ExchangeRate("JPY", "USD", 99.079));
        exchangeRates.add(new ExchangeRate("CNY", "USD", 6.16));
        exchangeRates.add(new ExchangeRate("USD", "EGP", 0.15));
        exchangeRates.add(new ExchangeRate("EGP", "EUR", 8.96));
        exchangeRates.add(new ExchangeRate("LKR", "USD", 126.87));
        exchangeRates.add(new ExchangeRate("USD", "GBP", 1.55));
        double pounds = exchange(exchangeRates, "INR", 500.0, "GBP");
        System.out.println("500 INR is same as " + pounds + " GBP");
    }

    public static Double exchange(List exchangeRates, String sourceCurrency, double amount, String targetCurrency) {
        double amt1 = amount;
        String intermediate = null;
        for (ExchangeRate ex : exchangeRates) {
            ex.rate = 1 / ex.rate;// for simplification
        }
        while (true) {
            for (ExchangeRate ex1 : exchangeRates) {
                if (ex1.targetCurrency.equals(sourceCurrency) && ex1.baseCurrency.equals(targetCurrency))
                    return amount * ex1.rate; //if both sourceCurrency and targetCurrency are present directly
            }
            for (ExchangeRate ex1 : exchangeRates) {
                if (ex1.targetCurrency.equals(targetCurrency) && ex1.baseCurrency.equals(sourceCurrency))
                    return amount * (1 / ex1.rate);//if both are present but in reverse order
            }
            for (int i = 0; i < exchangeRates.size(); i++) {
                ExchangeRate ex = exchangeRates.get(i);
                if (ex.targetCurrency.equals(sourceCurrency)) //if both are not present directly 
                {
                    int flag = 0;
                    double temp = amt1;
                    String temp1 = intermediate;
                    amt1 = ex.rate * amt1;
                    intermediate = ex.baseCurrency;
                    exchangeRates.remove(ex);
                    for (ExchangeRate ex1 : exchangeRates) {
                        if (ex1.targetCurrency.equals(intermediate) || ex1.baseCurrency.equals(intermediate)) {
                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 0) {
                        amt1 = temp;
                        intermediate = temp1;
                        i--;
                        continue;
                    } else {
                        i--;
                        break;
                    }
                }
            }
            for (int i = 0; i < exchangeRates.size(); i++) {
                ExchangeRate ex = exchangeRates.get(i);
                if (ex.baseCurrency.equals(sourceCurrency)) {
                    int flag = 0;
                    double temp = amt1;
                    String temp1 = intermediate;
                    amt1 = (1 / ex.rate) * amt1;
                    intermediate = ex.targetCurrency;
                    exchangeRates.remove(ex);
                    for (ExchangeRate ex1 : exchangeRates) {
                        if (ex1.targetCurrency.equals(intermediate) || ex1.baseCurrency.equals(intermediate)) {
                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 0) {
                        amt1 = temp;
                        intermediate = temp1;
                        System.out.println("so");
                        i--;
                        continue;
                    } else {
                        i--;
                        break;
                    }
                }
            }
            for (ExchangeRate ex1 : exchangeRates) {
                if (ex1.targetCurrency.equals(intermediate) && ex1.baseCurrency.equals(targetCurrency))
                    return amt1 * ex1.rate;
                if (ex1.targetCurrency.equals(targetCurrency) && ex1.baseCurrency.equals(intermediate)) {
                    return amt1 * (1 / ex1.rate);
                }
            }
            sourceCurrency = intermediate;
        }
    }
}

class ExchangeRate {

    String targetCurrency;
    String baseCurrency;
    double rate;

    public ExchangeRate(String targetCurrency, String baseCurrency, double rate) {
        this.targetCurrency = targetCurrency;
        this.baseCurrency = baseCurrency;
        this.rate = rate;
    }

    @Override
    public String toString() {
        return targetCurrency + "-" + baseCurrency + ":" + rate;
    }
}

Review Comments

Overall Rating (out of 10): 5

© meritcampus 2019

All Rights Reserved.

Open In App