0

I am having a file discount.txt that contain data in following format:

Less than $100 --> 0%
From $100 up to less than $500 --> 10%
From $500 up to less than $1,000 --> $40 PLUS 20%
From $1,000 up to less than $2,000 --> $140 PLUS 30%
$2,000 and above --> $440 PLUS 40%

It means Discount on the sale amount

Less than $100  0%
From $100 up to less than $500  10% for each dollar over $100
From $500 up to less than $1,000    $40 PLUS 20% of the total sale amount over $500
From $1,000 up to less than $2,000  $140 PLUS 30% of the total sale amount over $1,000
$2,000 and above    $440 PLUS 40% of the total sale amount over $2,000

That is, if the total sale amount is $100 then the discount will be $0. However, if the total sale amount is $101 then the discount will be $0.10. If the total sale amount is $500 then the discount will be $40, but if the total sale amount is $501 then the discount will be $40.20.

So to solve this , what I think is to have 4 lists : 1 list for storing lower limit of sale amount, one for upper limit, one for holding fixed increment corresponding to a range and one for holding additional discount. If there is no fixed increment then assume it zero.

Then for given sales amount if it lies in ith range then just do something like :

fixedIncrement[i] + (saleAmount-lowerLimit[i])*additionDiscount[i]

But problem am facing is parsing the given text file. Can someone please help to parse it and store it in lists in python

Here in given file the lists would be something like this :

lowerLimit[] = [1,100,500,1000,2000]
upperLimit[] = [100,500,1000,2000,MAX]
fixedIncrement[] = [0,0,40,140,440]
additionDiscount[] = [0,0.1,0.2,0.3,0.4]
ms8
  • 417
  • 2
  • 13
  • @PrerakSola I explained my approach. I am trying to know if there is any better alternate other than what am thinking. My code is a bit messy. I had taken each line and parsed it . I wish there is some elegant way to do it – ms8 May 20 '15 at 11:34
  • 1
    You explained your approach regarding the calculations. But the problem you have mentioned is regarding parsing the text file. So what have you tried so far to parse that text file? And what's the error/non-required behavior in that? – Prerak Sola May 20 '15 at 11:37
  • @python_slayer The approach most likely will be the same: you take a line, regexp for values and store them in a list of lists. Then you create new list of lists from i++ elements of each list in the initial list. – konart May 20 '15 at 11:40
  • @konart Why list of lists ? How am going to do it ? – ms8 May 20 '15 at 11:41
  • @python_slayer Because each line can be viewed as a list of 2-3 values and you have N of those lines. In other words you are building a matrix A and then transpose it to A'. What do you mean by "how"? – konart May 20 '15 at 11:45
  • @konart I mean please provide some answer to elaborate your comment – ms8 May 20 '15 at 11:47
  • "That is, if the total sale amount is $100 then the discount will be $0.", no that's not what "less than" typically means. – unwind May 20 '15 at 13:33

2 Answers2

0

You could follow this approach:

  1. Create a CSV file for your data. This will reduce the complexity in reading the file. The format could be something like:

lower_limit,upper_limit,fixed_increment,discount
1,100,0,0
100,500,0,0.1
500,1000,40,0.2

  1. Use the csv module of python to read the file and store the values in the corresponding lists. You can see the documentation regarding it over here: Python docs
  2. Calculate the amount as per your formula.
Prerak Sola
  • 9,517
  • 7
  • 36
  • 67
0

To open a .txt:

with open ("data.txt", "r") as myfile:
    data=myfile.readlines()

Each line should be a string in a list e.g. ['string one', string two'] Convert each string to a list of strings

all_lines = [i.split() for i in data]
>>> all_lines = [['string', 'one'], ['string', 'two']]

for l in all_lines:
    # Go through each line 

From $100 up to less than $500 --> 10% translates to:

l = ['From', '$100', 'up', 'to', 'less', 'than', '$500', '-->', '10%']

Now you should be able to parse it using logic. For example to get range parameters out of this line:

all_ranges = []
r1 = [i for i in l if "$" in i]
if l[0] == 'From':  # Contains a range       
    r = (int(r1[0][1:]), int(r1[1][1:]))
    all_ranges.append(r)

print all_ranges

>>> [(100, 500)]

edit:

elif l[0] == "Less":   # No range in this line
   r = (0, int(r1[0][1:]))  # Range is from 0 to $100
   all_ranges.append(r)
else:
    top = l[0][1:]
    all_ranges.append((top, 1000000))  # High range

>>>> [(100, 500), (0, 100), (2000, 1000000)]
kezzos
  • 3,023
  • 3
  • 20
  • 37
  • What about lines not containing 'From' ? Can you please explain in context of whole text file ? Like for first line Less than $100 its better to include range (1,100). Similarly for last line upto max limit – ms8 May 20 '15 at 12:30
  • Last doubt, How to load text file as csv string ? – ms8 May 20 '15 at 12:40
  • Use somthing like reader = csv.reader(csvfile, delimiter=' '). See http://stackoverflow.com/questions/17262256/reading-one-line-of-csv-data-in-python – kezzos May 20 '15 at 12:43
  • Sorry it was a .txt file you have, just realized. Seehttp://stackoverflow.com/questions/8369219/how-do-i-read-a-text-file-into-a-string-variable-in-python – kezzos May 20 '15 at 12:45
  • Its a txt file and not csv file. Will it work this way ? – ms8 May 20 '15 at 12:47
  • You please better add code lines from there to make a complete answer ..:) – ms8 May 20 '15 at 12:48