Question

In: Computer Science

This program is in Python ### simulates packets of data arriving at irregular times. The data...

This program is in Python
### simulates packets of data arriving at irregular times. The data gets
### read from the file and then you must place it in a queue where it will wait
### its turn to get processed. The "processing" happens in a threaded subroutine
### called "process_input_data". That function should pull data from the queue,
### hash the student ID number, and then store the data in a hash table which
### you have to implement. You should decide ahead of time (by looking at the
### student_data.txt file) what the hash function should be and how big the hash
### table will be. There are multiple acceptable answers. Finally, prove that
### your hash table works by retrieving and printing information about students
### with the following IDs:
### 427980112
### 258399712
### 948140115

# import the libraries needed to make all this work
import time
import os
from threading import Thread
# let the program know where you've put the data file
myDir = '/home/project_x'
filename = 'student_data.txt'


# create a class of type "student" that holds the data fields for each student.
# add whatever methods you see fit
class student:
# YOUR CODE HERE
# DEFINE A CLASS TO HOLD STUDENT DATA

# this function will pop students out of the queue and place them in the hash
# table, which is to be a global variable called "hash_table"
def process_input_data(stop):
global student_queue, hash_table

while not stop() or len(student_queue)>0:
if len(student_queue)>0 :

# YOUR CODE HERE
# POP ITEMS FROM QUEUE AND
# PUT THEM IN THE HASH TABLE
  

################################################################################
def main():

global student_queue
student_queue = []

# set up the thread so that it can process students once they're in the queue.
# do not modify this
stop_threads = False
thr = Thread(target=process_input_data,args =(lambda : stop_threads, ))
thr.start()
  

# load data from file, enforcing delays in order to simulate
# asynchronous data arrivals
os.chdir(myDir)
with open(filename,'r') as infile:
# mark the initial time
tStart = time.time()

# for each line in the input file...
for line in infile:

# ... split the line into its components, and then ...
t, firstname, lastname, gpa, id_nmbr, mjr = line.split()
  
# ... wait until the elapsed time matches the 'arrival' time of the
# line of data in the text file. This is how we are simulating
# data packets arriving at irregular times.
while ((time.time()-tStart)< float(t) ):
pass

# when it's time, create a new object of type "student" and push
# it onto the queue
# YOUR CODE HERE
# CREATE OBJECT USING NAMES, GPA, ID, MAJOR, & PUSH IT TO student_queue


# this is needed to stop the process_input_data function once all the data
# has been read from the file into the queue
stop_threads = True
thr.join()
  
# add code to search the hash table for the students with these student IDs
# and to print all their information
### 427980112
### 258399712
### 948140115
# YOUR CODE HERE
# FIND STUDENTS AND PRINT THEIR INFO


if __name__=="__main__":
main()

#create a small data to run the program named "student_data.txt"

Solutions

Expert Solution

Please find the program and code has been written wherever YOUR CODE has been written.

import time
import os
from threading import Thread

# let the program know where you've put the data file
myDir ="C:/Users/SAGAR/Desktop/"
filename = 'student_data.txt'
student_queue=[]
hash_table={}

class Student:
    def __init__(self,firstname,lastname,gpa, id_nmbr, mjr):
        self.fname=firstname
        self.lname=lastname
        self.gpa=gpa
        self.id_number=id_nmbr
        self.major=mjr

    def getid(self):
        return self.id_number

    def printinfo(self):
        print("Name:"+self.fname+" "+self.lname+" ID:"+self.id_number+" GPA:"+self.gpa+" Major:"+self.mjr)


def process_input_data(stop):
    global student_queue, hash_table

    while not stop or len(student_queue) > 0:
        if len(student_queue) > 0:
            student= student_queue.pop()
            hash_table.add(student.getid(),student)


