In: Computer Science
Programming language is in python 3
For this project, you will import the json module.
Write a class named NobelData that reads a JSON file containing data on Nobel Prizes and allows the user to search that data. It just needs to read a local JSON file - it doesn't need to access the internet. Specifically, your class should have an init method that reads the file, and it should have a method named search_nobel that takes as parameters a year and a category, and returns a sorted list (in normal English dictionary order) of the surnames for the winner(s) in that category for that year (up to three people can share the prize). The year will be a string (e.g. "1975"), not a number. The categories are: "chemistry", "economics", "literature", "peace", "physics", and "medicine". The JSON file will be named nobels.json and will be provided - you do not need to submit it. Any data members of the NobelData class must be private.
For example, your class could be used like this:
nd = NobelData() nd.search_nobel("2001", "economics")
The file must be named: NobelData.p
NOTE:- IN CASE OF ANY QUERY FEEL FREE TO ASK IN COMMENT ANYTIME.....HAPPY LEARNING......
PYTHON CODE:-
import requests
#Implementation of NobelData class
class NobelData:
#Implementation of init
def __init__(self):
responseObject = requests.get("http://api.nobelprize.org/v1/prize.json")
#load json format data
self.data = responseObject.json()
#Implementation of search_nobel method
def search_nobel(self,year,category):
#Declare n and store the length of self.data['prizes']
lengthofPrizes= len(self.data['prizes'])
#Declare winners as type of list
winners=[]
#Declare surnamesofthewinners as type of list
surnamesofthewinners=[]
#Iterate loop
for each in range(0,lengthofPrizes):
#check self.data['prizes'][each]['year'] is equal to year
#and check self.data['prizes'][each]['category'] is equal to category
if(self.data['prizes'][each]['year']==year and self.data['prizes'][each]['category']==category):
#assign self.data['prizes'][each]['laureates'] to winners
winners=self.data['prizes'][each]['laureates']
break
#caclulate the length of winners and store in lengthofWinners
lengthofWinners=len(winners)
#Iterate the loop
for each in range(0,lengthofWinners):
#append the winners[each]['surname'] to surnamesofthewinners
surnamesofthewinners.append(winners[each]['surname'])
#sort the surnamesofthewinners
surnamesofthewinners.sort()
return surnamesofthewinners
#Declare an object for NobelData
nd = NobelData()
#call serach_nobel method
nd.search_nobel("2001", "economics")
#Display statement
print(nd.search_nobel("2001", "economics"))
--------------I hope my answer met all your requirements......Please upvote..Thank You----------------