Written by

Senior Cloud Architect at InterSystems
Discussion Eduard Lebedyuk · Mar 11, 2024

Code Golf: Length Order

Let's have another round of code golf, with a different signature today!

Write a classmethod that will receive a variable number of parameters  containing comma-separated strings and/or positive numbers, and returns one of four possible string values.

  • Easy mode: each argument is guaranteed to be one sting or number without commas or white spaces.

Depending on the ordering of the lengths of the elements in the input, your method should return:

  • “Increasing” - if the lengths of the elements increase from left to right (although some neighboring elements may also be equal in length)
  • “Decreasing” - if the lengths of the elements decrease from left to right (although some neighboring elements may also be equal in length)
  • “Unsorted” - if the lengths of the elements fluctuate from left to right
  • “Constant” - if all element's lengths are the same.

Numbers and Strings should be evaluated based on the number of characters or digits used to write them.

The space character (" ") doesn't count as length. Wide characters are not allowed as an input.

The default answer (if there are 0 or 1 argument) must be "Constant".

Input:

"a, 34, 555, AAAAA"

Identical to:

"a", 34, 555, "AAAAA"

Output:

"Increasing"

Note

Rules

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

ClassMethod Type(args...) As %String
{
  // 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.LenghtOrderType Extends%UnitTest.TestCase
    {
    
    
    Method TestLengthOrderType()
    {
        Do$$$AssertEquals(##class(codeGolf.LengthOrder).Type("1, b, p, 2"), "Constant")
        Do$$$AssertEquals(##class(codeGolf.LengthOrder).Type("123, 1234, 12345, 123456"), "Increasing")
        Do$$$AssertEquals(##class(codeGolf.LengthOrder).Type("a, abc, abcde, ab"), "Unsorted")
        Do$$$AssertEquals(##class(codeGolf.LengthOrder).Type("buffalo"), "Constant")
        Do$$$AssertEquals(##class(codeGolf.LengthOrder).Type("1, 2, 3, 4", "5,6,7", "8, 9"), "Constant")
        Do$$$AssertEquals(##class(codeGolf.LengthOrder).Type("ab,cdef, g","hi,jk,lmnopq","rst,uv,wx","yz"), "Unsorted")
        Do$$$AssertEquals(##class(codeGolf.LengthOrder).Type("1, 2, 3, 4, 16"), "Increasing")
        Do$$$AssertEquals(##class(codeGolf.LengthOrder).Type("pyp, pyp, buffalo, pyp"), "Unsorted")
        Do$$$AssertEquals(##class(codeGolf.LengthOrder).Type(), "Constant")
        Do$$$AssertEquals(##class(codeGolf.LengthOrder).Type(",    , ,     "), "Constant")
        Do$$$AssertEquals(##class(codeGolf.LengthOrder).Type("abcde, abc, ab", "12,43,1"), "Decreasing")
    }
    
    }

     

Comments

Stuart Strickland · Mar 12, 2024

ClassMethod Type(args...) As %String
{
 // no attempt made at miniturizing the code, 362 chars not including comments
 res="Constant"
 i=1:1 {
  q:'$d(args(i)) w=$tr(args(i)," ")
  p=2:1:$l(w,",") {
   a=$l($p(w,",",p)),b=$l($p(w,",",p-1))
   s d=$s(a>b:1,a<b:-1,1:0)
   d>0 res=$case(res,"Constant":"Increasing","Increasing":"Increasing",:"Unsorted")
   d<0 res=$case(res,"Constant":"Decreasing","Decreasing":"Decreasing",:"Unsorted")
   }
  }
 res
}
 

0
Stuart Strickland  Mar 12, 2024 to Vitaliy Serdtsev

I'm afraid not. I didn't read the question properly but thankfully we have you who does a better job than test cases!

Corrected it now.

0
Timo Lindenschmid · Mar 13, 2024

size 258 249

all unit tests passed

ClassMethod Type(a...) As%String
{
 s (f,r)=0,c=2 i $g(a){f i=1:1:a{s$p(b,",",*+1)=$zstrip(a(i),"*"," ")} f{q:(c=$l(b,","))  s p=$l($p(b,",",$i(c)-1))-$l($p(b,",",c)),f=$s(p>0:-1,p<0:1,1:0) i f,r,r'=f{ret "Unsorted"} s:f r=f}} ret $s(r<0:"Decreasing",r>0:"Increasing",1:"Constant")
}
0
Vitaliy Serdtsev · Mar 13, 2024
 

size = 207 195 179

ClassMethod Type(a...) As %String
{
 j=$i(r):1:a{w=$tr(a(j)," "),p=$f(w,",")-2 i=2:1:$l(w,","c=$l($p(w,",",i)),r=$s(p=c:r,r+p-c<2:2,c<p*2#r:3,1:4),p=cq $p("Constant7Increasing7Decreasing7Unsorted",7,r)
}
0
Stuart Strickland · Mar 13, 2024

size=193

ClassMethod Type(a...) As %String
{
r=3 i=1:1:a{w=$tr(a(i)," "),b=$l($p(w,",")) p=2:1:$l(w,",") c=b,b=$l($p(w,",",p)),d=$s(c<b:1,c>b:2,1:3),r=$s(r=3:d,3_r[d:r,1:4)$p("Increasing1Decreasing1Constant1Unsorted",1,r)
}

0
Stuart Strickland  Mar 13, 2024 to Vitaliy Serdtsev

Yes this time, but unfortunately running tests agains ClassMethod Test1() and then posting ClassMethod Test() on here can make me look silly.

0
Vitaliy Serdtsev  Mar 14, 2024 to Stuart Strickland

Ah, got it.

I improved my score to 179 (see above).

0
Stuart Strickland  Mar 14, 2024 to Vitaliy Serdtsev

The for loop initialising r to 1 is top class thinking. You can still take 2 more characters off:

ClassMethod Type(a...) As %String
{
 j=$i(r):1:a{w=$tr(a(j)," "),p=$f(w,",")-2 i=2:1:$l(w,",") c=$l($p(w,",",i)),r=$s(p=c:r,p<c&(r<3):2,r#2:3,1:4),p=c$p("Constant7Increasing7Decreasing7Unsorted",7,r)
}
 

0
Robert Barbiaux  Mar 14, 2024 to Stuart Strickland

Indeed f j=$i(r)... would get a !! mark if it was a chess move 😉
Congratulations !

0
Eduard Lebedyuk  Mar 14, 2024 to Robert Barbiaux

Yes, I don't think we saw that on our Code Golfs before.

0
Stuart Strickland  Mar 14, 2024 to Stuart Strickland

Shaved down to 174. A dubious $Find-2 to get first string length. Changed a condition in the $Select from p<c&(r<3) to r<3*c>p

ClassMethod Type(a...) As %String
{
 j=$i(r):1:a{w=$tr(a(j)," "),p=$f(w,",")-2 i=2:1:$l(w,",") c=$l($p(w,",",i)),r=$s(p=c:r,r<3*c>p:2,r#2:3,1:4),p=c$p("Constant7Increasing7Decreasing7Unsorted",7,r)
}
also fits in @Robert Barbiaux attempt

ClassMethod Type(a...) As %String
{
 i=$i(r):1:$g(a){j=1:1:$l(a(i),","){l=$l($tr($p(a(i),",",j)," ")),c=$g(c,l),r=$s(l=c:r,r<3*l>c:2,r#2:3,1:4),c=lc$p("Constant1Increasing1Decreasing1Unsorted",1,r)
}
 

0
Vitaliy Serdtsev  Mar 15, 2024 to Stuart Strickland

There is no limit to perfection.

By the way, your code shows 175 characters, where did the number 174 come from?

0
Stuart Strickland  Mar 15, 2024 to Vitaliy Serdtsev

174 probably from an inaccurate count of the difference between some versions. You still have the r#2:3 problem

W ##Class(CodeGolf.LengthOrder).Type("abc,de","de,abc") should be Unsorted

0
Vitaliy Serdtsev  Mar 15, 2024 to Stuart Strickland

Thanks for the hint, I fixed it. By the way, your code can be reduced to 173:  <FONT COLOR="#0000ff">f </FONT><FONT COLOR="#800000">i</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#0000ff">$i</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">r</FONT><FONT COLOR="#000000">):1:</FONT><FONT COLOR="#800000">a</FONT>

0
Robert Barbiaux · Mar 13, 2024

Size 201 181 181, all unit tests passed (including undefined argument, and additional .Type("abc,de","de,abc") --> Unsorted)
Thanks Eduard, I missed the extraneous quotes (my mind is still not entirely purged of strongly typed languages habits 😅)

ClassMethod Type(a...) As%String
{
 f i=$i(r):1:$g(a){f j=1:1:$l(a(i),","){sl=$l($tr($p(a(i),",",j)," ")),c=$g(c,l),r=$s(l=c:r,r<3*l>c:2,r#2*c>l:3,1:4),c=l} k c} q$p("Constant1Increasing1Decreasing1Unsorted",1,r)
}
0
Eduard Lebedyuk  Mar 14, 2024 to Robert Barbiaux

At the last $piece a delimiter could be 1 instead of "1", no?

0
Stuart Strickland  Mar 15, 2024 to Eduard Lebedyuk

Any of these that use r#2:3 give the wrong answer for

W ##Class(CodeGolf.LengthOrder).Type("abc,de","de,abc")

0
Stuart Strickland  Mar 15, 2024 to Stuart Strickland

Size=177, problem fixed and another reduction found at the start of the $Select

ClassMethod Type(a...) As %String
{
 i=$i(r):1:$g(a){j=1:1:$L(a(i),","){L=$L($tr($p(a(i),",",j)," ")),r=$s(L=$g(c,L):r,r<3*L>c:2,r#2*c>L:3,1:4),c=Lc$p("Constant1Increasing1Decreasing1Unsorted",1,r)
}
 

0