using python to interact with the operating system week 4 answers
Practice Quiz: Data Streams
1. Which command will print out the exit value of a script that just ran successfully?
- wc variables.py
- echo $?
- echo $PATH
- import sys
2. Which command will create a new environment variable?
- export
- env
- input
- wc
3. Which I/O stream are we using when we use the input function to accept user input in a Python script?
- STDOUT
- STDERR
- STDIN
- SYS
4. What is the meaning of an exit code of 0?
- The program ended with an unspecified error.
- The program ended with a ValueError.
- The program ended with a TypeError.
- The program ended successfully.
5. Which statements are true about input and raw_input in Python 2? (select all that apply)
- input performs basic math operations.
- raw_input performs basic math operations.
- raw_input gets a string from the user.
- input gets a string from the user.
6. What type of object does a run function return?
- returncode
- capture_output
- stdout
- CompletedProcess
7. How can you change the current working directory where a command will be executed?
- Use the capture_output parameter.
- Use the cwd parameter.
- Use the shell parameter.
- Use the env parameter.
8. When a child process is run using the subprocess module, which of the following are true? (check all that apply)
- The child process is run in a secondary environment.
- The parent process is blocked while the child process finishes.
- The parent process and child process both run simultaneously.
- Control is returned to the parent process when the child process ends.
9. When using the run command of the subprocess module, what parameter, when set to True, allows us to store the output of a system command?
- cwd
- capture_output
- timeout
- shell
10. What does the copy method of os.environ do?
- Creates a new dictionary of environment variables
- Runs a second instance of an environment
- Joins two strings
- Removes a file from a directory
11. We're working with a CSV file, which contains employee information. Each record has a name field, followed by a phone number field, and a role field. The phone number field contains U.S. phone numbers, and needs to be modified to the international format, with "+1-" in front of the phone number. Fill in the regular expression, using groups, to use the transform_record function to do that.
import redef transform_record(record): new_record = re.sub(___) return new_record
print(transform_record("Sabrina Green,802-867-5309,System Administrator")) # Sabrina Green,+1-802-867-5309,System Administrator
print(transform_record("Eli Jones,684-3481127,IT specialist")) # Eli Jones,+1-684-3481127,IT specialist
print(transform_record("Melody Daniels,846-687-7436,Programmer")) # Melody Daniels,+1-846-687-7436,Programmer
print(transform_record("Charlie Rivera,698-746-3357,Web Developer")) # Charlie Rivera,+1-698-746-3357,Web Developer
import re
def transform_record(record):
new_record = re.sub(___)
return new_record
print(transform_record("Sabrina Green,802-867-5309,System Administrator"))
# Sabrina Green,+1-802-867-5309,System Administrator
print(transform_record("Eli Jones,684-3481127,IT specialist"))
# Eli Jones,+1-684-3481127,IT specialist
print(transform_record("Melody Daniels,846-687-7436,Programmer"))
# Melody Daniels,+1-846-687-7436,Programmer
print(transform_record("Charlie Rivera,698-746-3357,Web Developer"))
- new_record = re.sub(r”,([\d-]+)”,r”,+1-\1″ ,record)
12. The multi_vowel_words function returns all words with 3 or more consecutive vowels (a, e, i, o, u). Fill in the regular expression to do that.
import redef multi_vowel_words(text): pattern = ___ result = re.findall(pattern, text) return result
print(multi_vowel_words("Life is beautiful")) # ['beautiful']
print(multi_vowel_words("Obviously, the queen is courageous and gracious.")) # ['Obviously', 'queen', 'courageous', 'gracious']
print(multi_vowel_words("The rambunctious children had to sit quietly and await their delicious dinner.")) # ['rambunctious', 'quietly', 'delicious']
print(multi_vowel_words("The order of a data queue is First In First Out (FIFO)")) # ['queue']
print(multi_vowel_words("Hello world!")) # []
import re
def multi_vowel_words(text):
pattern = ___
result = re.findall(pattern, text)
return result
print(multi_vowel_words("Life is beautiful"))
# ['beautiful']
print(multi_vowel_words("Obviously, the queen is courageous and gracious."))
# ['Obviously', 'queen', 'courageous', 'gracious']
print(multi_vowel_words("The rambunctious children had to sit quietly and await their delicious dinner."))
# ['rambunctious', 'quietly', 'delicious']
print(multi_vowel_words("The order of a data queue is First In First Out (FIFO)"))
# ['queue']
print(multi_vowel_words("Hello world!"))
- pattern = r”\b\w*[aeiou]{3,}\w*\b”
13. When capturing regex groups, what datatype does the groups method return?
- A string
- A tuple
- A list
- A float
14. The transform_comments function converts comments in a Python script into those usable by a C compiler. This means looking for text that begins with a hash mark (#) and replacing it with double slashes (//), which is the C single-line comment indicator. For the purpose of this exercise, we'll ignore the possibility of a hash mark embedded inside of a Python command, and assume that it's only used to indicate a comment. We also want to treat repetitive hash marks (##), (###), etc., as a single comment indicator, to be replaced with just (//) and not (#//) or (//#). Fill in the parameters of the substitution method to complete this function:
import redef transform_comments(line_of_code): result = re.sub(___) return result
print(transform_comments("### Start of program")) # Should be "// Start of program"print(transform_comments(" number = 0 ## Initialize the variable")) # Should be " number = 0 // Initialize the variable"print(transform_comments(" number += 1 # Increment the variable")) # Should be " number += 1 // Increment the variable"print(transform_comments(" return(number)")) # Should be " return(number)"
import re
def transform_comments(line_of_code):
result = re.sub(___)
return result
print(transform_comments("### Start of program"))
# Should be "// Start of program"
print(transform_comments(" number = 0 ## Initialize the variable"))
# Should be " number = 0 // Initialize the variable"
print(transform_comments(" number += 1 # Increment the variable"))
# Should be " number += 1 // Increment the variable"
print(transform_comments(" return(number)"))
- result = re.sub(r”#+”, r”//”, line_of_code)
15. The convert_phone_number function checks for a U.S. phone number format: XXX-XXX-XXXX (3 digits followed by a dash, 3 more digits followed by a dash, and 4 digits), and converts it to a more formal format that looks like this: (XXX) XXX-XXXX. Fill in the regular expression to complete this function.
import redef convert_phone_number(phone): result = re.sub(___) return result
print(convert_phone_number("My number is 212-345-9999.")) # My number is (212) 345-9999.print(convert_phone_number("Please call 888-555-1234")) # Please call (888) 555-1234print(convert_phone_number("123-123-12345")) # 123-123-12345print(convert_phone_number("Phone number of Buckingham Palace is +44 303 123 7300")) # Phone number of Buckingham Palace is +44 303 123 7300
import re
def convert_phone_number(phone):
result = re.sub(___)
return result
print(convert_phone_number("My number is 212-345-9999.")) # My number is (212) 345-9999.
print(convert_phone_number("Please call 888-555-1234")) # Please call (888) 555-1234
print(convert_phone_number("123-123-12345")) # 123-123-12345
- result = re.sub(r”\b(\d{3})-(\d{3})-(\d{4})\b”, r”(\1) \2-\3″, phone)