Written by

Senior Cloud Architect at InterSystems
Discussion Eduard Lebedyuk · Nov 9, 2022

Code Golf: Word Order

We're back with a code golf!

You will receive a string. Each word in the string will contain a number. This number is the position that word should have in the sentence. If the input string is empty, return an empty string. The output can only be in words, without the given numbers.

##Input "i2s T1his Te4st a3"

##Output This is a Test

##Note

Rules

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

ClassMethod Order(a As %String) As %String
{
}

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

Comments

Julius Kavay · Nov 9, 2022

One of the possible solutions

/// You can change the s:b]"" to an comma if there is always exact one space between the words/// and remove the ,1) from $lts() if all word are numbered from 1..N with no number missingClassMethod WordOrder(s)
{
    s z="" f i=1:1:$l(s," ") s b=$p(s," ",i) s:b]""$li(z,$zstrip(b,"*ap"))=$zstrip(b,"*n")
    q$lts(z," ",1)
}

This is a working solution and maybe not the shortest

0
Julius Kavay  Nov 9, 2022 to Julius Kavay

and this is an updated version

ClassMethod Order(sas%String) As%String
{
	sd=" ",z="" f i=1:1:$l(s,d) s b=$p(s,d,i),c=$zstrip(b,"*n"),$p(z,d,$tr(b,c))=c
	q z
}
0
Eduard Lebedyuk  Nov 10, 2022 to Julius Kavay

I thought about a local but I like your $li/$p approach.

0
Julius Kavay  Nov 10, 2022 to Eduard Lebedyuk

Putting all in one line saves one byte

ClassMethod Order(sAs%String) As%String
{
	sd=" ",z="" f i=1:1:$l(s,d){s b=$p(s,d,i),c=$zstrip(b,"*n"),$p(z,d,$tr(b,c))=c} q z
}

Changing $zstrip() to a $tr() saves one more byte

ClassMethod Order(sAs%String) As%String
{
	sd=" ",z="" f i=1:1:$l(s,d){s b=$p(s,d,i),c=$tr(b,1E20/17),$p(z,d,$tr(b,c))=c} q z
}

So I end up with 86 bytes

0
Vitaliy Serdtsev  Nov 11, 2022 to Julius Kavay
 

size = 79

ClassMethod Order(As %StringAs %String
{
 r=i=1:1:5e5{s=$p(a," ",i),w=$tr(s,1e20/17),$p(r," ",$tr(s,w))=wr
}
0
Stuart Strickland  Nov 11, 2022 to Vitaliy Serdtsev

Nice generation of a pandigital number with 1e20/17.

Can save a character with $zpi_0

ClassMethod Order(As %String) As %String
{
 r=i=1:1:5e5{s=$p(a," ",i),w=$tr(s,$zpi_0),$p(r," ",$tr(s,w))=wr
}

This was mine also at 78:

ClassMethod Order(As %String) As %String
{
 i=1:1 s=$p(a," ",i),n=$zstrip(s,"*a"),$p(z," ",n)=$tr(s,n) ret:'$g(z)
}
 

0
Stuart Strickland  Nov 11, 2022 to Stuart Strickland

Make that 77 with this:

ClassMethod Order(As %String) As %String
{
 f{s=$p(a," ",$i(i)),n=$zstrip(s,"*a"),$p(z," ",n)=$tr(s,n) ret:'$g(z)}
}
 

0
Stuart Strickland  Nov 11, 2022 to Stuart Strickland

Shaved off another character

ClassMethod Order(As %String) As %String
{
s=$p(a," ",$i(i)),n=$zstrip(s,"*a"),$p(z," ",n)=$tr(s,n) 1:$g(z)
}
 

0
Julius Kavay · Nov 11, 2022

A small complaint:
- possible word delimiters weren't specified (space, tab, etc.)
- no specification about punctuation marks (allowed or disallowed)
- no specification about empty words (allowed or disallowed) and how to handle them, if allowed

So my question is, are the following examples legal or not:

"O2K. I'1m" --> "I'm OK."
"spac4es are2    1There     ma3ny" --> "There are many spaces."

0
Stuart Strickland  Nov 11, 2022 to Julius Kavay

I had the same thoughts but decided to make assumptions that allowed me to code using the least characters. If you cater for the tricky stuff you'd probably have to double the length of your code.

0
Eduard Lebedyuk  Nov 12, 2022 to Julius Kavay

possible word delimiters weren't specified (space, tab, etc.)

Single whitespace

no specification about punctuation marks (allowed or disallowed)

No punctuation

no specification about empty words (allowed or disallowed) and how to handle them, if allowed

No empty words in input.

"O2K. I'1m" --> "I'm OK."
"spac4es are2    1There     ma3ny" --> "There are many spaces."

Not a valid input for this golf.

0
Julius Kavay  Nov 12, 2022 to Eduard Lebedyuk

You say "No punctuation". OK, but then we have a contradiction: Example 4 of the test cases contains several commas, a dot and a question mark...

0
Julius Kavay  Nov 12, 2022 to Eduard Lebedyuk

OK, no punctuation, no empty words, etc.... My lowest bid: 74 chars

ClassMethod Order(s)
{
	f  s p=$p(s," ",$i(i)),w=$tr(p,1/17),$p(z," ",$tr(p,w))=w ret:p=w$g(z)
}

By the way, the following three variants all have the same size of 74

	f  s p=$p(s," ",$i(i)),w=$tr(p,1/17),$p(z," ",$tr(p,w))=w ret:p=w$g(z)
	f{s p=$p(s," ",$i(i)),w=$tr(p,1/17),$p(z," ",$tr(p,w))=w ret:p=w$g(z)}
1s p=$p(s," ",$i(i)),w=$tr(p,1/17),$p(z," ",$tr(p,w))=wq:p=w$g(z) g 1
0