title: finding time and memory inefficiencies in my program
body: <p>I need to find time and memory inefficiencies in this program. I've tried several things: changing the variable names and also changed the code to delete the for-loops. These things made the program not work unfortunately. Can anyone help?</p>
  
  <pre><code>import sys
  def longest(doc):
  
  infile = open(doc, "r")
  text = infile.read()
  
  longest_seq = []
  current_seq = []
  current_start = ""
  first_letters = [""]
  for word in text.split():
      current_start = word[0]
      if current_start == first_letters[-1]:
          # same first letter as previous word
          current_seq.append(word)
      elif current_start != first_letters[-1]:
          # done with the current sequence
          current_seq = [word]
  
      first_letters.append(word[0])
  
      if len(longest_seq) &lt; len(current_seq):
          longest_seq = current_seq
  
  infile.close()
  return longest_seq
  
  
  def main(argv):
      if len(argv) != 2:
      print("Usage: {} File".format(argv[0]), file=sys.stderr)
      exit(-1)
  
     seq = longest(argv[1])
     slength = len(longest(argv[1]))
  
  print("longest sequence of consecutive words starting with the same letter:")
  print("\'{}\'".format(' '.join(seq)))
  print("length: {}\n".format(slength))
  
  
  
  if __name__ == "__main__":
  main(sys.argv)
  </code></pre>