Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Don Coyote's scratchpad

by Don Coyote (Pilgrim)
on Jul 01, 2010 at 17:29 UTC ( #847567=scratchpad: print w/ replies, xml ) Need Help??

post 3 How do I write a program that calculates the sum of all numbers to a number, n? silly response given

This response is not intended to be taken literally. :p

The sum of ALL numbers being 1/12, would the sum of all numbers up to N not be some type of a function? In the manner of

sum( N,N-1,N-2..N=1 ) = N / ( 1/12 )

Oh yes, I see now n(n+1)/2, overthinking it again.

Although that might be N * 1/12 (but is probably neither). For another undefined definition, maybe just bitmasking the sum of all numbers to length N. This could work well if soan is truly irrational.

sub donotrun_this_is_an_example{ use constant Soan = (1/12); my $bitmask = ~ -256**($n/8); return Soan & $bitmask; }

similar idea would be to return the of multiplying Soan by 10 to power of N: int( Soan*10**$n )

Alert! vague conjecture recollection syndrome and Random thought process combination.

post 2

To replace the occurence string in perl

Off the top of my head (untested), I would suggest starting with a one liner such as:

perl -pi.bak -e " use features qw/state/; state $nameshash = { 'Poole' => 1 }; s/(<.*?CIT-)(\w+)(-\w\d+.*?>)/"$1$2".$nameshash->{$2}++."$3"/ge; " bibliofile

post 1

CRC-16 algorithm


print "CRC16 in hex:\n  ", unpack('H*', pack('S', $crc16)), "\n";

citation needed check pack pod.
This could be a problem caused by the use of unpack() on pack() - recalling some usage where internal representation does not necessarily behave to standards of other specifications.

post 0

  • iteration vs recursion: the iterator should do what its told. that is it should have an expected behaviour as of any particular version, though that behaviour can change. It is for the programmer to be up to date with the current usage.
  • Behaviour: can change as with anything, take for example hash behaviour, modified in regards of security (re collisions). The random nature of the iteration, should not be used as a defined behaviour, yet knowledge of that behaviour allows quick development. (uhm iteration or storage/retrieval different things?).
  • User configurable: perhaps user accessible. same thing maybe. For example, I can access symbol tables, which behave in a defined way. In general I do not need to access the stashes, but I can if I want to. A cost effective implementation, (will out perform in drag races), which can be user interactive/manipulated. (we want it to turn doughnuts). Basically, I don't want to be setting loads of flags. If I need to define the behaviour outside of the general dwimness of the iterator, I can set about writing some routines to do so. This also encourages legacy code to have documented implementations.
  • user defined operator: something along this line maybe.
  • DWIM: when I want it to invalidate on a resize operation, it should. When I don't want it to invalidate on a resize operation it should not. In my dreams, I want it do both within one.
  • TMTOWTDI: There would not be any need to write code if the compiler already knew what I wanted to do.
  • seem to be straying away from iterator behaviour a bit now...
  • Common complex usage requirement:
my %combinations = ( co1 => [ [ qw(one two three four five) ], [ qw(one one two three five) ] ], co2 => [ [ qw(one two three five seven) ], [ qw(one one two three five) ] ], co3 => [ [ qw(one two three five seven) ], [ qw(one two three four five) ] ], co4 => [ [ qw(one two three five seven) ], [ qw(one two three four five) ], [ qw(one one two three five) ] ], ); reorg($_,\%combinations) foreach(keys %combinations); sub reorg{ my($k,$h) = @_; foreach my $combiset(@{$h->{$k}}){ my @uniquesets = grep $_ =~ /\Aus/ keys %combinations; my $num = scalar(@us); foreach my $set ( @$combiset ){ $combinations{'us'.++$num } = $set unless grep @$set {<~~>} @$_, @combinations{@us}; # where {<~~>} is 'these arrays match' # or the front N indices do or ... } }

To end up with something like:

%combinations = ( co1 => [ [ qw(one two three four five) ], [ qw(one one two three five) ] ], co2 => [ [ qw(one two three five seven) ], [ qw(one one two three five) ] ], co3 => [ [ qw(one two three five seven) ], [ qw(one two three four five) ] ], co4 => [ [ qw(one two three five seven) ], [ qw(one two three four five) ], [ qw(one one two three five) ] ], us1 => [ qw(one two three four five) ], us2 => [ qw(one two three five seven) ], us3 => [ qw(one one two three five) ], );

While writing this example, I felt as though most of writing code is iterator definition. Within this example, I require several uses of iterator to define growing numbers of keys, these themselves need to be used to clarify the keys the initial map operation continues to apply the reorganising routine on the hash to. While also not then applying the new keys which have been added.

A kind of There's More Than One Way To Do What I Mean? I mean the iterator to know the keys I started with, and only iterate through them as the hash grows. I mean another iterator to build a set of unique values keyed in the original hash, and built out of the original keys values. I mean the newly built unique values to be used straight away in validation checks on the remaining keys.

If as here, I approach designing the code without considering what the internal behaviour of the iterator is, other than the existing definition that it should not be relied upon. The internal iteration behaviour may change between versions, and the code is easily maintained throughout minor transitions.

What I need from the iterator is that, a defined behaviour. If it is not of any impact as to the precise implementation, as the code is not well designed if it relies on that, then would it not be reasonable to approach implementation as a matter of cost and efficiency. What is the fastest, most cost effective implementation? Can that implementation be reasonably identified as being compatible for the next 15+ years. Can I access it and incant wizardry in the way I might with symbol tables. Such as by having a user defined operator.

Perhaps something more robust, like a Global Iterator Variable / Symbol, along the lines of ${*} or *INC,*ARGV, the match array $-[0..-1]. I don't want to be setting variables and flags in most cases. I want to have a simple concept of, don't rely on this behaviour, but this is how you can manipulate it if you want to. And consider maintainer is a must, will the code self-document?

Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2015-04-19 09:14 GMT
Find Nodes?
    Voting Booth?

    Who makes your decisions?

    Results (358 votes), past polls