Commit 4b8a6f64 authored by NEERAJ DHAKE's avatar NEERAJ DHAKE

Lab4

parents
Group No. 30 - "Prayas"
(Neeraj Dhake, 150050022) (Utsav Anand, 150050044) (Mukesh Pareek, 150050049)
Honour Code:
Neeraj: We pledge on our honour that we have not given or received any unauthorized assistance on this assignment.
Utsav: We pledge on our honour that we have not given or received any unauthorized assistance on this assignment.
Mukesh: We pledge on our honour that we have not given or received any unauthorized assistance on this assignment.
Citations:
http://try.github.io
Percentage contribution:
Neeraj 100%
Utsav 100%
Mukesh 100%
Extra Credit:
E1. The error reads "AttributeError: 'tuple' object has no attribute 'append'"
This tells us that the object tuple has no method append. In other words, append works only on the list and not on a tuple.To append an element you'll either have to convert the tuple to a list, append the list and then reconvert the list to tuple or create a 1-tuple of the element to be appended and add them using '+' operator.
E2. Observation:
If we append an element to a list which is a member of a tuple, it is successful in doing so.
Explanation:
This happens because-
Say the list is stored as i-th element of a n-tuple A, then the command used is A[i].append(element_to_be_appended).
A[i] is a list so append works normally as one would expect to work on a normal list. And since append() is a method of list and not of a tuple, this works perfectly fine. Also, this does not contradict the previous observation.
\ No newline at end of file
#!/usr/bin/python
def function1(string1,string2,listVariable):
newList=zip(string1,string2)
result=listVariable+newList
return result
initialList= ["Muk","Nee","Uts","Par","Dha"]
resultList=function1("CS251","Software",initialList)
print initialList
print resultList
#!/usr/bin/python
def function2(listVariable,n):
resultList=map(lambda x: x[n],listVariable)
print resultList #printing the n-th element of each tuple as a list
sortedList=sorted(listVariable, key=lambda x: x[n])
return sortedList #returning the list sorted according to n-th element of each tuple
#!/usr/bin/python
def function3():
opened_file=open("matrix.txt","r")
for i,line in enumerate(opened_file):
if(i == 0):
var="".join(line)
new_var=var.split()
w = int(new_var[0])
h = int(new_var[1])
matrix = [[None for x in range(h)] for y in range(w)] #initialising a 2-D array of given dimensions of None.
elif(i != 0):
var="".join(line)
matrix[i-1]=var.split() #storing data as 2-D array
if(i == w): #accounts for extra blank lines at the end of matrix.txt
break
opened_file.close()
Matrix1 = [[0 for x in range(w)] for y in range(h)]
for i in range(w):
for j in range(h):
Matrix1[j][i]=matrix[i][j] #calculating the transpose
transpose = open("transpose.txt","r+")
transpose.write(str(h) + " " + str(w) +"\n")
for k in range(h):
result=" ".join((Matrix1[k]))
transpose.write(result + "\n")
transpose.close()
function3()
5 5
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment