Guest User

Untitled

a guest
Jun 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. import string
  2.  
  3. def main():
  4. print " This program reads from a file and then prints out the"
  5. print " line with the longest length the line ,or with the highest sum"
  6. print " of ASCII values , or the line with the greatest number of words"
  7. infile = open("30075165.txt","r")
  8. for line in infile:
  9. print line
  10. infile.close()
  11. def length():
  12. maxlength = 0
  13. infile = open("30075165.txt","r")
  14. for line in infile:
  15. linelength = lengthofline
  16. if linelength > maxlength:
  17. #If linelength is greater than maxlength value the new value is linelength
  18. maxlength = linelength
  19. linelength = line
  20. print ,maxlinetext
  21. infile.close()
  22.  
  23. print max(open(your_filename, 'r'), key=len)
  24.  
  25. print(max(open(your_filename, 'r'), key=len))
  26.  
  27. large_line = ''
  28. large_line_len = 0
  29. filename = r"C:tmpTestFile.txt"
  30.  
  31. with open(filename, 'r') as f:
  32. for line in f:
  33. if len(line) > large_line_len:
  34. large_line_len = len(line)
  35. large_line = line
  36.  
  37. print large_line
  38.  
  39. This Should Be Largest Line
  40.  
  41. def get_longest_line(filename):
  42. large_line = ''
  43. large_line_len = 0
  44.  
  45. with open(filename, 'r') as f:
  46. for line in f:
  47. if len(line) > large_line_len:
  48. large_line_len = len(line)
  49. large_line = line
  50.  
  51. return large_line
  52.  
  53. print get_longest_line(r"C:tmpTestFile.txt")
  54.  
  55. def get_longest_line(filename):
  56. mydict = {}
  57.  
  58. for line in open(filename, 'r'):
  59. mydict[len(line)] = line
  60.  
  61. return mydict[sorted(mydict)[-1]]
  62.  
  63. Small Line
  64. Small Line
  65. Another Small Line
  66. This Should Be Largest Line
  67. Small Line
  68.  
  69. print " This program reads from a file and then prints out the"
  70. print " line with the longest length the line ,or with the highest sum"
  71. print " of ASCII values , or the line with the greatest number of words"
  72.  
  73. def get_file_data(filename):
  74. def ascii_sum(line):
  75. return sum([ord(x) for x in line])
  76. def word_count(line):
  77. return len(line.split(None))
  78.  
  79. filedata = [(line, len(line), ascii_sum(line), word_count(line))
  80. for line in open(filename, 'r')]
  81.  
  82. return filedata
  83.  
  84. afile = r"C:TmpTestFile.txt"
  85.  
  86. for line, line_len, ascii_sum, word_count in get_file_data(afile):
  87. print 'Line: %s, Len: %d, Sum: %d, WordCount: %d' % (
  88. line.strip(), line_len, ascii_sum, word_count)
  89.  
  90. Line: Small Line, Len: 11, Sum: 939, WordCount: 2
  91. Line: Small Line, Len: 11, Sum: 939, WordCount: 2
  92. Line: Another Small Line, Len: 19, Sum: 1692, WordCount: 3
  93. Line: This Should Be Largest Line, Len: 28, Sum: 2450, WordCount: 5
  94. Line: Small Line, Len: 11, Sum: 939, WordCount: 2
  95.  
  96. >>> afile = r"C:TmpTestFile.txt"
  97. >>> file_data = get_file_data(afile)
  98. >>> max(file_data, key=lambda line: line[1]) # Longest Line
  99. ('This Should Be Largest Linen', 28, 2450, 5)
  100. >>> max(file_data, key=lambda line: line[2]) # Largest ASCII sum
  101. ('This Should Be Largest Linen', 28, 2450, 5)
  102. >>> max(file_data, key=lambda line: line[3]) # Most Words
  103. ('This Should Be Largest Linen', 28, 2450, 5)
  104.  
  105. def main():
  106. print " This program reads from a file and then prints out the"
  107. print " line with the longest length the line ,or with the highest sum"
  108. print " of ASCII values , or the line with the greatest number of words"
  109. length()
  110.  
  111. def length():
  112. maxlength = 0
  113. maxlinetext = ""
  114. infile = open("30075165.txt","r")
  115. for line in infile:
  116. linelength = len(line)
  117. if linelength > maxlength:
  118. #If linelength is greater than maxlength value the new value is linelength
  119. maxlength = linelength
  120. maxlinetext = line
  121. print maxlinetext
  122. infile.close()
  123.  
  124. linelength = lengthofline # bug?
  125.  
  126. linelength = len(line) # fix
  127.  
  128. $ awk 'length() > n { n = length(); x = $0 } END { print x }' 30075165.txt
  129.  
  130. import os.path
  131.  
  132. def getLongestLineFromFile(fileName):
  133. longestLine = ""
  134.  
  135. if not os.path.exists(fileName):
  136. raise "File not found"
  137.  
  138. file = open(fileName, "r")
  139. for line in file:
  140. if len(line) > len(longestLine):
  141. longestLine = line
  142.  
  143. return longestLine
  144.  
  145.  
  146. if __name__ == "__main__":
  147. print getLongestLineFromFile("input.data")
  148.  
  149. 111111111
  150. 1111111111111111111111
  151. 111111111
  152. 22222222222222222
  153. 4444444444444444444444444444444
  154. 444444444444444
  155. 5555
Add Comment
Please, Sign In to add comment