All challenges from http://www.reddit.com/r/dailyprogrammer Feel free to send me a message with/for advice; I'm happy to give help with problems, or discuss my solutions. Ask Box | Games
I’m doing well! I’ve been working as a ruby dev for about a year and a half now, I got married, and I’m generally very busy haha! I should probably be making more of an effort to build things outside of work though :^)
Still happily taking coding questions here; this blog is dormant, not inactive.
hello! I was wondering, how would you write up a piece of pseudo code for the Monty Hall guessing game? Thank-you!
Hello! The nice thing about pseudo code is that it doesn’t need to work, it just needs to show the process that real code would take. The Monty Hall game is pretty straightforwards, so I’d just step through it.
1) Randomly select the winning door2) Randomly select the player’s choice (or take an input) 2) Randomly select a door other than the player’s choice and the winning door to be eliminated3) If the player is going to switch, their choice becomes a door that they have not already chosen and that was not eliminated. 4) The player wins if their choice is the same as the winning door.
So if you want that as more code-based pseudocode… I’ve already written a solution, so it’s hard to go back and make pseudocode that doesn’t look an awful lot like the solution. But you’d want something like this:
player_choice = rand(3)winning_door = rand(3)eliminated_door = # a number between 0 and 2 that does not equal player_choice or winning_door
if switch player_choice = # a number between 0 and 2 that does not equal player_choice or player_choiceif player_choice == winning_choice confetti()
The tricky part is picking those doors, and how it’s solved is going to be dependent on what language you’re using. I’d save figuring that out for the actual solution. But hey, pseudo-code - ‘solve thing’ is super valid.
I’ve stuck a solution below for funsies. I wrote it in Ruby since that’s mostly what I’m working in these days (and I because love it) (sorry Java)
Hi! How can I tell the number of 1 bits of an integer's binary representation, without loops? (An interviewer asked me this once)
My first thought would be to convert to binary, strip 0s and find the length. Like:Integer.toBinaryString(integer).replace(“0”, “”).length();Though, depending on the implementation of ‘replace’, that might not count as without loops. I wouldn’t be surprised if there’s a better trick to find out by looking at bits / distance from power of two or something along those lines, but my search isn’t turning anything good up right now. (Feel free to let me know, though)
First thing: it’s a shitty interview question. Don’t take that job.
Secondly, the name of this magic value we’re looking for is the Hamming Weight. That will help you enormously when you google to verify everything I’m saying later on.
Thirdly, “without looping” is the most mind-numbingly retarded stipulation ever stipulated for any programming question ever. You might as well say “without using a Turing machine”, because that’s literally what “without looping” means. I mean, when the fuck ever are you going to be using this knowledge of how to program in non-turing-complete environments?? It’s fucking stupid!
Finally, how to compute the hamming weight of an integer without looping.
Just the sort of thing you’ll be able to rattle off at any interview, right? Not at all something you’d have to spend time either researching or a lot more time actually figuring it out yourself.
For fuck’s sake. Can we just round up all the morons who set these kinds of interview questions and just summarily execute them? Please?
Hi! How can I tell the number of 1 bits of an integer's binary representation, without loops? (An interviewer asked me this once)
My first thought would be to convert to binary, strip 0s and find the length. Like:Integer.toBinaryString(integer).replace("0", "").length();Though, depending on the implementation of ‘replace’, that might not count as without loops. I wouldn’t be surprised if there’s a better trick to find out by looking at bits / distance from power of two or something along those lines, but my search isn’t turning anything good up right now. (Feel free to let me know, though)
Hello Please How can I write a code that finds the average of N number of students grade (0-100) from an exam
The simplest way is just iterating over them to get the sum, and then dividing to find the average. So justdouble sum = 0;for (int i = 0; i < n; i++){ sum += grade[i];}doubleaverage = sum/n;
Some of you might be interested to know that Code Academy has added a Java course recently! I think code academy is a great resource for beginners (I’ve just gone through all their Ruby on Rails courses) and might be something worth checking out =)
Hi everyone! Due to being at the end of my postgrad year/job hunting/other projects I want to work on more, finding time for this blog has been getting harder, and I’ve decided it’s about time to stop. This is a little project that ran on waaay longer than I expected it to, and it’s been a lot of fun but I need the time for other things.
I really didn’t expect to get any attention when I started last December, and I think it’s really cool how many of you have been interested in these little challenges! I’m going to leave everything up, of course, and I’ll still answer any questions that are sent to me, but I won’t be posting anything else here.
So this one is about finding ‘Ruth-Aaron pairs’, which are pairs of consecutive numbers where the prime factors for each sum to the same number. Hopefully it’s clear enough what the code does, but it just goes through each number breaking it down into prime factors using some leftover stuff from the prime factor tree code.
Due to crushing time constraints, today’s one is just a suuuuuper watered down version of this one; honestly it has little to do with the spreadsheet development thing, but I am super-busy and low on challenges, so! It can take input in the format cell = number, or cell = cell + number, it takes instructions in a set and iterates through them all, remembering set variables for each time. Basically it’s just a little variable-setting program but shhh
Today’s one is about outputting all the cells that would be selected from a spreadsheet range; it takes range : and & and one exclude ~ command per input (and it’s trusting), and it works in Char-Int format (though the actual cells convert the letters to numbers first) annnd it’s ‘part one’ but I don’t think I’ll do the next one (or it’ll be a very cut down version).
Maybe this one is easier in other languages, but it’s definitely one of the longer easy ones I’ve done. It’s about carry adding - showing addition like when you do it by hand, putting the ones on top - but it is one-hundred percent string alignment which makes it long and tedious and fiddly =P Basically, it just iterates along the rows summing up characters from each line and the carry line, and if the sum is more than one digit it adds it to the carry line. (Plus a whole bunch of string alignment stuff).
Thiiiis one is kind of like a spell checker; it checks words against dictionary words, finds the point where there are no more matches (where the typo was made) and prints it out with a ‘~’ before the first incorrect character. Then it prints all the words that start with the correct part of the word. Maybe a smarter solution would have been to print anagrams or suggestions only using the inputted letters, but I didn’t feel like digit matching today =B
This one is kind of a little ‘day planner’ thing? It’s not the fullest implementation because I really need to be doing other things, but it can add and delete events, and it does sort them by time, so it achieves the most basic features. You’ve probably seen this kind of code from me before though, pretty simple user input parsing with a scanner.
This one is a unit calculator - it can convert between a handful of predefined units, by looking at the relative ratios. It’s extensible, so long as you get the proper ratio between the new unit and meters/hogsheads for length or mass. I did pinch the ratios from someone else’s answer on the challenge page to save me the work hehehe
I really don’t understand why this challenge is called ‘vampire numbers’ but I guess I would have just called it ‘numbers that multiply into numbers with the same digits’ so maybe I’m boring. So! It’s numbers that multiply into numbers with the same digits! And it’s not quite an implementation of what the challenge wanted, since it’s strictly two two-digit numbers making a four-digit number, and not flexible lengths, but I am very tired today so this is the best I’m doing =B
This one is probably a pretty lazy implementation by me :V The thing is to take in header text and body text, and use those plus a really basic html format to generate an HTML file. So I did ask-user-with-scanner and wrote out with print writer, and I compressed the HTML template in a really ugly way, and it generates an incredibly plain little page. If you wanted to skip the outputting, a lot of Swing elements can display HTML-encoded text.
A verrrry long time ago I did part one; today I finally got around to part two of Rock Paper Scissors Lizard Spock! It uses the same base game code (cleaned up) except this time the computer’s choice isn’t random; instead it looks at all the player’s past inputs and picks one of the weaknesses to the one-or-two most common ones. I realised the printed output is a little out of order since the choice the user has just made isn’t included in the calculations, but hopefully all the extra print stuff makes that clear. The program will look for a common weakness between two inputs tied for most common, but if there’s three or more most common it just picks randomly.