In: Computer Science
Part 1
The questions in this part of the assignment will be graded.
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 a Comp208 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 (which is called mycourses). Instead, all the information needed has been compiled in files with the following characteristics.
File 1
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
You can take a look about how File 2 looks below.
#Comp208 is an amazing class
# 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 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 (24 points)
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
PYTHON PROGRAM
The explanation is provided as comments in grey and italic
def create_dictionary(file1):
words=[]
wordcount={}
for word in file1.read().split():
words.append(word)
#words[0] contains number of words
#The pattern is starting from first index,
#The word followed by amount followed by .
i=1
#Traverse the entire list
while(i<len(words)):
wordcount[words[i]]=int(words[i+1])
i=i+3
file1.close()
return wordcount
def job_description(file2):
job=[]
words=[]
job_description=[]
#Read all lines
lines=file2.readlines()
#For every line
for line in lines:
if line[0]!="#":
#Add the line as separate words in one list in job
words=line.split()
job.append(words)
#For every list in job,split as separate words
for i in job:
for j in i:
job_description.append(j)
file2.close()
return job_description
def hay_points(hay_dict,job_desc):
total_points=0
#keys hold all the keys in dictionary as a list
keys=[]
for i in hay_dict.keys():
keys.append(i)
#Read every word in job_desc- if the word is in keys, add its corresponding points
for i in job_desc:
for j in keys:
if i==j:
total_points+=hay_dict[i]
return total_points
def mytest():
file1=open("File1.txt","r")
hay_dict=create_dictionary(file1)
print("The dictionary read from file1.txt",hay_dict)
file_2=open("File2.txt","r")
job_desc=job_description(file_2)
print("The string read from file2.txt",job_desc)
points = hay_points(hay_dict, job_desc)
print("The salary computed is",points)
mytest()
SCREENSHOT OF CODE TO ASSIST IN INDENTATION
OUTPUT SCREENSHOT
FILE1.TXT
FILE2.TXT
OUTPUT SCREENSHOT