Recently in programming Category

Banning the spammers

| | Comments (0)

I am getting really tired of the trackback spam. I was looking around in my MySQL administrator and found the tables that Movable Type uses to track tentative trackbacks (mt_tbping) and banned IP addresses (mt_ipbanlist). I wrote up a little SQL to copy IP addresses from unapproved trackback locations to the banned list:

INSERT INTO mt_ipbanlist
(ipbanlist_blog_id, ipbanlist_ip)
SELECT MY_BLOG_ID, tbping_ip
FROM mt_tbping
WHERE tbping_ip <> 'MY_HOST_IP_ADDRESS'
AND tbping_ip NOT IN (
	SELECT ipbanlist_ip 
	FROM mt_ipbanlist
	WHERE ipbanlist_blog_id = MY_BLOG_ID)

Of course, I'll have to check that all unapproved trackbacks are spam first, but I am hoping that the volume drops in time.




2008-08-13: added my list of banned IP addresses, trackback spammers one and all.

LinqPad

| | Comments (0)

LinqPad is a subsystem interpreter for LINQ in the same vein as:

It allows you to test out all the LINQ features in .NET 3.5 against any data source of your choosing, plus it provides lots of test cases and sample data sources. I recommend LINQ Pocket Reference to go with this.

NYC .Net Developer Groups

| | Comments (0)

Dev102.com programming challenge #13

| | Comments (0)

Dev102.com poses this programming challenge:

Your input is a string which is composed from bracket characters. The allowed characters are:'(', ')', '['. ']', '{', '}', '<' and '>'. Your mission is to determine whether the brackets structure is legal or not.

Example of a legal expression: "([](<{}>))".

Example of an illegal expression: "({<)>}".

Provide the most efficient, elegant and simple solution for that problem.

It's pretty obvious to me that you push opening brackets on the stack and pop off closing brackets, ensuring that the correct closing bracket is encountered. Like this:

def Evaluate(str):
  stack = []
  pushChars, popChars = "<({[", ">)}]"
  for c in str :
    if c in pushChars :
      stack.append(c)
    elif c in popChars :
      if not len(stack) :
        return False
      else :
        stackTop = stack.pop()
        balancingBracket = pushChars[popChars.index(c)]
        if stackTop != balancingBracket :
          return False
    else :
      return False
  return not len(stack)

EvanJones.ca

| | Comments (0)

A guy I play frisbee with turns out to have a cool developer's website, EvanJones.ca. Here are a couple of the cool links I found there:

Python code coverage [via EvanJones.ca]

Yahoo Zookeeper (distributed systems infrastructure) [via EvanJones.ca]

Controls, components, and containers

| | Comments (0)
A control on the other hand is a component that provides a UI. To do this you need to implement System.Windows.Forms.Control OR System.Web.UI.Control. So a control is basically a component that has visual properties. component is a class that implements the System.ComponentModel.IComponent interface or derives from a class that does You need a place to hold your components and controls. This is done in a Container. A Container is a class that implements the System.ComponentModel.IContainer interface

.NET interview questions and answers

| | Comments (0)

Interview questions

.NET interview

Dev102.com programming challenge #10

| | Comments (0)

Programming challenge #10 has been posted at dev102.com.

If all the numbers 1..n were present, then they would sum to n(n+1)/2. If some x is absent, then they will sum to n(n+1)/2 - x. So to determine which number is missing, subtract the sum of the series from the expected sum.

This python code finds the missing number in the series named values:

def test(values) :
	max_val = len(values) + 1
	expected_sum = max_val*(max_val + 1) / 2
	return expected_sum - sum(values)

Here is a complete test:

import random
def test(values) :
	max_val = len(values) + 1
	expected_sum = max_val*(max_val + 1) / 2
	return expected_sum - sum(values)

test_range = 200
for i in range(1,test_range) :
	a = range(1, i) + range(i+1, test_range+1)
	random.shuffle(a)
	x = test(a)
	if x != i :
		print "Failed at ", i
		print a
		print sum(a)
		break

IronPython integration

| | Comments (0)
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using IronPython.Hosting;
using IronPython.Runtime;

namespace TabbedImages {

    class TabbedImages {

        [STAThread]
        static void Main(string[] rawArgs) {
            List args = new List(rawArgs);
            args.Insert(0, Application.ExecutablePath);

            string dir = Path.GetDirectoryName(Application.ExecutablePath);
            PythonEngine engine = new PythonEngine();
            engine.AddToPath(dir);
            engine.Sys.argv = List.Make(args);

            Dictionary<string, object> d = new Dictionary<string, object>();
            EngineModule engineModule = engine.CreateModule("__main__", d, true);
            engine.DefaultModule = engineModule;

            string path = Path.Combine(dir, "main.py");
            engine.ExecuteFile(path);
        }
    }
}

Dev102.com programming challenge #9

| | Comments (0)

Programming challenge #9 has been posted at dev102.com. I think that this is about the shortest solution possible.

I initially did this recursively, but I needed two functions: one for the base case using the root and one for all other cases. Here, I just set up the preconditions in variables outside the loop and iterate over the left and right nodes. Which node is second last depends upon whether we went left or right in the last iteration: if left, then the current; if right, then the parent.

Node secondLast(Node root)
{
 boolean wentRight = true;
 Node parent = NULL, current = root;
 while (current->left && current->right)
 {
  parent = current;
  wentRight = (current->right != NULL);
  current = wentRight ? current->right : current->left;
 }
 return (wentRight ? parent : current);
}

I suppose I could be perverse and write the loop with a body of a single line, assigning to parent, wentRight, and current within a single ternary operator statement:

  current = (wentRight = ((parent = current)->right != NULL)) ?
   current->right : current->left;

I think that's a bit obscure just for the sake of brevity.




2008-06-24: I could even drop the wentRight variable and use the parent variable to record which way we went:

 while (current->left && current->right)
 {
  parent = (current->right != NULL) ? current : NULL;
  current = (parent == NULL) ? current->right : current->left;
 }
 return (parent != NULL) ? parent : current;

Now we're down to a loop with just two variables. I don't think I can make the code shorter and simpler than that.

Powered by Movable Type 4.1

About this Archive

This page is a archive of recent entries in the programming category.

products is the previous category.

python is the next category.

Find recent content on the main index or look in the archives to find all content.