Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add practice exercise wordy #756

Merged
merged 1 commit into from
Jul 18, 2024
Merged

add practice exercise wordy #756

merged 1 commit into from
Jul 18, 2024

Conversation

depial
Copy link
Contributor

@depial depial commented Jul 17, 2024

Adding practice exercise wordy according to problem-specifications. A couple things to note:

  • Difficulty set at 3, but 4 could be possible due to needing regex or otherwise having slightly complicated error handling.

That is all for the note for the implementation, but there is something I've found about being able to "hack" the error handling questions.

Below is an algorithm which doesn't use regex. Please note the line which is commented out:

ENG2MATH = zip(("plus","minus","divided by","multiplied by","What is ","?"), ("+","-","/","*","",""))
ops = Dict(zip(("+","-","/","*"), (+,-,/,*)))

function wordy(problem)
    foreach(op -> problem = replace(problem, first(op) => last(op)), ENG2MATH)
    terms = split(problem)
    #iseven(length(terms)) && throw(ArgumentError(problem)) 
    total, operands = parse(Int, first(terms)), [+, 0]
    for (i, term) in enumerate(terms[2:end])
        try 
            operands[mod1(i, 2)] = isodd(i) ? ops[term] : parse(Int, term)
        catch error
            throw(ArgumentError(problem)) 
        end 
        iseven(i) && (total = first(operands)(total, last(operands)))
    end
    iseven(length(terms)) ? throw(ArgumentError(problem)) : total
end

When that line is commented in, and other error handling isn't implemented (i.e. try/catch block and final trinary), this will pass 7 out of 8 of the error handling tests, while it is only valid for two of them. The others that pass are "happy coincidences". For example, it passes with input "Who is the President of the United States?" but wouldn't pass "Who is the Prime Minister of India", simply because of word count (since a valid math expression has to have an odd number of operands). I'm not sure if this loophole should be changed or not

@depial depial mentioned this pull request Jul 17, 2024
10 tasks
@depial
Copy link
Contributor Author

depial commented Jul 17, 2024

Some potential added tests to address the loophole mentioned above:

    @testset "unknown operation" begin
        @test_throws ArgumentError wordy("What is 52 squared plus square root of 5?")
    end
    
    @testset "Non math question" begin
        @test_throws ArgumentError wordy("Who is the Prime Minister of India?")
    end
    
    @testset "reject two operations in a row" begin
        @test_throws ArgumentError wordy("What is 1 plus plus 2 minus minus 3?")
    end
    
    @testset "reject two numbers in a row" begin
        @test_throws ArgumentError wordy("What is 1 plus 2 1 plus 3 4?")
    end

@ErikSchierboom ErikSchierboom merged commit bd63d1f into exercism:main Jul 18, 2024
11 checks passed
@depial depial deleted the wordy branch July 18, 2024 06:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants