1. Exercism task, isogram

As with Perl, TMTOWTDI in Euphoria, but is this a good way? Granted, the example in the .meta subdirectory of a given task isn't necessarily supposed to be the be-all and end-all. Mostly it's there for the Github Actions CI workflow.

A-a-a-a-anyway, how's my Euphoria?

t_isogram.e

include std/unittest.e  
 
--include isogram.ex  
include .meta/example.ex 
 
test_true("empty string" , isogram("")  ) 
test_true("isogram with only lower case characters" , isogram("isogram") )  
test_false("word with one duplicated character" , isogram("eleven")  ) 
test_false("word with one duplicated character from the end of the alphabet" , isogram("zzyzx") )  
test_true("longest reported english isogram" , isogram("subdermatoglyphic")  ) 
test_false("word with duplicated character in mixed case" , isogram("Alphabet") )  
test_false("word with duplicated character in mixed case, lowercase first" , isogram("alphAbet"))   
test_true("hypothetical isogrammic word with hyphen" , isogram("thumbscrew-japingly")  ) 
test_false("hypothetical word with duplicated character following hyphen" , isogram("thumbscrew-jappingly"))   
test_true("isogram with duplicated hyphen" , isogram("six-year-old")  ) 
test_true("made-up name that is an isogram" , isogram("Emily Jung Schwartzkopf"))   
test_false("duplicated character in the middle" , isogram("accentor")  ) 
test_false("same first and last characters" , isogram("angola")  ) 
test_false("word with duplicated character and with two hyphens" , isogram("up-to-date"))   
  
test_report()  

and .meta/example.ex

include std/text.e 
include std/search.e 
include std/sequence.e  
 
public function isogram( sequence str ) 
    str = upper(str) 
    sequence toRemove = {} 
    for i = 1 to length(str) do 
        if not is_in_range(str[i], {'A', 'Z'}) then 
            toRemove = append(toRemove, str[i]) 
        end if 
    end for 
    for i = 1 to length(toRemove) do 
        str = remove_all(toRemove[i], str) 
    end for 
    for i = 1 to length(str) do 
        for j = i + 1 to length(str) do 
            if str[i] = str[j] then 
                return 0 
            end if 
        end for 
    end for 
    return 1 
end function 
new topic     » topic index » view message » categorize

2. Re: Exercism task, isogram

Here's a shorter version. Hope this helps.

include std/sequence.e 
include std/sort.e 
include std/text.e 
 
public function isogram( sequence str ) 
 
    str = filter( str, STDFLTR_ALPHA ) 
    str = sort( upper(str) ) 
 
    for i = 2 to length( str ) do 
        if str[i] = str[i-1] then 
            return 0 
        end if 
    end for 
 
    return 1 
end function 

-Greg

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

3. Re: Exercism task, isogram

ghaberek said...

Here's a shorter version. Hope this helps.

It does very much. I was looking for something like

    str = filter( str, STDFLTR_ALPHA ) 

but didn't know where to look. I got an "of course! why didn't I think of that" out of the loop optimisation.

-Bruce

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

4. Re: Exercism task, isogram

Phix version:

global function isogram(string s) 
    s = trim(sort(lower(s))," -") 
    return s=unique(s) 
end function  
new topic     » goto parent     » topic index » view message » categorize

5. Re: Exercism task, isogram

Impressive! Thanks.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu