7
$\begingroup$

Hi guys I am having problems with finding the longest word available in WordList["English"] I have found that the longest word has 24 characters thanks to this line

Max[StringLength[WordList[]]]

but I don't know how to find the position of the word Knowing her length. I thought I need some selection rule but I fail to implement it. any help?

$\endgroup$
13
  • $\begingroup$ So you want to know the longest word, its position in the list or both? $\endgroup$
    – Kuba
    Commented Aug 14, 2016 at 10:45
  • 3
    $\begingroup$ Answer to the title: MaximalBy[StringLength]@WordList[]. $\endgroup$
    – Kuba
    Commented Aug 14, 2016 at 10:46
  • $\begingroup$ the word, however i think i need the position to use Take[WordList[],n]... $\endgroup$
    – Alucard
    Commented Aug 14, 2016 at 10:46
  • $\begingroup$ You need the position for Take but don't assume you need Take. $\endgroup$
    – Kuba
    Commented Aug 14, 2016 at 10:47
  • 1
    $\begingroup$ Now that you know that the longest entry in WordList[] is 24 characters long, why not Select[] all the entries in it whose StringLength[] is == 24? (This is one of the things I like about this language; sometimes the solution statement directly translates to usable code.) $\endgroup$ Commented Aug 14, 2016 at 10:48

2 Answers 2

8
$\begingroup$

ok one solution I found, aside the one given by Kuba , is

length = Max[StringLength[WordList[]]]
Select[WordList[], StringLength[#] == length & ]

a second solution with the function Pick[] is

Pick[WordList[], StringLength[WordList[]], length]

A third one with Cases[]

f = StringLength[#] == 24 &;
Cases[WordList[], x_ /; f[x]]

a BarChart with the absolutetiming:

absolute timing of the different functions

$\endgroup$
4
  • $\begingroup$ Well done. If you need further exercise, think about how to implement this with Pick[] or Cases[]. $\endgroup$ Commented Aug 14, 2016 at 11:13
  • $\begingroup$ @J.M. i will thanks $\endgroup$
    – Alucard
    Commented Aug 14, 2016 at 11:18
  • $\begingroup$ @J.M. i found the two additional solutions but i did not understand the difference between the patterns used with Cases and the function with select[].what's the difference?they do exactly the same thing... $\endgroup$
    – Alucard
    Commented Aug 14, 2016 at 12:44
  • 1
    $\begingroup$ @Alucard This post should answer your question. $\endgroup$
    – user31159
    Commented Aug 14, 2016 at 14:36
1
$\begingroup$

With TakeLargestBy we can get the word and its length

TakeLargestBy[WordList[] -> {"Element", "Value"}, StringLength, 1]

{{"electroencephalographic", 23}}

The 10 longest words:

TakeLargestBy[WordList[] -> {"Element", "Value"}, StringLength, 10] // TableForm

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.