What is the most efficient way to find the length of the longest item in a list?

Given a list of sorted words of length, what is the best way to find the maximum length of any words?

For example, the following should return 6

findMaxLen("a,set,of,random,words")


Of course, it's pretty trivial to do this ...

<cffunction name="findMaxLen" returntype="Numeric">
    <cfset var CurMax = 0 />
    <cfset var CurItem = 0 />
    <cfloop index="CurItem" list="#Arguments[1]#">
        <cfif Len(CurItem) GT CurMax >
            <cfset CurMax = Len(CurItem)/>
        </cfif>
    </cfloop>
    <cfreturn CurMax />
</cffunction>


Or, a little shorter ...

<cffunction name="findMaxLen" returntype="Numeric">
    <cfset var CurMax = 0 />
    <cfset var CurItem = 0 />
    <cfloop index="CurItem" list="#Arguments[1]#">
        <cfset CurMax = Max( CurMax , Len(CurItem) ) />
    </cfloop>
    <cfreturn CurMax />
</cffunction>


But is there a better way - something more effective?

Perhaps some kind of Java method? Convert to array and sort by element length? Count the biggest gap between commas?


In practical terms, any of the above two examples will work perfectly for my current need, and this is not for what is critical for performance, so I don’t need an answer to this question, but I thought it was interesting to see anyway what people can come up with ...

+3
14

.

, - , ; it O(n), ( , ).

int FindLongestWord(char* str)
{
   char* lastComma = str - 1;
   int longest = 0;
   int length;
   char* pCheckChar;

   for(pCheckChar = str; *pCheckChar; pCheckChar++)
   {
      if(*pCheckChar == ',')
      {
         length = pCheckChar - lastComma - 1;
         if(length > longest)
         {
            longest = length;
         }

         lastComma = pCheckChar;
      }
   }

   // Check to see if the last word is the longest
   length = pCheckChar - lastComma - 1;
   if(length > longest)
   {
      longest = length;
   }

   return longest;
}

,

"a,set,of,random,words".Split(',').Max(w=>w.Length);

...;]

+11

Perl ( $max, ):

(length $1 > $max) && ($max = length $1) while "a,set,of,random,words" =~ /(\w+)/g;

:

(length $_ > $max) && ($max = length $_) foreach split /,/, "a,set,of,random,words";

:

$max = length((sort { length $b <=> length $a } split /,/, "a,set,of,random,words")[0]);

TMTOWTDI, .

EDIT: !

use List::Util 'reduce';
$max = length reduce { length $a > length $b ? $a : $b } split /,/, "a,set,of,random,words";

... - . , !

2: map():

use List::Util 'max';
$max = max map length, split /,/, "a,set,of,random,words";

, .

3: :

($max) = sort { $b <=> $a } map length, split /,/, "a,set,of,random,words";
+2

, code-golf, 52 #

"a,set,of,random,words".Split(',').Max(w=>w.Length);
+1

"" CFML - 72 ...

Len(ArrayMax("a,set,of,random,words".replaceAll('[^,]','1').split(',')))

, 78 , (. )...

Len(ListLast(ListSort("a,set,of,random,words".replaceAll('[^,]','1'),'text')))
+1

- 54 Python:

len(max("a,set,of,random,words".split(","), key=len))
0

java . ( : P) max 0

   int find(LinkedList strings, int max) {
      int i;
      String s=(String)strings.element();
      for(i=0;s.charAt(i)!='\0';i++);
      if(strings.hasNext())
         return find(strings.Next(),(i>max?i:max));
      return max;
    }

: , , , , :)

0

...;)

String longest(String...ss){String _="";for(String s:ss)if(_.length()<s.length())_=s;return _;}
0

, , . , . , , .

.

0

v++

int findMaxLen(const char *s)
{
    const char c = ',';
    int a = 0, b = 0;
    while(*s)
    {
        while(*s && *s++ != c)b++;
        if(b > a)a=b;
        b = 0;
    }
    return a;
}
0

J

, (L):

{.\:~>#&.>L

CSV:

{.\:~;>#&.>readcsv'test.csv'
0

scala (55 ):

",set,of,random,words".split(",").:\(0)(_.length max _)
0

Clojure: 49 .

(def l #(reduce max(for[x(.split%%2)](count x))))

:

(defn longest [astr sep]
  (reduce max
    (for [word (.split astr sep)]
      (count word))))

, .

0

Python , , ...

def maxlen(x):
    return len(sorted(x.split(), key=lambda y: len(y))[1])

.

0

?

<cffunction name="findMaxLen" returntype="Numeric">
    <cfset longest = 0>
    <cfloop list="#Arguments[1]#" index="w">
        <cfif len(w) gt longest>
            <cfset longest = len(w)>
        </cfif>
    </cfloop>
    <cfreturn longest>
</cffunction>
0

Source: https://habr.com/ru/post/1704093/


All Articles