There is a text that is read line by line from the file. It is necessary to find the smallest words (precisely words, since there may be many of them) that are present in this text. You can in any language, I'll figure it out. At least a small example. In general, the main question is how to find these words?


I read lines line by line from a file. Let him find the smallest words in the line. How to save them? Take to create a dynamic array? OK. I read the second line. Let her have even smaller words. Then how next? Free the memory of the array and fill it again? Or can read the entire file entirely and work with the entire file at the same time? What is the best algorithm to make?

  • For you, hardly anyone will think and solve your problem. Because in the future, such a programmer as you may become a colleague in the future and the question arises: "Who wants to work with a person who does not even try to solve the problem?" - sys_dev

2 answers 2

  1. Go through the file for the first time and determine the number of characters in the smallest word.
  2. Go through the second time and write the words of the appropriate length into any convenient data structure.

    In any language? Here you have C #:

    var allShortest = File.ReadLines(filename) .SelectMany(l => l.Split()) .GroupBy(w => w.Length) .OrderBy(g => g.Key) .First() .AsEnumerable(); 

    More efficient option:

     int? minlen = null; var allShortest = File.ReadLines(filename) .SelectMany(l => l.Split()) .OrderBy(w => w.Length) .TakeWhile(w => ((minlen = minlen ?? w.Length) == w.Length)); 

    Two-pass solution:

     var words = File.ReadLines(filename).SelectMany(l => l.Split()); var minlen = words.Select(w => w.Length).TakeUntil(l => l == 1).Min(); var allShortest = words.Where(w => w.Length = minlen); 
    • And this code? On any book? - alexlz
    • @alexlz: where do you see the plug? GroupBy and GroupBy lazy. - VladD
    • @alexlz: especially for you, a solution that does not do too much. - VladD
    • @VladD this is me for illiteracy. Does OrderBy definitely not require reading (with Split ()) a file into memory? - alexlz
    • one
      @VladD About the fact that two passes can save memory - I agree. And what about the "Venda" - is unclear. See how much of the ghc developers are employed at MS Research ... So haskell.org/platform/windows.html - alexlz