Written by

Senior Cloud Architect at InterSystems
MOD
Discussion Eduard Lebedyuk · Mar 4, 2022

Code Golf: Label Validation

New month, new code golf!

You will receive a string with a label code with numbers and letters. Our challenge is to check if this code begins with 1, 2, or 3 and ends with A, B, C, S, or R. It should return true(1) if so or return false(0) otherwise. As usual shortest solution wins.

##Input "198739A79D9R"

##Output 1

##Note

Rules

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

ClassMethod Validate(s As %String) As %Boolean
{
}

}
  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 Validate(s) As %Boolean 
{
  q ##class(myPackage.myClass).test(s)
}
  1. The use of $ZWPACK and $ZWBPACK is also discouraged.

My best attempt so far is 30 characters. What's yours?

Comments

Vitaliy Serdtsev · Mar 7, 2022

size = 30

<FONT COLOR="#000080">ClassMethod </FONT><FONT COLOR="#000000">Validate(</FONT><FONT COLOR="#ff00ff">s </FONT><FONT COLOR="#000080">As %String</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#000080">As %Boolean
</FONT><FONT COLOR="#000000">{
 </FONT><FONT COLOR="#0000ff">q </FONT><FONT COLOR="#008000">"ABCRS"</FONT><FONT COLOR="#000000">[</FONT><FONT COLOR="#0000ff">$e</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">s</FONT><FONT COLOR="#000000">,*)*123[</FONT><FONT COLOR="#0000ff">$e</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">s</FONT><FONT COLOR="#000000">)
}</FONT>
0
Eduard Lebedyuk  Mar 7, 2022 to Vitaliy Serdtsev

That was about my idea too.

Clever trick getting rid of the parenthesis!

0
Eduard Lebedyuk  Mar 7, 2022 to Vitaliy Serdtsev

Ah, no. Won't work without parenthesis.

For example "0Z" would return true instead of false.

0
Vitaliy Serdtsev  Mar 7, 2022 to Eduard Lebedyuk

size = 31

<FONT COLOR="#000080">ClassMethod </FONT><FONT COLOR="#000000">Validate(</FONT><FONT COLOR="#ff00ff">s </FONT><FONT COLOR="#000080">As %String</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#000080">As %Boolean
</FONT><FONT COLOR="#000000">{
 </FONT><FONT COLOR="#0000ff">q $MATCH</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">s</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#008000">"[1-3].*[A-CRS]"</FONT><FONT COLOR="#000000">)
}</FONT>
0