Oh, I just realized I completely misunderstood the problem. I thought it only 
takes the first one or two items of the list
each iteration.

This is my amended solution, which may or may not be correct:

countandsay2 =: 3 : 0
h=:__
t=: ''
str=: ''
max=:y
ctr=: 0
takewhile =:(3 : 'str=: str, (":@:# , {.) t' )]((3 :'t=:t,{.y')]}.)^:(3 : 
'(h&=@:{.y)')^:_ 
calc =: takewhile^:(3 : ' t=: '''''](0&<@:#)[(3 : 'h=:{.y'))^:_
val=. ((3 : 'str')[calc)^:(3 : 'ctr<max[str=:''''[ctr=:>:ctr')^:_ '1'
)
countandsay2 8

--------------------------------------------
On Fri, 9/2/16, 'Jon Hough' via Programming <[email protected]> wrote:

 Subject: [Jprogramming] Count and say
 To: "Programming Forum" <[email protected]>
 Date: Friday, September 2, 2016, 1:42 PM
 
 This is a leetcode question:
 https://leetcode.com/problems/count-and-say/
 Description:
 The count-and-say sequence is the sequence of integers
 beginning as follows:
 1, 11, 21, 1211, 111221, ...
 
 1 is read off as "one 1" or 11.
 11 is read off as "two 1s" or 21.
 21 is read off as "one 2, then one 1" or 1211.
 Given an integer n, generate the nth sequence.
 
 Note: The sequence of integers will be represented as a
 string.
 
 I thought it would be fun to tackle this in J...
 
 
 
 NB. Leetcode algorithm problem 38. Count and Say
 countandsay =: 3 : 0
 max=: y
 str=: ''
 tt=. 3 :'str=:str,''22'''
 to=. 3 :'str=:str,''21'''
 ot=. 3 :'str=:str,''12'''
 oo=. 3 :'str=:str,''11'''
 
 ones=. (3 : '1&}.y')[(ot`oo@.('1'&=@:{.))
 twos=. (3 : '2&}.y')[(tt`to@.('1'&=@:{.))
 f=. (ones`twos@.((0&{) =
 (1&{)))`ones@.(1&=@:#)^:(0&<@:#)^:_
 ctr=: 0
 val=. ((3 : 'str')[f)^:(3 :
 'ctr<max[str=:''''[ctr=:>:ctr')^:_ '1'
 )
 
 countandsay 5 NB. test value 5
 
 My main goal for this solution was figuring out how to do it
 with no while or for loops. 
 Anything more terse?
 ----------------------------------------------------------------------
 For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to