Tag Archives: actionscript

Prefix Notation (Lisp Style)

Today's quiz is not multiple choice. Instead, your task is to write a lisp style math parser. This may sound tricky, but it's surprisingly simple. (well... not simple exactly, it's just simple compared to what one might assume).

Lisp uses prefix notation... where the operator is placed before the operands:

10 * 10

becomes:

* 10 10

You could think of this as a function "*" with two arguments (10, 10). In Lisp this is enclosed with parens:

(* 10 10)

Let's see a few more examples:

100 / 2 + 10

becomes:

(+ (/ 100 2) 10)

...
2 * 4 * 6 * 7

becomes:

(* 2 4 6 7)

...

(2 + 2) * (10 - 2) * 2

becomes

(* (+ 2 2) (- 10 2) 2)

Remember, thinking "functions" really helps. The above can be though of as:

multiply( add(2, 2), subtract(10 , 2), 2)

You should create a function called parsePrefix() that takes a string and returns a number:

Here is some code to test if your parser works properly:

Actionscript:
  1. trace(parsePrefix("(* 10 10)"));
  2.  
  3. trace(parsePrefix("(* 1 (+ 20 2 (* 2 7) 1) 2)"));
  4.  
  5. trace(parsePrefix("(/ 22 7)"));
  6.  
  7. trace(parsePrefix("(+ (/ 1 1) (/ 1 2) (/ 1 3) (/ 1 4))"));
  8.  
  9. /* Should trace out:
  10. 100
  11. 74
  12. 3.142857142857143
  13. 2.083333333333333
  14. */

I highly recommend giving this a try, it was one of those cases where I assumed it would be much trickier than it was.

I've posted my solution here.

Posted in Math, QuickBox2D, Quiz | Also tagged , , , | Leave a comment

AS Quiz #15 (Hardest Quiz Yet)

This is probably the hardest quiz yet. After yesterdays very easy quiz I figured I'd do something really trickey... binary is always a good way to make a quiz hard... so most of the questions are about binary operators and ByteArray... there is also one question about Bezier math.

Number of Questions : 7
Difficulty : Hard
Topic : Binary and Bezier

Which random sequence would you associate the below snippet with?

for (var i:int = 0; i<4; i++){
  trace(int(Math.random()*2) - 1 | 1);
}






Which of the below sequences would you associate with the following snippet?

for (var i:int = 0; i<10; i++){
  trace(i & 2);
}
You may want to work this out on paper






A quadratic Bézier curve is the path traced by the function B(t), given points P0, P1 and P2.

The above is from wikipedia.

Which of the below lines of actionscript accurately represents the B(t) function? Hint: Try working it out yourself and then choosing the answer that is closest to your own. You can use this template over at wonderfl to help you work it out:





What is the length of the following ByteArray?

var bytes:ByteArray = new ByteArray();
bytes.writeInt(0xFFFFFF);





What is the length of the following ByteArray?

var bytes:ByteArray = new ByteArray();
bytes.writeInt(0xFFFFFF);
bytes.writeUnsignedInt(0xFFFFFF);







How many bytes does it take to represent a short in a ByteArray?





What will the following snippet trace to the output window?

var bytes:ByteArray = new ByteArray();
bytes[0] = 0;
bytes[1] = 0;
bytes[2] = 1;
bytes[3] = 1;

bytes.position = 0

trace(bytes.readInt());







Posted in Quiz | Also tagged , , | 3 Comments

AS Quiz #14

Today's quiz is about TextFields.

Number of Questions : 6
Difficulty : Easy
Topic : TextFields

Please go to AS Quiz #14 to view the quiz

Posted in Quiz | Also tagged , , | 1 Comment

AS Quiz #13

Today's quiz is not multiple choice. Instead, your task is to draw a spiral using a recursive function.

Optionally you can alter your function to draw other types of spiral forms:

(You can view various solutions in the comments - as well as my solution at wonderfl)

Posted in Quiz | Also tagged , , | 6 Comments

AS Quiz # 12

Today's quiz is not multiple choice. Instead, use your choice of the Graphics class or BitmapData to write a function that generates the below image:

Your function need not take any arguments, it need only return a display object containing the above image.

Feel free to post your solution in the comments.

[EDIT] Solutions:

The solutions that have been posted in the comments so far are REALLY nice - they're all worth checking out... and I think everyone posted their code to wonderfl so you can see the result without needing to open flash....

My two solutions:
The first solution is a variation on an example from the book I wrote with Rich Shupe:
First Solution

The next solution is actually in an old post from this site - it uses setPixel and HSV - RGB conversion:
Second Solution

Thanks to everyone who posted solutions so far - I'm surprised again by how many people participate in this format of quiz.

Posted in Quiz | Also tagged , , | 14 Comments

AS Quiz #11

Today's quiz jumps around from topic to topic, touching on filters, events, BitmapData and more...

Number of Questions : 6
Difficulty : Medium
Topic : Miscellaneous

Please go to AS Quiz #11 to view the quiz
Posted in Quiz | Also tagged , , | 6 Comments

AS Quiz #10

Today's quiz is about Events and OOP.

Number of Questions : 5
Difficulty : Easy
Topic : Events and OOP

Please go to AS Quiz #10 to view the quiz
Posted in Quiz | Also tagged , , | 3 Comments

AS Quiz #9

There were lots of great submissions to yesterdays quiz. If you haven't seen them, check them out here...
Click here to see two solutions two solutions to yesterdays quiz...

Today's quiz is medium level and covers a few different topics...

Number of Questions : 6
Difficulty : Medium
Topic : Graphics Class and more

Please go to AS Quiz #9 to view the quiz
Posted in Quiz | Also tagged , , | 5 Comments

AS Quiz #8

Today's quiz is not multiple choice. Instead, your task is to write a function that draws stairs that look like this:

Your function should have the following arguments:

drawStairs(graphics, stairNum);
// you may include additional arguments for size, depth etc..

Feel free to post your solution in the comments.

I'll post my solution for this in the comments tomorrow. There is also another multiple choice quiz in the pipeline for tomorrow...

BONUS: Try to use as few Graphics class method calls as possible.

You can see my solutions here.

Posted in Graphics, Quiz | Also tagged , , | 19 Comments

AS Quiz #7

This quiz is about sound and gradients...

Number of Questions : 5
Difficulty : Medium
Topic : Sound and Gradients

Please go to AS Quiz #7 to view the quiz
Posted in Quiz | Also tagged , , | 4 Comments