<jupyter_start><jupyter_text>Assignment 5: Reference, Dictionaries, Comprehensions, File I/O, and Unit Tests***__Question 1:__ (1.0 point) - Write a python code that creates a list called _listN_ with the numbers 0,1,2,3,4,5,6,7,8,9. - Make a copy of the content of _listN_ to a new list called _listM_ and append the numbers 10,11,12,13 to the ende of _listM_. - Creat another variable called _listO_ that is a reference to _listM_ and remove the number 4 from _listO_.- Print the content of the three lists and explain what you are observing.<jupyter_code># Solution

listN = list(range(0,10))

listM = listN[:]
listM.extend(list(range(10,14)))

listO = listM[:]
listO.remove(4)

print('listN:', listN)
print('listM:', listM)
print('listO:', listO)<jupyter_output>listN: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
listM: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
listO: [0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13]<jupyter_text>Printed above you see the content of the three lists created in the code above. Each list is created as a copy of the previous list that is then tweaked to achieve the desired result.  ***__Question 2:__ (3.0 points) Each row in the file "careerXskills.txt" (available in NYU Classes) contains a pair  _career_ and _skill_. A particular career can be associate to several different skills. For instances, a 'computer_scientist' can have the set of skills 'programming', 'math', 'statistic', 'data_base'. Therefore, a single career can show up multiple times in the file "careerXskills.txt", each associate to a particular skill.- Build a dictionary from the file "careerXskills.txt", using the career name as the _keys_ and setting the list of skills associate to each career as the _value_ associated to the key. In other words, each _key_ has a list associate to it.- The list of skills associated to a key should not have duplicate entries, that is, a particular skill should not appear multiple times in the list of skills associated to a key.- Your code should print the list of careers and associate skills in the following format:-> computer_scientist: &nbsp; &nbsp; programming, math, statistic, data_base-> engineer: &nbsp; &nbsp; math, design, structural_calculus<jupyter_code># solution


file_contents = open('careerXskills.txt', 'r')
career_skills_list = file_contents.readlines()
career_list = []
skills_list = []

career_skills_dict = {}

#print(career_skills_list)

for i in career_skills_list:
    x,y = i.split()
    career_list.append(x)

career_list_unique = set(career_list)

for c in career_list_unique: career_skills_dict.update({c:[]})

for i in career_skills_list:
    x,y = i.split()
    if y not in career_skills_dict[x]:
        career_skills_dict[x].append(y)
    else:
        pass
    
#print(career_skills_dict)

for i in career_skills_dict:
    print('-> ', i,':')
    
    skills_string = ''
    
    for j in career_skills_dict[i]:
        #print(j)
        skills_string += str(j)
        skills_string += ', '
        
        
    print(skills_string[0:len(skills_string)-2], '\n')<jupyter_output>->  Computer_scientist :
project_management, accurate_communication, math, innovative_thinking, statistic, programming, technologial_knowledge, resilience 

->  Accountant :
teamworking, innovative_thinking, business_acumen, project_management, technologial_knowledge, IT, manage_deadlines, numeracy 

->  Medical_physicist :
physical_fitness, work_under_pressure, resilience, communication, intuitive_reasoning, teamworking 

->  Aeronautical_engineer :
accurate_communication, project_management, statistic, innovative_thinking, technologial_knowledge, programming, math 

->  Sports_coach :
resilience, accurate_communication, statistic, teamworking, programming, communication, intuitive_reasoning, numeracy, project_management, technologial_knowledge<jupyter_text>***__Question 3:__ (2.0 points) Write a function called  format_person_info that receives the two mandatory parameters _name_ and _age_ and returns a string containing the first and last name (disregarding middle names) followed by the the phrase "is _age_ years old". For instance, ```pythonprint(format_person_info('<NAME>',48))```should result in:<NAME> is 48 years oldA third, optional parameter, can also be provided, which specify the job position. For instance,  ```pythonprint(format_person_info('<NAME>',48,'Data Scientist'))```should result in:<NAME>, Data Scientis, is 48 years old<jupyter_code># Solution

def format_person_info(name_input, age_input, job_input=None):
    name_split = name_input.split()
    
    if job_input == None:
        return('%s %s is %d years old' %(name_split[0], name_split[len(name_split) -1], age_input))
    else:
        return('%s %s, %s, is %d years old' %(name_split[0], name_split[len(name_split) -1],job_input, age_input))



print(format_person_info('<NAME>',48,'Data Scientist'))
print(format_person_info('<NAME>',48))<jupyter_output><NAME>, Data Scientist, is 48 years old
L<NAME> is 48 years old<jupyter_text>***__Question 4:__ (2.0 points) Write a unit test for the function  format_person_info you implemented in question 3. Your unit test should assert the function correctness when two or three parameters are provided.<jupyter_code># Solution
import unittest

class NameAgeJob_Test_Class(unittest.TestCase):
    
    def test_inputs(self):
        self.assertEqual('A B is 44 years old',format_person_info('A B', 44))
        self.assertEqual('A B, hacker, is 44 years old',format_person_info('A B', 44, 'hacker'))

# the code below run the unit test
t = NameAgeJob_Test_Class()
suite = unittest.TestLoader().loadTestsFromModule(t)
unittest.TextTestRunner().run(suite)<jupyter_output>.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK<jupyter_text>***__Question 5:__ (2.0 points) Write a python code the does exactly the same as ```pythonimport randomprint([x for x in list(map(lambda i: random.randint(-i,i),10*[10])) if x>0])```but without using comprehension.<jupyter_code># solution
import random
random.seed(a=1) # this is only necessary to make sure both codes will produce the same set of random numbers

res_list = []
fcn = lambda j: random.randint(-j,j)
for y in 10*[10]:
    intr = fcn(y)
    if intr > 0:
        res_list.append(intr)

print(res_list)       

random.seed(a=1) # this is only necessary to make sure both codes will produce the same set of random numbers
print([x for x in list(map(lambda i: random.randint(-i,i),10*[10])) if x>0])<jupyter_output>[8, 5, 4, 5, 10, 2]
[8, 5, 4, 5, 10, 2]
{'fraction_non_alphanumeric': 0.11537286063569682, 'fraction_numerical': 0.023685819070904647, 'mean_word_length': 4.696257615317667, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '409306', 'license': 'MIT', 'n_tokens_mistral': 2196, 'n_tokens_neox': 2055, 'n_words': 728, 'path': 'PDS_jupyter/Assignments/Assignment 5 Sam Forman.ipynb', 'repo_name': 'formansa/SRF_github', 'size': '6615'}