Author Topic: Array Code Challenge  (Read 6846 times)

ScriptBasic

  • Guest
Array Code Challenge
« on: February 07, 2016, 07:20:05 PM »
The BaCon project has a string append benchmark / research effort going to resolve BaCon's string management problems.

I would like to move on to array variables and how languages deal with them.

Challenge Definition:

The goal is to create an array that contains the the DEC, HEX, Symbol values for the ASCII table. (DEC 0 - 255)

Example:
Code: [Select]
ASCII[64,0] = 64
ASCII[64,1] = "40"
ASCII[64,2] = "@"

Interpreters, compilers and translators welcome.

« Last Edit: February 07, 2016, 07:28:38 PM by John »

B+

  • Guest
Re: Array Code Challenge
« Reply #1 on: February 07, 2016, 08:35:18 PM »
The BaCon project has a string append benchmark / research effort going to resolve BaCon's string management problems.

I would like to move on to array variables and how languages deal with them.

Challenge Definition:

The goal is to create an array that contains the the DEC, HEX, Symbol values for the ASCII table. (DEC 0 - 255)

Example:
Code: [Select]
ASCII[64,0] = 64
ASCII[64,1] = "40"
ASCII[64,2] = "@"

Interpreters, compilers and translators welcome.

What's the challenge?
Code: [Select]
REM SmallBASIC
REM created: 07/02/2016
dim ar(255,2)
for i=0 to 255
  ar(i,0)=i:ar(i,1)=hex(i):ar(i,2)=chr$(i)
next
? ar(64,0),ar(64,1),ar(64,2)
pause

ScriptBasic

  • Guest
Re: Array Code Challenge
« Reply #2 on: February 07, 2016, 08:57:59 PM »
Can you show some output from your example? (0 - 127 would be nice)
« Last Edit: February 07, 2016, 09:18:58 PM by John »

B+

  • Guest
Re: Array Code Challenge
« Reply #3 on: February 07, 2016, 09:21:23 PM »
Can you show some output from your example? (0 - 127 would be nice)

ScriptBasic

  • Guest
Re: Array Code Challenge
« Reply #4 on: February 07, 2016, 09:58:04 PM »
Looks great. Thanks.

Quote
What's the challenge?

I don't know of many languages that allow mix types for array elements. I know even less that don't require the array to be DIM'ed prior to use.

I wonder how many other languages can do the same?

Code: [Select]
FOR i = 0 TO 255
  ascii[i,0] = i
  ascii[i,1] = HEX(i)
  ascii[i,2] = CHR(i)
NEXT
FOR o = 0 TO 126
  PRINT ascii[o,0],"\t",ascii[o,1],"\t",ascii[o,2],"\n"
NEXT


jrs@laptop:~/sb/sb22/test$ scriba atbl.sb
0   0   
1   1   
2   2   
3   3   
4   4   
5   5   
6   6   
7   7   
8   8
9   9      
10   A   
11   B   
12   C   
13   D   
14   E   
15   F   
16   10   
17   11   
18   12   
19   13   
20   14   
21   15   
...
109   6D   m
110   6E   n
111   6F   o
112   70   p
113   71   q
114   72   r
115   73   s
116   74   t
117   75   u
118   76   v
119   77   w
120   78   x
121   79   y
122   7A   z
123   7B   {
124   7C   |
125   7D   }
126   7E   ~
jrs@laptop:~/sb/sb22/test$
« Last Edit: February 08, 2016, 06:41:10 AM by John »

ScriptBasic

  • Guest
Re: Array Code Challenge
« Reply #5 on: February 08, 2016, 07:02:22 AM »
I tried doing this in BaCon but ran into the single dimension issues before I could determine if mixed types were supported. (unlikely) BaCon has limited array support and what does work seems like a hack. BaCon only allows passing single dimension arrays as arguments to functions as well. I don't think BaCon allows returning arrays of any type. I should have saved the name C BASIC for Peter. (Crippled BASIC)

Maybe Tomaaz would have better luck as he is a BaCon fan.
« Last Edit: February 08, 2016, 07:50:44 AM by John »

