Question

In: Computer Science

How can I do this in python ??? In order to create the Exam Result objects...

How can I do this in python ???
In order to create the Exam Result objects correctly, one must find the student object and the subject object that the exam result should refer to. The distributed code contains O (n) methods that perform sequential searches to find them in the lists. Create new implementations of oving7. find _student and oving7.find_ topic that is more effective.

oving7. find _student:

def find _student (student no, student list):
     for student in student list:
         if student.get_studentnumber () == studentnr:
             return student
     return None

oving7.find_ topic:

def find _ topic (topic code, topic list):
     for subject in subject list:
         if topic.get_ topic code () == topic code:
             return topic
     return None

Solutions

Expert Solution

Above Python codes take O(n) time as they are performing linear search operation, So in order to make them efficient, we can apply Binary Search-

Python Code for the above two functions using Binary Search is as follows-

  • oving7. find _student:

#This function will return the index of the found element i.e; student_list[idx] will be the answer
def find _student(studentnr, student_list):
first = 0
last = len(student_list)-1
idx = -1
while (first <= last) and (idx == -1):
mid = (first+last)//2
if student_list[mid] == studentnr:
idx = mid
else:
if studentnr<student_list[mid]:
last = mid -1
else:
first = mid +1
return idx

  • oving7.find_ topic:


#This function will return the index of the found element i.e; topic_list[idx] will be the answer
def find _ topic(topic_code, topic_list):
first = 0
last = len(topic_list)-1
idx = -1
while (first <= last) and (idx == -1):
mid = (first+last)//2
if topic_list[mid] == topic_code:
idx = mid
else:
if topic_code<topic_list[mid]:
last = mid -1
else:
first = mid +1
return idx


Related Solutions

When using scapy in python how can I get the same result as the snippit bellow?...
When using scapy in python how can I get the same result as the snippit bellow? (which in run in terminal) >>> sr(IP(dst="192.168.8.1")/TCP(dport=[21,22,23])) Received 6 packets, got 3 answers, remaining 0 packets (<Results: UDP:0 TCP:3 ICMP:0 Other:0>, <Unanswered: UDP:0 TCP:0 ICMP:0 Other:0>) >>> ans,unans=_ >>> ans.summary() IP / TCP 192.168.8.14:20 > 192.168.8.1:21 S ==> Ether / IP / TCP 192.168.8.1:21 > 192.168.8.14:20 RA / Padding IP / TCP 192.168.8.14:20 > 192.168.8.1:22 S ==> Ether / IP / TCP 192.168.8.1:22 >...
Can you break it down how I should do it? Scores on a university exam are...
Can you break it down how I should do it? Scores on a university exam are Normally distributed with a mean of 70 and a standard deviation of 10. The professor teaching the class declares that a student will receive a “F” if his or her score is below 50. 1. Using the 68-95-99.7 rule, what percent of students will receive “F”? 2. Using the 68-95-99.7 rule, what percent of students will score between 60 and 90? 3. Using the...
How do i create a python function that will calculate the Levenshtein distance given a user...
How do i create a python function that will calculate the Levenshtein distance given a user input of two words? I am aware that there are packages already made to do this, but I want to create my own. Please explain all the steps. https://en.wikipedia.org/wiki/Levenshtein_distance#:~:text=Informally%2C%20the%20Levenshtein%20distance%20between,considered%20this%20distance%20in%201965.
How can I use Python without Pandas to create a new.csv file with the headers "c1,...
How can I use Python without Pandas to create a new.csv file with the headers "c1, c2, c3, c4, c5, c6" and the first line of data "8, 5, -9, 7, 2.1, 1.7" from the original.csv file?
Python Write a program that loops, prompting the user for their full name, their exam result...
Python Write a program that loops, prompting the user for their full name, their exam result (an integer between 1 and 100), and then writes that data out to file called ‘customers.txt’. The program should check inputs for validity according to the following rules: First and last names must use only alphabetical characters. No spaces, hyphens or special characters. Names must be less than 20 characters long. Exam result (an integer between 1 and 100 inclusive) The file should record...
Write a python program that loops, prompting the user for their full name, their exam result...
Write a python program that loops, prompting the user for their full name, their exam result (an integer between 1 and 100), and then writes that data out to file called ‘customers.txt’. The program should check inputs for validity according to the following rules: First and last names must use only alphabetical characters. No spaces, hyphens or special characters. Names must be less than 20 characters long. Exam result (an integer between 1 and 100 inclusive) The file should record...
How do I create a histogram for the following set of data? Legend: Result (1-100) Age...
How do I create a histogram for the following set of data? Legend: Result (1-100) Age and gender are self explanatory Relationship (are you in a romantic relationship?) Medu (Mothers highest lvl of education. 1= year 10; 2= year 12; 3=bachelor; 4= post grad Lectures (how many lectures missed) Tutorials (How many tutorials missed) RESULT Gender Age Medu Relationship Lectures Tutorials 55 F 20 4 NO 4 3 55 F 19 1 NO 2 3 65 M 18 4 NO...
How to do this in Python (using Lists): Create a python program that allows a user...
How to do this in Python (using Lists): Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird. You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions: 1. Display all U.S. States in Alphabetical order along with Capital and Bird 2. Search for a specific state and display...
How do I write a script for this in python in REPL or atom, NOT python...
How do I write a script for this in python in REPL or atom, NOT python shell Consider the following simple “community” in Python . . . triangle = [ ["top", [0, 1]], ["bottom-left", [0, 0]], ["bottom-right", [2, 0]], ] This is the calling of function. >>> nearestneighbor([0, 0.6], triangle, myeuclidean) 'top' The new point is (0, 0.6) and the distance function is Euclidean. Now let’s confirm this result . . . >>> myeuclidean([0, 0.6], [0, 1]) 0.4 >>> myeuclidean([0,...
can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT