Python

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Damaged Cargo:

lencon=input()
01
02 lencar=input()
03 numcar=input()
04 rownum=lencon/lencar
05 perrow=rownum*rownum
06 pendcar=numcar%perrow
07 filledrow=numcar/perrow
08
09 if pendcar>rownum:
10     car=pendcar%rownum
11     z=car
12 else:
13     z=pendcar
14 if z>0:
15     z-=1
16 if z == 0 or z%2 == 0:
17     zflag=0
18 else:
19     zflag=-1
20
21 x=pendcar/rownum
22 if(str(z) == '0'):
23     print "(4,1,1)"
24 else:
25     print "("+str(z)+","+str(x)+","+str(filledrow)+")"
26
27 if x == 0 or x%2 == 0:
28     xflag=0
29 else:
30     xflag=1
31 if filledrow == 0 or filledrow%2 == 0:
32     yflag=0
33 else:
34     yflag=1
35
if((xflag == 0 and yflag == 0) or (zflag==0 and yflag==0) or (xflag==0 and zflag 
36
== 0)):
37     print "Yes"
38 else:
39     print "No"
Identify the Container type:

1 a=int(input())
2
3 if(a>=0 and a<=1000):
4     print("Saver")
5 if(a>=1001 and a<=10000):
6     print("Economy")
7 if(a>10000):
8     print("Flexi")

Locate the Container:

01 a=int(input())
02 b=int(input())
03 c=int(input())
04 d=int(input())
05 e=int(input())
06
07 if(e>=(a+b+c+d)):
08     print("Not Possible")
09 else:
10     if(e>0 and e<=a):
11         print(1)
12     if(e>a and e<(a+b)):
13         print(2)
14     if(e>=(a+b) and e<(a+b+c)):
15         print(3)
16     if(e>=(a+b+c) and e<=(a+b+c+d)):
17         print(4)
Variables and Operators CC:

Spherical Cargos:

01 a = float(input())
02 b = float(input())
03 c = float(input())
04
05 vol = (a*a*a)
06 dia = (b/2)
07 volume = (1.3333333 * 3.14)
08 volOfSphere = (1.3333333 * 3.14)*(dia*dia*dia)
09 total = volOfSphere*c
10 remain = vol - total
11 #print(round(remain,3))
12 print(format(remain,'.2f'))

Celsius to Fahrenheit Converter:

1 print('Enter the Celsius : ')


2 a = float(input())
3
4 b = (a * (1.8))+32
print(str(a)+' degree Celsius is equal to '+str((round(b,1)))+' degree
5
Fahrenheit’)

File Handling LC:

File Copy:
1 with open("file_out.txt","w") as f:
2     with open("file_in.txt","r") as r:
3         x = r.readlines()
4     f.writelines(x)
5 f.close()

Heaviest Cargos:
29 print("Enter the n value")
30 n = int(raw_input())
31 with open("readnLines.txt","r") as f:
32     lines = f.readlines()
33     nec = lines[-n:]
34 for item in nec:
35     if item.endswith('\n'):
36         print(item[:-1])
37     else:
38         print(item)

Total cost from XML:

01 import xml.etree.ElementTree as ET
02 tree = ET.parse(r'product.xml')
03
04 headingList=[]
05 total = 0
06 root = tree.getroot()
07 for prod in root:
08     for names in prod:
09         headingList.append(names.tag)
10     break
11 for member in root.findall('product'):
12     for x in range(0,len(member)):
13         if headingList[x] == 'cost':
14             total = total + int (member.find(headingList[x]).text)
15 print("Total Cost : "+str(total))

CSV to XML:
01 import csv
02
03 csvFile = 'product.csv'
04 xmlFile = 'product.xml'
05
06 csvData = csv.reader(open(csvFile))
07 xmlData = open(xmlFile, 'w')
08 xmlData.write('<?xml version="1.0"?>' + "\n")
09 # there must be only one top-level tag
10 xmlData.write('<productList>' + "\n")
11
12 rowNum = 0
13 for row in csvData:
14     if rowNum == 0:
15         tags = row
16         # replace spaces w/ underscores in tag names
17         for i in range(0,len(tags)):
18             tags[i] = tags[i].replace(' ', '_')
19     else:
20         xmlData.write('<product>' + "\n")
21         for i in range(0,len(tags)):
22             xmlData.write('    ' + '<' + tags[i] + '>' \
23                           + row[i] + '</' + tags[i] + '>' + "\n")
24         xmlData.write('</product>' + "\n")
25
26     rowNum +=1
27
28 xmlData.write('</productList>' + "\n")
29 xmlData.close()
XML to CSV:
01 from __future__ import print_function
02 import xml.etree.ElementTree as ET
03 import csv
04 tree = ET.parse(r'product.xml')
05
06 with open('product.csv', 'w') as csvFile:
07     writer = csv.writer(csvFile)
08     headingList=[]
09     overallList=[]
10     root = tree.getroot()
11     for prod in root:
12         for names in prod:
13             headingList.append(names.tag)
14         break
15     for member in root.findall('product'):
16         writer.writerow(headingList)
17         break
18     for member in root.findall('product'):
19         rowList=[]
20         for x in range(0,len(member)):
21             rowList.append(member.find(headingList[x]).text)
22         writer.writerow((rowList))
23         overallList.append(rowList)
#     print(member.find('id').text,
24 member.find('productName').text,member.find('cost').text,
member.find('weight').text)
25 csvFile.close()
26 print(*headingList, sep=",")
27 for x in overallList:
28     print(*x,sep=",")

You might also like