Tag Archives: coding

Trying Too Hard

In a post titled The Problem with the FizzBuzz Problem, Gayle Laakmann McDowell discussed the cognitive trap that a high performer can fall into when faced with a deceptively inelegant real world problem. She called it the “Smart Person’s Mirage.”

I was caught in this trap in an interview once myself. I was asked to write a function that took an integer parameter and wrote out an ascii art diamond pattern in the given size, like this:

  *  
 *** 
*****
 *** 
  *  

The symmetry cried out to me. There must be some way, I was sure, to leverage it in both directions at once. It seemed like an affront to the concept of elegance if I didn’t! Also, there were so many options. Use a buffer, or several, or none? Make nested loops? How should an even parameter be handled?

In a matter of minutes, I had pieces of at least three conflicting solutions scattered all over the whiteboard. That was when the interviewer let me know that that was exactly what the question was designed to test for.

The way out of this failure mode is to not try too hard. As tempting as it is to write code poetry and dazzle my interviewer, the bottom line is that it’s not required. Moving forward with something, anything, that actually completes the task is. Optimizaton, neatness and elegance (and sometimes insight) can come later.

Inspired by Gayle’s post, I revisited the ascii art diamond problem. By letting go of finding a “perfect” solution, I came up with a workable one in a couple of minutes. Here it is in all its kludgy, messy glory.

    /* diamond.c
     * utility to take an integer on the command line and
     * output an ascii art diamond in that size made of splats
     */
    
    #include <stdio.h>
    #include <string.h>
    #include <stdarg.h>
    
    #define MAX_DIMENSION 41
    #define DEFAULT_DIMENSION 5
    
    int main (int argc, char **argv) {
    	char line[MAX_DIMENSION];
	static char on_char = '*';
	static char off_char = ' ';
	int dimension = DEFAULT_DIMENSION;
	int midpoint;
	int lineno = 1;
	int columnno = 1;
	int i, j;

	if (argc > 1) {
		dimension = atoi (argv[1]);
		/* even dimensions end up being treated as the
		 * next highest odd dimension (ex. 4 makes a 5x5)
		 */
    /*		if (dimension % 2 == 0)
			dimension += 1; */
		if (dimension > MAX_DIMENSION)
			dimension = DEFAULT_DIMENSION;
	}

	memset (line, off_char, MAX_DIMENSION * sizeof (char));
	midpoint = dimension / 2 + 1;

	/* print top half of lines to midline */
	for (i = 0; i < midpoint; i++) {
		printf ("%s\n", line);
		line[midpoint - i] = on_char;
		line[midpoint + i] = on_char;
	}
		
	/* print bottom lines */
	for (; i > -1; i--) {
		line[midpoint - i] = off_char;
		line[midpoint + i] = off_char;
		printf ("%s\n", line);
	}
    		
	return 0;
    }
Advertisements

New Update to Xtra Screen Hacks

It’s been quite a while since I added anything new to my collection of screen demos. There are no useable new hacks yet, but the new color cycling maps for the versatile Psychedelic hack are just as good.

Here’s a view of the plasma fractal map:

plasma0

And the “bumpy” map:

bumpy0

And the Stripes map:

stripes0

I added the Circle map as an afterthought, but it’s turned out to be my favorite:

circle0

Don’t worry, the demos play nicely with XScreenSaver once they’re installed:

configurator

Clone from Github and enjoy!

clone url: https://github.com/shegeek/xtrascreenhacks.git

Fizzbuzz in bash

I’ve been able to get by in bash for a long time. I could read it just fine, but if I wanted to write it, I needed a cheat sheet by my side. I’ve decided that that wasn’t good enough–I should be actively practicing it, so that I know it off the top of my head.

Here’s a fizzbuzz implementation.


#!/usr/bin/env bash

topval=${1:-100}

for i in $(seq 1 $topval);
do
	if [ "$(($i % 15))" -eq "0" ]
	then
		echo fizzbuzz!
	elif [ "$(($i % 3))" -eq "0" ]
	then
		echo fizz!
	elif [ "$(($i % 5))" -eq "0" ]
	then
		echo buzz!
	else
		printf '%d\n' $i
	fi
done


Neat less Tricks

If you forget which file you’re paging (I do all the time), hit ‘=’.

If you always (or usually) want line numbers, add this to your .bashrc file:

# make less show line numbers
export LESS='-N'

This sets the environment variable that less reads, assigning it the options that you want. -N and -n toggle the line numbers display. They work while you’re already in less, too (with the dash).

If you have vi/vim on your system, hitting ‘v’ (no dash) while paging with less will drop you into it. That way, you can page to the location in the file you want to edit and not worry about hitting random keys on the way.

In fact, vi navigation commands (hjkl) work in less, too.

Search for terms with the slash, like this:

/swap

Your search terms will be highlighted:
vimsearch
less will search forward (‘?’ searches backward), and search terms are case sensitive. The arrow keys will let you scroll back through your previous searches.

If you can’t remember any of this while you’re paging, hit ‘h’ (no dash) and less will display its help screen. To get back to your file, hit ‘q’ (although you don’t need me to tell you that, because it’s on the help screen :-)).