diff options
| -rw-r--r-- | challenge-006/duncan-c-white/README | 43 | ||||
| -rwxr-xr-x | challenge-006/duncan-c-white/perl5/ch-1.pl | 33 | ||||
| -rwxr-xr-x | challenge-006/duncan-c-white/perl5/ch-2.sh | 2 |
3 files changed, 46 insertions, 32 deletions
diff --git a/challenge-006/duncan-c-white/README b/challenge-006/duncan-c-white/README index b785f171b8..fb3a863c80 100644 --- a/challenge-006/duncan-c-white/README +++ b/challenge-006/duncan-c-white/README @@ -1,35 +1,14 @@ -Challenge 1: "Write a program which prints out all anagrams for a given word." +Challenge 1: "Create a script which takes a list of numbers from +command line and print the same in the compact form. For example, if +you pass 1,2,3,4,9,10,14,15,16 then it should print the compact form +like 1-4,9,10,14-16.." -Note: I assume that we'll need a wordlist to judge whether a rearrangement -of letters is actually a word, /usr/share/dict/words is the obvious one -to use. The simplest way of solving this problem is to use alphabetically -sorted SIGNATURES of words - simply the bag of letters in the word sorted. +Quite simple and dull problem. But ok, let's have a go. -So "hello"'s signature is "ehllo". The important thing for anagram purposes -is that "ehllo" is the signature not only of "hello", but of any other anagram -of hello too. +Challenge 2: "Create a script to calculate Ramanujan's constant with at +least 32 digits of precision." -So: calculate the signature of the given word, then for every word in the -dictionary (of the right length if we want to save time), calculate that -dictionary word's signature - then print out the dictionary word if it's -signature is the sameas the given word's signature. - -Note that ch-1.pl takes two command line arguments: the first is the -word, the second is the optional filename of the wordlist to use, -if it's not given then /usr/share/dict/words is used by default. - - -Challenge 2: "Write a program to find the sequence of characters that has -the most anagrams." - -That's rather badly worded, but I choose to interprete it as "find which -word in a wordlist has most anagrams also in that wordlist". - -Note that ch-2.pl takes a single optional command line argument: -the filename of the wordlist to use, if it's not given then -/usr/share/dict/words is used by default. - -So: Calculate the signature of every word in the dictionary, building %anag: -a mapping from signature -> list of words with that signature, and keep track -of the longest word list of any signature (i.e. the biggest anagram set so far) -as we go. Print out the signature with the longest word list at the end. +Never heard of this constant, seems to be e^(pi*sqrt(163)), which is +"very nearly an integer", I don't particularly care about abtruse mathematical +formulae. But ok, Perl's built in module biggrat will let you do this anyway, +specifying accuracy 32; see ch-2.sh for the oneliner. diff --git a/challenge-006/duncan-c-white/perl5/ch-1.pl b/challenge-006/duncan-c-white/perl5/ch-1.pl new file mode 100755 index 0000000000..799b0c89db --- /dev/null +++ b/challenge-006/duncan-c-white/perl5/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl + +# Challenge 1: "Create a script which takes a list of numbers from +# command line and print the same in the compact form. For example, if +# you pass 1,2,3,4,9,10,14,15,16 then it should print the compact form +# like 1-4,9,10,14-16.." + +use strict; +use warnings; +use Data::Dumper; + +die "Usage: ch-1.pl list_of_numbers\n" if @ARGV == 0; + +my $minlen = 3; +my @in = @ARGV; + +my @out; + +while( @in ) +{ + my $s = shift @in; # new sequence s..s + my $e = $s; + while( @in && $in[0] == $e+1 ) + { + $e = shift @in; + } + #print "s=$s, e=$e\n"; + push @out, + (( $e-$s+1 >= $minlen ) ? + "$s-$e" : + join( ',', $s..$e )); +} +print join(',',@out)."\n"; diff --git a/challenge-006/duncan-c-white/perl5/ch-2.sh b/challenge-006/duncan-c-white/perl5/ch-2.sh new file mode 100755 index 0000000000..f38beeacbb --- /dev/null +++ b/challenge-006/duncan-c-white/perl5/ch-2.sh @@ -0,0 +1,2 @@ +#!/bin/sh - +perl -Mbigrat=bexp,bpi -e 'print bexp(bpi(32)*sqrt(163),32)."\n"' |
