my exercises

16 views
Skip to first unread message

Ryan Davis

unread,
Jul 28, 2015, 10:31:24 PM7/28/15
to Seattle.rb Study Group
I'd like to make some challenges based on using my graphics library.

## Beginner:

WITHOUT looking at the examples, do the following (in phases):

1) Draw a ball on the screen in a random location.
2) Make the ball move over time.
3) Apply a constant downward force of "gravity" to the ball.
4) Make hitting the floor or wall slow the ball down a bit.
5) Add many balls and make them do the same thing

In the end, the code should be well-factored and have a clean object-oriented design.

The code should be performant for up to 500 balls

## Intermediate:

Add basic collision detection to the bouncing balls simulation. Again, it should still be well-factored and performant (tho obviously slightly less so).

Tip: you can use the "fps" call to calculate your frames per second.

## Advanced:

Give the balls actual behavior beyond responding to gravity. Possible examples include:

+ Make one the leader and have the others follow it around.
+ Make one a zombie. Any normal ball it touches becomes a zombie.
+ Make one randomly shoot. Any ball hit dies.

Scott Windsor

unread,
Jul 28, 2015, 10:52:51 PM7/28/15
to seattle...@googlegroups.com
Here's mine:

My exercises:

## Rainbow parens (beginner/intermediate)


Given some LISP code, we'd like to re-format it such that we colorize matching parenthesis, brackets, and square braces, and alternate those colors. This gives us a rainbow when it's all matched. If you run into unbalanced parenthesis, raise and error to let the caller know the code isn't properly formatted.

Valid parens:
"([{()}()}])"

Invalid parens:
"([{()}()}]"

Lisp code also contains other atoms as well, so we want to preserve those when we return the results.

Valid Lisp:
"(define square (lambda (x) (* x x)))"

Invalid Lisp:
"(define square (lambda (x) (* x x))"

How do you print colors on the terminal you ask? You can use escape sequences. On ANSI/VT100 terminals and terminal emulators you can first print an escape sequence, then an escape code to indicate the text formatting.

echo -e "\e[32mGreen"
echo -e "\e[33mYellow"
echo -e "\e[34mBlue"
echo -e "\e[35mMagenta"
echo -e "\e[36mCyan"

So If I had the code
(define (lambda (x) (add5 (+ 5 x)))

It would give us:
\e[31m(\e[0mdefine \e[32m(\e[0mlambda \e[33m(\e[0mx\e[33m)\e[0m \e[34m(\e[0madd5 \e[35m(\e[0m+ 5 x\e[35m)\e[0m\e[34m)\e[0m\e[32m)\e[0m

You can test in your terminal by running "echo -e" and the string above in quotes.


## Cheating at Scrabble (intermediate/advanced)

I'd like you to help me cheat at scrabble. I'm not very good at coming up with various works from my tiles, so I'd like you to write some code to give me all of the possible words that can be created given a set of tiles.

On most unix systems, you can find a word dictionary in "/usr/share/dict/words" - let's assume that all of these words (including "zymogenic"!) are up valid for scrabble. 

Javier Soto

unread,
Jul 28, 2015, 10:56:43 PM7/28/15
to Seattle.rb Study Group, zens...@gmail.com
My ideas:

#1 Code an algorithm 2 ways: Recursive and Iterative (from SICP)
     Examples are Fibonacci, Factorial or Multiplication using Addition.
     It is easy but I think it has good insights (i.e. analyze the differences)
     Extra points I. Find how to and implement tail recursion in Ruby. Compare with other 2 solutions
     Extra points II. If we do Fibonacci, code it in log time (SICP 1.19)

#2 Coin change algorithm
     Classic, annoying, but very useful (just did it in the SICP book and as a newbie I attest that it is useful)
     Extra points -> Golf it!

#3 Atari's Pong.
     Require graphics. A single ball/point inside a box, bounces against the walls.
     Minimum: the ball hits the wall and shows up in the opposite wall (like Asteroids)
     Intermediate: the ball hits the wall and it deflects with the right angle.
     Advanced: Add a paddle (horizontal movable wall) and play Pong!!

Other Ideas (I haven't done them)

Towers of Hanoi. An intro to recursion

NQueens. Cannot help it, I need to think how to solve it in Ruby.

Scott Windsor

unread,
Aug 5, 2015, 11:53:33 AM8/5/15
to seattle...@googlegroups.com, Ryan Davis
I posted readme & first problem.

Feedback before I send to the main list?

- scott

--
You received this message because you are subscribed to the Google Groups "Seattle.rb Study Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to seattlerb-stu...@googlegroups.com.
To post to this group, send email to seattle...@googlegroups.com.
Visit this group at http://groups.google.com/group/seattlerb-study.
For more options, visit https://groups.google.com/d/optout.

Aja Hammerly

unread,
Aug 5, 2015, 12:02:19 PM8/5/15
to seattle...@googlegroups.com
Looks great!


Javier Soto

unread,
Aug 5, 2015, 12:04:08 PM8/5/15
to Seattle.rb Study Group, zens...@gmail.com
I concur. Go for it!

Javier Soto

unread,
Sep 2, 2015, 12:45:17 PM9/2/15
to Seattle.rb Study Group, zens...@gmail.com
Hi Aja and Scott, here is another idea for future weeks: 


Pascal Triangle

What is a Pascal Triangle? For the 1% of the mathematically inclined it is just "a triangular array of the binomial coefficients". For us, the 99% of mere mortals, we know a Pascal Triangle when we see it:



Wikipedia Link

This week's exercise is to generate a Pascal Triangle two ways:

  • iteratively, a.k.a. non recursively (go figure), carry forward your results as you compute the triangle.
  • recursively, with a method that calls itself again and again.

If dutifully inclined spend a minute to think about the costs and benefits of each approach.

Extra Points:

Pat in the Back: Google how to implement tail recursion in Ruby (or language of your choice). See if it can be applied to your recursive code.

Standing Ovation: Take a Pascal Triangle, will travel. Can you think of other ways to use PTs in a freaky weird way? One idea pops to mind, use Pascal Triangles + graphics gem to create fractals (hint: Sierpinski).
Reply all
Reply to author
Forward
0 new messages