# Finding the greater word

**URL:** <https://discourse.julialang.org/t/finding-the-greater-word/123909>\
**Category:** New to Julia\
**Created:** [December 17, 2024, 6:50am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909 "2024-12-17T06:50:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 17, 2024, 6:50am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/1 "2024-12-17T06:50:07Z")

</div>

Hello,  
I have a file with the following contents:

```julia
>>>Goodness ^ Of ^ Grace: Come :;You broken! > Heavy laden.. ..
<<<+Come! You helpless!?.() come You in despair:-(..

```

I wrote the following code to find the greater word:

```julia
function extractor()
    inn = open("input.txt","r")
    temp1 = ""
    temp2 = ""
    while !eof(inn)
        ch = read(inn, Char)
        if isletter(ch)
            temp1 *= ch
        elseif isspace(ch) && (cmp(temp1,temp2) == 1)
            temp2 = temp1
            temp1 = ""
        end        
    end
    println(temp2)
end

extractor()

```

The result is:

```julia
Of

```

What is wrong?

Thank you.

---

<div class="post-metadata">

**Author:** ![g-gundam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/g-gundam/32/47593_2.png) [@g-gundam](https://discourse.julialang.org/u/g-gundam)\
**Post date:** [December 17, 2024, 6:51am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/2 "2024-12-17T06:51:19Z")

</div>

Define what you mean by “greater” for us. What word did you want to find and why?

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 17, 2024, 6:59am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/3 "2024-12-17T06:59:07Z")

</div>

Hi,  
Thank you so much for your reply.  
For example, `You` is smaller than `you`.

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [December 17, 2024, 7:01am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/4 "2024-12-17T07:01:08Z")

</div>

Why is You smaller than you?

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 17, 2024, 7:02am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/5 "2024-12-17T07:02:59Z")

</div>

Hi,  
Because:

```julia
julia> Int('Y')
89

julia> Int('y')
121

```

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [December 17, 2024, 7:10am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/6 "2024-12-17T07:10:21Z")

</div>

How do you compare an empty string “” with the string “abc”?

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 17, 2024, 7:12am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/7 "2024-12-17T07:12:42Z")

</div>

With the `cmp` function:

```julia
julia> temp1 = "Goodness"
"Goodness"

julia> temp2 = ""
""

julia> cmp(temp1,temp2)
1

```

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [December 17, 2024, 7:29am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/8 "2024-12-17T07:29:21Z")

</div>

```julia
julia> a="Of"
"Of"

julia> b="Goodness"
"Goodness"

julia> if cmp(a,b) == 1
       println("a is greater than b")
       else println("b is greater than a") end
a is greater than b

```

So “Of” is the greatest word of them all. Problem solved.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 17, 2024, 10:38am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/9 "2024-12-17T10:38:08Z")

</div>

Hi,  
But, `You` is greater than `Of`:

```julia
julia> cmp("Of","You")
-1

julia> Int('O')
79

julia> Int('Y')
89

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 17, 2024, 10:42am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/10 "2024-12-17T10:42:47Z")

</div>

> [@StevenSiew](#):
>
> Why is You smaller than you?

```julia-auto
julia> "You" < "you"
true

```

It’s lexical ordering.

> [@hack3rcon](#):
>
> `(cmp(temp1,temp2) == 1)`

Use `temp1 > temp2` instead. It does the same thing, but is clearer. I actually didn’t know about the `cmp` function until now.

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [December 17, 2024, 10:44am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/11 "2024-12-17T10:44:36Z")

</div>

The easiest approach here would be to leverage the fact that you can already compare strings directly:

```julia
julia> "You" < "you"
true

julia> "You" < "Of"
false

```

In Julia, comparing strings will treat them as bytearrays and compare each byte in the string. If all the bytes are the same, the larger string is greater. Not only is this efficient (it calls into Libc’s `memcmp`), it is generally also what you want when comparing strings.  
Therefore, you can find the largest word using `maximum`:

```julia
julia> largest_word(s::Union{String, SubString{String}}) = maximum(eachsplit(s); init="");

julia> largest_word(""">>>Goodness ^ Of ^ Grace: Come :;You broken! > Heavy laden.. ..
       <<<+Come! You helpless!?.() come You in despair:-(..""")
"laden.."

```

If you only want to compare words, you can use `strip` to remove non-letters:

```julia
function largest_word(s::Union{String, SubString{String}})
      words = Iterators.map(w -> strip(!isletter, w), eachsplit(s))
      maximum(words ;init="")
 end

