BASIC User Group > Code Challenges

Floyd's Triangle

(1/3) > >>

John:
Here is the Script BASIC version of the Floyd's Triangle on Rosetta Code.


--- Code: Script BASIC ---' Floyd's triangle PRINT "Number of rows: "LINE INPUT rowsrows = VAL(CHOMP(rows))num = 1FOR row = 1 TO rows  FOR col = 1 to row    PRINT RIGHT("  " & num, LEN(STR(col + rows * (rows - 1) / 2))), " "    num += 1  NEXT  PRINTNLNEXT 

jrs@jrs-laptop:~/sb/examples/test$ scriba floyd.sb
Number of rows: 4
1
2 3
4 5 6
7 8 9 10
jrs@jrs-laptop:~/sb/examples/test$ scriba floyd.sb
Number of rows: 14
 1
 2  3
 4  5  6
 7  8  9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44  45
46 47 48 49 50 51 52 53  54  55
56 57 58 59 60 61 62 63  64  65  66
67 68 69 70 71 72 73 74  75  76  77  78
79 80 81 82 83 84 85 86  87  88  89  90  91
92 93 94 95 96 97 98 99 100 101 102 103 104 105
jrs@jrs-laptop:~/sb/examples/test$ scriba floyd.sb
Number of rows: 20
  1
  2   3
  4   5   6
  7   8   9  10
 11  12  13  14  15
 16  17  18  19  20  21
 22  23  24  25  26  27  28
 29  30  31  32  33  34  35  36
 37  38  39  40  41  42  43  44  45
 46  47  48  49  50  51  52  53  54  55
 56  57  58  59  60  61  62  63  64  65  66
 67  68  69  70  71  72  73  74  75  76  77  78
 79  80  81  82  83  84  85  86  87  88  89  90  91
 92  93  94  95  96  97  98  99 100 101 102 103 104 105
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
jrs@jrs-laptop:~/sb/examples/test$



AlyssonR:
Whenever I see this, I wonder who Floyd was, and whether Floyd was just bored to tears in a maths lecture.

John:
I can align with that thought.  ;)

I'm surprised the amount of code the other languages used to accomplish the same goal.

John:

--- Quote from: Tomaaz@RetroB ---Here is my version of Floyd's Triangle. I wanted to make it as compact as possible and created this one line in Ruby:


--- Code: Ruby ---(1..14).each {|x| puts (((1 + x) * (x / 2.0) - (x - 1)).to_i..((1 + x) * (x / 2.0)).to_i).to_a.to_s.split(",").join} 
Anyone wants to post something in BASIC? But please, try to do something different than two loops and couple of variables.  ;)

--- End quote ---

You're a member here and welcome to create and contribute to existing threads.

Doing multiple statements in one line doesn't mean less code. You have no user input for number of lines which is almost half of the SB code before doing the challenge.

John:
Here is the no frils version of Floyd's Triangle in Script BASIC at 101 characters and with user row specification.

Note:  Any INCLUDEs will be counted in the total character count for challenging submissions.


--- Code: Script BASIC ---l=COMMANDn=1FOR r=1TO lFOR c=1TO rPRINT RIGHT("  "&n,LEN(c+l*(l-1)/2))," "n+=1NEXTPRINTNLNEXT 
Notice:
[*] No need to convert the command line string argument to a numeric type for use.
[*] No need to convert the number to a STR() before concatenation.
[*] No need to convert the numeric expression to a STR() to get its LEN().
[*] The consensus is there is no need for Script BASIC.
[/list]


$ time scriba ft.sb 14
 1
 2  3
 4  5  6
 7  8  9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44  45
46 47 48 49 50 51 52 53  54  55
56 57 58 59 60 61 62 63  64  65  66
67 68 69 70 71 72 73 74  75  76  77  78
79 80 81 82 83 84 85 86  87  88  89  90  91
92 93 94 95 96 97 98 99 100 101 102 103 104 105

real   0m0.006s
user   0m0.006s
sys   0m0.000s
$


Tomaaz's Ruby example totals at 116 characters with a fix number of rows.

Script BASIC wins while remaining highly readable. SB is < 780KB unlike other scripting languages like Ruby in the multi-megabytes. Tomaaz's submission also fails the Rosetta Code requirements due to alignment and enclosing the the results in brackets. Half baked Tomaaz!  :o

Ruby Install

jrs@jrs-laptop:~/sb/examples/test$ sudo apt install ruby
[sudo] password for jrs:
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libatkmm-1.6-1v5 libcairomm-1.0-1v5 libcryptui0a libgtkmm-3.0-1v5 libice6:i386 libjpeg62:i386 libnih-dbus1 libpangomm-1.4-1v5 libsm6:i386 libvte-common libvte9 libxtst6:i386 par2 python-glade2
  python-vte seahorse-daemon
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  fonts-lato libruby2.5 rake ruby-did-you-mean ruby-minitest ruby-net-telnet ruby-power-assert ruby-test-unit ruby2.5 rubygems-integration
Suggested packages:
  ri ruby-dev bundler
The following NEW packages will be installed:
  fonts-lato libruby2.5 rake ruby ruby-did-you-mean ruby-minitest ruby-net-telnet ruby-power-assert ruby-test-unit ruby2.5 rubygems-integration
0 upgraded, 11 newly installed, 0 to remove and 23 not upgraded.
Need to get 5,998 kB of archives.
After this operation, 27.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 fonts-lato all 2.0-2 [2,698 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 rubygems-integration all 1.11 [4,994 B]
Get:3 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 ruby2.5 amd64 2.5.1-1ubuntu1 [48.6 kB]
Get:4 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 ruby amd64 1:2.5.1 [5,712 B]
Get:5 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 rake all 12.3.1-1 [45.1 kB]
Get:6 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-did-you-mean all 1.2.0-2 [9,700 B]
Get:7 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-minitest all 5.10.3-1 [38.6 kB]
Get:8 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-net-telnet all 0.1.1-2 [12.6 kB]
Get:9 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-power-assert all 0.3.0-1 [7,952 B]
Get:10 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 ruby-test-unit all 3.2.5-1 [61.1 kB]
Get:11 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 libruby2.5 amd64 2.5.1-1ubuntu1 [3,066 kB]
Fetched 5,998 kB in 4s (1,565 kB/s)     
Selecting previously unselected package fonts-lato.
(Reading database ... 269832 files and directories currently installed.)
Preparing to unpack .../00-fonts-lato_2.0-2_all.deb ...
Unpacking fonts-lato (2.0-2) ...
Selecting previously unselected package rubygems-integration.
Preparing to unpack .../01-rubygems-integration_1.11_all.deb ...
Unpacking rubygems-integration (1.11) ...
Selecting previously unselected package ruby2.5.
Preparing to unpack .../02-ruby2.5_2.5.1-1ubuntu1_amd64.deb ...
Unpacking ruby2.5 (2.5.1-1ubuntu1) ...
Selecting previously unselected package ruby.
Preparing to unpack .../03-ruby_1%3a2.5.1_amd64.deb ...
Unpacking ruby (1:2.5.1) ...
Selecting previously unselected package rake.
Preparing to unpack .../04-rake_12.3.1-1_all.deb ...
Unpacking rake (12.3.1-1) ...
Selecting previously unselected package ruby-did-you-mean.
Preparing to unpack .../05-ruby-did-you-mean_1.2.0-2_all.deb ...
Unpacking ruby-did-you-mean (1.2.0-2) ...
Selecting previously unselected package ruby-minitest.
Preparing to unpack .../06-ruby-minitest_5.10.3-1_all.deb ...
Unpacking ruby-minitest (5.10.3-1) ...
Selecting previously unselected package ruby-net-telnet.
Preparing to unpack .../07-ruby-net-telnet_0.1.1-2_all.deb ...
Unpacking ruby-net-telnet (0.1.1-2) ...
Selecting previously unselected package ruby-power-assert.
Preparing to unpack .../08-ruby-power-assert_0.3.0-1_all.deb ...
Unpacking ruby-power-assert (0.3.0-1) ...
Selecting previously unselected package ruby-test-unit.
Preparing to unpack .../09-ruby-test-unit_3.2.5-1_all.deb ...
Unpacking ruby-test-unit (3.2.5-1) ...
Selecting previously unselected package libruby2.5:amd64.
Preparing to unpack .../10-libruby2.5_2.5.1-1ubuntu1_amd64.deb ...
Unpacking libruby2.5:amd64 (2.5.1-1ubuntu1) ...
Setting up fonts-lato (2.0-2) ...
Setting up ruby-did-you-mean (1.2.0-2) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Setting up ruby-net-telnet (0.1.1-2) ...
Setting up rubygems-integration (1.11) ...
Processing triggers for man-db (2.8.3-2) ...
Setting up ruby-minitest (5.10.3-1) ...
Processing triggers for fontconfig (2.12.6-0ubuntu2) ...
Setting up ruby-power-assert (0.3.0-1) ...
Setting up ruby2.5 (2.5.1-1ubuntu1) ...
Setting up ruby (1:2.5.1) ...
Setting up ruby-test-unit (3.2.5-1) ...
Setting up rake (12.3.1-1) ...
Setting up libruby2.5:amd64 (2.5.1-1ubuntu1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
jrs@jrs-laptop:~/sb/examples/test$



--- Code: Ruby ---(1..14).each {|x| puts (((1 + x) * (x / 2.0) - (x - 1)).to_i..((1 + x) * (x / 2.0)).to_i).to_a.to_s.split(",").join} 

jrs@jrs-laptop:~/Ruby$ time ruby ft.rb
[1]
[2 3]
[4 5 6]
[7 8 9 10]
[11 12 13 14 15]
[16 17 18 19 20 21]
[22 23 24 25 26 27 28]
[29 30 31 32 33 34 35 36]
[37 38 39 40 41 42 43 44 45]
[46 47 48 49 50 51 52 53 54 55]
[56 57 58 59 60 61 62 63 64 65 66]
[67 68 69 70 71 72 73 74 75 76 77 78]
[79 80 81 82 83 84 85 86 87 88 89 90 91]
[92 93 94 95 96 97 98 99 100 101 102 103 104 105]

real   0m0.116s
user   0m0.104s
sys   0m0.012s
jrs@jrs-laptop:~/Ruby$


You need a set of Rails to haul that PIG around.

Navigation

[0] Message Index

[#] Next page

Go to full version