Miles to go …

February 3, 2008

How NetBeans helped in 100th day of school celebration ?

Filed under: General — arungupta @ 5:14 pm

My friend’s daughter got a family assignment from school to commemorate 100th day in school. If each letter in the English alphabet is assigned a weight corresponding to it’s position (e.g. "a" is 1, "b" is 2, … "z" is 26) then she was supposed to collect 10 words whose letters sum total to 100.

They spent last 1.5 days and found only 3 words and were discussing the problem at the lunch table earlier today. Here is a small piece of code that I wrote to help them out:

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    private static final char[] WEIGHT = {
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
        'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
        'w', 'x', 'y', 'z'
    };

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) throws Exception {
        if (args.length == 0) {
            System.out.println("At least one argument required");
            return;
        }
        BufferedReader in = new BufferedReader(new FileReader(args[0]));
        String string = in.readLine();
        while (string != null) {
            StringTokenizer tokenizer = new StringTokenizer(string);
            while (tokenizer.hasMoreTokens()) {
                String word = tokenizer.nextToken();
                if (wordStrength(word) == 100)
                System.out.println("100 letters old: " + word);
            }
            string = in.readLine();
        }
    }

    static int wordStrength(String word) {
        int strength = 0;
        for (int i=0; i<word.length(); i++) {
            int charStrength = Arrays.binarySearch(WEIGHT, word.charAt(i));
            if (charStrength >= 0)
                strength += charStrength+1;
            }
            return strength;
        }
    }
}

This application was easily created using NetBeans 6. And then I copy/pasted articles from my favorite news sites in a file, passed it as command-line argument by right-clicking the project, selecting "Properties" and specifying the file name in "Arguments".

Hit F6, and voila got more than 10 words :)

Technorati: netbeans school assignment

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #144: CDI @Produces for container-managed @Resource
  2. TOTD #82: Getting Started with Servlet 3.0 and EJB 3.1 in Java EE 6 using NetBeans 6.7
  3. TOTD #148: JPA2 Metamodel Classes in NetBeans 7.0 – Writing type-safe Criteria API
  4. LOTD #12: Technorati State of the Blogosphere 2008
  5. TOTD #123: f:ajax, Bean Validation for JSF, CDI for JSF and JPA 2.0 Criteria API – all in one Java EE 6 sample application

3 Comments »

  1. Arun,

    Great help. Thanks!

    /.rocky

    Comment by Rocky — February 6, 2008 @ 2:48 pm

  2. Since you also talk about Ruby and Rails here a lot, I did the same thing in the ruby version of the Netbeans IDE. Here’s the ruby code:

    #!/bin/ruby

    @weight = %w{a b c d e f g h i j k l m n o p q r s t u v w x y z}

    if ARGV.size < 1 then
    puts ‘At least one argument required’;
    exit!
    end

    def word_strength(word)
    word.split(”).inject(0) { |sum, c|
    sum += (@weight.include? c) ? @weight.index(c) + 1 : 0;
    }
    end

    ARGF.each do |line|
    line.chomp.split(/ /).each { |word|
    puts "100 letters old: " + word if word_strength(word) == 100
    }
    end

    Comment by Erik — February 8, 2008 @ 2:34 pm

  3. This is nice, thanks!

    Comment by Arun Gupta — February 11, 2008 @ 5:54 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress