Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Code golf

343 views
Skip to first unread message

frangonve

unread,
Aug 23, 2012, 11:22:50 PM8/23/12
to
Currently golfscript and q language win.

Here you can find the requirements, tersesness rules here: http://codegolf.stackexchange.com/questions/7036/print-custom-alphabet

Cheers

Francisco

Aaron W. Hsu

unread,
Aug 27, 2012, 2:15:26 PM8/27/12
to
frangonve <fran...@gmail.com> writes:

>Currently golfscript and q language win.

>Here you can find the requirements, tersesness rules here: http://codegolf.stackexchange.com/questions/7036/print-custom-alphabet

I put up an APL solution. :-) Here's the one without the global:

⍞{1 1≡(2⌈⍴⍺)⍴⍺∊'.',⍵:⊃(~=/2⍴⍺)⌷(,X,' ')(X←25 1⍴1↓(X⍳⊃⍺)⌽X←(⎕A∊⍨⊃⍺)⌷2 26⍴⍵)}6↓26⌽⎕UCS 65+⍳58

And if I have the following I can make the main code shorter:

A←2 26⍴6↓26⌽⎕UCS 65+⍳58
{1 1≡(2⌈⍴⍵)⍴⍵∊'.',,A:⊃(~=/2⍴⍵)⌷(,X,' ')(X←25 1⍴1↓(X⍳⊃⍵)⌽X←(⎕A∊⍨⊃⍵)⌷A)}⍞

Of course, I think there are ways of making this even shorter, so if
someone has some suggestions, I'm all ears!

This is in Dyalog APL.

--
Aaron W. Hsu | arc...@sacrideo.us | http://www.sacrideo.us
Programming is just another word for the lost art of thinking.

david.doon...@gtempaccount.com

unread,
Aug 28, 2012, 8:03:44 AM8/28/12
to
On Thursday, August 23, 2012 11:22:50 PM UTC-4, frangonve wrote:
> Currently golfscript and q language win. Here you can find the requirements, tersesness rules here: http://codegolf.stackexchange.com/questions/7036/print-custom-alphabet Cheers Francisco

Another solution is

Z←GOLF R;A;B
A←⎕AV[65+⍳58]~'[\]^_`'
Z←B⌽(26ׯ1*26<B←A⍳↑R)↑A
→(2>⍴,R)/0
Z←,['']Z

or, bundled into a single line

Z←GOLF2 R;A;B
Z←B⌽(26ׯ1*26<B←A⍳↑R)↑A←⎕AV[65+⍳58]~'[\]^_`'⋄⍎(2≤⍴,R)/'Z←,['''']Z'

David




Graham

unread,
Aug 29, 2012, 9:49:12 AM8/29/12
to

"Aaron W. Hsu" <arc...@sacrideo.us> wrote in message news:6KydnZMn1IOjJKbN...@giganews.com...
A different approach in APL+WIN but at 73 characters still not competitive:

(('.'≠¯1↑x)⌽25 1××⍴d)⍴d←' ',¨⎕av[(⌊/s)⌽∊(26≥s←a⍳¨⎕av⍳↑x←⍞)/a←65 97+¨⊂⍳26]

It seems pretty efficient reading from the right up until the central ⎕av which deals with the selection and rotation of the alphabet case and the invalid input case. Its the formatting that currently looks a bit clumsy! Any offers???

Graham.

Graham

unread,
Aug 30, 2012, 6:00:12 AM8/30/12
to

"Graham" <h2gt2g42-mi...@yahoo.co.uk> wrote in message news:K--dnX8scehEgKPN...@bt.com...
P.S.

A bit better at 68 but still way off the competition!

' ',[1+.1×'.'=¯1↑x]¯1↓∊¨⎕av[(⌊/s)⌽∊(26≥s←a⍳¨⎕av⍳↑x←⍞)/a←65 97+¨⊂⍳26]

Graham.

Stefano Lanzavecchia

unread,
Sep 6, 2012, 11:34:27 AM9/6/12
to
Try as I may, I cannot get this one to work.

I propose (at 71, to be executed in Dyalog APL with []ML<-1):

⍞{(2-b←'.'≠⊃⌽z)=+/(z←,⍺)∊⍵,'.':{¯1↓,⍵,' '}⍣b+⍪26↑⍵⌽⍨⍵⍳⊃z}⎕UCS∊96 64+⊂52⍴⍳26

Stefano Lanzavecchia

unread,
Sep 6, 2012, 11:46:57 AM9/6/12
to
On Monday, 27 August 2012 20:15:26 UTC+2, Aaron W. Hsu wrote:

> I put up an APL solution. :-) Here's the one without the global:
>
>
>
> ⍞{1 1≡(2⌈⍴⍺)⍴⍺∊'.',⍵:⊃(~=/2⍴⍺)⌷(,X,' ')(X←25 1⍴1↓(X⍳⊃⍺)⌽X←(⎕A∊⍨⊃⍺)⌷2 26⍴⍵)}6↓26⌽⎕UCS 65+⍳58
>
>
>
> And if I have the following I can make the main code shorter:
>
>
>
> A←2 26⍴6↓26⌽⎕UCS 65+⍳58
>
> {1 1≡(2⌈⍴⍵)⍴⍵∊'.',,A:⊃(~=/2⍴⍵)⌷(,X,' ')(X←25 1⍴1↓(X⍳⊃⍵)⌽X←(⎕A∊⍨⊃⍵)⌷A)}⍞
>

Unfortunately, they both crash if you use 'dc' as input.

Stefano Lanzavecchia

unread,
Sep 6, 2012, 12:04:37 PM9/6/12
to
On Thursday, 6 September 2012 17:34:27 UTC+2, Stefano Lanzavecchia wrote:

> ⍞{(2-b←'.'≠⊃⌽z)=+/(z←,⍺)∊⍵,'.':{¯1↓,⍵,' '}⍣b+⍪26↑⍵⌽⍨⍵⍳⊃z}⎕UCS∊96 64+⊂52⍴⍳26

This can be shortened to:

⍞{(2-b←'.'≠⊃⌽⍺)=+/⍺∊⍵,'.':{¯1↓,⍵,' '}⍣b+⍪26↑⍵⌽⍨⍵⍳⊃⍺}⎕UCS∊96 64+⊂52⍴⍳26

david.doon...@gtempaccount.com

unread,
Sep 6, 2012, 5:00:42 PM9/6/12
to
On Thursday, August 23, 2012 11:22:50 PM UTC-4, frangonve wrote:
> Currently golfscript and q language win. Here you can find the requirements, tersesness rules here: http://codegolf.stackexchange.com/questions/7036/print-custom-alphabet Cheers Francisco

Using APL2, I have the following at 64 characters:

C←⍉⊃2↑(26>B)/(B←A⍳¨↑R←⍞)⌽¨A←26↑¨65 97↓¨⊂⎕AV⋄⍎((2×=/B)+1≠⍴R)↓'∊C'

Stefano Lanzavecchia

unread,
Sep 7, 2012, 11:56:01 AM9/7/12
to
When given input "dc" it answers:
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
a
b
c
d

which is wrong because "dc" is an invalid input.

Also, when given input "d" it produces a 52 elements vector, which is wrong according to the specs because the last character should not be a space.
Finally, when given input "d." it produces a 26x2 matrix which, once again, according to the specs, is wrong because there should be no space between the alphabetic character and the CR/LF terminator.

Close, but no cigar ;)

david.doon...@gtempaccount.com

unread,
Sep 7, 2012, 3:01:58 PM9/7/12
to

'The Challenge' states the following:

If an additiontal parameter is added to the input (a simple dot .) the alphabet
should be printed one letter in each line.

Ok, as I interpret, an additional parameter can be added to the input, a simple dot . being an example. For me,
'c' in 'dc' is an additional parameter. As such,‘'dc'’is valid input. Additionally, I interpret
'dcc' as valid input. Namely, multiple additional parameters are possible.

Next,‘'d'’does produce a 52-element vector. I do not read in the specs where the last character
should not be a space. For me, 'The Challenge' states

The alphabet printed must end in the precedent letter of the one inserted.

In this case, the "alphabet printed" ends in the precedent letter. Printed is the end space but this end trailing space is not part of the alphabet. I do not see 'The Challenge' violated in this case. Sorry.

Finally, 'd.' produces a 26x2 matrix that includes a trailing end space on each line. Per above, the output satisfies the specifications; namely one letter per line. Ok, there is more but only one letter per line. For me, a black and white’photo is a color photo with colors black and white only.

Apologies if I misread 'The Challenge'.

James J. Weinkam

unread,
Sep 12, 2012, 2:57:26 AM9/12/12
to
I first saw this thread about 10 days ago but didn't get a chance to work on it until Sunday. It took about ten tries to
come up with the APL2 solution below which I believe is correct. The trickiest aspect was finding a way to display
nothing for erroneous input. The algorithm requires ⎕IO←1, which is the default.

c←1↓50⍴' ',d←25 1⍴(a←c⍳e)⌽(c←26↑(65+32×97≤⎕AF e←↑b←⍞)↓⎕AV)◊⍎↑((b≡e,'.')(1=↑⍴b)^a<27)/'dc'

The algorithm could also be simplified if <, ≤, ≥, and > applied to characters. Does anyone know the reason for this
omission?


Roger Hui

unread,
Sep 12, 2012, 8:41:02 AM9/12/12
to
> The algorithm could also be simplified if <, ≤, ≥, and > applied to characters. Does anyone know the reason for this
> omission?

One reason for not doing it is to avoid having a dependence on the
underlying character set/collating sequence.

James J. Weinkam

unread,
Sep 12, 2012, 2:58:31 PM9/12/12
to
Roger Hui wrote:
>> The algorithm could also be simplified if<, ≤, ≥, and> applied to characters. Does anyone know the reason for this
>> omission?
>
> One reason for not doing it is to avoid having a dependence on the
> underlying character set/collating sequence.
>
Yeah, I thought of that; but is seems a weak reason to me. For one thing, it isn't much of a dependence: extending the
domain of the relational functions to characters would not affect anything else in the system or any existing correct
code. Existing code that includes an application of, say, < to character arguments would get a result instead of a
DOMAIN error; but is there any?

I just checked the Dyalog reference manual. It too requires the arguments of the relational functions to be numeric
(real) but, unlike APL2, it allows the grade functions to be applied to character data without a left argument using the
unicode codepoints as the collating sequence.

kai

unread,
Sep 17, 2012, 4:01:54 AM9/17/12
to
Since I started using APL I have seen countless competitions aiming to
spare a couple of symbols when solving problems. I can see the
intellectual challenge but I think that all programmers but APLers in
particular should rather spend their time on discussing and agreeing
how to make the code more readable and maintainable. Trying to shorten
the code quite often leads to the exact opposite.

In Dyalog I would do it this way with (⎕IO ⎕ML)←0 3:

∇ Golf←{
[1] 1≠≡,⍵:0 0⍴'' ⍝ Check ...
[2] ~(1=⍴,⍵)∨(2=⍴,⍵)∧1↑1↓,⍵='.':0 0⍴'' ⍝ ... right argument
[3] a←⎕A((⎕A ⎕R'\l0')⎕A) ⍝ Uppercase and
lowercase alphabet
[4] f←'.'≠↑1↑1↓⍵ ⍝ Scalar flag: not
horizontal
[5] c←↑⍵ ⍝ The input char
[6] ul←c∊¨a ⍝ Which alphabet?
[7] 0=+/ul:0 0⍴'' ⍝ Is input valid?
[8] ({1↓,' ',⍵}⍣f)⍪c{1↓⍵⌽⍨⍵⍳⍺}a⊃⍨ul⍳1 ⍝ Off we go...
[9] }

Display ⍪Golf3¨'c' 'c.' 'D' 'Z.' '/' 'Z/' '//' 'asd' '' (⊂⊂'')(1 2)
┌→────────────────────────────────────────────────────┐
↓ ┌→────────────────────────────────────────────────┐ │
│ │d e f g h i j k l m n o p q r s t u v w x y z a b│ │
│ └─────────────────────────────────────────────────┘ │
│ ┌→┐ │
│ ↓d│ │
│ │e│ │


│ │f│ │
│ │g│ │
│ │h│ │
│ │i│ │
│ │j│ │
│ │k│ │
│ │l│ │
│ │m│ │
│ │n│ │
│ │o│ │
│ │p│ │
│ │q│ │
│ │r│ │
│ │s│ │
│ │t│ │
│ │u│ │
│ │v│ │
│ │w│ │
│ │x│ │
│ │y│ │
│ │z│ │
│ │a│ │

│ │b│ │
│ └─┘ │
│ ┌→────────────────────────────────────────────────┐ │
│ │E F G H I J K L M N O P Q R S T U V W X Y Z A B C│ │
│ └─────────────────────────────────────────────────┘ │
│ ┌→┐ │
│ ↓A│ │
│ │B│ │
│ │C│ │
│ │D│ │
│ │E│ │
│ │F│ │
│ │G│ │
│ │H│ │
│ │I│ │
│ │J│ │
│ │K│ │
│ │L│ │
│ │M│ │
│ │N│ │
│ │O│ │
│ │P│ │
│ │Q│ │
│ │R│ │
│ │S│ │
│ │T│ │
│ │U│ │
│ │V│ │
│ │W│ │
│ │X│ │
│ │Y│ │
│ └─┘ │
│ ┌⊖┐ │
│ ⌽ │ │
│ └─┘ │
│ ┌⊖┐ │
│ ⌽ │ │
│ └─┘ │
│ ┌⊖┐ │
│ ⌽ │ │
│ └─┘ │
│ ┌⊖┐ │
│ ⌽ │ │
│ └─┘ │
│ ┌⊖┐ │
│ ⌽ │ │
│ └─┘ │
│ ┌⊖┐ │
│ ⌽ │ │
│ └─┘ │
│ ┌⊖┐ │
│ ⌽ │ │
│ └─┘ │
└∊────────────────────────────────────────────────────┘

I am aware that the function could easily be shortened but at the
expense of readability.

Note that the real solution in line 8 is actually both, short and
quite complex, but it is still possible to read it in one go from
right to left.

Shortening the function would mean to integrate building up the
alphabets and the checks for the argument being valid and what the
function is supposed to do into the solving line. Clearly this cannot
be right.

For general reasons it also seems to be wrong to me to spend resources
on processing data before I am sure that the argument is valid: those
resources are wasted if the argument turns out to be invalid.

Morten Kromberg

unread,
Sep 22, 2012, 9:07:36 AM9/22/12
to
On Thursday, 6 September 2012 18:04:38 UTC+2, Stefano Lanzavecchia wrote:
> This can be shortened to:
> ⍞{(2-b←'.'≠⊃⌽⍺)=+/⍺∊⍵,'.':{¯1↓,⍵,' '}⍣b+⍪26↑⍵⌽⍨⍵⍳⊃⍺}⎕UCS∊96 64+⊂52⍴⍳26

How about the following, which is slightly longer but doesn't use dfns, commute, or power - and is (IMHO) "clearer":

(c p b∧.∊(2 3) i ' .')⌿,⎕UCS⌽(13+19×b=' '),⍪p⌽(i←⍳26)+32×⊃c p←0 32⊤⎕UCS⊃a b←2↑⍞

Annotated version:

a b←2↑⍞ ⍝ Name each of the first two input chars
c p←0 32⊤⎕UCS⊃a b ⍝ c is "charset" and p "position" within charset
⍪p⌽(i←⍳26)+32×⊃c p ⍝ Make 1-column matrix with suitably rotated alphabet
,⎕UCS⌽(13+19×b=' '), ⍝ Ravel after appending space or CR depending on 2nd input char
(c p b∧.∊(2 3) i ' .')⌿ ⍝ Discard result if input invalid

?

Morten

P.S. Just returning to comp.lang.apl after losing my feed for a few months without noticing(!)

James J. Weinkam

unread,
Sep 22, 2012, 5:44:13 PM9/22/12
to
It looks to me as if your version will accept invalid inputs such as 's.z' and 'Q '. Also, I don't understand how the
result of rotate becomes a 1 column matrix or where the decision to display as a row or column occurs.



Morten Kromberg

unread,
Sep 22, 2012, 5:54:13 PM9/22/12
to
On Saturday, 22 September 2012 23:44:16 UTC+2, James J. Weinkam wrote:
> It looks to me as if your version will accept invalid inputs such as 's.z' and 'Q '.

Yes, I am not sure how strict the validation needs to be, perhaps I need to read the original problem statement more carefully.

> Also, I don't understand how the result of rotate becomes a 1 column matrix or where the decision to display as a row or column occurs.

Turning the data into a 1 column matrix is done by the monadic commabar (⍪ - UCS 9066). The decision to display as a row or column is a result of padding each row with either space (UCS 32=13+19) or carriage return (UCS 13), before the final ravel.

James J. Weinkam

unread,
Sep 23, 2012, 7:54:04 PM9/23/12
to
Morten Kromberg wrote:
> On Saturday, 22 September 2012 23:44:16 UTC+2, James J. Weinkam wrote:
>> It looks to me as if your version will accept invalid inputs such as 's.z' and 'Q '.
>
> Yes, I am not sure how strict the validation needs to be, perhaps I need to read the original problem statement more carefully.

I have looked at it, and the consensus is that valid input consists of a single upper or lower case alphabetic character
optionally followed by a period. If the input is invalid, nothing whatever is to be displayed. If the input is valid the
alphabet is to be displayed in the same case as the input character, starting with the character following the input
character and ending with the character just before it in cyclic order. If a period was specified, each output character
is to be displayed by itself on a separate line. If a period was not specified, the 25 letters are to be separated by 24
spaces on a single line (49 characters in all).

>
>> Also, I don't understand how the result of rotate becomes a 1 column matrix or where the decision to display as a row or column occurs.
>
> Turning the data into a 1 column matrix is done by the monadic commabar (⍪ - UCS 9066). The decision to display as a row or column is a result of padding each row with either space (UCS 32=13+19) or carriage return (UCS 13), before the final ravel.
>

Aha! Another Dyalog feature I didn't know about; that makes four now - I learned about guards in dynamic functions, the
commute operator, and the fact the squad can take a partial left argument from Aaron Hsu's solution. I have downloaded a
copy of the Dyalog Reference Manual and skimmed through it. I thought I had spotted most of the differences between
Dyalog and APL2 but more keep cropping up. By the way, when I was struggling with guard, that fact that I didn't know
what it was called coupled with the fact the ':' is not in the symbol index except as a label indicator made it hard for
me to look it up myself. Thanks to Aaron for pointing me in the right direction.

david.doon...@gtempaccount.com

unread,
Sep 24, 2012, 1:20:10 PM9/24/12
to
All,

If I am not mistaken, the original post provided the following link

http://codegolf.stackexchange.com/questions/7036/print-custom-alphabet

Is this the correct link with all the details? If not, please share the proper link. If so, please share where inputs such as ‘s.z’ and ‘dc’ are not allowed. Also, if a letter is not followed by a period, please indicate where in the challenge that the number of characters displayed totals 49.

Thanks,
David

James J. Weinkam

unread,
Sep 24, 2012, 3:47:18 PM9/24/12
to
The statement of the challenge uses the phrase "separated by" not "followed by." Also, the challenge is accompanied by
examples of correct input and the required output. If you highlight the outputs shown you will see that there are no
leading or trailing spaces in either format.

david.doon...@gtempaccount.com

unread,
Sep 25, 2012, 11:04:58 AM9/25/12
to


Thanks for sharing. For me, followed by is more inclusive in this case in that each letter is also separated by a simple space. It would be easier to accept if the challenge included ‘only separated by a simple space’. Again, I struggle to appreciate why inputs such ‘dc’ and ‘s.z’ are invalid. As I understand, ‘c’ in ‘dc’ is an additional parameter.


James J. Weinkam

unread,
Sep 25, 2012, 5:18:56 PM9/25/12
to
david.dooner%upr...@gtempaccount.com wrote:
>
>
> Thanks for sharing. For me, followed by is more inclusive in this case in that each letter is also separated by a simple space. It would be easier to accept if the challenge included ‘only separated by a simple space’. Again, I struggle to appreciate why inputs such ‘dc’ and ‘s.z’ are invalid. As I understand, ‘c’ in ‘dc’ is an additional parameter.
>
>
No doubt, but the OP said "separated" and his examples of correct output do not include leading or trailing spaces.

Also, with your interpretation the only requirement is that the input begin with a letter. This seems unlikely.

The style of the challenge suggests that the OP is not a native speaker of English. I think we need to cut him a little
slack.

0 new messages