Code Golf: Isogram
It's time for a Code Golf round!
A word or phrase that has no repeating letters, consecutive or non-consecutive.
Implement a method that checks if the received string is an isogram or not.
Assume the empty string is an isogram.
Ignore the letter case.
Allowed inputs: A-Z, a-z.
As usual, the shortest solution wins!
Input
"pseudomythical"Output
1Note
Rules
- The signature of the contest entry MUST be:
Class codeGolf.Isogram
{
ClassMethod Check(word As %String) As %Boolean
{
// Your solution here
}
}
- It is forbidden to modify class/signature, including but not limited to:
- Adding inheritance
- Setting default argument values
- Adding class elements (Parameters, Methods, Includes, etc).
- It is forbidden to refer to non-system code from your entry. For example, this is not a valid entry:
ClassMethod Build(f As %Integer)
{
W ##class(myPackage.myClass).test(a)
}
- The use of $ZWPACK and $ZWBPACK is also discouraged.
- You can use this test case:
Class tests.codeGolf.unittests.TestIsogram Extends %UnitTest.TestCase { Method TestCheckIsIsogram() { Do $$$AssertTrue(##class(codeGolf.Isogram).Check("uncopyrightable")) Do $$$AssertTrue(##class(codeGolf.Isogram).Check("upholstering")) Do $$$AssertTrue(##class(codeGolf.Isogram).Check("shlockumentary")) Do $$$AssertTrue(##class(codeGolf.Isogram).Check("Isogram")) Do $$$AssertTrue(##class(codeGolf.Isogram).Check("rock")) Do $$$AssertTrue(##class(codeGolf.Isogram).Check("ambiDEXtrously")) Do $$$AssertTrue(##class(codeGolf.Isogram).Check("neurolymphatic")) Do $$$AssertTrue(##class(codeGolf.Isogram).Check(""), "an empty string is a valid isogram") Do $$$AssertNotTrue(##class(codeGolf.Isogram).Check("goose")) Do $$$AssertNotTrue(##class(codeGolf.Isogram).Check("goOse")) Do $$$AssertNotTrue(##class(codeGolf.Isogram).Check("Balboa")) Do $$$AssertNotTrue(##class(codeGolf.Isogram).Check("isIsogram")) Do $$$AssertNotTrue(##class(codeGolf.Isogram).Check("aba")) Do $$$AssertNotTrue(##class(codeGolf.Isogram).Check("Silmarillion")) Do $$$AssertNotTrue(##class(codeGolf.Isogram).Check("JohnRambo")) } }
Discussion (11)1
Comments
62
s a=$zcvt(a,"U") for i=1:1:90{return:$l(a,$c(i))>2 0} q 158
s r=1 f i=1:1:$l(a){s r=$i(r($a(a,i)#32))<2 q:'r} q rFor an empty string (a=""), the <UNDEFINED> error occurs because the variable r is not defined.
Thank you, Vitaliy. Corrected.
52
ClassMethod Check(w As %String) As %Boolean
{
a s w=$zu(28,w,5),l=$l(w,$e(w,$i(i))) g:l=2 a q 'l
}48
ClassMethod Check(w As %String) As %Boolean
{
f i=1:1:90{ret:$l($zu(28,w,5),$c(i))>2 0} q 1
}47 (pure COS w/o undocumented features :)
a q:$i(r($a(w,$i(i))#32))=2 0 q:i>$l(w) 1 g a48 (the same example, but with documented feature)
ClassMethod Check(w As %String) As %Boolean
{
f i=1:1:90{ret:$l($$$UPPER(w),$c(i))>2 0} q 1
}By the way, your example takes 47, not 48 characters
changing functional spec is not allowed ;)
40
ClassMethod Check(word As %String) As %Boolean {
ret '$match($ZCVT(word,"U"),"(.).*\1.*")
}or 39 using $$$UPPER
ClassMethod Check(word As %String) As %Boolean {
ret '$match($$$UPPER(word),"(.).*\1.*")
}34
ClassMethod Check(w As %String) As %Boolean [ Language = python ]
{
return len(w)==len(set(w.lower()))
}