# Program to read names, surnames from a file and arrange them in alphabetical order of surnames
# and write them in an another file.
n=input("Enter the no of peoples:")
n=int(n)
f1=open("name.d","w")
for i in range (n):
s=input("Enter the name of people "+str(i+1)+":")
print(s,file=f1)
f1.close()
f1=open("name.d","r")
name=[]
sname=[]
for k in f1:
x=k.split()
name.append(x[0])
sname.append(x[1])
for j in range (n):
for i in range(n-j-1):
if sname[i]>sname[i+1]:
sname[i],sname[i+1]=sname[i+1],sname[i]
name[i],name[i+1]=name[i+1],name[i]
f2=open("sname_arranged.d","w")
for k in range (n):
print(name[k],sname[k],file=f2)
f2.close()
# and write them in an another file.
n=input("Enter the no of peoples:")
n=int(n)
f1=open("name.d","w")
for i in range (n):
s=input("Enter the name of people "+str(i+1)+":")
print(s,file=f1)
f1.close()
f1=open("name.d","r")
name=[]
sname=[]
for k in f1:
x=k.split()
name.append(x[0])
sname.append(x[1])
for j in range (n):
for i in range(n-j-1):
if sname[i]>sname[i+1]:
sname[i],sname[i+1]=sname[i+1],sname[i]
name[i],name[i+1]=name[i+1],name[i]
f2=open("sname_arranged.d","w")
for k in range (n):
print(name[k],sname[k],file=f2)
f2.close()
Comments
Post a Comment