def main():
    global student_queue
    student_queue = []

    global hash_table
    hash_table={}

    # set up the thread so that it can process students once they're in the queue.
    # do not modify this
    stop_threads = False
    thr = Thread(target=process_input_data, args=(lambda: stop_threads,))
    thr.start()

    # load data from file, enforcing delays in order to simulate
    # asynchronous data arrivals
    os.chdir(myDir)
    with open(filename, 'r') as infile:
    # mark the initial time
    tStart = time.time()

    # for each line in the input file...
    for line in infile:
        # ... split the line into its components, and then ...
        t, firstname, lastname, gpa, id_nmbr, mjr = line.split()

    # ... wait until the elapsed time matches the 'arrival' time of the
    # line of data in the text file. This is how we are simulating
    # data packets arriving at irregular times.
    while (time.time() - tStart) < float(t):
        pass

    # when it's time, create a new object of type "student" and push
    # it onto the queue
    student= Student(firstname,lastname,gpa,id_nmbr,mjr)
    student_queue.append(student)

    # this is needed to stop the process_input_data function once all the data
    # has been read from the file into the queue
    stop_threads = True
    thr.join()

    # add code to search the hash table for the students with these student IDs
    # and to print all their information
    ### 427980112
    student_1= hash_table.get("427980112")

    ###258399712
    student_2= hash_table.get("258399712")

    ### 948140115
    student_3= hash_table.get("948140115")

    student_1.printinfo()
    student_2.printinfo()
    student_3.printinfo()


if __name__ == "__main__":
    main()

Related Solutions

Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
You will write a program using Python that simulates an Automatic Teller Machine (ATM). For this...
You will write a program using Python that simulates an Automatic Teller Machine (ATM). For this program, your code can have user defined functions (but not required), the program must not call on any external functions or modules to handle any of the input, computational, and output requirements. Requirements: There is a customer with the following credential and can only access the system if the Access Code is correct: • Name: Peter Parker, Access Code: 2222 • When the program...
PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided...
PYTHON BEGINNER Problem Write a program which, given a number, simulates rolling a pair of six-sided dice that number of times. The program should keep track of how many times each possible sum comes up using a list. The list's first element should contain how many times a total of 2 was rolled, the second should contain how many times a total of 3 was rolled, and so on all the way through to 12. When all rolls are complete,...
Write a Python program that reads in two times, an earlier time and a later time,...
Write a Python program that reads in two times, an earlier time and a later time, and prints the difference between the two times in minutes as well as in hours/minutes. The format of the time is HH:MMAM or H:MMAM or HH:MMPM or H:MMPM. Also the AM or PM may be in lower case. A sample run of the program is shown below: $ python3 Time.py Enter Earlier Time: 9:36aM Enter Later Time: 6:22PM Number of minutes between 9:36aM and...
python Write a program that prints your name 100 times to the screen. Write a function...
python Write a program that prints your name 100 times to the screen. Write a function that takes a string s and an integer n as parameters and prints the string s a total of n times (once per line). Write a for loop that prints all the integers from 3141 to 5926, skipping every other one. Write a for loop that prints every 5th integer from 5926 down to 3141. Write a program (using a for loop) to print...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
trying to make a code that simulates a predator prey relationship in python, however for some...
trying to make a code that simulates a predator prey relationship in python, however for some reason my code will not show the plot, i cant find any issues with syntax or anything else, please help. import matplotlib.pyplot as plt predatorcount = [] preycount = []    def simulate(initialpred, initialprey, preygrowth, predationrate, predshrink, predbirthrate): predatorcount.append(initialpred) preycount.appened(initialprey) i=0; prey = 1 predator = 1 while True: prey = preycount[i]*(1+(preygrowth-predationrate*predatorcount[i])) predator = predatorcount[i]*(1-(predshrink+predbirthrate*preycount[i])) if prey <= 0 or predator <=0:   break; else:  ...
The following code simulates data on resting heart rate vs. age, then simulates running multiple regression...
The following code simulates data on resting heart rate vs. age, then simulates running multiple regression analysis repeatedly and saving the coefficients (i.e. slopes) from the model. This model assumes a slightly quadratic relationship between heart rate and age. Copy and run this code, then answer the questions below. n <- 50 coefs_age <- rep(0,1e5) coefs_age2 <- rep(0,1e5) for (i in 1:1e5) { Age=round(runif(n,min=18,max=90)) Age2 <- Age^2 HR <- 84-Age*0.5+Age2*0.035+rnorm(n,sd=15) model <- lm(HR~Age+Age2) coefs_age[i] <- summary(model)$coefficients[2,1] coefs_age2[i] <- summary(model)$coefficients[3,1] }...
In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT