Jul 14

3Monkeys on Emacs: Part 6 – Some Fun

Published in Emacs, Fun, Programming, Software by 3Monkeys | 2 comments »

Previous in this series : Part 5 – Replace

Sometimes you need to step back and have a little fun. In keeping with that philosophy, I’m taking a small break from some of the more serious Emacs topics, and presenting a few fun things. Emacs has several built in stress relievers, from funny quotes to interactive games. Below are some diversions for your enjoyment.

Yow M-x yow A simple diversion that displays a random Zippy quote in the echo area.
Doctor M-x doctor Need a little psychotherapy? Call in the doctor.
Life M-x life Conway’s Game of Life simulation. Still a fascination after all of these years.
Hanoi M-x hanoi Solves the Tower of Hanoi puzzle. To specify the number of rings us C-u number M-x hanoi, where number is the desired number of rings.
Mpuz M-x mpuz A multiplication puzzle where numbers are replaced with letters, the user must determine which numbers map to which letters.
Blackbox M-x blackbox An analytical puzzle where the user must deduce the locations of balls based on certain reactions to events. See the full documentation with this command C-h f blackbox.
Pegs M-x solitaire The classic peg jump game, where the user jumps one peg with a neighboring peg removing it in the process. The goal is to leave only one peg.
Snake M-x snake The user navigates a snake in a square box, avoiding doubling back on itself or running into a wall, all while trying to collect tokens.
Tetris M-x tetris The classic Tetris game we all are familiar with in one form or another.
Adventure M-x dunnet A text based adventure game much like Zork. We all remember playing Zork on PDP-10′s right?

Until next time.

-3Monkeys

Popularity: 27% [?]

  • DZone
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Slashdot
  • Digg
  • Reddit
  • NewsVine
  • SphereIt
  • e-mail
  • Facebook
  • Google Bookmarks
  • Live
  • Propeller
1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (4 votes, average: 7.25 out of 10)
Loading ... Loading ...
Jul 12

Fibonacci Series in One Line of Perl

Published in Fun, Golf, Perl, Programming by 3Monkeys | 66 comments »

Not ground breaking but here are the first 20 terms in the Fibonacci Series.

Code
perl -e'@p=(0,1);until($#p>20){print"$p[-2]\n";push @p,$p[-2]+$p[-1]}'
Output

0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181

Can you do better? What about different languages?

Update: it has been suggested that for languages such as Java and C to be included in this experiment, that only the total number of pure source characters be included in the count. Therefore my perl solution in the comments below has 56 pure source characters.

For more information on the Fibonacci Series see http://en.wikipedia.org/wiki/Fibonacci_number

-3Monkeys

Popularity: 69% [?]

  • DZone
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Slashdot
  • Digg
  • Reddit
  • NewsVine
  • SphereIt
  • e-mail
  • Facebook
  • Google Bookmarks
  • Live
  • Propeller
1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (7 votes, average: 5.86 out of 10)
Loading ... Loading ...
Jul 12

3Monkeys on Emacs: Part 5 – Replace

Published in Emacs, Programming, Software by 3Monkeys | 6 comments »

Previous in this series : Part 4 – Search

In my last post I discussed searching, frequently when searching you are looking for something to replace. As with search Emacs offers rich replace commands as well.

Unconditional and Conditional Replace

Unconditional – The command for basic replace is M-x replace-string. This command will prompt you for a target string and a destination string. When executed, it will unconditionally replace all occurrences of the target string with the destination string, from the cursor to the last occurrence of the target string. Two things should be noted. First this command does not have a reverse or backward mode. Second, a source string containing no uppercase letters will be treated as case insensitive.

Conditional – For the more paranoid and the case where only certain occurrences should be replaced, Emacs provides the command M-x query-replace or M-%. Similar to replace-string, this command prompts for a target string and a destination string. When executed the first occurrence of the target string is highlighted and the cursor is placed at its end. At this point the user has a few options to replace or not replace this occurrence, to move to the next occurrence, to unconditionally replace all remaining instance,or abort the command entirely. All of these options are specified by a single keystroke as described in the table below.

Key Effect
SPACE Replace this occurrence and move to the next
DEL Skip this occurrence and move to the next
, Replace this occurrence and do not move on
! Unconditionally replace all remaining occurrences
^ Backup to the previous matching occurrence
ESC Exit query-replace

These two commands will work for most needs, however, we can get more power from the regexp versions.

Regexp Replace

Like the non-regexp versions, the regexp versions come in both conditional, M-x query-replace-regexp and unconditional, M-x replace-regexp flavors. Regular expressions are specified in the same manner as search regular expressions covered in the previous article. What comes in handy is the ability to use prior group construct to build your destination string. For example, suppose you want to change a string such as “Name: lastname, firstname” to “Dear firstname lastname,“. This can easily be accomplished with a target regexp of

Name: +([A-Za-z]+), +([A-Za-z]+)

and a destination string of

Dear 2 1,

Bonus: Narrowing and Widening

One particular method for restricting replace is to narrow the buffer. By narrowing a buffer you limit the portion of the buffer that you can currently view and edit. This can especially be useful when performing a replace operation. For example you may want to replace a variable name with in a particular function. You would first narrow the buffer to the function in question and then perform an unconditional replace operation. This would be much safer and possibly faster than a conditional replace operation.

Narrowing a buffer is comprised of two basic steps. First mark the region you want to narrow to using the M-x set-mark-command, C-SPACE or C-@, and then repositioning the buffer to the beginning or ending of the region of interest. Once a region is defined issue the M-x narrow-to-region or C-x n n command to narrow the buffer. Now any editing actions you take will be applied to the narrowed buffer only, with the exception of M-x save-buffer and like commands which will save the entire buffer. To continue editing the full document you must widen it with the M-x widen or C-x n w command.

Next in this series: Part 6 – Some Fun

Until next time.

-3Monkeys

Popularity: 28% [?]

  • DZone
  • StumbleUpon
  • Technorati
  • del.icio.us
  • Slashdot
  • Digg
  • Reddit
  • NewsVine
  • SphereIt
  • e-mail
  • Facebook
  • Google Bookmarks
  • Live
  • Propeller
1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 8.00 out of 10)
Loading ... Loading ...