[SOLVED] C data structure python 9/24/2019 Problems_reading_writing Jupyter Notebook

$25

File Name: C_data_structure_python_9/24/2019_Problems_reading_writing__Jupyter_Notebook.zip
File Size: 715.92 KB

5/5 - (1 vote)

9/24/2019 Problems_reading_writing Jupyter Notebook
Reading and Writing Text Files in Python
In [ ]:
Question 1
1. Copy this text into a txt file. Make sure the file is in the same folder as this Python notebook. Each sentence should be on a new line. So your file will have 4 lines.
Our models not only build on the stream of dynamic capital structure analyses to include frictions imposed by BEE regulations, but also on a stream of literature involving stock based lending. Rather than sell shares, borrowers provide their shares as collateral to lenders and in return receive a lower interest rate. In these transactions the dividends from the stock may be used to pay down the loan. A very interesting literature has emerged regarding how these deals can be valued by translating the problem to that of evaluating complex American call options.
2. create a function read_from_file( filename) a. This function reads this text file into the notebook
In [ ]: In [5]:
Question 2
1. create a function, read_nlines_from_file( filename, nlines) that reads the first nlines
In [2]:
Question 3
1. Write a Python function, file_read_return_list(fname), to read a file line by line and store it into a list that is returned.
2. Use the with command to open the file.
3. Print out the second line only. ( Use the text file in Question 1 and 2)
def read_from_file(fname):

def read_nlines_from_file(fname, nlines): pass
localhost:8892/notebooks/Downloads/CWRU/2019 Fall/Python/L4 Reading and Writing ASCI Files/Problems_reading_writing.ipynb 1/4

9/24/2019 Problems_reading_writing Jupyter Notebook
In [3]:
Question 4
Write a Python function, file_read_return_last_two(fname) that prints out the last 2 lines ( sentences) of the file.
In [4]:
Question 5
def file_read_return_last_two(fname): pass
In [5]:
In [6]:
In [7]:
def file_read_return_list(fname): pass
1. Create a dictionary with keys 1 through 100 and values equal to the key**2
2. Once you have created this dictionary save it as a text file. The odd lines should be the keys
and the even rows should be their values. A function should be developed,save_dict(dicname,
fname) that saves the dictionary in fname.
3. Now create a second function,read_dict(fname), that reads this txt file and recreates the
original dictionary
#Create a dictionary as follows
A = [a for a in range(10)]
B = [b**2 for b in range(10)]
C = list(zip(A,B))
d = dict(C)
print(d)
# Then create a function that writes the dictionary to a file as a text fil # Then create a function that recreates the dictionary
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
def save_dict(dicname, fname): pass
def read_dict(fname): #dicname = {}
#f = open(fname, r)
pass
#return dicname
#dict = read_dict(one.txt)
#print(dict)
localhost:8892/notebooks/Downloads/CWRU/2019 Fall/Python/L4 Reading and Writing ASCI Files/Problems_reading_writing.ipynb 2/4

9/24/2019 Problems_reading_writing Jupyter Notebook
In [ ]:
Question 6
Prepare for a Case Study: Making a Phone Directory
1. We build a system where we ask the user to
a. enter record
b. Do a query
c. Load a file
d Save a file
e Exit
2. In our case enter a record means entering a name and a telephone
number
3. So our system is simple. We just want a mechanism of storing nam
es and phone numbers
4. Any given a name we want to pull out a phone number
1. We build a system where we ask the user to a. enter a new record b. Do a query c. Load a file d Save a file e Exit
2. In our case enter a record means entering a name and a telephone number
3. So our system is simple. We just want a mechanism of storing names and phone numbers
4. Any given a name we want to pull out a phone number
4.1 There are many problems which have this format.
4.2 A menu with a list of possible actions that can be taken
4.3 A mechanism for retrieving data from a file and storing new data that you have 4.4 We have different routines for the different actions.
4.5 We have to be very careful on how we select data structures for the different activities
5. In practice the input and output routines have to be carefully designed so as to be idiot proof.
6. We do not want bad data to enter the system.
7. For example we will add some minimal features to ensure the telephone number is entered in a
format that we can use.
8. We may also want to provide some form of assistance to the query function. For example as
the person types a name the computer gives suggestions from the data base.
Please take a crack at building this system.
1. and come to class ready to help collectively to build such a sys
tem
localhost:8892/notebooks/Downloads/CWRU/2019 Fall/Python/L4 Reading and Writing ASCI Files/Problems_reading_writing.ipynb 3/4

9/24/2019 Problems_reading_writing Jupyter Notebook
localhost:8892/notebooks/Downloads/CWRU/2019 Fall/Python/L4 Reading and Writing ASCI Files/Problems_reading_writing.ipynb 4/4

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] C data structure python 9/24/2019 Problems_reading_writing Jupyter Notebook
$25