Programming - Misc


From: wings <xuchu@comp.nus.edu.sg>
Subject: [IQ]what's the number
Date: 18 Jan 1999 09:27:44 GMT

Generate a 9 digit number using the number 1-9, without using any number
more than once. this number must also be divisible such that if you take
the left most digit the number is divisible by 1, if you take the two left
most digits the number is divisible by 2, if you take the three left most
digits the number is divisible by 3, etc., etc, What is the number?
From: wings <xuchu@comp.nus.edu.sg>
Date: 19 Jan 1999 03:40:38 GMT

:> 381654729? 

it's right. i am wondering if this is the only right answer? 
From: JnZ <jouweihu@comp.nus.edu.sg>
Date: 19 Jan 1999 03:56:24 GMT

Try this little perl script: *8)
-----cut-here-----
#!/usr/local/bin/perl5 -w
$START = "123456789";
$END = "987654321";
$d = $START;
DIGIT: while($d++ ne $END) {
    foreach(split(//, $d)) {
        next DIGIT if ($_ eq '0');
        next DIGIT if (index($d, $_) != rindex($d, $_));
    }

    foreach(2..9) {
        my($t) = substr($d, 0, $_);
        next DIGIT unless (($t % $_) == 0);
    }
    print "And the number is... $d!!!\n";
}
-----cut-here-----

From: Tintin <chairenl@comp.nus.edu.sg>
Subject: NRIC Check Digit Formula
Date: 20 Jan 1999 08:31:07 GMT

Can anyone share with me the formula for the check digit for our NRIC
number?
From: Excalibur <swanhuey@comp.nus.edu.sg>
Date: 20 Jan 1999 11:36:10 GMT
Eg:
My NRIC -> 7521356
Multiply by 2765432 (by digits)
Results = 14, 35, 12, 5, 12, 15, 12.
Add together = 14 + 35 + 12 + 5 + 12 + 15 + 12 = 105.
Divide by 11, get reminder 6.

Mapping: 0 = Z(?)
1 = J
2 = I
3 = H
4 = G
5 = F
6 = E
7 = D
8 = C
9 = B
10 = A
From: Tan Tuck Choy Aaron <tantc@comp.nus.edu.sg>
Date: 20 Jan 1999 11:39:42 GMT

Excalibur <swanhuey@comp.nus.edu.sg> wrote:
: My NRIC -> 7521356
: Multiply by 2765432 (by digits)
: Results = 14, 35, 12, 5, 12, 15, 12.
: Add together = 14 + 35 + 12 + 5 + 12 + 15 + 12
: = 105.
: Divide by 11, get reminder 6.
^^^^^^^^ reminder: should be remainder

up to here okay.
i prefer to do 1 more (optional) step, substract the remainder
from 11.

the mapping is then 
1 2 3 4 5 6 7 8 9 10 11
A B C D E F G H I Z J

From: Choon Leong <chuacl@comp.nus.edu.sg>
Subject: Re: NRIC Check Digit Formula
Date: 21 Jan 1999 02:19:56 GMT

Below is a simple perl program for computing the check digit using the algo 
provided by Tiak Jung in the bbs (Dec 1996)

#!/usr/local/bin/perl5
# 
# Purpose: Compute the alphabet of a nric number

die "Usage $0 nric_no1 nric_no2 .. \n" if $#ARGV < 0;

%Check_Digit=(
	1 => A,
	2 => B,
	3 => C,
	4 => D,
	5 => E,
	6 => F,
	7 => G,
	8 => H,
	9 => I,
	10 => Z,
	11 => J );

@weights=(2,7,6,5,4,3,2);

while (@ARGV) {
	for($i=0,$sum=0;$i<7;$i++) {
	$sum += ($weights[$i] * substr($ARGV[0],$i,1));
}

$digit = 11 - ($sum % 11);
print "Alphabet for nric no $ARGV[0] is $Check_Digit{$digit}\n";
shift;
}
From: Choon Leong <chuacl@comp.nus.edu.sg>
Date: 21 Jan 1999 02:37:07 GMT

To sidetrack, I was able to generate quite a list of IC numbers and I could
use it to check what those pple borrowed. In fact, you can also use it to
reserve books or extend loans for pple. I was thinking of bringing this
problem up to the notice of National Library, somehow I'm always too bz to do 
it. 8(

From: Tintin <chairenl@comp.nus.edu.sg>
Subject: Leap Year Formula
Date: 22 Jan 1999 08:25:52 GMT

Anyone knows the formula for checking leap year?
I faintly remember (year mod 4) = 0 means leap year.
But I think there's something missing here.
But I forgot what is it.
Can someone enlighten me?
Thanks alot!
:)
From: Tan Tuck Choy Aaron <tantc@comp.nus.edu.sg>
Date: 22 Jan 1999 08:36:28 GMT

a leap year is:
divible by 400
OR
(divible by 4 but not by 100)

i hope there is no ambiquity.
From: Lai Zit Seng <laizs@comp.nus.edu.sg>
Date: 22 Jan 1999 10:47:06 GMT

> should be correct.
> funny spelling though, 8)
> is it on purpose?
> the 400 thing is important for y2k. (to make this post relevant) :)

