In: Computer Science
Write a Python program
The Hay System is a job performance evaluation method that is widely used by organizations around the world. Corporations use the system to map out their job roles in the context of the organizational structure. One of the key benefits of the method is that it allows for setting competitive, value-based pay policies. The main idea is that for each job evaluation, a number of factors (such as Skill, Effort, Responsibility and Working Conditions) are evaluated and scored by using a point system. Then, the cumulative total of points obtained will be correlated with the salary associated with the job position. As an engineering student, you have been commissioned to implement a simplified version of the Hay method. Particularly, the hiring company (which is called David and Joseph Ltd) is interested in getting the salary (or hay system score) for several job descriptions currently performed in the company. Data Representation Unfortunately, the company David and Joseph Ltd. has very strict security policies. Then, you will not be granted access to the main data base ). Instead, all the information needed has been compiled in files with the following characteristics. File 1 The first line of the file contains 1 positive integer: num words≤ 10000, the number of words in the Hay Point dictionary. num words lines follow; each contains a word (a string of up to 16 lower-case letters) and a dollar value (an integer between 0 and 1000000).
You can safely assume that the num words words in the dictionary are distinct. Each description word-value is terminated by a line containing a period. You can take a look about how File 1 looks below.
7
administer 100000
.
spending 200000
.
manage 50000
.
responsibility 25000
.
expertise 100
.
skill 50
.
money 75000
.
Please note that for this file, num words is equal to 7. File 2 The first lines of the file contains a varyingly number of comments that must be ignored. You can recognize a comment line because it always start with the # character. Following the comments there is one job description. A job description consists of between 1 and 200 lines of text; for your convenience the text has been converted to lower case and has no characters other than letters, numbers, and spaces. Each line is at most 200 characters long. You can take a look about how File 2.1 looks below.
#Hello how are you
# This comment does not make sense
# It is just to make it harder
# The job description starts after this comment, notice that it has 4 lines.
# This job description has 700150 hay system points \\
the incumbent will administer the spending of kindergarden milk money and exercise responsibility for making change he or she will share responsibility for the task of managing the money with the assistant whose skill and expertise shall ensure the successful spending exercise
Below, you can find a second example of how File 2.2 could look like.
#This example has only one comment
this individual must have the skill to perform a heart transplant
and expertise in rocket science
The Hay System When applying the Hay System to the latest File 2 example (i.e., this individual must have the skill to perform a heart transplant and expertise in rocket science ) on the Hay Point dictionary coded in File 1, the job description gets a total of 150 points (or salary in dollars). This score is obtained because exactly two words (i.e., expertise and skill) of the job description are found in the dictionary. Particularly, expertise and skill have a score of 100 and 50 dollars, respectivelly.
Question 1: create dictionary
Complete the create dictionary function, which reads the information coded in the File 1 and returns a hay points dictionary. See below for an explanation of exactly what is expected. from typing import Dict, TextIO def create_dictionary(file1: TextIO) -> Dict[str, int]: ’’’Return a dictionary populated with the information coded in file1. >>> File_1 = open(’File1.txt’) >>> hay_dict = create_dictionary(File_1) >>> hay_dict {’administer’: 100000, ’spending’: 200000, ’manage’: 50000, ’responsibility’: 25000, ’expertise’: 100, ’skill’: 50, ’money’: 75000} """ Please note that the variable file1 is of type TextIO, then you can assume that file was already open and it is ready to be read.
Question 2: job description (24 points) Complete the job description function, which reads the information coded in the File 2 to return a list of strings with the job description. See below for an explanation of exactly what is expected. def job_description(file2: TextIO) -> List[str]: ’’’Return a string with the job description information coded in file2. >>> File_2 = open(’File2_1.txt’) >>> job_desc = job_description(File_2) >>> job_desc [’the’, ’incumbent’, ’will’, ’administer’, ’the’, ’spending’, ’of’, ’kindergarden’, ’milk’, ’money’, ’and’, ’exercise’, ’responsibility’, ’for’, ’making’, ’change’, ’he’, ’or’, ’she’, ’will’, ’share’, ’responsibility’, ’for’, ’the’, ’task’, ’of’, ’managing’, ’the’, ’money’, ’with’, ’the’, ’assistant’, ’whose’, ’skill’, ’and’, ’expertise’, ’shall’, ’ensure’, ’the’, ’successful’, ’spending’, ’exercise’] ’’’ Please note that the variable file2 is of type TextIO, then you can assume that file was already open and it is ready to be read.
Question 3: hay points (24 points) Complete the hay points function, which for a job description, output the corresponding salary computed as the sum of the Hay Point values for all words that appear in the description. Words that do not appear in the dictionary have a value of 0. See below for an explanation of exactly what is expected. def hay_points(hay_dictionary: Dict[str, int], job_description: List[str]) -> int: ’’’Returns the salary computed as the sum of the Hay Point values for all words that appear in job_description based on the points coded in hay_dictionary >>> File_1 = open(’File1.txt’) >>> File_2 = open(’File2_1.txt’) >>> hay_dict = create_dictionary(File_1) >>> job_desc = job_description(File_2) >>> points = hay_points(hay_dict, job_desc) >>> print(points) >>> 700150 ’’’
Question 4: my test (0 points) The function my test is there to give you a starting point to test your functions. Please note that this function will not be graded, and it is there only to make sure that you understand what every function is expected to do and to test your own code. Note: In order for you to test your functions one at a time, comment out the portions of the my test() function that call functions you have not yet written. The expected output for the function calls is as follows:
The dictionary read from File1.txt is: {’administer’: 100000, ’spending’: 200000, ’manage’: 50000, ’responsibility’: 25000, ’expertise’: 100, ’skill’: 50, ’money’: 75000}
The string read from File2_1.txt is: [’the’, ’incumbent’, ’will’, ’administer’, ’the’, ’spending’, ’of’, ’kindergarden’, ’milk’, ’money’, ’and’, ’exercise’, ’responsibility’, ’for’, ’making’, ’change’, ’he’, ’or’, ’she’, ’will’, ’share’, ’responsibility’, ’for’, ’the’, ’task’, ’of’, ’managing’, ’the’, ’money’, ’with’, ’the’, ’assistant’, ’whose’, ’skill’, ’and’, ’expertise’, ’shall’, ’ensure’, ’the’, ’successful’, ’spending’, ’exercise’]
The salary computed is 700150
Ok i will complete this assigmnemt completely.
So , first I had created the two files as follows:
data = '''7
administer 100000
.
spending 200000
.
manage 50000
.
responsibility 25000
.
expertise 100
.
skill 50
.
money 75000
.'''
f = open('File1.txt',"w")
f.write(data)
f.close()
data = '''#Hello how are you
# This comment does not make sense
# It is just to make it harder
# The job description starts after this comment, notice that it has 4 lines.
# This job description has 700150 hay system points \\
the incumbent will administer the spending of kindergarden milk money and exercise responsibility for making change he or she will share responsibility for the task of managing the money with the assistant whose skill and expertise shall ensure the successful spending exercise
Below, you can find a second example of how File 2.2 could look like.'''
f = open("File2_1.txt","w")
f.write(data)
f.close()
Question 1:
################################
# QUESTION 1
################################
File_1 = open("File1.txt")
def create_dictionary(File_1):
data = File_1.read().split("\n")
hay_dict = {}
n = int(data[0])
data = data[1:] # remove the first elemtent
for i in data:
if i == ".": # remove the dots line
data.remove(i)
for i in range(n): # convert to dictionary
i = data[i].split()
hay_dict[i[0]] = int(i[1] )
return hay_dict
hay_dict = create_dictionary(File_1)
File_1.close() # close the file
print(hay_dict)
Screenshot of output and code:
Question 2 :
################################
# QUESTION 2
################################
def job_description(file2):
data = file2.read().split("\n")
newData = []
# # remove comments
for i in data:
i = i.strip()
if i[0] != '#': # REMOVING THE COMMENTS
newData.append(i)
data = []
for i in range(len(newData)):
data.extend(newData[i].split())
return data
File_2 = open('File2_1.txt')
job_desc = job_description(File_2)
print(job_desc)
Screenshot of output and code:
Question 3 :
################################
# QUESTION 3
################################
def hay_points(hay_dictionary, job_description) -> int:
total = 0
for i in job_description:
if i in hay_dictionary:
total += hay_dictionary[i]
return total
File_1 = open("File1.txt")
File_2 = open("File2_1.txt")
hay_dict = create_dictionary(File_1)
job_desc = job_description(File_2)
points = hay_points(hay_dict, job_desc)
print(points)
Screenshot of output and code:
Question 4:)
################################
# QUESTION 4
################################
def my_test(file1, file2):
File_1 = open(file1)
hay_dict = create_dictionary(File_1)
File_1.close()
print(f"The dictionary read from {file1} is : {hay_dict}")
File_2 = open(file2)
job_desc = job_description(File_2)
File_2.close()
print(f"The string read from {file2} is: {job_desc}")
points = hay_points(hay_dict, job_desc)
print(f"The salary computed is {points}")
my_test("File1.txt", "File2_1.txt")
Screenshot of output and code:
The complete code :
################################
# QUESTION 1
################################
File_1 = open("File1.txt")
def create_dictionary(File_1):
data = File_1.read().split("\n")
hay_dict = {}
n = int(data[0])
data = data[1:] # remove the first elemtent
for i in data:
if i == ".": # remove the dots line
data.remove(i)
for i in range(n): # convert to dictionary
i = data[i].split()
hay_dict[i[0]] = int(i[1] )
return hay_dict
hay_dict = create_dictionary(File_1)
File_1.close() # close the file
print(hay_dict)
################################
# QUESTION 2
################################
def job_description(file2):
data = file2.read().split("\n")
newData = []
# # remove comments
for i in data:
i = i.strip()
if i[0] != '#': # REMOVING THE COMMENTS
newData.append(i)
data = []
for i in range(len(newData)):
data.extend(newData[i].split())
return data
File_2 = open('File2_1.txt')
job_desc = job_description(File_2)
print(job_desc)
################################
# QUESTION 3
################################
def hay_points(hay_dictionary, job_description) -> int:
total = 0
for i in job_description:
if i in hay_dictionary:
total += hay_dictionary[i]
return total
File_1 = open("File1.txt")
File_2 = open("File2_1.txt")
hay_dict = create_dictionary(File_1)
job_desc = job_description(File_2)
points = hay_points(hay_dict, job_desc)
print(points)
################################
# QUESTION 4
################################
def my_test(file1, file2):
File_1 = open(file1)
hay_dict = create_dictionary(File_1)
File_1.close()
print(f"The dictionary read from {file1} is : {hay_dict}")
File_2 = open(file2)
job_desc = job_description(File_2)
File_2.close()
print(f"The string read from {file2} is: {job_desc}")
points = hay_points(hay_dict, job_desc)
print(f"The salary computed is {points}")
my_test("File1.txt", "File2_1.txt")
post a comment below , if any more help is required.
--------------------------------------------------
Hope you like the answer. Please upvote. Thank you.
--------------------------------------------------