Author Topic: [CHALLENGE] Rosetta Code - "Tasks not implemented in torquescript"  (Read 3569 times)

Going to do the renamefile one now.

Ninja: Finished! :D

Double ninja edit: I did A+B as well. :) so easy.
« Last Edit: June 17, 2012, 11:12:10 PM by Ipquarx »

Here is the 100 doors one

Code: [Select]
function HundredDoors()
{
    for(%a=1;%a<101;%a++)
    {
        %b = 0;
        while(%b < 100)
        {
            %b += %a;
            if($Door[%b] $= "open")
                $Door[%b] = "closed";
            else if($Door[%b] $= "closed" || $Door[%b] $= "")
                $Door[%b] = "open";
        }
    }
}
Where %a represents the pass number, %b represents the door number, and $Door[%b] represents the status of that door.

If the function included an optional process to list the final status of all 100 doors at the end of the function, local variables could be utilized and the code would read as follows:
Code: [Select]
function HundredDoors()
{
    for(%a=1;%a<101;%a++)
    {
        %b = 0;
        while(%b < 100)
        {
            %b += %a;
            if(%door[%b] $= "open.")
                %door[%b] = "closed.";
            else if(%door[%b] $= "closed." || $Door[%b] $= "")
                %door[%b] = "open.";
        }
    }
    for(%c=1;%c<101;%c++)
        echo("Door " @ %c @ " is " @ %door[%c]);
}

Also finished the factorial example, with a recursive and iterative code shown.


Going to do the renamefile one now.

Ninja: Finished! :D

You did this one? http://rosettacode.org/wiki/Rename_a_file#TorqueScript
FileCopy and FileDelete are functions added by Badspot. You should probably remove that.

I'll leave this one for Zacko.

http://rosettacode.org/wiki/Time_a_function

I've got this:
Code: [Select]
/////////////////
/// Benchmark ///
/////////////////
function benchmark(%times,%function,%a,%b,%c,%d,%e,%f,%g,%h,%i,%j,%k,%l,%m,%n,%o)
{
if(!isFunction(%function))
{
warn("BENCHMARKING RESULT FOR" SPC %function @ ":" NL "Function does not exist.");
return -1;
}

%start = getRealTime();

for(%i=0; %i < %times; %i++)
{
call(%function,%a,%b,%c,%d,%e,%f,%g,%h,%i,%j,%k,%l,%m,%n,%o);
}

%end = getRealTime();

%result = (%end-%start) / %times;

warn("BENCHMARKING RESULT FOR" SPC %function @ ":" NL %result);

return %result;
}

///////////////
/// EXAMPLE ///
///////////////
function example(%var1,%var2)
{
//put stuff here
}

benchmark(500,"example","blah","variables");

Can you post that for me? I've used it for troubleshooting and it works great.

I'll leave this one for Zacko.

http://rosettacode.org/wiki/Time_a_function
Code: [Select]
function timeFunction(%function)
{
%start = getSimTime();
eval(%function);
%end = getSimTime();
echo(%function SPC "took" SPC (%end - %start) SPC "milliseconds to complete.");
}
?

EDIT: Greek's is far more superior, haha.
« Last Edit: June 18, 2012, 01:09:32 AM by Daenth »

Code: [Select]
function timeFunction(%function)
{
%start = getSimTime();
eval(%function);
%end = getSimTime();
echo(%function SPC "took" SPC (%end - %start) SPC "milliseconds to complete.");
}
?

EDIT: Greek's is far more superior, haha.

This wouldn't work anyway. getSimTime measures how many milliseconds the engine has been simulating so far. It doesn't simulate while the main thread (well really, the only thread) is running TorqueScript code.

This wouldn't work anyway. getSimTime measures how many milliseconds the engine has been simulating so far. It doesn't simulate while the main thread (well really, the only thread) is running TorqueScript code.
Ah, that makes sense.

25% of those tasks should be not considered.

25% of those tasks should be not considered.
because they aren't possible?
because they are too stupid?

what do you mean

because they aren't possible?
because they are too stupid?

what do you mean
Quote
Not Considered

These tasks are not generally counted toward "unimplemented", as somebody, somewhere, decided that TorqueScript was inappropriate for these tasks, or implementations of them would be unenlightening. Feel free to try your hand at them anyway.
"OpenGL"
"Random number generator (device)"
etc.

MD5 and a lot of other things on the page can't be natively implemented because Torque's number system is too stuff for them (you can however use hacks to have bigger numbers with strings, but you won't be able to have a clean snippet to post), so they should also be not considered.
« Last Edit: June 18, 2012, 03:10:02 AM by Destiny/Zack0Wack0 »

Two implementations of "Fibonacci sequence":

Code: [Select]
function fibIter( %n )
{
    if ( %n < 2 )
    {
        return %n;
    }
   
    %fibPrev = 1;
    %fib = 1;
   
    for ( %num = 2 ; %num < %n ; %num++ )
    {
        %_fibPrev = %fibPrev;
        %fibPrev = %fib;
        %fib = %fib + %_fibPrev;
    }
   
    return %fib;
}

function fibRec( %n )
{
    if ( %n < 2 )
    {
        return %n;
    }
    else
    {
        return fibRec( %n - 1 ) + fibRec( %n - 2 );
    }
}

"Repeat a string":

Code: [Select]
function stringRepeat( %string, %times )
{
    %result = "";
   
    for ( %i = 0 ; %i < %times ; %i++ )
    {
        %result = %result @ %string;
    }
   
    return %result;
}

"Real constants and functions"

  • E: Not available.
  • Pi: $pi
  • Square Root: mSqrt(x)
  • Natural Logarithm: Not available.
  • Base 10 Logarithm: mLog(x)
  • Exponential: Not available.
  • Absolute Value: mAbs(x)
  • Floor: mFloor(x)
  • Ceiling: mCeil(x)
  • Exponentiation: mPow(x, y)
  • Exponentiation Modulo: Not available.

"Assertion":

Code: [Select]
if ( !expression )
{
    // possibly display an error
    // error( x );
    
    return;
    // instead, possibly return a value to parent function to abort as well
    // return x;
}



"Add a variable to a class instance at runtime":

x.y = z;



"Higher-order functions":

Code: [Select]
function first( %x )
{
    return call( %x );
}

function second()
{
    return "foobar";
}

==>echo( first( second ) );
foobar




"Include a file":

exec("path/to/file.cs");



"Singletons":

Code: [Select]
singleton ShaderData( SSAOShader )
{  
    DXVertexShaderFile = "shaders/common/postFx/postFxV.hlsl";
    DXPixelShaderFile = "shaders/common/postFx/ssao/SSAO_P.hlsl";            
    pixVersion = 3.0;
};



"Reverse a string":

Code: [Select]
function stringReverse( %string )
{
    %result = "";
    
    for ( %i = strLen( %string ) - 1 ; %i >= 0 ; %i-- )
    {
        %result = %result @ getSubStr( %string, %i, 1 ),
    }
    
    return %result;
}



"Binary string":

"\xFA\x62"
« Last Edit: June 18, 2012, 04:13:09 AM by Port »