There is something about a super-leap year every 4000 years too?

From: Choon Leong <chuacl@comp.nus.edu.sg>
Subject: Using appropriate programming languages
Date: 24 Jan 1999 04:51:56 GMT

Just some of my humble opinion. There have been too many times when I've seen
pple using inappropriate language to solve a problem. For eg, using Perl when
bourne shell programming is sufficient, using C for manipulating strings when
Perl is more suitable. 
So think carefully b4 u stick religiously to your favourite language and
also, learn more languages for possible future usage.
From: Lee Yeow Leong <leeyeowl@comp.nus.edu.sg>
Date: Sun, 24 Jan 1999 14:28:18 +0800

Perhaps u can start a column by posting a problem(each week?) and
ask people to contribute answers(in their favourite language).
We can all learn to appreciate all the Programming Languages. :)
From: Choon Leong <chuacl@comp.nus.edu.sg>
Date: 25 Jan 1999 11:19:51 GMT

Once a while can but every week will make my hair go white. 8) Recently I've
this problem which I was thinking for a while as how to solve it quickly. 

Supposed there are 4 columns of data but I only need column 2 and 4 where they
are the tutorial group number and userid respectively. What I need is to
output the userids in sorted order to their respective filenames ie group 1 
userids go to filename 1.
The file is an email with the actual data starting from line 30, so using
bourne shell programming, we have (assuming the filename is temp.dat)

#!/bin/sh

tail +30 temp.dat|awk '{print $2 ":" $4}'|sort -t: | while read line ; do
group=`echo $line|cut -f1 -d:`
userid=`echo $line|cut -f2 -d:`

echo $userid >> $group
done
From: valk <ngkaboon@comp.nus.edu.sg>
Date: 25 Jan 1999 13:41:30 GMT

a bash one-liner is enough, isn't it?

tail +30 temp.dat| awk '{print $4 ">>" $2}' | sort -t: -k 1 | xargs \
/usr/bin/sh -c 'echo {}'
From: valk <ngkaboon@comp.nus.edu.sg>
Date: 25 Jan 1999 14:10:43 GMT

: tail +30 temp.dat| awk '{print $4 ">>" $2}' | sort -t: -k 1 | xargs \
: /usr/bin/sh -c 'echo {}'

bad example !! :)
a better way...

tail +30 temp.dat | awk '{print $4 ":" $2}' | sort -t: -k 1| awk \
'BEGIN { FS=":" } { print $1 >> $2 }'

From: KooL Shot <koopingt@comp.nus.edu.sg>
Subject: [lisp] breaking page for help
Date: 24 Jan 1999 15:01:49 GMT

May I know how can I break a page, similar to a " |more" in bash, when I issue a
help command?

e.g.
(help* "val")
will give me a lot of commands, which will scroll none stop. How can I control
the scrolling? Thanks.
From: Lee Yeow Leong <leeyeowl@comp.nus.edu.sg>
Date: Sun, 24 Jan 1999 23:26:25 +0800

All the info are in /usr/local/doc/gcl, perhaps u want to view them
raw?