exercism / elixir

Exercism exercises in Elixir.
https://exercism.org/tracks/elixir
MIT License
624 stars 399 forks source link

Darts exercise hint for sqrt workaround doesn't seem to work #1430

Closed boabdilperez closed 10 months ago

boabdilperez commented 10 months ago

I was just trying to solve the "Darts" exercise. The hints include the correct formula to get the distance from origin to any given coordinate on a plane, but the provided workaround for the lack of a native square root function in elixir doesn't seem to work. The spec for Integer.pow() is expecting integers for both the base and the exponent arguments. The spec for Float.pow() is explicitly expecting a float for the base, and a number for the exponent.

For the first test case of (-9, 9)

iex(3)> -9 * -9 + 9 * 9
162
iex(4)> Integer.pow(162, 0.5)
** (FunctionClauseError) no function clause matching in Integer.pow/2

    The following arguments were given to Integer.pow/2:

        # 1
        162

        # 2
        0.5

    Attempted function clauses (showing 1 out of 1):

        def pow(base, exponent) when is_integer(base) and is_integer(exponent)

    (elixir 1.16.0) lib/integer.ex:105: Integer.pow/2
    iex:4: (file)
iex(4)> Float.pow(162, 0.5)
** (FunctionClauseError) no function clause matching in Float.pow/2

    The following arguments were given to Float.pow/2:

        # 1
        162

        # 2
        0.5

    Attempted function clauses (showing 1 out of 1):

        def pow(base, exponent) when is_float(base) and is_number(exponent)

    (elixir 1.16.0) lib/float.ex:116: Float.pow/2
    iex:4: (file)
iex(4)>

Checking the example.ex in the repo, I see that neither Integer.pow or Float.pow were used there, but rather the erlang math library. I'm apologize if I am missing something else, as I'm not great at geometry OR elixir.

jiegillet commented 10 months ago

Hi @boabdilperez. Those hints are common to all tracks, they are not Elixir specific, so they might not help as much with the lack of a proper sqrt function in Elixir, which I agree is a little frustrating.

As you noted yourself, Integer.pow() can't be used. Float.pow() can be used, but you have to trick integers into becoming floats, like this Float.pow( 25 * 1.0, 0.5). It is a bit strange that there is no Integer.to_float() or something. Finally, :math.sqrt works as well, Elixir is proudly built on top of Erlang, so Erlang functions are fair game.

angelikatyborska commented 10 months ago

Those hints are common to all tracks, they are not Elixir specific

@jiegillet I can't find the hints in problem-specs, where did you take them from? Are they really global?

jiegillet commented 10 months ago

Oh no, you're right, I got mislead by something, they are not global -_- In that case, let's modify the file to mention what I said in the previous comment.

boabdilperez commented 10 months ago

I was ultimately able to figure it out, but a bit less organically than I think is the intent of these exercises. Modifying those hints will be a big help to anyone else who comes along, I think. Thanks for looking into this y'all!

jiegillet commented 10 months ago

By the way, did you see the hints in the online editor or in offline mode? I can't find them at all.

boabdilperez commented 10 months ago

I used the CLI but I see the hints in both. From the online editor there's a small button in the lower left "Stuck? Get Help" and if you download the test it's in HINTS.md in the top level of the directory created by the CLI tool.

-- Boabdil Perez

On Thu, Feb 1, 2024 at 10:35 PM Jie @.***> wrote:

By the way, did you see the hints in the online editor or in offline mode? I can't find them at all.

— Reply to this email directly, view it on GitHub https://github.com/exercism/elixir/issues/1430#issuecomment-1922800176, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI6Q6JC72NEKHC2ZIDX4VZLYRRUJTAVCNFSM6AAAAABCUST6LOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMRSHAYDAMJXGY . You are receiving this because you were mentioned.Message ID: @.***>

jiegillet commented 10 months ago

I see, for me it's "Stuck? Ask ChatGPT". I tried it, asked me which model I wanted, then I could see the hints. When I reloaded I saw ChatGPT 3.5's suggestions (which were not helpful, but my code already had the square root part done).

I opened a PR, may I ask you to give it a review?