Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a885081
commit 3293073
Showing
13 changed files
with
475 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Here are Algorithm solutions, simple solution of common problems or Data Structures kind of stuff |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(" "))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
|
||
} |
Oops, something went wrong.