Question

python code:

Task 1: Write a Python program that takes as input from the user the name of a file containing postcode/location information.Improving Readability Modify your program in Task 1 so that it prints the list in a more readable format. For example: your p

Task 1: Write a Python program that takes as input from the user the name of a file containing postcode/location information. Each line in the file consists of the postcode followed by a tab followed by a comma-separated list of locations that have that postcode. For example, the file Small.txt contains: 3015 Newport,South Kingsville,Spotswood 3016 Williamstown 3018 Altona,Seaholme 3019 3021 Albanvale,Kealba,Kings Park,St Albans Braybrook, Robinson Your program should create a list of postcode/location pairs with each pair stored in the list. It should print the list, so that you can check that your program has performed correctly For example: your program may do the following: Enter name of postcode file: Small.txt [[3015, 'Newport'], [3015, 'South Kingsville'], [3015, 'Spotswood'], [3016, 'Williamstown'1, [3018, 'Altona'], [3018, 'Seaholme'], [3019, 'Braybrook'l, [3019, 'Robinson', [3021, 'Albanvale'], [3021, 'Kealba'], [3021, 'Kings Park'], [3021, 'St Albans'11 Note You will need to use split twice: first to separate the postcode from the rest of the string and secondly to separate the rest of the strings into individual locations.
Improving Readability Modify your program in Task 1 so that it prints the list in a more readable format. For example: your program may do the following: Enter name of postcode file: Small.txt 3015 Newport 3015 South Kingsville 3015 Spotswood 3016 Williamstown 3018 Altona 3018 Seaholme 3019 Braybrook 3019 Robinson 3021 Albanvale 3021 Kealba 3021 Kings Park 3021 St Albans Task 2 Write a function BinarySearch1 takes as input a list L and a string and uses a recursive binary search to find an entry in the list with a suburb that matches the given string. Your function should return the index in L if the string is found or -1 if it is not found. You may find it useful to look at the Python code for iterative Binary Search given in: Divide and conquer (Lecture 13) Modify your program in Task 2 so that it asks the user for the name of a suburb and uses BinarySearch1 to find the postcode of the given suburb. If the suburb is not in the database, your program should print "Not Found, otherwise it should print the postcode Note: Since the suburb names are not sorted, you will first be required to sort the list according to the suburb names. To do this, you may use the selection sort code you developed in Week 5 Task 3. If you struggled with week 5 task 3, you may ask your tutor for assistance. For Example: Your program may output: Enter name of postcode file: SMALL.txt Enter Suburb Name: Newport Post code is 3015
0 0
Add a comment Improve this question Transcribed image text
Answer #1

TASK1:

if __name__ == "__main__":
#input file name from user
fname=raw_input("Enter name of the postcode file: ")
#open file
file=open(fname)
#to store the post codes
code=[]
#to store the suburbs
sub=[]
for line in file:
#first split line using '\t'
l1=line.split("\t")
#next split using ','
for suburb in l1[1].split(','):
#remove '\n' in last term
if(suburb[-1]=='\n'):
suburb=suburb[:-1]
print l1[0],suburb

CODE IMAGE:

if name main #input file name from user fname=raw_input (Enter name of the postcode file: ) #open file file-open (fname)

OUTPUT:

Enter name of the postcode file: input.txt 3015 Newport 3015 South Kingsville 3015 Spotswood 3016 Willianstown 3018 Altona 30

TASK2:

#binart search using recursion
def binary_search(srt, f, l, key):
#if front more than last then not found
if not f < l:
return -1
#calculate mid
mid = (f + l)//2
#compate key with arrays mid index element
if srt[mid] < key:
return binary_search(srt, mid + 1, l, key)
elif srt[mid] > key:
return binary_search(srt, f, mid, key)
else:
return mid
#return index position

def selection(srt):
for i in range(len(srt)):
min_idx = i
for j in range(i+1, len(srt)):
if srt[min_idx] > srt[j]:
min_idx = j
srt[i], srt[min_idx] = srt[min_idx], srt[i]
return srt

if __name__ == "__main__":
#input file name from user
fname=raw_input("Enter name of the postcode file: ")
#open file
file=open(fname)
#to store the post codes
code=[]
#to store the suburbs
sub=[]
for line in file:
#first split line using '\t'
l1=line.split("\t")
#next split using ','
for suburb in l1[1].split(','):
#remove '\n' in last term
if(suburb[-1]=='\n'):
suburb=suburb[:-1]
#print l1[0],suburb
code.append(l1[0])
sub.append(suburb)
#copy suburbs to srt to sort further
srt=sub[:]
#sort the array
srt=selection(srt)
#get suburb fromuser
suburb=raw_input("Enter Suburb Name: ")
#search suburb in list
res=binary_search(srt, 0, len(srt), suburb)
if(res==-1):
print "Not Found"
else:
print "Post code is "+code[sub.index(suburb)]

CODE IMAGES:

#binart search using recursion def binary search (srt, f, l, key): #if front more than last then not found if not f < 1: retumain if name #input file name from user fname-raw input (Enter name of the postcode file: ) #open file file-open (fname)

OUTPUT:

Enter name of the postcode file: input.txt Enter Suburb Name: Newport Post code is 3015

Add a comment
Know the answer?
Add Answer to:
Task 1: Write a Python program that takes as input from the user the name of a file containing po...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a PYTHON program that reads a file (prompt user for the input file name) containing...

    Write a PYTHON program that reads a file (prompt user for the input file name) containing two columns of floating-point numbers (Use split). Print the average of each column. Use the following data forthe input file: 1   0.5 2   0.5 3   0.5 4   0.5 The output should be:    The averages are 2.50 and 0.5. a)   Your code with comments b)   A screenshot of the execution Version 3.7.2

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • 1. Write a program called Numbers that a. prompts the user for a file name. b....

    1. Write a program called Numbers that a. prompts the user for a file name. b. reads that file assuming that its contents consist entirely of integers. c. prints the maximum, minimum, sum, count (number of integers in the file), and average of the numbers. For example, if the file numberinput.dat has the following content: 4 -2 18 15 31 27 Your program should produce the following output: csc% java Numbers Enter file name: numberinput.daft Maximum31 Minimum- -2 Sum -...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • Task 1: Calling a Function PROGRAM IS IN PYTHON) SHOW INPUT AND OUTPUT SCREEN SHOTS Task...

    Task 1: Calling a Function PROGRAM IS IN PYTHON) SHOW INPUT AND OUTPUT SCREEN SHOTS Task 1: Calling a Function Defining a function gives it a name, specifies the parameters that are to be included in the function and structures the blocks of code. Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt. Following is an example to call the printme() function #!/usr/bin/python 3...

  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

  • IN PYTHON, Write a program that reads a string from the user containing a date in...

    IN PYTHON, Write a program that reads a string from the user containing a date in the form mm/dd/ yyyy. It should print the date in the form April 12, 2017. Hint: Get the "mm" from the user entered date "mm/dd/yyyy", then convert the "mm" into a month number. You may use an list month_list = ['January', 'February','March','April', 'May','June', 'July','August', 'September', 'October', 'November', 'December']. Use the month number you got from "mm" to get the month name.

  • 3. Write Python statements that will do the following: a) Input a string from the user....

    3. Write Python statements that will do the following: a) Input a string from the user. *** Print meaningful messages along with your output.* b) Print the length of the String. Example input: The sky is blue. Correct output: The length of the string is 16 Incorrect output: 16 c) Print the first character of the string. d) Print the last character of the string. 4. Save the program. Double-check the left edge for syntax errors or warning symbols before...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT