php-general Digest 10 Oct 2007 14:37:22 -0000 Issue 5065
Topics (messages 263022 through 263041):
Re: Beginner Tutorials for using CLASSES in PHP4
263022 by: Nathan Nobbe
263026 by: tedd
263028 by: Tony Marston
263029 by: Tony Marston
263032 by: Nathan Nobbe
263034 by: Robert Cummings
263035 by: Tony Marston
263036 by: Tony Marston
263038 by: Robert Cummings
263039 by: Nathan Nobbe
263041 by: Nathan Nobbe
Re: Looking for help with a complex algorithm
263023 by: Per Jessen
263024 by: Robin Vickery
263025 by: Jay Blanchard
263033 by: Robert Cummings
Editing an element within an array
263027 by: Brian Dunning
263031 by: Stut
263037 by: Robert Cummings
263040 by: Stut
Re: Something you can do with AJAX + PHP as well
263030 by: Per Jessen
Administrivia:
To subscribe to the digest, e-mail:
[EMAIL PROTECTED]
To unsubscribe from the digest, e-mail:
[EMAIL PROTECTED]
To post to the list, e-mail:
[EMAIL PROTECTED]
----------------------------------------------------------------------
--- Begin Message ---
On 10/9/07, Robert Cummings <[EMAIL PROTECTED]> wrote:
>
> On Tue, 2007-10-09 at 21:37 -0400, Nathan Nobbe wrote:
> >
> > does a lawn mower not have blades; and gas and a control panel; does it
> not
> > have
> > wheels? my object modeling of a lawn mower would certainly have these
> > things;
> > and most likely i would mark components of the engine as private.
> > and what does a lawn mower cut anyway; grass; i thought that was it. so
> if
> > there
> > was no grass could there still be an algorithm to cut it?
> > i say grass is part of the grass-cutting algorithm.
>
> Well it helps to refine the algorithm, but let's say we use fingers
> instead as our measure... I'm sure we would find that we could cut grass
> also >:)
good point.
the lawn mower is still consistent of its own parts and they
comprise its implementation.
-nathan
--- End Message ---
--- Begin Message ---
At 9:54 PM -0400 10/9/07, Nathan Nobbe wrote:
i hope i have been able to contribute something to someone
amidst all the arguing.
-nathan
Yeah, you gave me a better appreciation of some of the members of
this list -- we have some smart people here (i.e., Rob, Tony, Larry).
Cheers,
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--- End Message ---
--- Begin Message ---
"Larry Garfield" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> On Tuesday 09 October 2007, Tony Marston wrote:
>
>> I have bought and read several books on design patterns without being
>> impressed. I have seen countless examples of how particular design
>> patterns
>> can be implemented, and I am even more unimpressed. Unless you can
>> explain,
>> simply and with simple examples, of things that can ONLY be achieved
>> through the use of interfaces then I will continue to say tha they are a
>> waste of time.
>
> Classic OOP (C++/PHP/Java, etc.) is, really, an extension on top of
> procedural
> programming. Nothing that you do with objects couldn't be done without
> them.
> They are equally expressive.
>
> OOP is just syntactic sugar to make writing certain logical structures
> easier
> (less code, more readable code, code that's harder, but not impossible,
> for
> someone else to break, etc.). There is absolutely nothing that can ONLY
> be
> achieved through, for instance, interfaces. It's just that interfaces
> make
> it a hell of a lot easier to solve certain problems than doing everything
> with non-dynamic functions.
>
> Anything you can do with functions you can also do with GOTO statements.
> functions are just syntactic sugar to make writing logical structures
> easier.
> Until you can explain, with examples, some problem that can ONLY be solved
> through the use of functions I will continue to say that they are a waste
> of
> time.
>
> Anything that you can do in PHP you could also do in assembler. PHP is
> just
> syntactic sugar to make writing logical structures easier. Until you can
> explain, with examples, some problem that can ONLY be solved through the
> use
> of PHP I will continue to say that it is a waste of time.
>
> Yes, I am making fun of you for a piss-stupid Luddite argument. :-)
But you still cannot provide a convincing argument for using interfaces. The
fact that you CAN use interfaces does not mean that you MUST use interfaces.
I manage to get by without them.
>> *My* definition of encapsulation is *the* definition of encapsulation as
>> it
>> was originally intended. The fact that other smart arse numbskulls have
>> corrupted it with their own stupid embellishments is something which I
>> choose to ignore.
Encapsulation does not REQUIRE data to be hidden. It is an option, therefore
I have the right to exercse the option of ignoring it.
> *My* definition of dark green is *the* definition as it was originally
> intended. The fact that other smart-ass numbskulls have corrupted it with
> their own stupid saturation embellishments is something which I choose to
> ignore.
>
> (Yes, I am making fun of you again.)
What a pathetic example.
>> > but you admit, the php5 construct is better and therefore strengthen
>> > my
>> > initial argument, which was, if php5 is better and php4 is deprecated;
>> > why bother
>> > w/ php4.
>>
>> Your statement was that it would be absolutely pointless trying to learn
>> OOP with PHP 4 which implies that it is not possible to write object
>> oriented programs wth PHP 4 at all. That is absolute crap as PHP 4
>> provides
>> ALL the basic necessities for OOP, which are encapsulation, inheritance
>> and
>> polyorphism.
>
> Here's a better reason to not bother learning PHP 4-style OOP. You have
> less
> than a year until running PHP 4 at all is considered an unsupported
> security
> risk. Don't waste your time, since PHP 5 OOP IS different, because
> reference
> handling changed drastically which means what is "fast and easy" in PHP 4
> OOP
> and what is "fast and easy" in PHP 5 OOP is hugely different.
That still does not hide the fact that PHP 4's OOP style is still perfactly
valid. As for the new features in PHP 5, I shall only use them when they can
provide demonstrable benefits, and not just "because they are there".
>> > if you read that one chapter i recommended you will see; acquiring the
>> > behavior of the parent is not always desirable.
>>
>> Then why inherit from the parent in the first place? Or why not create a
>> method in the subclass which overrides the method in the superclass, thus
>> changing the behaviour to what you prefer? I certainly don't need
>> interfaces to do that.
>
> Because I want to syntactically guarantee the existence of certain methods
> so
> that I can reliably call functionality on them.
But if you have defined a method in a class then the method exists. You do
not need a separate interface to make that method accessible. That is my
whole point - interfaces are optional, not mandatory, and I have never found
agood reason to use them.
> Call-and-pray is an
> unreliable methodology. Yes, that is syntactic sugar. See above.
If you try to call an object method which did not exist you would soon know
about it, interface or not.
--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
> Can we stop with the pissing contest now?
>
> --
> Larry Garfield AIM: LOLG42
> [EMAIL PROTECTED] ICQ: 6817012
>
> "If nature has made any one thing less susceptible than all others of
> exclusive property, it is the action of the thinking power called an idea,
> which an individual may exclusively possess as long as he keeps it to
> himself; but the moment it is divulged, it forces itself into the
> possession
> of every one, and the receiver cannot dispossess himself of it." --
> Thomas
> Jefferson
--- End Message ---
--- Begin Message ---
"tedd" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> At 9:54 PM -0400 10/9/07, Nathan Nobbe wrote:
>> i hope i have been able to contribute something to someone
>>amidst all the arguing.
>>
>>-nathan
>
>
> Yeah, you gave me a better appreciation of some of the members of this
> list -- we have some smart people here (i.e., Rob, Tony, Larry).
When people use the word "smart" to describe me they usually follow it up
with "ass". :)
--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
> Cheers,
>
> tedd
>
> --
> -------
> http://sperling.com http://ancientstones.com http://earthstones.com
--- End Message ---
--- Begin Message ---
On 10/10/07, tedd <[EMAIL PROTECTED]> wrote:
>
> At 9:54 PM -0400 10/9/07, Nathan Nobbe wrote:
> > i hope i have been able to contribute something to someone
> >amidst all the arguing.
> >
> >-nathan
>
>
> Yeah, you gave me a better appreciation of some of the members of
> this list -- we have some smart people here (i.e., Rob, Tony, Larry).
>
> Cheers,
>
> tedd
i was waiting for an insult to that remark; thanks for filling the slot
tedd.
-nathan
--- End Message ---
--- Begin Message ---
On Wed, 2007-10-10 at 08:55 -0400, tedd wrote:
> At 9:54 PM -0400 10/9/07, Nathan Nobbe wrote:
> > i hope i have been able to contribute something to someone
> >amidst all the arguing.
> >
> >-nathan
>
>
> Yeah, you gave me a better appreciation of some of the members of
> this list -- we have some smart people here (i.e., Rob, Tony, Larry).
All lies... I'm winging it... someday you'll see through my smoke and
mirrors :)
Cheers,
Rob.
--
...........................................................
SwarmBuy.com - http://www.swarmbuy.com
Leveraging the buying power of the masses!
...........................................................
--- End Message ---
--- Begin Message ---
""Nathan Nobbe"" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> On 10/9/07, Tony Marston <[EMAIL PROTECTED]> wrote:
>>
>>
>> ""Nathan Nobbe"" <[EMAIL PROTECTED]> wrote in message
>> news:[EMAIL PROTECTED]
>> > On 10/9/07, Tony Marston <[EMAIL PROTECTED]> wrote:
>> >>
>> >> There's your problem, then. You are wasting your time trying to
>> implement
>> >> fancy design patterns just because design patterns are supposed to be
>> >> "cool". Loader class, Controller Base class, Front Controller - I
>> >> have
>> >> none
>> >> of that crap in my code, so I don't encounter the problems you have.
>> >>
>> >
>> > your site talks about the use of mvc in radicore so we can assume youre
>> > using at least one design pattern;
>>
>> When I say that I do not use design patterns I mean that I do not try to
>> implemet as many design patterns as possible,
>
>
> i never suggested any one do that nor did i suggest that i do that.
>
> and I certainly do not litter
>> my conversations with this pattern or that pattern.
>
> then you failed to grasp one of the most important concepts about design
> patterns.
> they provide a common vocabulary to allow designers to talk about
> implementing
> systems at a high level without having to waste time talking about
> specific
> details.
Yes, but many of today's programmers regard design patterns with a religious
zeal which borders on fanaticism. Such people cannot bring anything of value
to the conversation.
> In the whole of my
>> career there have only ever been two patterns that I have read about and
>> subsequently sought to implement, and thoose are the Three Tier
>> Architecture
>> and the Singleton. The fact that my framework contains an example of MVC
>> is
>> pure coincidence. It just happened that way. It was only after someone
>> else
>> looked at my code and said "You've implemented the MVC pattern" that I
>> read
>> about it and discovered that it was a perfect match. I certainly did not
>> read about MVC and then try to implement it.
>
>
> that is very common. many if not most people implement patterns all the
> time and dont realize it.
Just because someone may see a pattern in my code does not mean that I first
studied the pattern and then implemented it. I wrote the code without any
knowledge of the pattern, therefore I did not use the pattern when writing
my code.
> one benefit of patterns is people can communicate at the pattern
> level. recognition is powerful as well; if you happen to see a pattern
> when
> you are working out a design; the pieces just fall into place; or at least
> you have
> a road map.
I don't waste my time talking about design patterns to the pattern zealots.
It is as productive as attempting to have a theological discussion with a
religeous fanatic - a total waste of time unless you share their point of
view.
Too many of today's programmers can only "see" patterns after they have read
about them in a book. They vie with each other to see how many patterns they
can juggle at the same time, or complain that someone els'e implementation
of a particular pattern is "unsound" or "impure".
>> and if radicore has a single point of entry you are using a front
>> controller.
>>
>> I do not have a single point entry. Each page has its own controller, so
>> I
>> have hundreds of page controllers. A single front controller is something
>> that is used in compiled languages.
>
>
> its also used heavily in php; take a look at code igniter for a first
> example.
Just because people use font controllers in PHP does not make them a good
thing. I personally think they are crap.
>> might want to knock the dust off the old design pattern books just to jog
>> > your memory:)
>>
>> I've put my books of design patterns on the top shelf where they shall
>> remain untouched as they contain nothing of use to me. They may give
>> hints
>> to novices, but I am no longer a novice. I no longer need training wheels
>> on my bicycle, so why do I need books on design patterns?
>
>
> ill be honest; i like what youve done w/ xsl, you even taught me some
> things; but when it comes to oop; well i think you should get the books
> back down from the top shelf.
>
> -nathan
>
Why? What is wrong with my implementation of OOP? Surely I have used
encapsulation, inheritance and polymorphism to increase code reuse and
decrease maintenance, therefore my implementation can only be regarded as
effective. The fact that you don't like my style is your problem, not mine.
--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
--- End Message ---
--- Begin Message ---
""Nathan Nobbe"" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> On 10/9/07, Robert Cummings <[EMAIL PROTECTED]> wrote:
>>
>> On Tue, 2007-10-09 at 21:37 -0400, Nathan Nobbe wrote:
>> >
>> > does a lawn mower not have blades; and gas and a control panel; does it
>> not
>> > have
>> > wheels? my object modeling of a lawn mower would certainly have these
>> > things;
>> > and most likely i would mark components of the engine as private.
>> > and what does a lawn mower cut anyway; grass; i thought that was it.
>> > so
>> if
>> > there
>> > was no grass could there still be an algorithm to cut it?
>> > i say grass is part of the grass-cutting algorithm.
>>
>> Well it helps to refine the algorithm, but let's say we use fingers
>> instead as our measure... I'm sure we would find that we could cut grass
>> also >:)
>
>
> good point.
> the lawn mower is still consistent of its own parts and they
> comprise its implementation.
>
> -nathan
Yes, but a lawnmower does not contain grass, it is used to cut grass. Whole
grass goes in and cut grass comes out. The lawnmower does not have any grass
in its construction, therefore grass is not part of the implementation.
--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
--- End Message ---
--- Begin Message ---
On Wed, 2007-10-10 at 14:44 +0100, Tony Marston wrote:
> ""Nathan Nobbe"" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > On 10/9/07, Robert Cummings <[EMAIL PROTECTED]> wrote:
> >>
> >> On Tue, 2007-10-09 at 21:37 -0400, Nathan Nobbe wrote:
> >> >
> >> > does a lawn mower not have blades; and gas and a control panel; does it
> >> not
> >> > have
> >> > wheels? my object modeling of a lawn mower would certainly have these
> >> > things;
> >> > and most likely i would mark components of the engine as private.
> >> > and what does a lawn mower cut anyway; grass; i thought that was it.
> >> > so
> >> if
> >> > there
> >> > was no grass could there still be an algorithm to cut it?
> >> > i say grass is part of the grass-cutting algorithm.
> >>
> >> Well it helps to refine the algorithm, but let's say we use fingers
> >> instead as our measure... I'm sure we would find that we could cut grass
> >> also >:)
> >
> >
> > good point.
> > the lawn mower is still consistent of its own parts and they
> > comprise its implementation.
> >
> > -nathan
>
> Yes, but a lawnmower does not contain grass, it is used to cut grass. Whole
> grass goes in and cut grass comes out. The lawnmower does not have any grass
> in its construction, therefore grass is not part of the implementation.
It may not be part of it's construction, but it certainly is a member
variable. It's along the lines of the following:
<?php
class Lawnmower
{
var $works = true;
var $grassyMulchCrapStuckToUndersideOfLawnmower = 0;
function mow( $what )
{
if( !$this->works )
{
return false;
}
if( $what == 'grass' )
{
$this->grassyMulchCrapStuckToUndersideOfLawnmower +=
rand( 1, 100 );
if( $this->grassyMulchCrapStuckToUndersideOfLawnmower >
1000 )
{
return $this->seize();
}
}
else
if( $what == 'fingers' )
{
// >:)
}
return true;
}
function seize()
{
return ($this->works = false);
}
}
?>
Cheers,
Rob.
--
...........................................................
SwarmBuy.com - http://www.swarmbuy.com
Leveraging the buying power of the masses!
...........................................................
--- End Message ---
--- Begin Message ---
On 10/10/07, Tony Marston <[EMAIL PROTECTED]> wrote:
>
>
> ""Nathan Nobbe"" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > On 10/9/07, Tony Marston <[EMAIL PROTECTED]> wrote:
> >>
> >>
> >> ""Nathan Nobbe"" <[EMAIL PROTECTED]> wrote in message
> >> news:[EMAIL PROTECTED]
> >> > On 10/9/07, Tony Marston <[EMAIL PROTECTED]> wrote:
> >> >>
> >> >> There's your problem, then. You are wasting your time trying to
> >> implement
> >> >> fancy design patterns just because design patterns are supposed to
> be
> >> >> "cool". Loader class, Controller Base class, Front Controller - I
> >> >> have
> >> >> none
> >> >> of that crap in my code, so I don't encounter the problems you have.
> >> >>
> >> >
> >> > your site talks about the use of mvc in radicore so we can assume
> youre
> >> > using at least one design pattern;
> >>
> >> When I say that I do not use design patterns I mean that I do not try
> to
> >> implemet as many design patterns as possible,
> >
> >
> > i never suggested any one do that nor did i suggest that i do that.
> >
> > and I certainly do not litter
> >> my conversations with this pattern or that pattern.
> >
> > then you failed to grasp one of the most important concepts about
> design
> > patterns.
> > they provide a common vocabulary to allow designers to talk about
> > implementing
> > systems at a high level without having to waste time talking about
> > specific
> > details.
>
> Yes, but many of today's programmers regard design patterns with a
> religious
> zeal which borders on fanaticism. Such people cannot bring anything of
> value
> to the conversation.
many is a generalization; i have never suggested that patterns be used
without regard
or care.
> In the whole of my
> >> career there have only ever been two patterns that I have read about
> and
> >> subsequently sought to implement, and thoose are the Three Tier
> >> Architecture
> >> and the Singleton. The fact that my framework contains an example of
> MVC
> >> is
> >> pure coincidence. It just happened that way. It was only after someone
> >> else
> >> looked at my code and said "You've implemented the MVC pattern" that I
> >> read
> >> about it and discovered that it was a perfect match. I certainly did
> not
> >> read about MVC and then try to implement it.
> >
> >
> > that is very common. many if not most people implement patterns all the
> > time and dont realize it.
>
> Just because someone may see a pattern in my code does not mean that I
> first
> studied the pattern and then implemented it.
precisely
I wrote the code without any
> knowledge of the pattern, therefore I did not use the pattern when writing
> my code.
you used the pattern and you didnt realize it until later; like i said;
very common.
> one benefit of patterns is people can communicate at the pattern
> > level. recognition is powerful as well; if you happen to see a pattern
> > when
> > you are working out a design; the pieces just fall into place; or at
> least
> > you have
> > a road map.
>
> I don't waste my time talking about design patterns to the pattern
> zealots.
> It is as productive as attempting to have a theological discussion with a
> religeous fanatic - a total waste of time unless you share their point of
> view.
>
> Too many of today's programmers can only "see" patterns after they have
> read
> about them in a book. They vie with each other to see how many patterns
> they
> can juggle at the same time, or complain that someone els'e implementation
> of a particular pattern is "unsound" or "impure".
>
> >> and if radicore has a single point of entry you are using a front
> >> controller.
> >>
> >> I do not have a single point entry. Each page has its own controller,
> so
> >> I
> >> have hundreds of page controllers. A single front controller is
> something
> >> that is used in compiled languages.
> >
> >
> > its also used heavily in php; take a look at code igniter for a first
> > example.
>
> Just because people use font controllers in PHP does not make them a good
> thing. I personally think they are crap.
i prefer them over multiple points of entry; apparently code igniter does
too.
>> might want to knock the dust off the old design pattern books just to jog
> >> > your memory:)
> >>
> >> I've put my books of design patterns on the top shelf where they shall
> >> remain untouched as they contain nothing of use to me. They may give
> >> hints
> >> to novices, but I am no longer a novice. I no longer need training
> wheels
> >> on my bicycle, so why do I need books on design patterns?
> >
> >
> > ill be honest; i like what youve done w/ xsl, you even taught me some
> > things; but when it comes to oop; well i think you should get the books
> > back down from the top shelf.
> >
> > -nathan
> >
>
> Why? What is wrong with my implementation of OOP? Surely I have used
> encapsulation, inheritance and polymorphism to increase code reuse and
> decrease maintenance, therefore my implementation can only be regarded as
> effective. The fact that you don't like my style is your problem, not
> mine.
because you keep saying you dont see the benefits of the features in php5;
repeatedly you ask why, why, why; why interfaces for example.
as i have suggested; pick up a book and read about it.
-nathan
--- End Message ---
--- Begin Message ---
On 10/10/07, Tony Marston <[EMAIL PROTECTED]> wrote:
>
>
> ""Nathan Nobbe"" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > On 10/9/07, Robert Cummings <[EMAIL PROTECTED]> wrote:
> >>
> >> On Tue, 2007-10-09 at 21:37 -0400, Nathan Nobbe wrote:
> >> >
> >> > does a lawn mower not have blades; and gas and a control panel; does
> it
> >> not
> >> > have
> >> > wheels? my object modeling of a lawn mower would certainly have
> these
> >> > things;
> >> > and most likely i would mark components of the engine as private.
> >> > and what does a lawn mower cut anyway; grass; i thought that was it.
> >> > so
> >> if
> >> > there
> >> > was no grass could there still be an algorithm to cut it?
> >> > i say grass is part of the grass-cutting algorithm.
> >>
> >> Well it helps to refine the algorithm, but let's say we use fingers
> >> instead as our measure... I'm sure we would find that we could cut
> grass
> >> also >:)
> >
> >
> > good point.
> > the lawn mower is still consistent of its own parts and they
> > comprise its implementation.
> >
> > -nathan
>
> Yes, but a lawnmower does not contain grass, it is used to cut grass.
> Whole
> grass goes in and cut grass comes out. The lawnmower does not have any
> grass
> in its construction, therefore grass is not part of the implementation.
yes, you are right about the grass not being part of the implementation; i
agree.
however; the mower as i pointed out has its own member variables that
comprise its
implementation; and earlier you suggested that member variables dont
constitute the
implementation of a class; which is wrong;
here is my example; note; no grass; but it has member variables which
comprise its implementation.
<?php
class Lawnmower {
const GAS_MIN = 0;
const GAS_MAX = 10;
private $engineOn = false;
private $gasLevel = 5;
public function turnOn() {
if($this->canRun()) {
$this->engineOn = true;
} else {
echo 'you need to add gas to run the mower!';
}
}
public function mowGrass() {
echo 'the grass [or pehaps fingers] are being cut...';
}
public function turnOf() {
$this->enginOn = false;
}
public function addGas($amountOfGas) {
if(($amountOfGas + $this->gasLevel) > self::GAS_MAX) {
$this->amountOfGas += $amountOfGas;
} else {
echo 'you will overflow the tank w/ all that gas.';
return false;
}
}
public function checkGasLevel() {
return $this->gasLevel;
}
private function canRun() {
$canRun = false;
if($this->gasLevel > self::GAS_MIN) {
$canRun = true;
}
return $canRun;
}
}
?>
-nathan
--- End Message ---
--- Begin Message ---
Robert Cummings wrote:
> On Tue, 2007-10-09 at 15:28 -0500, Jay Blanchard wrote:
>> [snip]
>> This *IS* the knapsack problem. Just because you specify date ranges
>> to get your values and the value isn't a knapsack doesn't change the
>> fact that it is the same problem :) I remember having fun with
>> genetic algorithms and the knapsack problem back in University.
>> [/snip]
>>
>> You're right save for the knapsack problem considered not only
>> volume, but value as well.
>
> So imagine the value of all of your items is equal to it's size.
> Voila, same problem.
>
Which means it surely been solved before:
http://en.wikipedia.org/wiki/Knapsack_problem
I've never heard of it either, but it sounds a lot like the travelling
salesman problem?
/Per Jessen, Zürich
--- End Message ---
--- Begin Message ---
On 09/10/2007, Jay Blanchard <[EMAIL PROTECTED]> wrote:
> Good afternoon gurus and guru-ettes!
>
> I am searching for an algorithm that will take a list of monetary values
> and determine which of these values totals a value supplied to the
> widget.
>
> 1. I supply a value to the application and give a date range
> 2. The application will query for all of the values in the date range
> 3. The application will determine which of the values will total the
> supplied value
> a. if the total values in the date range do not add up to the
> supplied value the application will return that info. (I have this done
> already)
> 4. The application will return the records comprising the total value
> given
>
> For instance I supply 10.22 and a date range of 2007-10-01 to 2007-10-05
>
> Values in the range;
> 3.98
> 9.77
> 3.76
> 4.13
> 7.86
> 1.45
> 12.87
> 10.01
> 0.88
>
> Values comprising the total;
> 3.76
> 4.13
> 1.45
> 0.88
>
> It is possible to have duplicate values, so we will have to assume that
> the first one of the dupes is correct, the records will be sorted by
> date. I have been working with a recursive function, but so far the
> results are not pretty and it is getting too complex.
>
> FYI, this is very similar to the "knapsack problem"" in dynamic
> programming.
it is a special case of the knapsack problem - the "subset sum" problem.
You'll need to be aware that it's of exponential complexity. So make
sure your lists of possible values don't get big. Anyway, here's my
answer:
<?php
function subsetSum ($target, $list) {
$partition = (int) (sizeof($list)/2);
$listA = powerset(array_slice($list, 0, $partition));
$listB = powerset(array_slice($list, $partition));
krsort($listA, SORT_NUMERIC);
ksort($listB, SORT_NUMERIC);
$target = round($target, 2);
foreach (array_keys($listA) as $A) {
foreach (array_keys($listB) as $B) {
$sum = round($A + $B, 2);
if ($sum < $target) continue 1;
if ($sum > $target) continue 2;
return array_merge($listA[$A], $listB[$B]);
}
}
return false;
}
function powerset($list) {
if (empty($list)) return array();
$value = array_pop($list);
$A = powerset($list);
$B = array();
foreach ($A as $set) {
$set[] = $value;
$B[(string) array_sum($set)] = $set;
}
return array_merge(array("$value" => array($value)), $A, $B);
}
?>
--- End Message ---
--- Begin Message ---
[snip]
it is a special case of the knapsack problem - the "subset sum" problem.
You'll need to be aware that it's of exponential complexity. So make
sure your lists of possible values don't get big. Anyway, here's my
answer:
[/snip]
Thanks to all who responded, some very cool solutions and some more
insight to the problem ("subset sum" as Robin mentions above) has given
me the wheels that I need to solve the issue at hand.
I wanted to point out something here while we are discussing this; there
are tons of algorithms out there for solutions to problems such as these
and we sometimes as developers (instead of mathematicians or logicians)
fail to look for these when trying to solve the problem. My problem in
this case was that I knew that there was an algorithm I just could not
recall what it was. I managed to get close (knapsack problem) and the
solutions provided by several of you were elegant and work well.
There is another ongoing thread about OOP in PHP4 that has approached
holy war status. I fear that some are overlooking solutions to problems
that already have been solved. Things such as interfaces and design
patterns exist because they are solutions to problems just as
identifying the right problem here leads to a solution (the algorithm).
--- End Message ---
--- Begin Message ---
On Wed, 2007-10-10 at 07:00 -0500, Jay Blanchard wrote:
>
> There is another ongoing thread about OOP in PHP4 that has approached
> holy war status. I fear that some are overlooking solutions to problems
> that already have been solved. Things such as interfaces and design
> patterns exist because they are solutions to problems just as
> identifying the right problem here leads to a solution (the algorithm).
This is why in computer science they expose you to a lot of different
kinds of algorithms and have you determine their complexity. And when I
say complexity I don't mean complexity to implement, I mean run-time
complexity. As has been pointed out, the knapsack problem has
exponential complexity and therefore will almost certainly suck for any
moderately large data set-- regardless of who provides you with a
solution :)
As for interfaces and design patterns, these are more subjective than
pure solutions and that is why they are open prone to Jihad :) This is
more akin to implementations of the knapsack solution than it is to the
knapsack algorithm itself. As someone pointed out, all programming
languages are Turing complete and as such any language can solve the
problems another language solves. This is easy to visualize when you
know that all languages must be converted to machine language to run. As
such they are all equivalent. There is nothing possible in OOP that is
not possible in assembler. However, there is often great convenience in
using some of the higher level constructs, but as with anything
subjective, your mileage may vary.
Cheers,
Rob.
--
...........................................................
SwarmBuy.com - http://www.swarmbuy.com
Leveraging the buying power of the masses!
...........................................................
--- End Message ---
--- Begin Message ---
I have a two-dimensional array that looks like this:
array(
array(0, 123),
array(0, 234),
array(0, 345),
array(0, 456)
)
I want to REMOVE any element that contains 234, and I want to
INCREMENT the first value for any element that contains 345, to make
it look like this:
array(
array(0, 123),
array(1, 345),
array(0, 456)
)
A pint of cachaça to he who sets me on the path to success.
--- End Message ---
--- Begin Message ---
Brian Dunning wrote:
I have a two-dimensional array that looks like this:
array(
array(0, 123),
array(0, 234),
array(0, 345),
array(0, 456)
)
I want to REMOVE any element that contains 234, and I want to INCREMENT
the first value for any element that contains 345, to make it look like
this:
array(
array(0, 123),
array(1, 345),
array(0, 456)
)
http://dev.stut.net/php/dunning.php
A pint of cachaça to he who sets me on the path to success.
Don't really know what that is, but OK!
-Stut
--
http://stut.net/
--- End Message ---
--- Begin Message ---
On Wed, 2007-10-10 at 14:36 +0100, Stut wrote:
> Brian Dunning wrote:
> >
> > A pint of cachaça to he who sets me on the path to success.
>
> Don't really know what that is, but OK!
I'll be back with your pint a minute... right now I have to go to the
washroom... why am I carrying this empty pint glass?? *cough cough* Um,
no reason. Ok, I'll be right back.
.
..
...
Ok, all done and here's your cachaça. Why's it warm? Oh that's just
traditional serving style. Why's it yellow? Oh that's because it's
loaded with unprocessed^H^H^H^H^H^H^H^H^H^H^H niacin.
Cheers,
Rob.
--
...........................................................
SwarmBuy.com - http://www.swarmbuy.com
Leveraging the buying power of the masses!
...........................................................
--- End Message ---
--- Begin Message ---
Robert Cummings wrote:
On Wed, 2007-10-10 at 14:36 +0100, Stut wrote:
Brian Dunning wrote:
A pint of cachaça to he who sets me on the path to success.
Don't really know what that is, but OK!
I'll be back with your pint a minute... right now I have to go to the
washroom... why am I carrying this empty pint glass?? *cough cough* Um,
no reason. Ok, I'll be right back.
.
..
...
Ok, all done and here's your cachaça. Why's it warm? Oh that's just
traditional serving style. Why's it yellow? Oh that's because it's
loaded with unprocessed^H^H^H^H^H^H^H^H^H^H^H niacin.
That'll teach me for accepting a bribe!
-Stut
--
http://stut.net/
--- End Message ---
--- Begin Message ---
Colin Guthrie wrote:
>> What do you think of this?
>>
>> http://webbytedd.com/b/timed-php/
>>
>> It's a combination of using both ajax and php together.
>
> I'd say overkill for the end result (could be done in plain Javascript
> without any "Ajax" calls),
That was my reaction too. I've used iframe/rpc for about three years -
ajax doesn't improve on the basic functionality.
/Per Jessen, Zürich
--- End Message ---