Discussion
· 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.
Discussion (13)1
Log in or sign up to continue

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 missing
ClassMethod 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

Putting all in one line saves one byte

ClassMethod Order(s As %String) As %String
{
	s d=" ",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(s As %String) As %String
{
	s d=" ",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

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

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."

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.

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)}
1	s p=$p(s," ",$i(i)),w=$tr(p,1/17),$p(z," ",$tr(p,w))=w q:p=w $g(z) g 1