Skip to content

Commit

Permalink
DP L2 problems added
Browse files Browse the repository at this point in the history
  • Loading branch information
ArunrajaShanmugavel committed Jun 24, 2012
1 parent a885081 commit 3293073
Show file tree
Hide file tree
Showing 13 changed files with 475 additions and 4 deletions.
1 change: 1 addition & 0 deletions .classpath
Expand Up @@ -2,5 +2,6 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions README
@@ -0,0 +1 @@
Here are Algorithm solutions, simple solution of common problems or Data Structures kind of stuff
Binary file modified bin/Btree/BinaryTreeExample.class
Binary file not shown.
Binary file added bin/Iw/SalesTaxCalculator.class
Binary file not shown.
Binary file added bin/Iw/SalesTaxCalculatorTest.class
Binary file not shown.
Binary file added bin/TConHold.class
Binary file not shown.
Binary file modified bin/Topcoder.class
Binary file not shown.
72 changes: 72 additions & 0 deletions bin/generic-tryouts/hello.py
@@ -0,0 +1,72 @@
print 'Hello, world!'

# 1 Types of print statements

n = raw_input('Name is -')
print 'Hi, %s'%n
print 'Name2 is'+ raw_input('Name2 is -')
t = 'test'
print t ,'hardcoded'

# 2 for loops
ar = ['a','b']
for i,name in enumerate(ar):
print "i is {i},value is {name}".format(i=i,name=name)

# 3 fibonacci
parents, babies = (1, 1)
while babies < 100:
print 'This generation has {0} babies'.format(babies)
parents, babies = (babies, parents + babies) # elements refers to previous state

# 4 funs
def test_fun(a):
print a

test_fun('arun')

# 5 import libs
import re

for t in ['12','13']:
if re.match(r'^\d{2}$',t):
print t, 'is matching'
else:
print t, 'is not matching'

# 6 high-level iteration
price = {'badam':2.1,'spices':1.1}
my_purchase = {'badam':2,'spices':1}

bill = sum(my_purchase[fruit] * price[fruit] for fruit in my_purchase)

print 'Rs.%.2f' % bill

# 7 sum of Cmd line args
#!/usr/bin/env python

import sys

try:
tot = sum (int(arg) for arg in sys.argv[1:])
print 'sum is ',tot
except ValueError:
print 'Please supply integer only'


# 8 file operatoins

import glob

a = glob.glob('*.py')

for file in sorted(a):
print ' -----'+file

with open(file) as f:
for line in f:
print ' '+line.rstrip()

print


4 changes: 2 additions & 2 deletions src/Btree/BinaryTreeExample.java
Expand Up @@ -41,10 +41,10 @@ public void run()

System.out.print("\nBFS-");bfs(rootnode);
System.out.print("\nDFS-"); dfs(rootnode);
System.out.print("\nDFS with recursion-"); dfsRec(rootnode); // postfix is same
/* System.out.print("\nDFS with recursion-"); dfsRec(rootnode); // postfix is same
System.out.println("\nInserting on BTree-"); bfs(insert(rootnode,77));
System.out.println("\nInserting on BTree-"); bfs(insertChg(rootnode,77));
System.out.println("\nInserting on BTree-"); bfs(insertChg(rootnode,77));*/

}

Expand Down
112 changes: 112 additions & 0 deletions src/Iw/SalesTaxCalculator.java
@@ -0,0 +1,112 @@
package Iw;

