Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. # Get the filepath from the command line
  2. import sys
  3. P= sys.argv[1]
  4. F= sys.argv[2]
  5. L= sys.argv[3]
  6. B= sys.argv[4]
  7.  
  8. # ----------------------------------------------------------------
  9. #
  10. # Our Helper functions:
  11. #
  12. # ----------------------------------------------------------------
  13.  
  14. #
  15. # Loads the file at filepath
  16. # Returns a 2d array with the data
  17. #
  18. def load2dArrayFromFile(filepath):
  19. # Your code goes here:
  20. with open(filepath, 'r') as read_file:
  21. lines = read_file.readlines()
  22.  
  23. result = []
  24. for line in lines:
  25. fields = line.strip().split('|')
  26. result.append(fields)
  27.  
  28. return result
  29. #
  30. # Searches the 2d array 'records' for firstname, lastname.
  31. # Returns the index of the record or -1 if no record exists
  32. #
  33. def findIndex(records, firstname, lastname):
  34. # Your code goes here:
  35. for index in range(len(records)):
  36. if records[index][0] == firstname and records[index][1] == lastname:
  37. return index
  38.  
  39. # Sets the birthday of the record at the given index
  40. # Returns: nothing
  41. def setBirthday(records, index, newBirthday):
  42. # Your code goes here:
  43. records[index][2] = newBirthday
  44. # Convert the 2d array back into a string
  45. # Return the text of the 2d array
  46. def makeTextFrom2dArray(records):
  47. # Your code goes here:
  48. result = ''
  49. for row in records:
  50. result += '|'.join(row)
  51. result += '/n'
  52. return result.rstrip()
  53. # ----------------------------------------------------------------
  54. #
  55. # Our main code body, where we call our functions.
  56. #
  57. # ----------------------------------------------------------------
  58.  
  59. # Load our records from the file into a 2d array
  60. records= load2dArrayFromFile(P)
  61.  
  62. # Find out which index, if any, has the name we are hunting
  63. indexWeAreHunting= findIndex(records, F, L)
  64.  
  65. # Set the birthday record to the one we were passed
  66. setBirthday(records, indexWeAreHunting, B)
  67.  
  68. # Convert the records into a text string
  69. output= makeTextFrom2dArray(records)
  70.  
  71. # Your code goes here
  72. # write the text string out to the file
  73. output_string = makeTextFrom2dArray(records)
  74.  
  75. with open(P, 'w') as write_file:
  76. lines = write_file.write(output_string)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement