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"
ObjectScriptObjectScript
Identical to:
"a", 34, 555, "AAAAA"
ObjectScriptObjectScript
Output:
"Increasing"
ObjectScriptObjectScript
Note
Rules
- The signature of the contest entry MUST be:
Class codeGolf.LengthOrder
{
ClassMethod Type(args...) As %String
{
// 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.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") } }
ObjectScriptObjectScript
{
// no attempt made at miniturizing the code, 362 chars not including comments
s res="Constant"
f i=1:1 {
q:'$d(args(i)) s w=$tr(args(i)," ")
f p=2:1:$l(w,",") {
s a=$l($p(w,",",p)),b=$l($p(w,",",p-1))
s d=$s(a>b:1,a<b:-1,1:0)
i d>0 s res=$case(res,"Constant":"Increasing","Increasing":"Increasing",:"Unsorted")
i d<0 s res=$case(res,"Constant":"Decreasing","Decreasing":"Decreasing",:"Unsorted")
}
}
q res
}
Have you used a test case?
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.
size
258249all 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") }
207 195179size=193
{
}
Have you used a test case?
Yes this time, but unfortunately running tests agains ClassMethod Test1() and then posting ClassMethod Test() on here can make me look silly.
Ah, got it.
I improved my score to 179 (see above).
The for loop initialising r to 1 is top class thinking. You can still take 2 more characters off:
{
f j=$i(r):1:a{s w=$tr(a(j)," "),p=$f(w,",")-2 f i=2:1:$l(w,",") s c=$l($p(w,",",i)),r=$s(p=c:r,p<c&(r<3):2,r#2:3,1:4),p=c} q $p("Constant7Increasing7Decreasing7Unsorted",7,r)
}
Indeed f j=$i(r)... would get a !! mark if it was a chess move 😉
Congratulations !
Yes, I don't think we saw that on our Code Golfs before.
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
{
f j=$i(r):1:a{s w=$tr(a(j)," "),p=$f(w,",")-2 f i=2:1:$l(w,",") s c=$l($p(w,",",i)),r=$s(p=c:r,r<3*c>p:2,r#2:3,1:4),p=c} q $p("Constant7Increasing7Decreasing7Unsorted",7,r)
}
also fits in @Robert Barbiaux attempt
{
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:3,1:4),c=l} k c} q $p("Constant1Increasing1Decreasing1Unsorted",1,r)
}
There is no limit to perfection.
By the way, your code shows 175 characters, where did the number 174 come from?
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
Thanks for the hint, I fixed it.
By the way, your code can be reduced to 173: f i=$i(r):1:a
Size
201181181, 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) }
At the last $piece a delimiter could be 1 instead of "1", no?
Any of these that use r#2:3 give the wrong answer for
W ##Class(CodeGolf.LengthOrder).Type("abc,de","de,abc")
Size=177, problem fixed and another reduction found at the start of the $Select
{
f i=$i(r):1:$g(a){f j=1:1:$L(a(i),","){s 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=L} k c} q $p("Constant1Increasing1Decreasing1Unsorted",1,r)
}