Tomaaz

  • Guest
Re: Array Code Challenge
« Reply #6 on: February 08, 2016, 03:28:01 PM »
I don't know of many languages that allow mix types for array elements.

Python, Ruby, Lua, Tcl, Java, C#, JavaScript, Perl...

I know even less that don't require the array to be DIM'ed prior to use.

Python, Ruby, Lua, Tcl, Java, C#, JavaScript, Perl...

The whole challenge is completely pointless. What about something like this:

Write a program that writes on the screen values of sinus for numbers form 1 to 100 without using any variable name.

Perl
Code: [Select]
print for (map {sin() (1..100)})

How many other languages can do it?

Or:

What language let you do arithmetic operations on whole arrays?

Ruby

Code: [Select]
cities  = %w[ London Oslo Paris Amsterdam Berlin ]
visited = %w[Berlin Oslo]
puts "I still need to visit the following cities:", cities - visited

How many other languages can do it?

You can find something specific for almost every language (either good or bad), but what does it proof?

ScriptBasic

  • Guest
Re: Array Code Challenge
« Reply #7 on: February 08, 2016, 05:10:57 PM »
Quote
The whole challenge is completely pointless.

Can you post a Python example for grins?


Tomaaz

  • Guest
Re: Array Code Challenge
« Reply #8 on: February 08, 2016, 05:42:54 PM »
Can you post a Python example for grins?

Code: [Select]
a = []
for x in range(0, 256):
a.append([x, hex(x), chr(x)])
for x in range(0, 256):
print(a[x][0], a[x][1], a[x][2])

Result:

Quote
0 0x0
1 0x1 
2 0x2 
3 0x3 
4 0x4 
5 0x5
6 0x6 
7 0x7
8 0x8
9 0x9    
10 0xa

11 0xb

12 0xc

13 0xd
14 0xe
15 0xf
16 0x10 
17 0x11 
18 0x12 
19 0x13 
20 0x14 
21 0x15 
22 0x16 
23 0x17 
24 0x18 
25 0x19 
26 0x1a 
27 0x1b 
28 0x1c 
29 0x1d 
30 0x1e 
31 0x1f 
32 0x20 
33 0x21 !
34 0x22 "
35 0x23 #
36 0x24 $
37 0x25 %
38 0x26 &
39 0x27 '
40 0x28 (
41 0x29 )
42 0x2a *
43 0x2b +
44 0x2c ,
45 0x2d -
46 0x2e .
47 0x2f /
48 0x30 0
49 0x31 1
50 0x32 2
51 0x33 3
52 0x34 4
53 0x35 5
54 0x36 6
55 0x37 7
56 0x38 8
57 0x39 9
58 0x3a :
59 0x3b ;
60 0x3c <
61 0x3d =
62 0x3e >
63 0x3f ?
64 0x40 @
65 0x41 A
66 0x42 B
67 0x43 C
68 0x44 D
69 0x45 E
70 0x46 F
71 0x47 G
72 0x48 H
73 0x49 I
74 0x4a J
75 0x4b K
76 0x4c L
77 0x4d M
78 0x4e N
79 0x4f O
80 0x50 P
81 0x51 Q
82 0x52 R
83 0x53 S
84 0x54 T
85 0x55 U
86 0x56 V
87 0x57 W
88 0x58 X
89 0x59 Y
90 0x5a Z
91 0x5b [
92 0x5c \
93 0x5d ]
94 0x5e ^
95 0x5f _
96 0x60 `
97 0x61 a
98 0x62 b
99 0x63 c
100 0x64 d
101 0x65 e
102 0x66 f
103 0x67 g
104 0x68 h
105 0x69 i
106 0x6a j
107 0x6b k
108 0x6c l
109 0x6d m
110 0x6e n
111 0x6f o
112 0x70 p
113 0x71 q
114 0x72 r
115 0x73 s
116 0x74 t
117 0x75 u
118 0x76 v
119 0x77 w
120 0x78 x
121 0x79 y
122 0x7a z
123 0x7b {
124 0x7c |
125 0x7d }
126 0x7e ~
127 0x7f 
128 0x80 €
129 0x81 
130 0x82 ‚
131 0x83 ƒ
132 0x84 „
133 0x85 …
134 0x86 †
135 0x87 ‡
136 0x88 ˆ
137 0x89 ‰
138 0x8a Š
139 0x8b ‹
140 0x8c Œ
141 0x8d 
142 0x8e Ž
143 0x8f 
144 0x90 
145 0x91 ‘
146 0x92 ’
147 0x93 “
148 0x94 ”
149 0x95 •
150 0x96 –
151 0x97 —
152 0x98 ˜
153 0x99 ™
154 0x9a š
155 0x9b ›
156 0x9c œ
157 0x9d 
158 0x9e ž
159 0x9f Ÿ
160 0xa0  
161 0xa1 ¡
162 0xa2 ¢
163 0xa3 £
164 0xa4 ¤
165 0xa5 ¥
166 0xa6 ¦
167 0xa7 §
168 0xa8 ¨
169 0xa9 ©
170 0xaa ª
171 0xab «
172 0xac ¬
173 0xad ­
174 0xae ®
175 0xaf ¯
176 0xb0 °
177 0xb1 ±
178 0xb2 ²
179 0xb3 ³
180 0xb4 ´
181 0xb5 µ
182 0xb6 ¶
183 0xb7 ·
184 0xb8 ¸
185 0xb9 ¹
186 0xba º
187 0xbb »
188 0xbc ¼
189 0xbd ½
190 0xbe ¾
191 0xbf ¿
192 0xc0 À
193 0xc1 Á
194 0xc2 Â
195 0xc3 Ã
196 0xc4 Ä
197 0xc5 Å
198 0xc6 Æ
199 0xc7 Ç
200 0xc8 È
201 0xc9 É
202 0xca Ê
203 0xcb Ë
204 0xcc Ì
205 0xcd Í
206 0xce Î
207 0xcf Ï
208 0xd0 Ð
209 0xd1 Ñ
210 0xd2 Ò
211 0xd3 Ó
212 0xd4 Ô
213 0xd5 Õ
214 0xd6 Ö
215 0xd7 ×
216 0xd8 Ø
217 0xd9 Ù
218 0xda Ú
219 0xdb Û
220 0xdc Ü
221 0xdd Ý
222 0xde Þ
223 0xdf ß
224 0xe0 à
225 0xe1 á
226 0xe2 â
227 0xe3 ã
228 0xe4 ä
229 0xe5 å
230 0xe6 æ
231 0xe7 ç
232 0xe8 è
233 0xe9 é
234 0xea ê
235 0xeb ë
236 0xec ì
237 0xed í
238 0xee î
239 0xef ï
240 0xf0 ð
241 0xf1 ñ
242 0xf2 ò
243 0xf3 ó
244 0xf4 ô
245 0xf5 õ
246 0xf6 ö
247 0xf7 ÷
248 0xf8 ø
249 0xf9 ù
250 0xfa ú
251 0xfb û
252 0xfc ü
253 0xfd ý
254 0xfe þ
255 0xff ÿ

Happy?

ScriptBasic

  • Guest
Re: Array Code Challenge
« Reply #9 on: February 08, 2016, 05:58:18 PM »
Quote
Happy?

Yes. Thanks for your time and efforts on this topic.

Tomaaz

  • Guest
Re: Array Code Challenge
« Reply #10 on: February 08, 2016, 07:50:08 PM »
Here is a Perl code. Just copy it to your terminal (you need to have Perl installed, of course) and hit enter:

Code: [Select]
perl -e 'for (0..255) {$a[$_] = [$_, sprintf("%x", $_), chr($_)]; print $a[$_][0], " ", $a[$_][1], " ", $a[$_ ][2], "\n"};'

Or you can run this from the file:

Code: [Select]
for (0..255) {$a[$_] = [$_, sprintf("%x", $_), chr($_)]};
for (0..255) {print $a[$_][0], " ", $a[$_][1], " ", $a[$_ ][2], "\n"};

Or something more readable:

Code: [Select]
for $x (0..255) {
$a[$x] = [$x, sprintf("%x", $x), chr($x)];
}
for $x (0..255) {
print $a[$x][0], " ", $a[$x][1], " ", $a[$x][2], "\n";
}
« Last Edit: February 08, 2016, 08:01:10 PM by Tomaaz »

ScriptBasic

  • Guest
Re: Array Code Challenge
« Reply #11 on: February 08, 2016, 08:08:14 PM »
Quote
Here is a Perl code. Just copy it to your terminal (you need to have Perl installed, of course) and hit enter:

Or I could just use my Perl extension module and run it from Script BASIC8)

Code: [Select]
DECLARE SUB pl_Init ALIAS "pl_Init" LIB "sbperl"
DECLARE SUB pl_Eval ALIAS "pl_Eval" LIB "sbperl"
DECLARE SUB pl_Destroy ALIAS "pl_Destroy" LIB "sbperl"

pl_Init
pl_code = """
for (0..255) {$a[$_] = [$_, sprintf("%x", $_), chr($_)]};
for (0..255) {print $a[$_][0], " ", $a[$_][1], " ", $a[$_ ][2], "\n"};
"""
pl_Eval pl_code
pl_Destroy


Tomaaz

  • Guest
Re: Array Code Challenge
« Reply #12 on: February 09, 2016, 06:02:05 PM »
Wow!  :) Myself, I would just use Perl, but if someone feels more familiar with BASIC (but wants to have Perl power) it may be a good solution. ;)

ScriptBasic

  • Guest
Re: Array Code Challenge
« Reply #13 on: February 10, 2016, 04:50:53 AM »
Here is a Script BASIC Perl SQLite example. Perl has an extensive library of goodies that are only a use away. This is a simple interface that allows me to load Perl scripts dynamically, run them and get variable values.

Code: [Select]
DECLARE SUB pl_Init ALIAS "pl_Init" LIB "sbperl"
DECLARE SUB pl_Eval ALIAS "pl_Eval" LIB "sbperl"
DECLARE SUB pl_GetInt ALIAS "pl_GetInt" LIB "sbperl"
DECLARE SUB pl_GetDbl ALIAS "pl_GetDbl" LIB "sbperl"
DECLARE SUB pl_GetStr ALIAS "pl_GetStr" LIB "sbperl"
DECLARE SUB pl_Destroy ALIAS "pl_Destroy" LIB "sbperl"

pl_Init

pl_code = """use DBI; use strict;

my $driver   = "SQLite";
my $database = "test.db";
my $dsn = "DBI:$driver:dbname=$database";
my $userid = "";
my $password = "";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
                      or die $DBI::errstr;
print "Opened database successfully\n";

my $stmt = qq(CREATE TABLE COMPANY
      (ID INT PRIMARY KEY     NOT NULL,
       NAME           TEXT    NOT NULL,
       AGE            INT     NOT NULL,
       ADDRESS        CHAR(50),
       SALARY         REAL););
my $rv = $dbh->do($stmt);
if($rv < 0){
   print $DBI::errstr;
} else {
   print "Table created successfully\n";
}
$dbh->disconnect();
"""
PRINTNL

pl_Eval pl_code

pl_Destroy

I also have language extension modules for TinyScheme and MyBASIC. The BBC extension module only addresses the graphic syntax of the BASIC.
« Last Edit: February 10, 2016, 05:03:26 AM by John »

Tomaaz

  • Guest
Re: Array Code Challenge
« Reply #14 on: February 12, 2016, 12:01:39 PM »
Pike

Code: [Select]
void main() {
array|mixed a = ({});
int x;
for (x = 0; x < 256; x++) {
a = Array.push(a, ({x, int2hex(x), int2char(x)}));
}
for (x = 0; x < 256; x++) {
write((string)a[x][0]);
write("  ");
write((string)a[x][1]);
write("  ");
write((string)a[x][2]);
write("\n");
}
}