Coin Change: Minimum number of coins

Problem: You are given n types of coin denominations of values v(1)<v(2)<…<v(n) (all integers). Assume v(1)=1, so you can always make change for any amount of money C. Give an algorithm which makes change for an amount of money C with as few coins as possible. #include <stdio.h> #include <stdlib.h> int main() { int i,n,den[20],temp[20],min,min_idx, … Read more

Removing entries from a dictionary containing bad words

I have a dictionary with each item containing a quote in another dictionary: { ‘example1’: {‘quote’: ‘And it must follow, as the night the day, thou canst not then be false to any man.\n’}, ‘example2’: {‘quote’: ‘What a piece of work is man! how noble in reason!.\n’} } I need to completely remove each entry … Read more

Filtering a list of vertices that lie inside a cylinder, with and without LINQ

Since I don’t know how LINQ works under the hood, I can’t decide what version is best to use in term of rapidity of execution. I’ve done some testing with my testing data (Point Cloud) but I can’t see a clear difference between the two. The only thing I know is that the real life … Read more

Firefox and Chrome slow on localhost; known fix doesn’t work on Windows 7

Firefox and Chrome are known to be slow on localhost when IP6 is enabled. In previous versions of Windows, the simplest fix is to comment out this line from the hosts file, as explained in the answer to this question. ::1 localhost However, as noted in this question, in Windows 7 this line is already … Read more

Why does a small change to this Scala code make such a huge difference to performance?

I’m running on a 32-bit Debian 6.0 (Squeeze) system (a 2.5 GHz Core 2 CPU), sun-java6 6.24-1 but with the Scala 2.8.1 packages from Wheezy. This code, compiled with scalac -optimise, takes over 30 seconds to run: object Performance { import scala.annotation.tailrec @tailrec def gcd(x:Int,y:Int):Int = { if (x == 0) y else gcd(y%x,x) } val p … Read more

PreparedStatements and performance

So I keep hearing that PreparedStatements are good for performance. We have a Java application in which we use the regular ‘Statement’ more than we use the ‘PreparedStatement’. While trying to move towards using more PreparedStatements, I am trying to get a more thorough understanding of how PreparedStatements work – on the client side and … Read more

Case-insensitive string startswith in Python

Here is how I check whether mystring begins with some string: >>> mystring.lower().startswith(“he”) True The problem is that mystring is very long (thousands of characters), so the lower() operation takes a lot of time. QUESTION: Is there a more efficient way? My unsuccessful attempt: >>> import re; >>> mystring.startswith(“he”, re.I) False Answer You could use … Read more

jQuery appending an array of elements

For the purpose of this question lets say we need to append() 1000 objects to the body element. You could go about it like this: for(x = 0; x < 1000; x++) { var element = $(‘<div>’+x+'</div>’); $(‘body’).append(element); } This works, however it seems inefficient to me as AFAIK this will cause 1000 document reflows. … Read more