Tag Archives: potd

Problems of the Days

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.

Advertisements

Problem of the Day: Tic-Tac-Toe (update)

Whew. TicTacToe is quite the time suck. I made a new TicTacToe class to handle creating a board, adding moves, checking for a winner, and printing the results. The board is still pretty dumb; each move attempt is started by getting a random integer [0-3), checking that it’s a legal move, then making the move and switching players. The entire board is always filled up but only prints out if there is a winner. Because “X” gets to go first every time it also wins a large percentage of the time, I check for “X” winning first to avoid a board having two win-conditions.

I did re-learn the ternary operation and figuring out how to teach a computer to play TicTacToe was a useful thought exercise. I might spend some time making the board smarter as far as choosing positions and checking for wins. Maybe.


Problem of the Day: Tic-Tac-Toe

Computers are great at repetitive tasks, and tic-tac-toe more often than not is very repetitive. Today’s problem is to make a Tic-Tac-Toe solver with or without an AI; to make things easy at first I’ll just make it play randomly.

  • in the case of a draw, print an empty board and “Draw”
  • in the case of a win, print out the completed board and who wins
  • run until either player has 10 wins

Finding win conditions is the hardest part so far as the program has to watch each move and check for three matching pieces on each horizontal, vertical, and diagonal axis. Initially I’ll do the check manually after each move; it’d be much cooler for each piece to check for a win on its own by analyzing each adjacent piece. Mark that down for the second iteration and the AI for the third.

Tic-Tac-Toe

HAPPY MONDAY!!!

Today’s Problem of the Day is to implement an automated version of our favorite childhood game, tic-tac-toe. Your program can assign random moves for X and O or you can implement some AI to favor one over the other. When someone wins print out the board and who won. If the game is going to be a draw print out the board and print out that it will be a draw.

Based on these conditions your program should never print out a full board unless the final move is a game winning move. If a game is going to end in a draw just print out the board. The program should run until X or O has won 10 games.

X


Problem of the Day: The Lone Survivor (update)

Double-linked-list worked but the link to move backwards, .prev, wasn’t necessary. I created a package of datatypes I’ve made then imported a Node object to create the list; since I had the option of including a link to the previous node I figured I’d use it. My 64-bit Java 1.8 install and Eclipse install can handle a total of about 3,500 servants; anything higher and it runs out of memory and starts losing nodes which causes null pointer issues.

The comments and I agree that given 1,500 servants the winner is the 953rd servant who kills the 1,456th servant. Most used some sort of data structure like I did, but a few pointed out that there’s a mathematical function, a circular left-shift, that does the same result. Probably can handle much larger groups of servants as well since it doesn’t actually have to create an object or instance of each one. LEARNDING!

So far doing the Problem of the Day has been entertaining and dare I say something I look forward to each day. I don’t try to finish first as I’m sure they’re put up while I’m still asleep and solved about an hour later. As far as giving something programming to do everyday I’m the real winner. /cue sappy “lesson learned” music from 80’s high school movie


Problem of the Day: The Lone Survivor

This is a bit morbid and could easily be more of a math problem but computers are just so good at maths it’s an easy fit. Recursive or repetitive function that continues to eliminate the even numbered entries until only one is left. It seems like the 1st servant would always be the one to start everything but the comments and I agree that it continues in a loop; not necessarily stopping then starting with the 1st servant again.

This moves to a circular pattern throughout the servants. I thought an array at first, I always go to arrays it seems, but a linked-list (or conveniently a double-linked-list that I already made a couple POTDs ago) would be best since the size is constantly being dropped by one. The modification will require the last entry to point to the first entry once the entire thing is set up, then it just keeps removing the next-next entry until there is only one entry left.

But how to track only one entry being left…

The Lone Survivor

There are 1,500 loyal servants sitting around the king’s table when they decide to play a little game. The 1st servant gets a sword and kills the 2nd servant. He then gives the sword to the 3rd servant, who then kills the 4th servant and then gives the sword to the 5th. This continues so that the 1,499th servant kills the 1,500th servant and gives the sword back to the 1st servant.

Now the 1st servant kills the 3rd and gives the sword to the 5th. This process continues until only one servant remains. Which number servant is the lone survivor?

X


Problem of the Day: Duplicate Entries (update)

I found a hash-table I made while in a college course that fit the bill so I didn’t actually remake my own. It has more than the desired functions including type declarations and three different hashing methods: linear, double, and chain. I used the linear method since duplicate entries were just going to be reported and ignored; to achieve this I had to modify the class a bit but I made note of it when reusing the class file.

Class reuse at its finest.


Problem of the Day: Duplicate Numbers

Today’s problem is actually one I’ve seen before in a technical interview. I tried it then using nested loops that was O(n^2) complex and took longer than I expected trying to keep track of when an item was found and how to handle more than one duplicate entry. My interviewer suggested using a hash which I had never considered; that’s experience for you.

Since the loops are O(n^2) and I got lost in the truth tables the first time, I’m going to take my interviewers advice and use hashes instead. The plan is that as each value is added to the hash-table a collision will occur when a second number is added and throwing a duplicate entry. To do this I’m going to make my own simple hash object that will support:

  • adding values to the table
  • automatically increasing the table size if it gets >80% allocated
  • reporting a duplicate value
  • reporting the current hash allocation percent

Should be fun.

Duplicate Numbers

Hopefully while filling out your taxes you didn’t run in to any issues with duplicate numbers. However, if you did now is your chance to make up for it. For today’s problem you’ll be passed in an array of integers and asked to identify all the duplicate numbers.

For a bonus solve it in under O(n^2) without making use of a set/hash (unless you want to implement your own).

linky linky