```

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 17, 2024, 11:01am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/12 "2024-12-17T11:01:18Z")

</div>

Hello,  
Thank you, but I’m a beginner and I don’t want to use the `maximum` function. Which part of my code is wrong? I expect this code to print `in`!

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [December 17, 2024, 11:08am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/13 "2024-12-17T11:08:14Z")

</div>

The issue with your code is that you don’t reset `temp1` on every word. That is:

1. You read the word `Goodness`: `temp2` is now `Goodness`
2. You read the word `Of`. This is larger than `Goodness`, so `temp2` is now `Of`
3. You read the word `Grace`. This is smaller than `Of`, so `temp2` stays `Of`
4. You read the word `Come`. However, you did not clear `temp1` after reading `Grace`. So, `temp1` is now `GraceCome`. Since `temp1` is never cleared, but more letters is added to its end, it will keep being smaller than `Of`.

So, you need to make `temp1 = ""` unconditional on wheter `temp1 < temp2`:

```julia
        if isletter(ch)
            temp1 *= ch
        elseif isspace(ch)
            if temp1 < temp2
                temp2 = temp1
            end
            temp1 = ""
        end    

```

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 17, 2024, 11:20am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/14 "2024-12-17T11:20:45Z")

</div>

Hi,  
Not really, I cleared `temp1` variable:

```julia
while !eof(inn)
        ch = read(inn, Char)
        if isletter(ch)
            temp1 *= ch
        elseif isspace(ch) && (cmp(temp1,temp2) == 1)
            temp2 = temp1
            temp1 = ""
        end        
    end

```

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [December 17, 2024, 11:35am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/15 "2024-12-17T11:35:44Z")

</div>

Please read carefully.  
You only clear `temp1` if it becomes greater than `temp2`.  
You need to clear it after any word regardless of it being the current greatest or not.

There is also a question how you treat a sequence like `aaa!!bb` which is not a continuous letter sequence but also not a space-separated sequence.

And, whenever you open a file, you must close it. It’s best to write:

```julia
open("input.txt", "r") do inn
   # do your stuff
end

```

which is a shorthand for

```julia
inn = open("input.txt", "r")
try
    # do your stuff
finally
    close(inn)
end

```

The `try ... finally` construct is crucial because `finally` block will run even if there is an error in the `try` block. Without that, the file will certainly be closed only if there were no errors thrown from the function.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 18, 2024, 5:38am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/16 "2024-12-18T05:38:41Z")

</div>

Hi,  
Thank you so much for your reply.  
I guess the code is correct. I removed the comparison operation from the code:

```julia
function extractor()
    inn = open("input.txt","r")
    temp1 = ""
    while !eof(inn)
        ch = read(inn, Char)
        if isletter(ch)
            temp1 *= ch
        elseif isspace(ch)
            println(temp1)
            temp1 = ""
        end
    end
end

extractor()

```

The output is:

```julia
Goodness

Of

Grace
Come
You
broken

Heavy
laden
Come
You
helpless
come
You
in
despair

```

So I have emptied the `temp1` variable in the right place.  
What is your opinion?

Thank you.

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 18, 2024, 6:19am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/17 "2024-12-18T06:19:08Z")

</div>

Hi,  
Thank you @jakobnissen. You were right. I should have used two `if` statements and cleared the `temp1` variable somewhere else. The correct code is as follows:

```julia
function extractor()
    inn = open("input.txt","r")
    temp1 = ""
    temp2 = ""
    n = 0
    while !eof(inn)
        ch = read(inn, Char)
        if isletter(ch)
            temp1 *= ch
        elseif isspace(ch)
            if (cmp(temp1,temp2) == 1)
            temp2 = temp1
            end
            temp1 = "" 
        end         
    end
    println(temp2)
end

extractor()

```

---

<div class="post-metadata">

**Author:** ![NonDairyNeutrino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nondairyneutrino/32/221496_2.png) [@NonDairyNeutrino](https://discourse.julialang.org/u/NonDairyNeutrino)\
**Post date:** [December 18, 2024, 7:06am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/18 "2024-12-18T07:06:03Z")

</div>

If I understand the problem correctly, I believe this would serve as a “Julionic” way to do it

```julia
julia> input = """">>>Goodness ^ Of ^ Grace: Come :;You broken! > Heavy laden.. ..\n<<<+Come! You helpless!?.() come You in despair:-(..""";

julia> getproperty.(eachmatch(r"\w+", input), :match) |> findmax
("laden", 8)

```

---

<div class="post-metadata">

**Author:** ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)\
**Post date:** [December 18, 2024, 9:46am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/19 "2024-12-18T09:46:53Z")

</div>

Hi,  
Thank you so much for your solution, but I’m a newbie.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 18, 2024, 9:53am UTC](https://discourse.julialang.org/t/finding-the-greater-word/123909/20 "2024-12-18T09:53:03Z")

</div>

> [@hack3rcon](#):
>
> `(cmp(temp1,temp2) == 1)`

It much nicer and easier to understand if you write `temp1 > temp2` instead, as suggested before.
