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.Ā
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
ā Live Streamingā Interactive Chatā Private Showsā HD Qualityā Free Actions
Free to watch ⢠No registration required ⢠HD streaming
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;
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
ā Live Streamingā Interactive Chatā Private Showsā HD Qualityā Free Actions
Free to watch ⢠No registration required ⢠HD streaming
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).Ā
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
ā Live Streamingā Interactive Chatā Private Showsā HD Qualityā Free Actions
Free to watch ⢠No registration required ⢠HD streaming
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
Anya is live and ready to show you everything. Watch her strip, dance, and perform exclusive shows just for you. Interact in real-time and make your fantasies come true.
ā Live Streamingā Interactive Chatā Private Showsā HD Qualityā Free Actions
Free to watch ⢠No registration required ⢠HD streaming
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.Ā