The spoiler tag can be used with [-spoiler] ... [-/spoiler] (without the '-'). Example of adding code in spoiler tags:
[-spoiler]
[-code]
print("This code should be hidden!");
[-/code]
[-/spoiler]
The '-' is added to allow you to see the tags. Otherwise would be treated as a tag and would be converted to HTML. You don't need he '-'.
RULES:
- >STAFF MEMBERS EXCLUDED! WE ALL KNOW YOU CAN READ A DOCUMENTATION UNLIKE THE USUAL SCRIPTER
Also we do have a spoiler tag
Damn.
Code: [Select] local array = ["This", "And", "That", "Why", "Not"];
if (array.len() <= 1) print(array[0]);
if (array.len() <= 2) print(array[1]);
if (array.len() <= 3) print(array[2]);
if (array.len() <= 4) print(array[3]);
if (array.len() <= 5) print(array[4]);
//And so on...
This is a pretty horrible way but
...
Use spoiler tags.Quote from NE.CrystalBlue on June 10th, 2015, 12:15 PM This is a pretty horrible way but
...
Code: [Select] // Print all the values of an array without using any loop
local array = [];
local _index1 = array.push( "a" );
local _index2 = array.push( "b" );
local _index3 = array.push( "c" );
print( "Array contains index " + _index1 );
print( "Array contains index " + _index2 );
print( "Array contains index " + _index3 );
print( "Lets hope SLC doesn't disagree with this" );
[-/code]
[-/spoiler]
Hint about the first approach: Recursive...
Hint about the second approach: Delegates...
Code: [Select] function onScriptLoad()
{
_test_();
}
// function for defining & printing array entities
function _test_(ctr=0)
{
// sample array holding the entities
local array = [ "mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune" ];
// printing the array slot indexed at 'ctr'
print(array[ctr]);
// updating the counter by 1
++ctr;
// checking if the updated counter goes beyond the range of array slots ( 0 - n-1 )
if( ctr >= array.len() ) return 0;
// calling the function with updated counter [ Tail Recursion ]
_test_(ctr);
}
** Kratos_ opened forum & boom all fun destroyed . ( S.L.C disclosed )
function PrintArray(array, num=0)
{
print(array[num]);
num+=1;
if(array.len() != num) PrintArray(array, num);
}
Code: [Select] local arr = [2, 354, "foo", 43, 5.234, 675, "bar", true, {a = 23, b = 34.546}];
// Nethod 3 --------------------------------------------------------
function VarargsApproach(arr)
{
// Insert the format string in the argument pack
arr.insert(0, "");
// Generate the format string
local fmt = function(arr, idx = 0)
{
if (idx < arr.len())
{
switch (typeof arr[idx])
{
case "integer": arr[0] += "%d\n"; break;
case "float": arr[0] += "%f\n"; break;
case "string": arr[0] += "%s\n"; break;
default:
arr[0] += "%s\n";
try {
arr[idx] = arr[idx].tostring();
} catch (e) {
arr[idx] = "(null : 0x00000000)";
}
break;
}
// Go down the rabbit hole
callee().call(this, arr, ++idx);
}
}
// Skip the format string
fmt(arr, 1);
// Insert the 'this' environment
arr.insert(0, this);
// Print the f*er
::print(::format.acall(arr));
}
// We make a shallow copy of the array
// Otherwise we edit the original
VarargsApproach(clone arr);
Code: [Select] function onScriptLoad()
{
local array = ["once", "upon", "a", "time", "Cinderella", "was", "going", "somewhere"];
loopn <- 0;
NewTimer("PrintAllOfThese", 1, array.len(), array);
}
function PrintAllOfThese(array)
{
print(array[loopn]);
loopn++;
}
Well you said that we cannot use loops but said absolutely nothing about using timers, so here it is!
And that functionCode: [Select] local ra = ["Cinderella", "ate", "nuttella", "then", "blah"];
print(arraytostring(ra, ", "));Code: [Select] function arraytostring(xarray, xsplitter, xstarting=0, xcurrent="")
{
if (xstarting >= xarray.len()) return xcurrent;
else return (arraytostring(xarray, xsplitter, xstarting+1, xcurrent + ((xstarting == 0) ? "" : xsplitter) + xarray[xstarting]));
}
Code: [Select] local arrayTest = ["test1","test2","test3","test4"];
// .apply replaces each value in an array with the returned value.
//
// This is a cheaty way of printing stuff without loops and we need to
// return the original value to avoid filling the array with nulls.
arrayTest.apply(function(v) { print(v); return v; });
Code: [Select] // Squirrel internally clones the array and uses the return value from map to
// fill the cloned array.
//
// If you were to look at the returned array from .map, you'd see a bunch
// of nulls, as we never return anything.
arrayTest.map(@(v) print(v));
One-line delegate abuse (two lines if counting the array).
Code: [Select] local arr = [2, 354, "foo", 43, 5.234, 675, "bar", true, {a = 23, b = 34.546}];
// Nethod 1 --------------------------------------------------------
// Recursive functions
function RecursiveApproach(arr, idx = 0)
{
if (idx < arr.len())
{
::print(arr[idx]);
// Go down the rabbit hole
RecursiveApproach(arr, ++idx);
}
}
// Initial call
RecursiveApproach(arr, 0);
print("---- ---- ---- ---- ---- ----");
// Nethod 2 --------------------------------------------------------
// Default delegates + lambda
function DelegatesApproach1(arr)
{
// NOTE: arr becomes filled with nulls now
arr.apply(@(v) ::print(v));
}
// We make a shallow copy of the array
// Otherwise we fill the original with nulls
DelegatesApproach1(clone arr);
print("---- ---- ---- ---- ---- ----");
// Default dlegates + function
function DelegatesApproach2(arr)
{
// NOTE: arr doesn't become filled with nulls now
arr.apply(function(v) { ::print(v); return v; });
}
DelegatesApproach2(arr);
print("---- ---- ---- ---- ---- ----");
// Nethod 3 --------------------------------------------------------
function VarargsApproach(arr)
{
// Insert the format string in the argument pack
arr.insert(0, "");
// Generate the format string
local fmt = function(arr, idx = 0)
{
if (idx < arr.len())
{
switch (typeof arr[idx])
{
case "integer": arr[0] += "%d\n"; break;
case "float": arr[0] += "%f\n"; break;
case "string": arr[0] += "%s\n"; break;
default:
arr[0] += "%s\n";
try {
arr[idx] = arr[idx].tostring();
} catch (e) {
arr[idx] = "(null : 0x00000000)";
}
break;
}
// Go down the rabbit hole
callee().call(this, arr, ++idx);
}
}
// Skip the format string
fmt(arr, 1);
// Insert the 'this' environment
arr.insert(0, this);
// Print the f*er
::print(::format.acall(arr));
}
// We make a shallow copy of the array
// Otherwise we edit the original
VarargsApproach(clone arr);
function say(a)
{
print(a);
}
say("Wazzaaaa!");
When will be the next challenge I wonder...
Here's a special challenge just for you: Be patient.Quote from NE.CrystalBlue on June 13th, 2015, 04:53 AM When will be the next challenge I wonder...
This requires CreateSprite which is not yet compiled...
.Func(_SC("CreateSprite"), CreateSprite, 8, _SC("tsiiiini"))
What do you mean it's not yet compiled? Have you created a sprite before?Quote from NE.CrystalBlue on June 14th, 2015, 01:31 PM This requires CreateSprite which is not yet compiled...Code: [Select] .Func(_SC("CreateSprite"), CreateSprite, 8, _SC("tsiiiini"))
Who said we load the whole module? We just load squirrel... Pure and native support only
Are you trolling me?Quote from NE.CrystalBlue on June 14th, 2015, 03:58 PM Who said we load the whole module? We just load squirrel... Pure and native support only
Who said we load the whole module? We just load squirrel... Pure and native support onlyQuote from S.L.C on June 14th, 2015, 03:48 PM What do you mean it's not yet compiled? Have you created a sprite before?Quote from NE.CrystalBlue on June 14th, 2015, 01:31 PM This requires CreateSprite which is not yet compiled...Code: [Select] .Func(_SC("CreateSprite"), CreateSprite, 8, _SC("tsiiiini"))
Code:
http://pastebin.com/tum2Xgj4
Sprites:
http://imgur.com/a/tcdaz
Filenames for them should be board.png, cellx.png and cello.png.
... sprites are pure and native to Squirrel. End of discussion.Quote from NE.CrystalBlue on June 14th, 2015, 03:58 PM Who said we load the whole module? We just load squirrel... Pure and native support onlyQuote from S.L.C on June 14th, 2015, 03:48 PM What do you mean it's not yet compiled? Have you created a sprite before?Quote from NE.CrystalBlue on June 14th, 2015, 01:31 PM This requires CreateSprite which is not yet compiled...Code: [Select] .Func(_SC("CreateSprite"), CreateSprite, 8, _SC("tsiiiini"))
They are pure and native to VCMP. End of discussion.
They are pure and native to VCMP. End of discussion.Quote from Stormeus on June 14th, 2015, 05:45 PM ... sprites are pure and native to Squirrel. End of discussion.Quote from NE.CrystalBlue on June 14th, 2015, 03:58 PM Who said we load the whole module? We just load squirrel... Pure and native support only
I know this is an 18-month bump, but these were quite nice. Do you have any plans on continuing them?
Code: [Select] local mm = {
_get = function(k) { return null; }
_set = function(k, v) { this.rawset(k, v); }
};
local person = { name = "Bawss" }.setdelegate(mm);
print(person.name);
// Do not exist (yet)
print(person.age);
print(person.city);
// Note the '=' and not '<-'
person.age = 37;
person.city = "Candyland";
print(person.name);
// Now they exist
print(person.age);
print(person.city);
t1 <- {a=1, b=2};
try
{
t1["d"] = t1["c"];
}
catch (e) {}
t1["d"] <- 3;
...
There's a difference between your code and doom's challenge. Anyone can insert a new slot( using the '<-' sign ). The question was completely different. Try to understand the question again.Quote from EK.CrystalBlue on December 21st, 2016, 03:27 PM ...
Code: [Select] function onPlayerCommand( player, cmd, text ) {
if( cmd == "g" ) {
local maxStars = text.tointeger();
if( maxStars ) {
for( local mainRun = 0; mainRun <= maxStars-1; mainRun++ ) {
local string = "";
for( local j = 1; j <= maxStars; j++ ) {
if( maxStars - j > mainRun ) {
string += ".";
}
else {
string += "*";
}
}
print( string );
}
}
}
}
Code: [Select] function PrintRowz(num)
{
print("Number of rows: " + num);
if (typeof(num) != "integer" || num < 1) return print("Rejected");
local Repeat = function (str, num)
{
local ret = "";
for (local i = 0; i < num; ++i) ret += str;
return ret;
}
for (local i = 0; i < num; ++i)
{
print(Repeat(".", num - i) + Repeat("*", i));
}
}
Code: [Select] function Generate(l) for (local f = function(p, l, x = 0, b = "") { while (++x <= l) b += x < (l-p) ? "." : "*"; return b; }, p = 0; p < l; ++p) print(f(p, l));
Generate(32);
...............................*
..............................**
.............................***
............................****
...........................*****
..........................******
.........................*******
........................********
.......................*********
......................**********
.....................***********
....................************
...................*************
..................**************
.................***************
................****************
...............*****************
..............******************
.............*******************
............********************
...........*********************
..........**********************
.........***********************
........************************
.......*************************
......**************************
.....***************************
....****************************
...*****************************
..******************************
.*******************************
********************************
(click to show/hide) Code: [Select] function Generate(l) for (local f = function(p, l, x = 0, b = "") { while (++x <= l) b += x < (l-p) ? "." : "*"; return b; }, p = 0; p < l; ++p) print(f(p, l));
Ex:Code: [Select] Generate(32);
Out:Code: [Select] ...............................*
..............................**
.............................***
............................****
...........................*****
..........................******
.........................*******
........................********
.......................*********
......................**********
.....................***********
....................************
...................*************
..................**************
.................***************
................****************
...............*****************
..............******************
.............*******************
............********************
...........*********************
..........**********************
.........***********************
........************************
.......*************************
......**************************
.....***************************
....****************************
...*****************************
..******************************
.*******************************
********************************
I think I should be banned from these ;D
Looks almost identical to mine...
...but a bit more inefficient...
...(you declare the function for every iteration rather than once outside the loop).
[SCRIPT] Starting Benchmark for doom's method:
[SCRIPT] ....*
[SCRIPT] ...**
[SCRIPT] ..***
[SCRIPT] .****
[SCRIPT] *****
[SCRIPT] Benchmark ended: 0.003
[SCRIPT] -
[SCRIPT] Starting Benchmark for iceflake's method:
[SCRIPT] Number of rows: 5
[SCRIPT] .....
[SCRIPT] ....*
[SCRIPT] ...**
[SCRIPT] ..***
[SCRIPT] .****
[SCRIPT] Benchmark ended: 0.004
[SCRIPT] -
[SCRIPT] Starting Benchmark for slc's method:
[SCRIPT] ....*
[SCRIPT] ...**
[SCRIPT] ..***
[SCRIPT] .****
[SCRIPT] *****
[SCRIPT] Benchmark ended: 0.007
[SCRIPT] -
I did the benchmarks, and I didn't expect to see this.Code: [Select] [SCRIPT] Starting Benchmark for doom's method:
[SCRIPT] ....*
[SCRIPT] ...**
[SCRIPT] ..***
[SCRIPT] .****
[SCRIPT] *****
[SCRIPT] Benchmark ended: 0.003
[SCRIPT] -
[SCRIPT] Starting Benchmark for iceflake's method:
[SCRIPT] Number of rows: 5
[SCRIPT] .....
[SCRIPT] ....*
[SCRIPT] ...**
[SCRIPT] ..***
[SCRIPT] .****
[SCRIPT] Benchmark ended: 0.004
[SCRIPT] -
[SCRIPT] Starting Benchmark for slc's method:
[SCRIPT] ....*
[SCRIPT] ...**
[SCRIPT] ..***
[SCRIPT] .****
[SCRIPT] *****
[SCRIPT] Benchmark ended: 0.007
[SCRIPT] -
You don't just benchmark one call. You make a big number of calls (like 1000000) then divide the execution time to this number of calls to get an accurate result (which is called an average weight).Quote from Doom_Kill3R on April 12th, 2017, 03:53 PM I did the benchmarks, and I didn't expect to see this.Code: [Select] [SCRIPT] Starting Benchmark for doom's method:
[SCRIPT] ....*
[SCRIPT] ...**
[SCRIPT] ..***
[SCRIPT] .****
[SCRIPT] *****
[SCRIPT] Benchmark ended: 0.003
[SCRIPT] -
[SCRIPT] Starting Benchmark for iceflake's method:
[SCRIPT] Number of rows: 5
[SCRIPT] .....
[SCRIPT] ....*
[SCRIPT] ...**
[SCRIPT] ..***
[SCRIPT] .****
[SCRIPT] Benchmark ended: 0.004
[SCRIPT] -
[SCRIPT] Starting Benchmark for slc's method:
[SCRIPT] ....*
[SCRIPT] ...**
[SCRIPT] ..***
[SCRIPT] .****
[SCRIPT] *****
[SCRIPT] Benchmark ended: 0.007
[SCRIPT] -
I did the benchmarks, and I didn't expect to see this.Code: [Select] [SCRIPT] Starting Benchmark for doom's method:
[SCRIPT] ....*
[SCRIPT] ...**
[SCRIPT] ..***
[SCRIPT] .****
[SCRIPT] *****
[SCRIPT] Benchmark ended: 0.003
[SCRIPT] -
[SCRIPT] Starting Benchmark for iceflake's method:
[SCRIPT] Number of rows: 5
[SCRIPT] .....
[SCRIPT] ....*
[SCRIPT] ...**
[SCRIPT] ..***
[SCRIPT] .****
[SCRIPT] Benchmark ended: 0.004
[SCRIPT] -
[SCRIPT] Starting Benchmark for slc's method:
[SCRIPT] ....*
[SCRIPT] ...**
[SCRIPT] ..***
[SCRIPT] .****
[SCRIPT] *****
[SCRIPT] Benchmark ended: 0.007
[SCRIPT] -
Code: [Select] function Generate(l) for (local f = function(p, l, x = 0, b = "") { while (++x <= l) b += x < (l-p) ? "." : "*"; return b; }, p = 0; p < l; ++p) print(f(p, l));
Christ, what a horrible coding style you have. Why do you have to make everything in one line when it looks so ugly.
Code: [Select] function kprint(val)
{
}
function PrintRowz(num)
{
//kprint("Number of rows: " + num);
//if (typeof(num) != "integer" || num < 1) return kprint("Rejected");
local Repeat = function (str, num)
{
local ret = "";
for (local i = 0; i < num; ++i) ret += str;
return ret;
}
for (local i = 0; i < num; ++i)
{
kprint(Repeat(".", num - i) + Repeat("*", i));
}
}
function Generate(l) for (local f = function(p, l, x = 0, b = "") { while (++x <= l) b += x < (l-p) ? "." : "*"; return b; }, p = 0; p < l; ++p) kprint(f(p, l));
function dum( maxStars ) {
if( maxStars ) {
for( local mainRun = 0; mainRun <= maxStars-1; mainRun++ ) {
local string = "";
for( local j = 1; j <= maxStars; j++ ) {
if( maxStars - j > mainRun ) {
string += ".";
}
else {
string += "*";
}
}
kprint( string );
}
}
}
local totalflake = 0;
local totaldoom = 0;
local totalslc = 0;
local iterations = 500;
for (local i = 1; i <= iterations; ++i)
{
local t, to;
print("Iteration " + i);
print("---");
print("Flake's method: ");
t = SqTimer();
PrintRowz(12);
to = t.Elapsed.Microseconds.tointeger();
print("Took " + to + " us");
totalflake += to;
print("Total counter for Flake: " + totalflake);
print("");
print("Doom's method: ");
t = SqTimer();
dum(12);
to = t.Elapsed.Microseconds.tointeger();
print("Took " + to + " us");
totaldoom += to;
print("Total counter for Doom: " + totaldoom);
print("");
print("SLC's method: ");
t = SqTimer();
Generate(12);
to = t.Elapsed.Microseconds.tointeger();
print("Took " + to + " us");
totalslc += to;
print("Total counter for SLC: " + totalslc);
print("");
print("---");
print("Iteration complete");
print("");
}
print("Total iterations: " + iterations);
print("Average for Flake: " + totalflake / iterations + " us");
print("Average for Doom: " + totaldoom / iterations + " us");
print("Average for SLC: " + totalslc / iterations + " us");
... I'm pretty sure you'll fall for the newbie trap when you do. And you'll get some misleading results.
But did you disable the console output? Because that takes more time than the code itself. That's the newbie mistake #1 when doing benchmarks of code that outputs something.
function kprint(v) return; // > dev/null
//---------------------------------------------------------------------------------------------------------------------
// warm up to avoid CPU throttling.
for (local n = 0, x = 0; n < 10000000; ++n) x = abs(n - x) % 2 * 2 / 2;
print("warm up complete");
//---------------------------------------------------------------------------------------------------------------------
function PrintRowz(num)
{
if (typeof(num) != "integer" || num < 1) return;
local Repeat = function (str, num)
{
local ret = "";
for (local i = 0; i < num; ++i) ret += str;
return ret;
}
for (local i = 0; i < num; ++i)
{
kprint(Repeat(".", num - i) + Repeat("*", i));
}
}
local tm = clock();
for (local n = 0; n < 10000; ++n) PrintRowz(32);
print("Flake: " + (clock() - tm));
//---------------------------------------------------------------------------------------------------------------------
function doom( maxStars ) {
if( maxStars ) {
for( local mainRun = 0; mainRun <= maxStars-1; mainRun++ ) {
local string = "";
for( local j = 1; j <= maxStars; j++ ) {
if( maxStars - j > mainRun ) {
string += ".";
} else {
string += "*";
}
}
kprint( string );
}
}
}
local tm = clock();
for (local n = 0; n < 10000; ++n) doom(32);
print("Doom: " + (clock() - tm));
//---------------------------------------------------------------------------------------------------------------------
function Generate(l) for (local f = function(p, l, x = 0, b = "") { while (++x <= l) b += x < (l-p) ? "." : "*"; return b; }, p = 0; p < l; ++p) kprint(f(p, l));
local tm = clock();
for (local n = 0; n < 10000; ++n) Generate(32);
print("SLC1: " + (clock() - tm));
//---------------------------------------------------------------------------------------------------------------------
const G_PERIOD_CONST = "...";
const G_ASTERIX_CONST = "********************************************************************************************************************************";
function Generate2(l) for (local p = 1; p <= l; ++p) kprint(G_PERIOD_CONST.slice(0, l-p) + G_ASTERIX_CONST.slice(0, p));
local tm = clock();
for (local n = 0; n < 10000; ++n) Generate2(32);
print("SLC2: " + (clock() - tm));
[USR] Flake: 2.75
[USR] Doom: 3.108
[USR] SLC1: 3.094
[USR] SLC2: 0.359
[USR] Flake: 2.312
[USR] Doom: 2.798
[USR] SLC1: 2.707
[USR] SLC2: 0.297
[SCRIPT] Flake: 3.043
[SCRIPT] Doom: 3.573
[SCRIPT] SLC1: 3.575
[SCRIPT] SLC2: 0.392
[SCRIPT] Flake: 2.441
[SCRIPT] Doom: 2.915
[SCRIPT] SLC1: 2.85
[SCRIPT] SLC2: 0.297
Not too fast.
Your script depends on you adjusting for length.
In which case:
function PrintRowz(num)
{
switch (num)
{
case 0: break;
case 1: print("*"); break;
case 2: print("*.\n**"); break;
case 3: print("*..\n**.\n***"); break;
}
}
And so on
And what's the next challenge? Because these are fun.
fast json bridge server <-> client side, at least it will be usefulQuote from happymint on April 12th, 2017, 07:19 PM And what's the next challenge? Because these are fun.
... at least it will be useful
const STARTMASK = "****************************************************************";
const DOTMASK = "................................................................";
function dgen( num ) {
local counter = num;
do {
local sz = DOTMASK.slice( 0, counter ) + STARTMASK.slice( 0, num - counter );
kprint( sz );
counter--;
} while( counter > 0 )
}
[USR] Flake: 2.77
[USR] Doom: 3.11
[USR] SLC1: 3.122
[USR] SLC2: 0.348
[USR] Doom2: 0.370
[USR] Flake: 2.287
[USR] Doom: 2.77
[USR] SLC1: 2.664
[USR] SLC2: 0.284
[USR] Doom2: 0.290
[USR] Flake: 2.728
[USR] Doom: 3.083
[USR] SLC1: 3.127
[USR] SLC2: 0.333
[USR] Doom2: 0.342
[USR] Flake: 2.271
[USR] Doom: 2.759
[USR] SLC1: 2.651
[USR] SLC2: 0.282
[USR] Doom2: 0.292
[USR] SLC3: 0.146
[USR] SLC3: 0.123
Code: [Select] function anik( num )
{
local dotstr = "", starstr = "";
for( local i = 0; i < num; ++i ) dotstr += ".";
for( local i = 0; i <= num; ++i ) print( dotstr.slice( 0, num - i ) + ( starstr += "*" ) );
}
There's really no other way of getting a better time than using constant tables and loops as @S.L.C mentioned above.
function dum( maxStars ) {
if( maxStars ) {
for( local mainRun = 0; mainRun <= maxStars-1; mainRun++ ) {
local string = "";
for( local j = 1; j <= maxStars; j++ ) {
if( maxStars - j > mainRun ) {
string += ".";
}
else {
string += "*";
}
}
kprint( string );
}
}
}
const G_PERIOD_CONST = "...";
const G_ASTERIX_CONST = "********************************************************************************************************************************";
function Generate(l) for (local p = 1; p <= l; ++p) kprint(G_PERIOD_CONST.slice(0, l-p) + G_ASTERIX_CONST.slice(0, p));
...
On the same note, I could also make a C++ plugin which would destroy squirrel at the execution time. The simple fact that your solution consists of 150 cases inside a switch renders it useless.Quote from EK.IceFlake on April 13th, 2017, 01:58 PM ...
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
function kprint(v) return; // > dev/null
// warm up to avoid CPU throttling.
for (local n = 0, x = 0; n < 10000000; ++n) x = abs(n - x) % 2 * 2 / 2;
const G_SPACE_CONST = " ";
const G_ASTERIX_CONST = "********************************************************************************************************************************";
local G_SPACE_POOL = array(G_SPACE_CONST.len());
local G_ASTERIX_POOL = array(G_SPACE_CONST.len());
for (local i = 0, n = G_SPACE_CONST.len(); i < n; ++i) {
G_SPACE_POOL[i] = G_SPACE_CONST.slice(0, i);
G_ASTERIX_POOL[i] = G_ASTERIX_CONST.slice(0, i);
}
function Generate(l) for (local p = 0.5; p <= (l/2); ++p) kprint(G_SPACE_POOL [(l/2) - p] + G_ASTERIX_POOL[p*2] + G_SPACE_POOL [p + (l/2)]);
local tm = clock();
for(local n = 0; n < 10000; ++n) Generate(32);
print("SLC: " + (clock() - tm));
It's getting quite repetitive butCode: [Select] function kprint(v) return; // > dev/null
// warm up to avoid CPU throttling.
for (local n = 0, x = 0; n < 10000000; ++n) x = abs(n - x) % 2 * 2 / 2;
const G_SPACE_CONST = " ";
const G_ASTERIX_CONST = "********************************************************************************************************************************";
local G_SPACE_POOL = array(G_SPACE_CONST.len());
local G_ASTERIX_POOL = array(G_SPACE_CONST.len());
for (local i = 0, n = G_SPACE_CONST.len(); i < n; ++i) {
G_SPACE_POOL[i] = G_SPACE_CONST.slice(0, i);
G_ASTERIX_POOL[i] = G_ASTERIX_CONST.slice(0, i);
}
function Generate(l) for (local p = 0.5; p <= (l/2); ++p) kprint(G_SPACE_POOL [(l/2) - p] + G_ASTERIX_POOL[p*2] + G_SPACE_POOL [p + (l/2)]);
local tm = clock();
for(local n = 0; n < 10000; ++n) Generate(32);
print("SLC: " + (clock() - tm));
.. just a simple adaptation
(https://i.gyazo.com/c727a3908492a87c2b9236343bd2afe2.png)
function StarPyramid( rows )
{
local star=0, val1 = "", val2 = "";
for( local i = 1;i <= rows; i++, star = 0, val1 = "", val2 = "")
{
for(local space = 1; space <= rows-i; space++) val1 += " ";
while(star != i-1)
{
val2 += "* ";
++star;
}
print(val1 + val2);
}
}
None of your code printed the desired output. Both of you fail haha!
The only difference is the incremental stars. I'd say it does the job. |
print(" * ");
print(" * * ");
print(" * * * ");
print(" * * * * ");
print(" * * * * * ");
print(" * * * * * * ");
print(" * * * * * * * ");
print(" * * * * * * * * ");
print(" * * * * * * * * *");
Code: [Select] print(" * ");
print(" * * ");
print(" * * * ");
print(" * * * * ");
print(" * * * * * ");
print(" * * * * * * ");
print(" * * * * * * * ");
print(" * * * * * * * * ");
print(" * * * * * * * * *");
Code: [Select] print(" * ");
print(" * * ");
print(" * * * ");
print(" * * * * ");
print(" * * * * * ");
print(" * * * * * * ");
print(" * * * * * * * ");
print(" * * * * * * * * ");
print(" * * * * * * * * *");
None of your code printed the desired output. Both of you fail haha!
Code: [Select] print(" * ");
print(" * * ");
print(" * * * ");
print(" * * * * ");
print(" * * * * * ");
print(" * * * * * * ");
print(" * * * * * * * ");
print(" * * * * * * * * ");
print(" * * * * * * * * *");
print(@"
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *");
One-line delegate abuse (two lines if counting the array).(click to show/hide) Code: [Select] local arrayTest = ["test1","test2","test3","test4"];
// .apply replaces each value in an array with the returned value.
//
// This is a cheaty way of printing stuff without loops and we need to
// return the original value to avoid filling the array with nulls.
arrayTest.apply(function(v) { print(v); return v; });
One-line delegate abuse featuring lambdas:(click to show/hide) Code: [Select] // Squirrel internally clones the array and uses the return value from map to
// fill the cloned array.
//
// If you were to look at the returned array from .map, you'd see a bunch
// of nulls, as we never return anything.
arrayTest.map(@(v) print(v));
arrayTest.apply(print);
Sorry for the bump but...Quote from Stormeus on June 10th, 2015, 06:16 PM One-line delegate abuse (two lines if counting the array).(click to show/hide) Code: [Select] local arrayTest = ["test1","test2","test3","test4"];
// .apply replaces each value in an array with the returned value.
//
// This is a cheaty way of printing stuff without loops and we need to
// return the original value to avoid filling the array with nulls.
arrayTest.apply(function(v) { print(v); return v; });
One-line delegate abuse featuring lambdas:(click to show/hide) Code: [Select] // Squirrel internally clones the array and uses the return value from map to
// fill the cloned array.
//
// If you were to look at the returned array from .map, you'd see a bunch
// of nulls, as we never return anything.
arrayTest.map(@(v) print(v));???Code: [Select] arrayTest.apply(print);
and we need to return the original value to avoid filling the array with nulls.
Code: [Select] function a(b){c<-[(1<<63)-1];b.map(function(d){foreach(e,f in c)if(f>=d){c.insert(e,d);break}});c.remove(c.len()-1);return c}
function a( b )
{
c <- [ ( 1 << 63 ) - 1 ];
b.map( function( d )
{
foreach( e, f in c )
if( f >= d )
{
c.insert( e, d );
break
}
} );
c.remove( c.len() - 1 );
return c
}