<\/span><\/h2>\n\n\n\nCollections.namedtuple()<\/strong>
Basically, namedtuples<\/em> are easy to create, lightweight object types.
They turn tuples into convenient containers for simple tasks.
With namedtuples<\/em>, you don\u2019t have to use integer indices for accessing members of a tuple.<\/p>\n\n\n\nExample<\/strong>
Code 01<\/strong><\/sub><\/p>\n\n\n\n>>> from collections import namedtuple\n>>> Point = namedtuple('Point','x,y')\n>>> pt1 = Point(1,2)\n>>> pt2 = Point(3,4)\n>>> dot_product = ( pt1.x * pt2.x ) +( pt1.y * pt2.y )\n>>> print dot_product\n11<\/code><\/pre>\n\n\n\nCode 02<\/strong><\/p>\n\n\n\n>>> from collections import namedtuple\n>>> Car = namedtuple('Car','Price Mileage Colour Class')\n>>> xyz = Car(Price = 100000, Mileage = 30, Colour = 'Cyan', Class = 'Y')\n>>> print xyz\nCar(Price=100000, Mileage=30, Colour='Cyan', Class='Y')\n>>> print xyz.Class\nY<\/code><\/pre>\n\n\n\n<\/span>Task<\/strong><\/span><\/h2>\n\n\n\nDr. John Wesley has a spreadsheet containing a list of student’s IDs<\/strong><\/em>, marks<\/strong><\/em>, class<\/strong><\/em> and name<\/strong><\/em>.<\/p>\n\n\n\nYour task is to help Dr. Wesley calculate the average marks of the students.<\/p>\n\n\n\n
Average<\/em> = Sum of all marks<\/em> \/ Total Students<\/em><\/strong><\/p>\n\n\n\nNote:
1. Columns can be in any order. IDs, marks, class and name can be written in any order in the spreadsheet.
2. Column names are ID<\/code>, MARKS<\/code>, CLASS<\/code> and NAME<\/code>. (The spelling and case type of these names won’t change.)<\/sub><\/strong><\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line contains an integer N<\/strong><\/em>, the total number of students.
The second line contains the names of the columns in any order.
The next N<\/em><\/strong> lines contains the marks<\/strong><\/em>, IDs<\/strong><\/em>, name<\/strong><\/em> and class<\/strong><\/em>, under their respective column names.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 0 < N<\/em> <= 100<\/strong><\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nPrint the average marks of the list corrected to 2 decimal places.<\/p>\n\n\n\n
Sample Input<\/strong><\/p>\n\n\n\nTESTCASE 01<\/strong><\/p>\n\n\n\n5\nID MARKS NAME CLASS \n1 97 Raymond 7 \n2 50 Steven 4 \n3 91 Adrian 9 \n4 72 Stewart 5 \n5 80 Peter 6 <\/code><\/pre>\n\n\n\nTESTCASE 02<\/strong><\/p>\n\n\n\n5\nMARKS CLASS NAME ID \n92 2 Calum 1 \n82 5 Scott 2 \n94 2 Jason 3 \n55 8 Glenn 4 \n82 2 Fergus 5<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\nTESTCASE 01<\/strong><\/p>\n\n\n\n78.00<\/code><\/pre>\n\n\n\nTESTCASE 02<\/strong><\/p>\n\n\n\n81.00<\/code><\/pre>\n\n\n\nExplanation<\/strong><\/p>\n\n\n\nTESTCASE 01<\/strong><\/sub><\/p>\n\n\n\nAverage = (97 + 50 + 91 + 72 + 80) \/ 5<\/strong><\/p>\n\n\n\nCan you solve this challenge in 4 lines of code or less<\/code>?<\/em>
NOTE<\/strong>: There is no penalty<\/code> for solutions that are correct but have more than 4 lines.<\/p>\n\n\n\n<\/span>Solution – Collections.namedtuple() in Python <\/strong><\/span><\/h2>\n\n\n\n# Enter your code here. Read input from STDIN. Print output to STDOUT\nfrom collections import namedtuple\n\nn = int(input())\nfields = input().split()\n\ntotal_marks = 0\nfor _ in range(n):\n students = namedtuple('student', fields)\n MARKS, CLASS, NAME, ID = input().split()\n student = students(MARKS, CLASS, NAME, ID)\n total_marks += int(student.MARKS)\nprint('{:.2f}'.format(total_marks \/ n))\n<\/pre>\n\n\n\n