Problem 2 dictionariesfilesinfinite loopandahalf break 22 p

Problem 2 – dictionaries,files,infinite loop-and-a-half, break (22 pts)

You have a comma-separated file tickers.csv that stores company names and their stock (ticker) symbols. Use a text editor to see format of lines:

The company names often contain commas themselves, but the data has been cleaned and all commas removed (Cache’s official name is \" Cache, Inc. \", not \" Cache Inc. \").

Looking at file snippet in sample run, we see ticker symbol CAB represents Cabela’s Inc., and Cincinnati Bell Inc. uses ticker symbol CBB.

We will do this problem into two parts (a) and (b).

(a) 10 pts

Write a function read_ticker() that has one parameter: filename. The function will read tickers.csv which you will save down in same directory where your program is running. Your function will store the ticker (key)/name (value) in a dictionary and return the resulting dictionary so we can look up what company corresponds to a particular ticker symbol then reading business news.

1. Theoretically this file could get pretty large so do not read entire file into memory. Instead, iterate over file using for line in infile

2. Remove newline character from company names before storing them as values in the dictionary

3. Make sure to close file when done processing; dictionary should have 585 key/value pairs

(1a cont’d)

Sample run:

TEST calling function in interactive shell by assigning return value of function call to variable tickers. Then echo the value for key BJRI.

(b) 12 pts

Write a function ticker() that has one parameter: filename. The function will run read_ticker() created in part (a) and then assign the returned dictionary to a variable.

The function will then do the following:

1. Use infinite loop and a half pattern to run an interactive loop which prompts user for a ticker symbol

2. If the user just hits Enter/Return key without entering a name, the loop exits (remember our additional iteration control structures section 5.6 in book)

3. If the user enters a ticker symbol and it is in the list, the corresponding company name is printed

4. If the ticker symbol is not in the list, a warning is printing informing the user

5. Use .format() method of the str type to print output as highlighted in below sample run

6. For simplicity type ticker symbol at keyboard in all caps

Sample run:

TEST calling function and entering values when prompted: \'YHOO\', \'DKS\', \'GOOGLE\', <Enter>

Solution

def read_ticker(filename):
   with open(filename) as f:
       d = {}
       for line in f:
           d[line.split(\',\', 1)[0]] = line.split(\',\', 1)[1][:-1]
   return d


def ticker(filename):
   vals = read_ticker(filename)
   while True:
       i = raw_input(\'Enter a ticker symbol: \')
       if len(i) == 0:
           break
       else:
           if i in vals:
               print vals[i]
           else:
               print \'Symbol not found\'

print ticker(\'data.txt\')

Problem 2 – dictionaries,files,infinite loop-and-a-half, break (22 pts) You have a comma-separated file tickers.csv that stores company names and their stock (t
Problem 2 – dictionaries,files,infinite loop-and-a-half, break (22 pts) You have a comma-separated file tickers.csv that stores company names and their stock (t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site