Discussion
· Mar 11

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")
    }
    
    }
     
Discussion (20)1
Log in or sign up to continue

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
}
 

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)
}
 

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),","){s l=$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)
}