Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #50870 > unrolled thread

Rosetta Code: Zig-zag matrix

Started by"WJ" <w_a_x_man@yahoo.com>
First post2016-09-12 02:52 +0000
Last post2016-09-12 14:15 -0700
Articles 6 — 3 participants

Back to article view | Back to comp.lang.forth


Contents

  Rosetta Code: Zig-zag matrix "WJ" <w_a_x_man@yahoo.com> - 2016-09-12 02:52 +0000
    Re: Rosetta Code: Zig-zag matrix Paul Rubin <no.email@nospam.invalid> - 2016-09-11 22:05 -0700
      Re: Rosetta Code: Zig-zag matrix albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-09-12 13:50 +0200
        Re: Rosetta Code: Zig-zag matrix Paul Rubin <no.email@nospam.invalid> - 2016-09-12 13:12 -0700
          Re: Rosetta Code: Zig-zag matrix albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-09-12 22:38 +0200
            Re: Rosetta Code: Zig-zag matrix Paul Rubin <no.email@nospam.invalid> - 2016-09-12 14:15 -0700

#50870 — Rosetta Code: Zig-zag matrix

From"WJ" <w_a_x_man@yahoo.com>
Date2016-09-12 02:52 +0000
SubjectRosetta Code: Zig-zag matrix
Message-ID<nr558f$3l8$1@dont-email.me>
> A  zig-zag  array is a square arrangement of the first  N^2
> integers, where the numbers increase sequentially as you
> zig-zag along the array's anti-diagonals.
> 
> For a graphical representation, see   JPG zigzag   (JPG uses
> such arrays to encode images).
> 
> For example, given 5, produce this array: 
> 
>  0  1  5  6 14
>  2  4  7 13 15
>  3  8 12 16 21
>  9 11 17 20 22
> 10 18 19 23 24


Ruby:

def out(n, limit)
  not (0 ... limit).include?(n)
end

n = 12
board = Array.new(n){Array.new(n,0)}
x=y=0 ; delta = 1
1.upto(n*n-1){|i|
  newx = x + delta; newy = y - delta
  if out(newy,n)
    newy = y
    newx = x + 1
  end
  if out(newx,n)
    newx = x
    newy = y + 1
  end
  delta = -delta if x==newx or y==newy
  board[y=newy][x=newx] = i
}

def show board
  w = (board.last.last).to_s.size
  puts board.map{|row| row.map{|n| n.to_s.rjust(w)}.join " "}
end

show board

  0   1   5   6  14  15  27  28  44  45  65  66
  2   4   7  13  16  26  29  43  46  64  67  88
  3   8  12  17  25  30  42  47  63  68  87  89
  9  11  18  24  31  41  48  62  69  86  90 107
 10  19  23  32  40  49  61  70  85  91 106 108
 20  22  33  39  50  60  71  84  92 105 109 122
 21  34  38  51  59  72  83  93 104 110 121 123
 35  37  52  58  73  82  94 103 111 120 124 133
 36  53  57  74  81  95 102 112 119 125 132 134
 54  56  75  80  96 101 113 118 126 131 135 140
 55  76  79  97 100 114 117 127 130 136 139 141
 77  78  98  99 115 116 128 129 137 138 142 143

In Forth?


