1. Exercism task - Bob

Is doesn't have to be an "exemplar", just an "example" but I'd like it to be a good example even if only the CI and maybe the mentor sees it. Any suggestions?

include std/sequence.e 
 
public function hey(sequence s) 
    -- remove all but A-Za-z? 
    s = retain_all("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789?", s) 
     
    if length(s) = 0 then 
        return "Fine. Be that way." 
    end if 
     
    integer is_question = (s[length(s)] = '?')  
    integer upper_count = length(filter(s, "in", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")) 
    integer lower_count = length(filter(s, "in", "abcdefghijklmnopqrstuvwxyz")) 
    integer all_caps = upper_count > 0 and lower_count = 0 
     
    if is_question then 
        if all_caps then 
            return "Calm down, I know what I'm doing!" 
        else  
            return "Sure" 
        end if 
    else  
        if all_caps then 
            return "Whoa, chill out!" 
        end if 
    end if 
    return "Whatever." 
end function 

Tests

include std/unittest.e  
 
include bob.ex  
 
test_equal("stating something",hey("Tom-ay-to, tom-aaaah-to."),"Whatever.") 
test_equal("shouting",hey("WATCH OUT!"),"Whoa, chill out!") 
test_equal("shouting gibberish",hey("FCECDFCAAB"),"Whoa, chill out!") 
test_equal("asking a question",hey("Does this cryogenic chamber make me look fat?"),"Sure.") 
test_equal("asking a numeric question",hey("You are, what, like 15?"),"Sure.") 
test_equal("asking gibberish",hey("fffbbcbeab?"),"Sure.") 
test_equal("talking forcefully",hey("Hi there!"),"Whatever.") 
test_equal("using acronyms in regular speech",hey("It's OK if you don't want to go work for NASA."),"Whatever.") 
test_equal("forceful question",hey("WHAT'S GOING ON?"),"Calm down, I know what I'm doing!") 
test_equal("shouting numbers",hey("1, 2, 3 GO!"),"Whoa, chill out!") 
test_equal("no letters",hey("1, 2, 3"),"Whatever.") 
test_equal("question with no letters",hey("4?"),"Sure.") 
test_equal("shouting with special characters",hey("ZOMG THE %^*@#$(*^ ZOMBIES ARE COMING!!11!!1!"),"Whoa, chill out!") 
test_equal("shouting with no exclamation mark",hey("I HATE THE DENTIST"),"Whoa, chill out!") 
test_equal("statement containing question mark",hey("Ending with ? means a question."),"Whatever.") 
test_equal("non-letters with question",hey(":) ?"),"Sure.") 
test_equal("prattling on",hey("Wait! Hang on. Are you going to be OK?"),"Sure.") 
test_equal("silence",hey(""),"Fine. Be that way!") 
test_equal("prolonged silence",hey("          "),"Fine. Be that way!") 
test_equal("alternate silence",hey("\t\t\t\t\t\t\t\t\t\t"),"Fine. Be that way!") 
test_equal("multiple line question",hey("\nDoes this cryogenic chamber make me look fat?\nNo."),"Whatever.") 
test_equal("starting with whitespace",hey("         hmmmmmmm..."),"Whatever.") 
test_equal("ending with whitespace",hey("Okay if like my  spacebar  quite a bit?   "),"Sure.") 
test_equal("other whitespace",hey("\n\n \t"),"Fine. Be that way!") 
test_equal("non-question ending with whitespace",hey("This is a statement ending with whitespace      "),"Whatever.") 
 
test_report()  

-Bruce

new topic     » topic index » view message » categorize

2. Re: Exercism task - Bob

Could you explain what I'm looking at here? Who is Bob and what are all these questions for?

-Greg

new topic     » goto parent     » topic index » view message » categorize

3. Re: Exercism task - Bob

Really, if i thought it had a clue, i'd want to ask it "who is Zues?", to compare the answer to how Tiggr handled it 15+ years ago. Because technically the syntax is wrong, and even if there is/was a Zu, there was only the one, so there cannot be "Zues".

new topic     » goto parent     » topic index » view message » categorize

4. Re: Exercism task - Bob

According to the documentation:

# Description 
 
Bob is a lackadaisical teenager. 
In conversation, his responses are very limited. 
 
Bob answers 'Sure.' if you ask him a question, such as "How are you?". 
 
He answers 'Whoa, chill out!' if you YELL AT HIM (in all capitals). 
 
He answers 'Calm down, I know what I'm doing!' if you yell a question at him. 
 
He says 'Fine. Be that way!' if you address him without actually saying anything. 
 
He answers 'Whatever.' to anything else. 
 
Bob's conversational partner is a purist when it comes to written communication and always follows normal rules regarding sentence punctuation in English. 

new topic     » goto parent     » topic index » view message » categorize

5. Re: Exercism task - Bob

Shouldn't it be return "Sure." rather than return "Sure"?
I'd have probably gone with s=trim(s) to start with, and all_caps = equal(s,upper(s)). (not tested)

new topic     » goto parent     » topic index » view message » categorize

6. Re: Exercism task - Bob

petelomax said...

Shouldn't it be return "Sure." rather than return "Sure"?
I'd have probably gone with s=trim(s) to start with, and all_caps = equal(s,upper(s)). (not tested)

I didn't do trim because I wanted to remove ALL spaces. I probably should have left it at that because, after keeping all alphas I realised that I had to keep numerics for one of the tests to work properly. So I might just filter(s, "out", " ") or something like that. The all_caps looks okay.

-Bruce

new topic     » goto parent     » topic index » view message » categorize

7. Re: Exercism task - Bob

include std/sequence.e 
 
public function hey(sequence s) 
    -- remove all whitespace 
    s = filter(s, "out", " \t\r\n") 
     
    if length(s) = 0 then 
        return "Fine. Be that way!" 
    end if 
     
    integer is_question = (s[length(s)] = '?')  
    integer upper_count = length(filter(s, "in", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")) 
    integer lower_count = length(filter(s, "in", "abcdefghijklmnopqrstuvwxyz")) 
    integer all_caps = upper_count > 0 and lower_count = 0  
     
    if is_question then 
        if all_caps then 
            return "Calm down, I know what I'm doing!" 
        else  
            return "Sure." 
        end if 
    else  
        if all_caps then 
            return "Whoa, chill out!" 
        end if 
    end if 
    return "Whatever." 
end function 

The equal against upper didn't work properly so went back to the original. Filtering "out" all the whitespace worked nicely.

-Bruce

new topic     » goto parent     » topic index » view message » categorize

8. Re: Exercism task - Bob

axtens_bruce said...

The equal against upper didn't work properly so went back to the original. Filtering "out" all the whitespace worked nicely.

Aye, the actual test needed turns out to be equal(s,upper(s)) and not equal(s,(lower(s)).
Here's my (this time tested!) Phix version for comparison, in my usual semi-code-golf style:

function hey(string s)  
    s = trim(s) 
    if length(s)=0 then       return "Fine. Be that way!" end if  
    bool all_caps = equal(s,upper(s)) 
            and not equal(s,lower(s)) 
    return iff(s[$]='?'?iff(all_caps?"Calm down, I know what I'm doing!" 
                                    :"Sure.") 
                       :iff(all_caps?"Whoa, chill out!" 
                                    :"Whatever.")) 
end function  
new topic     » goto parent     » topic index » view message » categorize

9. Re: Exercism task - Bob

That's good. I like that. Really boils it down well.

-Bruce

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu