Written by

Senior Cloud Architect at InterSystems
MOD
Discussion Eduard Lebedyuk · Mar 2, 2023

Code Golf: Isogram

It's time for a Code Golf round!

Isogram 

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

1

Note

Rules

  1. The signature of the contest entry MUST be:
Class codeGolf.Isogram
{ 

ClassMethod Check(word As %String) As %Boolean
{
  // Your solution here
}

}
  1. 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).
  1. 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)
}
  1. The use of $ZWPACK and $ZWBPACK is also discouraged.
  2. 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"))
    }
    
    }

Comments

Eduard Lebedyuk · Mar 3, 2023
 

62

	s a=$zcvt(a,"U") for i=1:1:90{return:$l(a,$c(i))>2 0} q 1
0
Alexey Maslov · Mar 3, 2023
 

58

s r=1 f i=1:1:$l(a){s r=$i(r($a(a,i)#32))<2 q:'r} q r
0
Vitaliy Serdtsev  Mar 6, 2023 to Alexey Maslov

For an empty string (a=""), the <UNDEFINED> error occurs because the variable r is not defined.

0
Alexey Maslov  Mar 6, 2023 to Vitaliy Serdtsev

Thank you, Vitaliy. Corrected.

0
Vitaliy Serdtsev · Mar 6, 2023
 

52

ClassMethod Check(As %StringAs %Boolean
{
w=$zu(28,w,5),l=$l(w,$e(w,$i(i))) g:l=2 'l
}
 

48

ClassMethod Check(As %StringAs %Boolean
{
 i=1:1:90{ret:$l($zu(28,w,5),$c(i))>2 01
}
0
Alexey Maslov · Mar 6, 2023
 

47 (pure COS w/o undocumented features :)

a q:$i(r($a(w,$i(i))#32))=2 0 q:i>$l(w) 1 g a
0
Vitaliy Serdtsev  Mar 6, 2023 to Alexey Maslov
 

48 (the same example, but with documented feature)

ClassMethod Check(As %StringAs %Boolean
{
 i=1:1:90{ret:$l($$$UPPER(w),$c(i))>2 01
}
0
Vitaliy Serdtsev  Mar 6, 2023 to Alexey Maslov

By the way, your example takes 47, not 48 characters

0
Timo Lindenschmid  Jul 4, 2023 to Vitaliy Serdtsev

changing functional spec is not allowed ;)

0
Timo Lindenschmid · Jul 4, 2023

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.*")

}
0
Dmitry Maslennikov · Jul 4, 2023

34

ClassMethod Check(w As %String) As %Boolean [ Language = python ]
{
return len(w)==len(set(w.lower()))
}
0