In: Computer Science
with python
Fatima wants to purchase a new dining table at an affordable price,
so she collected the models name, description and prices of
different dining tables from IKEA website and stored them in the
file DiningTables.txt.
The file contains records with the following format:
model_name(string)\ndescription\nprice (int)\n
Help Fatima get some statistics about the dining table records by performing the following tasks:
Here is a sample run for your reference:
—————-
TÄRENDÖ / GUNDE
Table and 4 chairs110 cm
231
TÄRENDÖ / ADDE
Table and 4 chairs110 cm
255
MELLTORP / ADDE
Table and 4 chairs125 cm
275
JOKKMOKK
Table and 4 chairs
595
LANEBERG / EKEDALEN
Table and 4 chairs130/190x80 cm
1375
LANEBERG / KARLJAN
Table and 4 chairs130/190x80 cm
1175
EKEDALEN
Table with 2 chairs and bench120/180 cm
1530
NORDVIKEN / LEIFARNE
Table and 4 chairs152/223x95 cm
1995
LERHAMN
Table and 2 chairs74x74 cm
495
EKEDALEN
Table and 4 chairs120/180 cm
1575
MÖRBYLÅNGA / LEIFARNE
Table and 6 chairs220x100 cm
3545
EKEDALEN
Table and 6 chairs180/240 cm
2165
IKEA PS 2012 / TEODORES
Table and 2 chairs
823
SKOGSTA / NORRARYD
Table and 6 chairs235x100 cm
3645
MELLTORP / TEODORES
Table and 4 chairs125 cm
491
MELLTORP / JANINGE
Table and 4 chairs125 cm
715
MELLTORP / TEODORES
Table and 2 chairs75x75 cm
273
MELLTORP / ADDE
Table and 2 chairs75 cm
165
INGATORP / INGOLF
Table and 4 chairs155/215 cm
1895
INGATORP / INGOLF
Table and 6 chairs155/215 cm
2685
INGATORP / INGOLF
Table and 6 chairs155/215x87 cm
3185
LANEBERG / SVENBERTIL
Table and 4 chairs130/190x80 cm
1295
NORDVIKEN / LEIFARNE
Table and 6 chairs210/289x105 cm
2745
NORDVIKEN
Table and 6 chairs210/289x105 cm
2865
NORDVIKEN
Table and 4 chairs152/223x95 cm
2075
MELLTORP / NILSOVE
Table and 2 chairs75x75 cm
785
MELLTORP / NISSE
Table and 2 folding chairs75 cm
265
MÖRBYLÅNGA / LEIFARNE
Table and 4 chairs140x85 cm
2995
VOXLÖV / ODGER
Table and 4 chairs180x90 cm
2175
EKEDALEN / ODGER
Table and 4 chairs120/180 cm
1975
MÖRBYLÅNGA / TOSSBERG
Table and 4 chairs145 cm
4275
GAMLARED / STEFAN
Table and 2 chairs85 cm
535
TINGBY / NILSOVE
Table and 4 chairs180x90 cm
2075
YPPERLIG / NILSOVE
Table and 4 chairs200x90 cm
2275
TINGBY / LEIFARNE
Table and 6 chairs180x90 cm
1745
MÖCKELBY / NORRARYD
Table and 6 chairs235x100 cm
4445
MÖCKELBY / ODGER
Table and 6 chairs235x100 cm
4265
VANGSTA / TEODORES
Table and 4 chairs120/180 cm
701
MÖRBYLÅNGA / BALTSAR
Table and 4 chairs140x85 cm
4475
MÖRBYLÅNGA / TOSSBERG
Table and 6 armchairs220x100 cm
5465
LISABO / SVENBERTIL
Table and 4 chairs140x78 cm
1295
MÖRBYLÅNGA / BALTSAR
Table and 6 chairs220x100 cm
5765
def tables_statistics(file_name):
file1 = open(file_name,"r")
l=[]
l=file1.readlines();
x=[]
y=[]
z=[]
i=0
while i<len(l):
x.append(l[i])
y.append(l[i+1])
z.append(l[i+2])
i=i+3
print("Number of records in the file is:"+str(i/3))
max=int(z[0])
min=int(z[0])
avg=0
for i in z:
if(max<int(i)):
max=int(i)
if(min>int(i)):
min=int(i)
avg=avg+int(i)
avg=avg/len(z)
ind=z.index(str(max))
ind1=z.index(str(min)+'\n')
print("\nThe most expensive dining table is")
print(x[ind]+""+y[ind]+""+z[ind])
print("\nThe cheapest dining table is")
print(x[ind1]+""+y[ind1]+""+z[ind1])
print("The average price of all dining tables in the file is:"+str(round(avg,5)))
fName="test.txt"
tables_statistics(fName)
Thank you! if you have any queries post it below in the comment
section I will try my best to resolve your queries and I will add
it to my answer if required. Please give upvote if you like
it.