Exercism task - Bob

new topic     » topic index » view thread      » older message » newer message

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 thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu