Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Codio challenge Challenge: variable length records Variable Length Record Load the pipe-delimited file P. It is organized with 3 fields on each line: firstname|lastname|birthday. Search

Codio challenge

Challenge: variable length records

Variable Length Record

Load the pipe-delimited file P. It is organized with 3 fields on each line: firstname|lastname|birthday.

Search for the firstname F and lastname L, replacing the birthday with B. Write the file back out in the same pipe-delimited format.

# Get the filepath from the command line import sys P= sys.argv[1] F= sys.argv[2] L= sys.argv[3] B= sys.argv[4]

# ----------------------------------------------------------------

#

# Our Helper functions:

#

# ----------------------------------------------------------------

#

# Loads the file at filepath

# Returns a 2d array with the data

#

def load2dArrayFromFile(filepath): # Your code goes here:

#open the file in read mode with open(filepath, 'r') as rfile: totallines = rfile.read().split(' ')

while '' in totallines: totallines.remove('')

loadedArray = [eachline.split('|') for eachline in totallines]

return loadedArray

#

# Searches the 2d array 'records' for firstname, lastname.

# Returns the index of the record or -1 if no record exists

#

def findIndex(records, firstname, lastname):

# Your code goes here: for eachline in records: if firstname == eachline[0] and lastname == eachline[1]: return records.index(eachline)

return -1

# Sets the birthday of the record at the given index

# Returns: nothing

def setBirthday(records, index, newBirthday): # Your code goes here: line = records[index]

#store the birthday date line[2] = newBirthday

records[index] = line

# Convert the 2d array back into a string

# Return the text of the 2d array

def makeTextFrom2dArray(records): # Your code goes here:

filewriter = str()

for eachline in records: filewriter += "|".join(eachline) + " "

return filewriter

# ----------------------------------------------------------------

#

# Our main code body, where we call our functions.

#

# ----------------------------------------------------------------

# Load our records from the file into a 2d array

records= load2dArrayFromFile(P)

# Find out which index, if any, has the name we are hunting

indexWeAreHunting= findIndex(records, F, L)

# Set the birthday record to the one we were passed

setBirthday(records, indexWeAreHunting, B)

# Convert the records into a text string

output= makeTextFrom2dArray(records)

# Your code goes here

# write the text string out to the file

with open(P,'w') as wfile: wfile.write(output)

LAST RUN on 2/17/2018, 2:54:36 PM

 

Program Output

Your program output did not match the expected output. Your output: Adam|Smith|11111985 Theodore|Anderson|20031990 Adam K|Smith|09091999 Monty|Biscuit-Barrel|18101980 Adam|Smithers|10101960 Ruthy|Anderson|00000000 Expected output: Adam|Smith|11111985 Theodore|Anderson|20031990 Adam K|Smith|09091999 Monty|Biscuit-Barrel|18101980 Adam|Smithers|00000000 Ruthy|Anderson|06062010

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

IBM Db2 11 1 Certification Guide Explore Techniques To Master Database Programming And Administration Tasks In IBM Db2

Authors: Mohankumar Saraswatipura ,Robert Collins

1st Edition

1788626915, 978-1788626910

Students also viewed these Databases questions

Question

Identify conflict triggers in yourself and others

Answered: 1 week ago