/**
* JUnit Test Case attached
*
* DESIGN ASPECTS considered :
*
* 1. Expandable exemptedItems list
* 2. Float (instead of Double) level calculation of price to avoid overhead
* 3. Object orientedness of functions would result in easier maintanance
*
*/
public class SalesTaxCalculator {

String exemptedItems[] = {"book","chocolate","pill"};

float fPriceSum=0, fTaxSum=0;

public String[] returnTaxedBill(String[] sItemList)
{
// Retrun Array contains result for UnitTest
String[] sRetArr = new String[sItemList.length+2];
fTaxSum = 0; fPriceSum = 0;
int i;

for(i=0;i<sItemList.length;i++)
{
String sItem= sItemList[i];

float dPrice = getPriceFromItemString(sItem) , fTaxedPrice = dPrice;

if(isSalesTaxApplicable(sItem))
{
float temp = roundOffToNearest5paise(dPrice/10.0f);
fTaxedPrice += temp;
fTaxSum += temp;
}

if(isImportTaxApplicable(sItem))
{
float temp = roundOffToNearest5paise (dPrice/20.0f);
fTaxedPrice += temp ;
fTaxSum += temp;
}

int iItemCnt = getItemCntFromItemString(sItem);

String sRndTaxedPrice = roundOffTo2DecPlaces( fTaxedPrice * iItemCnt);

// Add to total sum
fPriceSum += Float.parseFloat(sRndTaxedPrice);

// Print Current item price
String temp = sItem.substring(0,sItem.lastIndexOf("at")-1)+": "+ sRndTaxedPrice;
System.out.println(temp); sRetArr[i] = temp;
}

// Sales Tax
String temp = "Sales Taxes: "+ roundOffTo2DecPlaces(fTaxSum);
System.out.println(temp); sRetArr[i] =temp;

// Total
temp = "Total: "+roundOffTo2DecPlaces(fPriceSum);
System.out.println(temp); sRetArr[i+1] =temp;

return sRetArr;
}

boolean isSalesTaxApplicable(String sItem)
{
for(String sExmpItem : exemptedItems)
if(sItem.contains(sExmpItem))
{
return false;
}

return true;
}

boolean isImportTaxApplicable(String sItem)
{
if(sItem.contains("import"))
return true;
return false;
}

float roundOffToNearest5paise(float val)
{
return ((float)(Math.ceil(val*20)))/20.0f; // Scale down the ceil method to work at 0.05 level
}

String roundOffTo2DecPlaces(float val)
{
return String.format("%.2f", val);

// truncate on 2 decimal places

//DecimalFormat df = new DecimalFormat(".##");
// df.format(val);
}

float getPriceFromItemString(String s)
{
return Float.valueOf(s.substring(s.lastIndexOf("at")+3, s.length()));
}

int getItemCntFromItemString(String s)
{
return Integer.parseInt(s.substring(0,s.indexOf(" ")));
}

}
30 changes: 30 additions & 0 deletions src/Iw/SalesTaxCalculatorTest.java
@@ -0,0 +1,30 @@
package Iw;

import static org.junit.Assert.*;

import org.junit.Test;

public class SalesTaxCalculatorTest {

@Test
public void testSalesTax()
{
SalesTaxCalculator tester = new SalesTaxCalculator();

// Case 1
String[] op = new String[]{"1 book: 12.49","1 music CD: 16.49","1 chocolate bar: 0.85","Sales Taxes: 1.50","Total: 29.83"};
String[] ip = new String [] {"1 book at 12.49","1 music CD at 14.99","1 chocolate bar at 0.85"};
assertArrayEquals( op, tester.returnTaxedBill(ip));

// Case 2
op = new String[]{"1 imported box of chocolates: 10.50","1 imported bottle of perfume: 54.65","Sales Taxes: 7.65","Total: 65.15"};
ip = new String [] {"1 imported box of chocolates at 10.00","1 imported bottle of perfume at 47.50"};
assertArrayEquals( op, tester.returnTaxedBill(ip));

// Case 3
op = new String[]{"1 imported bottle of perfume: 32.19","1 bottle of perfume: 20.89","1 packet of headache pills: 9.75","1 imported box of chocolates: 11.85","Sales Taxes: 6.70","Total: 74.68"};
ip = new String [] {"1 imported bottle of perfume at 27.99","1 bottle of perfume at 18.99","1 packet of headache pills at 9.75","1 imported box of chocolates at 11.25"};
assertArrayEquals( op, tester.returnTaxedBill(ip));
}

}

0 comments on commit 3293073

Please sign in to comment.