I’ve been sitting on this one for a while but I think it’s time. The idea is simple: sort the numbers so it looks like a pyramid: smaller numbers on the outside and alternating sides, the largest number in the middle.
Working with odd-numbered arrays seems the only way to make it work but any positive number should behave the same when sorting. I’m going to keep two markers, one for the left side and one for the right, that are going to keep moving inward as the array is sorted. Big-O notation is going to be slow since my initial idea is going to move through the array once per value in the array: 5 values needs 5 runs through the array. O(n^n)
The design is for a psort() class to do the work and a main() to send appropriate arrays to it. Someday I’ll have to learn how to use JUnit testing (in Java) to ensure it behaves as expected but for now I’ll do the hard way.
The pyramids of Egypt are some of the most fascinating structures in the world. To honor the great pyramids we’ll be introducing pyramid sort. Pyramid sort works so that the highest numbers are in the center of the array and the lowest are on the edges. When comparing 2 numbers the smaller number goes on the left. So if the array contains 1,2,3 then 1 goes on the left with 2 on the far right and 3 in the middle.
Here are 2 more examples:
Easy peasy, even got it on the first compile (honestly that’s not something to brag about). There’s an interesting wrinkle for today’s (the original problem is actually yesterday’s problem) version that is to minimize the character count by 25%. Eclipse doesn’t have a built-in character counter so I’m going to try to use the file size listed in WinExplorer. Current size is 1,168 bytes; my goal is 75% of that or 876 bytes. This is a neat addition since sometimes (embedded systems, mobile) size is a concern. Not as much as years ago when a computer only had 256kb of memory space but sure let’s be retro.
I removed all the white space I could (spaces, line breaks), the javadoc comments, the regular comments, and put all the if-statements on one line. This reduced the file size to 345 bytes! This puts the total at 30% of the original file size. Quite a shrink but some of the comments should probably be in there still and it’s more difficult for a human to read now. Ideally this could be minimized before compile time so the useful information sticks around somewhere so the next poor bastard who has to use the code has some idea of what was going on.
Building on yesterday’s problem, take your solution and cut the amount of code used by at least 25%. That means that if your solution was 100 characters your new one should be no more than 75 characters. New line, tab, and space characters all count towards your character total.
This is similar to the “FizzBuzz” problem that seems to be pretty popular in technical interviews. I think I used if-else statements when I did it. In this case it’d be very similar other than for “fizzbuzz” I checked higher-level multiples first; in order to print “problemoftheday” properly it has to go lower-to-higher multiples. But let’s see.
You’ve got an easy one for today. Create a program that iterates over the numbers [1-100] (inclusive). If a number is divisible by 1 print “problem”. If a number is divisible by 5 print “of”. If a number is divisible by 25 print “the”. If a number is divisible by 100 print “day”. Thus when you reach 100 your final line of output should be “problemoftheday”.
Easy peasy, make a random number displayed as hex-value, increment 100 times, print message. The interesting part is how to get the hex number since most numbers are considered as integers or floats/doubles; Java provides a simple function included in Strings to output as a certain format, like say as the hex value. Something they took from C, actually.
Pot Of Gold
Rainbows tend to be pretty cool to look at. If you’re lucky you’ll even find a pot of gold at the end of one. Today’s problem is to create a program that generates a random RGB hex value (#7A340E) then prints out the next 100 values (#7A340F, #7A3410, etc.) after that. Afterwards it should print out “POT OF GOLD!!” and then exit.
I haven’t stopped doing these but I haven’t seen any this week that got my attention enough to do them. Not a mark against them, I’ll certainly keep checking them out, just a personal preference.