Your task is write the following functions: 1. create file o takes filename as a parameter....
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Your task is write the following functions: 1. create file o takes filename as a parameter. The function creates an empty file. • An example is shown below: o create_file("myfile.txt") File myfile. txt should now be visible in the working directory 2. read_file o takes filename as a parameter and returns a string with file content. • An example is shown below, for the file myfile.txt assuming it has two separate lines, which are "This is The Pennsylvania " and "State University". oread_file("myfile.txt") Output: "This is The Pennsylvania State University" 3. read file lines o This function takes filename as a parameter, and returns a list whose elements are representative of the lines in the file. • An example is shown below, for the file myfile.txt assuming it has two separate lines, which are "This is The Pennsylvania" and "State University". read_file_lines("myfile.txt") Output: ['This is The Pennsylvania \n', 'State University\n"] 4. write_to_file • takes filename and a list as a parameter. This function overwrites the file using list element-by-element where each element is on the newline into the file. It is necessary to check if the list is empty before start inserting elements to file. If the list is empty, print "list is empty" and perform no operations. An example is show below, for the file myfile.txt assuming it has two separate lines, which are "This is The Pennsylvania " and "State University". write_to_file("myfile.txt", ["University Park", "PA USA"]) File myfile.txt now looks like the following:- myfile.txt X 1 University Park 2 PA USA 3 5. append_to_file o takes filename and a list as a parameter. This function writes the entire list element-by-element into the file. It is necessary to check if the list is empty before start inserting elements to file. An example is show below, for the file myfile.txt assuming it has two separate lines, which are "This is The Pennsylvania " and "State University". append_to_file("myfile.txt", ["University Park", "PA USA"]) File myfile.txt now looks like the following: O myfile.txt X 1 This is The Pennsylvania 2 State University 3 University Park 4 PA USA 5 Return values must be assigned to variables before used. • You are NOT allowed to use the following constructs unless specified in the assignment directions. Using the following constructs will result in 0 (zero) for the entire submission (assignment, timed test, etc.). The restricted items are as follows: ■ Concepts not discussed in lectures yet ▪ String functions ■ Member functions ▪ Exceptions (can use): len () and x=x+[y1, y2,y3] Built-in functions & types ▪ Exceptions (can use): str(), readline(), open(), close(), write(), read(), range(),.split() ■ Cannot use .append, .sort, etc. ■ Cannot use "Slicing" ■ Cannot use "list comprehension" ▪ Cannot use "not in" (together) Cannot use "in" not allowed with conditionals ▪ Exception (can use): with range () ▪ Cannot use* and {} ▪ Exception (can use): mathematical operations (multiply & exponents) ■ Data type functions ■ Exceptions (can use): int(), str (), float () ■ Break ■ Continue Nested Constructs ▪ Exceptions (can use): 2-D list ▪ Multiple returns & Multiple assignments ■ Recursion (not allowed unless the question explicitly states otherwise) ■ Functions within functions (Definitions) -- invocation is fine ▪ Default Parameters ■ Global variables ▪ Keyword as variable names ▪ while true or without proper condition Your task is write the following functions: 1. create file o takes filename as a parameter. The function creates an empty file. • An example is shown below: o create_file("myfile.txt") File myfile. txt should now be visible in the working directory 2. read_file o takes filename as a parameter and returns a string with file content. • An example is shown below, for the file myfile.txt assuming it has two separate lines, which are "This is The Pennsylvania " and "State University". oread_file("myfile.txt") Output: "This is The Pennsylvania State University" 3. read file lines o This function takes filename as a parameter, and returns a list whose elements are representative of the lines in the file. • An example is shown below, for the file myfile.txt assuming it has two separate lines, which are "This is The Pennsylvania" and "State University". read_file_lines("myfile.txt") Output: ['This is The Pennsylvania \n', 'State University\n"] 4. write_to_file • takes filename and a list as a parameter. This function overwrites the file using list element-by-element where each element is on the newline into the file. It is necessary to check if the list is empty before start inserting elements to file. If the list is empty, print "list is empty" and perform no operations. An example is show below, for the file myfile.txt assuming it has two separate lines, which are "This is The Pennsylvania " and "State University". write_to_file("myfile.txt", ["University Park", "PA USA"]) File myfile.txt now looks like the following:- myfile.txt X 1 University Park 2 PA USA 3 5. append_to_file o takes filename and a list as a parameter. This function writes the entire list element-by-element into the file. It is necessary to check if the list is empty before start inserting elements to file. An example is show below, for the file myfile.txt assuming it has two separate lines, which are "This is The Pennsylvania " and "State University". append_to_file("myfile.txt", ["University Park", "PA USA"]) File myfile.txt now looks like the following: O myfile.txt X 1 This is The Pennsylvania 2 State University 3 University Park 4 PA USA 5 Return values must be assigned to variables before used. • You are NOT allowed to use the following constructs unless specified in the assignment directions. Using the following constructs will result in 0 (zero) for the entire submission (assignment, timed test, etc.). The restricted items are as follows: ■ Concepts not discussed in lectures yet ▪ String functions ■ Member functions ▪ Exceptions (can use): len () and x=x+[y1, y2,y3] Built-in functions & types ▪ Exceptions (can use): str(), readline(), open(), close(), write(), read(), range(),.split() ■ Cannot use .append, .sort, etc. ■ Cannot use "Slicing" ■ Cannot use "list comprehension" ▪ Cannot use "not in" (together) Cannot use "in" not allowed with conditionals ▪ Exception (can use): with range () ▪ Cannot use* and {} ▪ Exception (can use): mathematical operations (multiply & exponents) ■ Data type functions ■ Exceptions (can use): int(), str (), float () ■ Break ■ Continue Nested Constructs ▪ Exceptions (can use): 2-D list ▪ Multiple returns & Multiple assignments ■ Recursion (not allowed unless the question explicitly states otherwise) ■ Functions within functions (Definitions) -- invocation is fine ▪ Default Parameters ■ Global variables ▪ Keyword as variable names ▪ while true or without proper condition
Expert Answer:
Answer rating: 100% (QA)
import string This is the main function which is the function declaration We can call any ... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
What would be the exposure irradiance (mj/cm 2 ) in the beam of a laser pointer for a duration of 0.25 seconds if the laser has a power output of 10 mW and an aperture of 5 mm? Assume there is no...
-
A vehicle moves with velocity vo and acceleration ao with respect to the fixed OXYZ reference frame. The vehicle carries a pendulum that has angular velocity wo and angular acceleration wo with...
-
This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of an array of objects as a class data...
-
Suppose the following game is repeated once (that is, played twice in total). Bridget SB (10, 74) SA Alex S'A SA SB (70,70) (74, 10) (-10,-10) (40,40) (-10,-10) SB (-10,-10) (-10,-10) (50, 50) (a)...
-
An automobile with a mass of 1000 kg, including passengers, settles 1.0 cm closer to the road for every additional 100kg of passengers. It is driven with a constant horizontal component of speed...
-
1. What can charisma do for you? 2. What are the characteristics of charisma? 3. What are the clues of power? 4. What inhibits power? 5. How do you dismantle the 'imposter' syndrome? 6. Why are the...
-
If among \(n\) objects \(k\) are alike and the others are all distinct, the number of permutations of these \(n\) objects taken all together is \(n ! / k !\). (a) How many permutations are there of...
-
At December 31, MediSharp Precision Instruments owes $50,000 on accounts payable, salary payable of $16,000, and income tax payable of $8,000. MediSharp also has $280,000 of bonds payable that were...
-
MARKET RESEARCH PROBLEM: To determine the best location in North America to launch a chocolate bar. (CHOCOLATE COMPANY WE ARE LAUNCHING - HEAVEN BAR) Question 1: State the market research problem...
-
Rainmaker Environmental Consultants is just finishing its second year of operations. The companys unadjusted trial balance at October 31, 2023, follows. Rainmaker prepares adjustments each October...
-
Richard, age 35, single, is employed (W2) as an actuary. He will itemize deductions for the tax year. For calendar year 2020, he had AGI of $63.000 and paid the following medical expenses in cash:...
-
Can fuel hedging lower fuel costs? Why do some airlines hedge fuel prices?
-
Suppose you purchase a 10-year bond with 11% annual coupons. You hold the bond for four years, and sell it immediately after receiving the fourth coupon. If the bonds yield to maturity was 9.94% when...
-
What are the various ways that a state- owned airline can be privatized?
-
Define ASM (or ASK), RASM (or RASK), CASM (or CASK), LF, and yield. How might a change in one affect another?
-
Your best taxable investment opportunity has an EAR of 6.4%. Your best tax-free investment opportunity has an EAR of 2.8%. If your tax rate is 32%, which opportunity provides the higher after-tax...
-
a-2) Number of observation a-3) customer proportion spent money on amazon prime b-2) frequency vs bin graph Question: Amazon Prime is a $119-per-year service that gives the company's customers free...
-
A horizontal annulus with inside and outside diameters of 8 and 10 cm, respectively, contains liquid water. The inside and outside surfaces are maintained at 40 and 20oC, respectively. Calculate the...
-
Sherry Hopson owns a retail family clothing store. Her store is located at 4321 Circle Drive, Houston, TX 77001. Her employer identification number is 95- 1234321 and her Social Security number is...
-
During the 2012 tax year, Irma incurred the following expenses: Union dues..............................................................$275 Tax return preparation...
-
Quince Corporation has taxable income of $450,000 for its 2012 calendar tax year. Calculate the corporation's income tax liability for 2012 before tax credits. $_________
-
Consider expansion of a function in terms of a series \(F_{n}(x)\) in the following form: \[f(x)=\sum_{n} A_{n} F_{n}(x)\] If the functions \(F_{n}\) are orthogonal then this property helps to unfold...
-
A concrete wall \(20 \mathrm{~cm}\) thick is initially at a temperature of \(20^{\circ} \mathrm{C}\), and is exposed to steam at pressure \(1 \mathrm{~atm}\) on both sides. Find the time for the...
-
Analyze the transient problem with the Dirichlet condition for a long cylinder and for a sphere. Derive expressions for the eigenfunctions, eigenconditions, and eigenvalues. Find the series...
Study smarter with the SolutionInn App