-- 
[A]n unholy alliance of leftists, capitalists, and Zionist supremacists has
schemed to promote immigration and miscegenation with the deliberate aim of
breeding us out of existence in our own homelands....  [T]he real aim stays the
same: the biggest genocide in human history....  --- Nick Griffin
(https://www.youtube.com/watch?v=K0hD7IffTJs)

[toc] | [next] | [standalone]


#50871

FromPaul Rubin <no.email@nospam.invalid>
Date2016-09-11 22:05 -0700
Message-ID<87oa3thfmd.fsf@jester.gateway.pace.com>
In reply to#50870
"WJ" <w_a_x_man@yahoo.com> writes:
>   0   1   5   6  14  15  27  28  44  45  65  66
>   2   4   7  13  16  26  29  43  46  64  67  88
>   3   8  12  17  25  30  42  47  63  68  87  89
>   9  11  18  24  31  41  48  62  69  86  90 107
>  10  19  23  32  40  49  61  70  85  91 106 108
>  20  22  33  39  50  60  71  84  92 105 109 122
>  21  34  38  51  59  72  83  93 104 110 121 123
>  35  37  52  58  73  82  94 103 111 120 124 133
>  36  53  57  74  81  95 102 112 119 125 132 134
>  54  56  75  80  96 101 113 118 126 131 135 140
>  55  76  79  97 100 114 117 127 130 136 139 141
>  77  78  98  99 115 116 128 129 137 138 142 143

12 constant n
n n  *  1- constant km
: t ( x y -- n ) + dup 1+ * 2/ ;
: reflect ( x y -- x y ) negate swap negate n 1- tuck + -rot + ;
: av ( x y -- n ) 2dup t -rot 2dup + 1 and if nip else drop then + ;
: bv ( x y -- n ) reflect av km  swap - ;
: v ( x y -- n ) 2dup + n >= if bv else av then ; 
: go cr n 0 do n 0 do i j v . loop cr loop ;

go 
0 1 5 6 14 15 27 28 44 45 65 66 
2 4 7 13 16 26 29 43 46 64 67 88 
3 8 12 17 25 30 42 47 63 68 87 89 
9 11 18 24 31 41 48 62 69 86 90 107 
10 19 23 32 40 49 61 70 85 91 106 108 
20 22 33 39 50 60 71 84 92 105 109 122 
21 34 38 51 59 72 83 93 104 110 121 123 
35 37 52 58 73 82 94 103 111 120 124 133 
36 53 57 74 81 95 102 112 119 125 132 134 
54 56 75 80 96 101 113 118 126 131 135 140 
55 76 79 97 100 114 117 127 130 136 139 141 
77 78 98 99 115 116 128 129 137 138 142 143 
 ok

[toc] | [prev] | [next] | [standalone]


#50888

Fromalbert@cherry.spenarnc.xs4all.nl (Albert van der Horst)
Date2016-09-12 13:50 +0200
Message-ID<nr64qr$jbm$1@cherry.spenarnc.xs4all.nl>
In reply to#50871
In article <87oa3thfmd.fsf@jester.gateway.pace.com>,
Paul Rubin  <no.email@nospam.invalid> wrote:
>"WJ" <w_a_x_man@yahoo.com> writes:
>>   0   1   5   6  14  15  27  28  44  45  65  66
>>   2   4   7  13  16  26  29  43  46  64  67  88
>>   3   8  12  17  25  30  42  47  63  68  87  89
>>   9  11  18  24  31  41  48  62  69  86  90 107
>>  10  19  23  32  40  49  61  70  85  91 106 108
>>  20  22  33  39  50  60  71  84  92 105 109 122
>>  21  34  38  51  59  72  83  93 104 110 121 123
>>  35  37  52  58  73  82  94 103 111 120 124 133
>>  36  53  57  74  81  95 102 112 119 125 132 134
>>  54  56  75  80  96 101 113 118 126 131 135 140
>>  55  76  79  97 100 114 117 127 130 136 139 141
>>  77  78  98  99 115 116 128 129 137 138 142 143
>
>12 constant n
>n n  *  1- constant km
>: t ( x y -- n ) + dup 1+ * 2/ ;
>: reflect ( x y -- x y ) negate swap negate n 1- tuck + -rot + ;
>: av ( x y -- n ) 2dup t -rot 2dup + 1 and if nip else drop then + ;
>: bv ( x y -- n ) reflect av km  swap - ;
>: v ( x y -- n ) 2dup + n >= if bv else av then ;
>: go cr n 0 do n 0 do i j v . loop cr loop ;
>
>go
>0 1 5 6 14 15 27 28 44 45 65 66
>2 4 7 13 16 26 29 43 46 64 67 88
>3 8 12 17 25 30 42 47 63 68 87 89
>9 11 18 24 31 41 48 62 69 86 90 107
>10 19 23 32 40 49 61 70 85 91 106 108
>20 22 33 39 50 60 71 84 92 105 109 122
>21 34 38 51 59 72 83 93 104 110 121 123
>35 37 52 58 73 82 94 103 111 120 124 133
>36 53 57 74 81 95 102 112 119 125 132 134
>54 56 75 80 96 101 113 118 126 131 135 140
>55 76 79 97 100 114 117 127 130 136 139 141
>77 78 98 99 115 116 128 129 137 138 142 143
> ok

That is not enough context.
Remember most of us have WJ kill-filed.

Groetjes Albert
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [next] | [standalone]


#50911

FromPaul Rubin <no.email@nospam.invalid>
Date2016-09-12 13:12 -0700
Message-ID<87y42wg9mn.fsf@jester.gateway.pace.com>
In reply to#50888
albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) writes:
> That is not enough context.
> Remember most of us have WJ kill-filed.

He posted dozens of lines of crazy Ruby code that built up that table as
a matrix in memory before printing it, i.e. using memory space quadratic
in the number of rows.  Mine used constant memory and was 8 lines of
Forth, calculating and printing the numbers on the fly.

[toc] | [prev] | [next] | [standalone]


#50915

Fromalbert@cherry.spenarnc.xs4all.nl (Albert van der Horst)
Date2016-09-12 22:38 +0200
Message-ID<nr73ne$n1g$1@cherry.spenarnc.xs4all.nl>
In reply to#50911
In article <87y42wg9mn.fsf@jester.gateway.pace.com>,
Paul Rubin  <no.email@nospam.invalid> wrote:
>albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) writes:
>> That is not enough context.
>> Remember most of us have WJ kill-filed.
>
>He posted dozens of lines of crazy Ruby code that built up that table as
>a matrix in memory before printing it, i.e. using memory space quadratic
>in the number of rows.  Mine used constant memory and was 8 lines of
>Forth, calculating and printing the numbers on the fly.

I don't want to see WJ's code, but I can't appreciate your code
unless I know what problem is supposedly solved.

Groetjes Albert
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [next] | [standalone]


#50919

FromPaul Rubin <no.email@nospam.invalid>
Date2016-09-12 14:15 -0700
Message-ID<87poo8g6q2.fsf@jester.gateway.pace.com>
In reply to#50915
albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) writes:
> I don't want to see WJ's code, but I can't appreciate your code
> unless I know what problem is supposedly solved.

If you look at that matrix, the upper left entry is 0, then 1 is
adjacent to it, then 2 is diagonally adjacent to the 1, and if you
follow the path through 3,4,...143 it takes a zig-zag pattern through
the 12x12 square.  Not exactly a thesis project.  

Also, it's a Rosetta problem: http://rosettacode.org/wiki/Zig-zag_matrix

and it looks like someone has already put a Forth solution there.

It would be nice if the Rosetta tasks were numbered like Euler problems.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.forth


csiph-web