Warning to newbies

S

Seebs

What does it mean for code to "work"? One meaning is that it
meets its specification, in the sense that if the input meets the
stated preconditions the output meets the stated postconditions.
As best I can tell, Seebs's code does that. That he didn't write
a more-general function for replacing text within a string -- I
suppose one could make a case for the idea that this is not best
practice, because some future reader of the code might think,
despite clear documentation of its limitations, that it was more
general than it is. Whether that's worse than not solving the
immediate problem within the time constraints ....

In a fit of irony, the spec has since been changed such that it now needs
to be a general replace, so I conveniently dropped in the one I had lying
around from this thread, and it's apparently working nicely in production.
Well, I would certainly never have guessed that as a reason.

Me neither. My experience has been that my manager is the guy who keeps
other people from breathing down my neck and ensures that I have the resources
and tools needed to get things done properly.

-s
 
S

spinoza1111

spinoza1111wrote:


I've skipped past most of thespinoza1111junk that's accumulated in the
day or two I've been away, but I found the above sentence particularly
amusing. I have practically zero knowledge of TV, since I don't watch TV
  (except accidentally, e.g. in friends' houses), and have not done so
for many a long year. My knowledge of TV is quite possibly the
shallowest of almost any adult in the developed world.


Bugs in your code aren't sufficient justification for capering and
gibbering. If they were, I'd be a gibbering wreck by now. No, they are
opportunities for you to learn. Since you have in the past proved
unwilling to learn from your bugs, I tend only to point out the first
one I see (if that).

Screw you, asshole. I've been fixing all bugs found. You've been
running your mouth.
 
S

spinoza1111

Boyer Moore doesn't require preprocessing/tables.  Knuth Morris
Pratt does.  Both can be implemented as re-entrant routines.
There are faster algorithms that do require significant
reprocessing.  BM and KMP can be implemented re-entrantly as can
almost all algorithms.  Requiring re-entrancy is (almost)
orthogonal to OO.

That's not my information: cf. "A Fast String Searching Algorithm"
CACM 1977.

Sure, you can make calls to a KMP or BM re-entrant...by requiring the
user to pass the address of a pointer to the tables which you then set
when it is zero, and re-use whenever necessary, perhaps by hashing on
the target. The OO solution is much nicer although it means that BM
and KMP have state.
 
S

spinoza1111

spinoza1111  said:
[ snip ]

[ "he" == Seebs ]
So don't agree. However, he's established it:
"The heap is a DOS term".

A statement which he later acknowledged was not the best way
of making his intended point.  Why keep quoting a phrase that
its author has acknowledged was unclear?  (Mostly a rhetorical
question.)

Because Seebach insists on hounding Schildt and in his latest stunt,
presenting a solution of mine that was out of date.

He wants to be forgiven for his mistakes but won't let others' errors
alone.
What does it mean for code to "work"?  One meaning is that it
meets its specification, in the sense that if the input meets the
stated preconditions the output meets the stated postconditions.
As best I can tell, Seebs's code does that.  That he didn't write

No, it doesn't. If percent occurs anywhere else in the input the code
will break. If it happens to occur at the end, his code may very well
crash. Other people have presented similarly buggy solutions without a
testing framework like mine and have admitted bugs without, for the
most part, fixing them. Whereas I have responded to each and every bug
report I've seen.
a more-general function for replacing text within a string -- I
suppose one could make a case for the idea that this is not best
practice, because some future reader of the code might think,
despite clear documentation of its limitations, that it was more
general than it is.  Whether that's worse than not solving the
immediate problem within the time constraints ....

C was the wrong tool for the specific job. I am not saying that I
would have solved the same problem with a fully general replace(),
although I might if I foresaw a need for it elsewhere. Seebach wanted
to establish chops in C, so he presented the solution of a problem
that should have used a language with replace() already available.
I think I'll leave it at that.







Well, I would certainly never have guessed that as a reason.
In my experience, CS instructors are not immune to the temptation
to invent silly rules and penalize those who break them.  *Good*
CS instructors may encourage their students to think deeply
about what they're doing, but it's far from clear to me that all
instructors meet this lofty goal.

That's what I did when I taught computer science. I agree that there's
a lot of bozos out there. I mean rather the entire intellectual
climate of the university, and its encouragement of a common search
for truth. I realize, however, that for many universities and
students, "the common search for truth" is a sick joke. Which is why I
counted myself fortunate that my REXX and IBM VM skills landed me a
job at Princeton, a place with many students actually interested in
truth.
Again, probably best to stop there.

[ snip ]
 
S

spinoza1111

[ snip ]
It's in the fact that he's unqualified,
[ "he" == Seebs ]
I don't agree with you that this is an established fact.
So don't agree. However, he's established it:
"The heap is a DOS term".
A statement which he later acknowledged was not the best way
of making his intended point.  Why keep quoting a phrase that
its author has acknowledged was unclear?  (Mostly a rhetorical
question.)

Because Spinny's not interested in facts or the truth. He's interested
in *winning*. This puts him in the same camp as the Soviets, North
Vietnamese, Nazis, and other fascists who were not interested in
negotiating in good faith.

You got that shit essentially right. But in order to win, I've sat on
my ass coding, testing and fixing bugs, while other posters have been
running their mouths.

The Soviets won the Second World War, not the Americans and British.

The North Vietnamese negotiated in good faith. Nixon's response was
the "mad bomber" Christmas bombing of North Vietnam, which alone
unleashed more tonnage that was released by the Allies in WWII. The
North Vietnamese then went on to WIN because their cause was JUST.

And neither were Nazis or Fascists.
 
S

Seebs

I know you'll find this hard to believe, but
there's more to life than comp.lang.c.

Eek!

No wonder my boss never seems to accept "argued with Edward Nilges" as
a week's work. :p

Actually, in a fit of irony, we ended up needing the revised string replace
function I posted here the other day, because new data revealed that our
original spec wasn't flexible enough. Needless to say, it worked fine
on the first try. :p

-s
 
S

spinoza1111

spinoza1111wrote:

The gobble-d-gook generator is back. It was a nice day and a half.

It's not gobble-d-gook. It's knowing that in C, "static" or "global"
"tables" are points of failure because it's impossible to hide them
inside the object. Instead, for re-entrancy, the calling program has
to hold their values on behalf of the re-entrant routine. This means
that C programming is essentially stuck in the era of the IBM 360
where in calling a routine in assembler, the caller had to provide a
region where the subroutine could save registers.
 
S

spinoza1111

Eek!

No wonder my boss never seems to accept "argued with Edward Nilges" as
a week's work.  :p

Actually, in a fit of irony, we ended up needing the revised string replace
function I posted here the other day, because new data revealed that our
original spec wasn't flexible enough.  Needless to say, it worked fine
on the first try.  :p

....and when will you get around to fixing its bugs? "It works fine" is
incompetent programmerese for "it seems to work".

Your behavior from the start of this thread has been to post buggy
code and refuse to fix it, and lie about and backstab people as in the
case where in another thread you presented an old version of my code.

I really am glad that I do not work at your company, and have to clean
up your messes.

How do you know whether the specific string.h library functions you
use are correct? Someone here has mentioned implementations of
bsearch.h that don't work for large data structures. Does strstr work
for all possibilities?

And doesn't it encourage sloppy thinking? If a programmer mindlessly
uses strstr, in your spirit in other words, there's a strong
possibility he will get to the next string by ADDING THE LENGTH of a
found sequence to an index:

char * ptrIndex1;
ptrIndex1 = strMaster;
while (*ptrIndex1)
{ ptrIndex1 = strstr(strMaster, strTarget); if (*ptrIndex1) intCount+
+; ptrIndex1 += strlen(strTarget); }

In any application that needs to worry about overlapping strings, this
code is wrong. In real "shops", the reaction when such a bug is
exposed (when, for example, the user "wanted" overlapping strings, or,
far better, the facts and the law require it, many users themselves
being themselves ignorant of requirements), the reaction is without
dignity, intellect or heart: it is to finger-point at the user, and to
renarrate him or her in the feminine register as one who "can't make
up their mind".

A fundamental strategy is in fact renarration of human problem solving
as feminine, as in the case of the programmer who conscientiously on
the job documents and fixes bugs as they arise, only to be renarrated
as "writing code with many bugs" by the local office thugs, who
themselves (unable to declare that they are the perfect Father who
never in the dreamtime makes a mistake) admit their bugs but then
don't fix them, saying instead that only a (feminized) Fool would
worry about such marginal conditions (as a percent sign not followed
by an s).

When I started at Princeton, another employee with considerable macho
issues, a member of the Olympics rowing team, vandalized a change
record I'd made with lines like "Ed added a bug". I'd been hired to
control his behavior which included putting public messages on
Princeton's IBM mainframe such as "baby locked in microwave chews off
own foot". With a student employee (who later became a Microsoft
executive) I fixed this problem. But such employees often dominate
commercial "shops" and are celebrated as "hackers", while in fact they
worship technology and silly ideas...it's people they shit upon.

In short, the saw, that "good programmers never blame their tools and
always use libraries" is just wrong. Good, or great programmers are
not little tradesmen who love their tools. They are instead critical
intellectuals who prefer people to tools, humanity to ideas. They have
a critical relationship to their tools and they are in fact eager to
solve problems in a revolutionary and disruptive fashion, like Che
Guevara and unlike Barack Obama.

"Many will call me an adventurer — and that I am, only one of a
different sort: one of those who risks his skin to prove his
platitudes." - Che Guevara

And if this is alarming: note that in places like "Wind River
Systems", the leaders often have modeled their careers of capitalistic
and Schumpeterian "primitive accumulation" and "creative
destruction" (of lives and careers). But this expropriation results in
what I call the "monastic" or "madrassah" programming "shop".

This is one where the programmers must cultivate a different ethic,
one of absolute subservience to the authority of people, ideas and
tools, which has in my experience become almost mediaeval, lest the
top men (and they are usually men) be expropriated in turn of that
which they have expropriated.
 
S

spinoza1111

1.  Such an instruction may exist.  Some processors have hardware
string processing.

These were popular when memory was inexpensive in the 1970s relative
to CPUs, and such unbounded operations could be implemented in
microcode. Today, the existence of such instructions is folklore, and
folklore isn't an argument.
2.  "Best" algorithm is always restricted by input set.  For the input
set I was using, a fairly naive strstr wins handily.  For some input
sets, yours will be horrible and the "super studly" algorithms will
win by enough that the preprocessing and tables are worth it.  So
no, yours wasn't "the best", it was just "a pretty good bet most of the
time" -- and on at least one system I know, strstr() uses precisely
that algorithm, only without the bugs.

How would you know whether any given implementation of strstr contains
bugs? You have no way in fact of knowing this.
Not really.


No.

"No" is an argument only for a toddler.

No, Walter descended into incoherence. What does "orthogonal to OO"
even mean?
 
S

spinoza1111

The more I think about it, the more I think this is not a mere
quibble about terminology, and that it does matter whether it's
the problem or the solution that's "NP complete" -- I mean, the
point is not that an NP-complete problem has *A* solution that's
not polynomial-time, but that *ALL* solutions are thought to be
in that category.

That such problems may have approximate solutions that are
polynomial-time is useful in practice but somewhat beside the
point.



You know, I'm inclined to think that quibbling about terminology
is apt to be *more* prevalent in academia than elsewhere.
Perhaps your experience differs from mine in that regard.

That is indeed a common perception and it's a reality insofar as some
universities (especially mega-state schools where most of the students
don't know what the hell they are even doing there, apart from
majoring in pre-wealth and having sex). It's created by a form of
"blowback" in which the "businesslike" ethos of mindlessly mastering
terminology enters the university, and to get tenure, English
professors learn to master fashionable buzzwords such as
"deconstruction".

They do so in the USA without typically being exposed to a classical
and European corpus. For example, they never have read Plato's
Republic in its entirety despite the fact that the late Derrida,
supposed an avatar of deconstruction, reread Plato's complete works
every year for *merde et rire* (shits and giggles).

They get their ticket punched, and in fact as English professors are
more like little office creeps than what I would consider a professor,
precorrupted by the Darwinian brutality of tenure in universities
dominated by used car salesmen and insect exterminators in state
legislatures.

Marked, wounded, as are people here by Adorno's "inner contour of
their weakness", they are then unleashed on survey and intermediate
classes, there to subject their students to the only form of
"discipline" they or their charges have ever known: basically rote and
grade school discipline which everyone tacitly regards as a sort of
meaningless ritual that is a prerequisite for entry into the American
Orgy of consumption.

So yeah, "terminology" is also used in academia. I am referring to my
Idea of academia, not the devolved reality.
 
S

spinoza1111

spinoza1111wrote:


Actually, no, I've been doing something rather more productive than
that. I've been developing real software and putting it into real
production in the real world, where your deconstructionist nonsense
wouldn't last five minutes. I know you'll find this hard to believe, but
there's more to life than comp.lang.c.

What I find hard to believe is:

* That you are currently anything but unemployed
* That you are not in fact obsessed with posting here (you don't take
sabbaticals), or SAMS does not pay you

You haven't contributed a single meaningful bug report. Nor have you,
or Seebach, contributed any bug fixes or code that meets the problem
statement.

Whereas I've evolved a solution to a hard problem and fixed bugs as
they've arisen.
 
W

Willem

For the terminally insane: Figure this one out.
Untested, about half an hour's work.
I'm sure somebody can pick this one apart.

#include <stdlib.h>

char *repstr(char *s, char *m, char *r, char *t)
{
size_t sl = 0, ml = 0, tl = 0;
if (!s || !m || !r || !*m) { return 0; }
while (s[sl]) {
if (m[ml] == 0) {
if (t) { for (rl = 0; r[rl]; rl++) { t[rl] = r[rl]; } }
tl += rl;
sl += ml;
ml = 0;
} else if (m[ml] != s[sl+ml]) {
if (t) { t[tl] = s[sl]; }
tl++;
sl++;
ml = 0;
} else {
ml++;
}
}
if (t) {
t[tl] = 0;
return t;
} else if (t = malloc(tl + 1)) {
return repstr(s, m, r, t);
} else {
return 0;
}
}

char *replace_string(char *str, char *match, char *replace)
{
return repstr(str, match, replace, 0);
}


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
S

spinoza1111

For the terminally insane: Figure this one out.

"You talkin' to me?" - Taxi Driver
Untested, about half an hour's work.

Why do people always brag about how quick they work, at the cost of
presenting software that is either untested or buggy? All the animals
come out at night...queers fairies dopers junkies...lissen you
screwheads...
I'm sure somebody can pick this one apart.
Looks like I'm the only one crazy enough...
#include <stdlib.h>

char *repstr(char *s, char *m, char *r, char *t)
{
 size_t sl = 0, ml = 0, tl = 0;
 if (!s || !m || !r || !*m) { return 0; }
 while (s[sl]) {
  if (m[ml] == 0) {
   if (t) { for (rl = 0; r[rl]; rl++) { t[rl] = r[rl]; } }
   tl += rl;
   sl += ml;
   ml = 0;
  } else if (m[ml] != s[sl+ml]) {
   if (t) { t[tl] = s[sl]; }
   tl++;
   sl++;
   ml = 0;
  } else {
   ml++;
  }
 }
 if (t) {
  t[tl] = 0;
  return t;
 } else if (t = malloc(tl + 1)) {
  return repstr(s, m, r, t);

Ahhhhh...

"A thing of beauty is a joy forever! Mah man John Keats said that!" -
White Men Can't Jump

You know, mijn Herr, in Amerika we have a saying. "Never play poker
with a man named Doc". Well, "always expect a Dutchman to transform a
data structure into a recursive procedure." You are Dutch, right? "Of
Nassau and Orania, a true Dutch Prince am I!" Cue Het Wilhelmus.

An ELEGANT appearing solution. I shall test it for you. This may be
the one entry that is better than mine.
 
S

spinoza1111

For the terminally insane: Figure this one out.
Untested, about half an hour's work.
I'm sure somebody can pick this one apart.

#include <stdlib.h>

char *repstr(char *s, char *m, char *r, char *t)
{
 size_t sl = 0, ml = 0, tl = 0;
 if (!s || !m || !r || !*m) { return 0; }
 while (s[sl]) {
  if (m[ml] == 0) {
   if (t) { for (rl = 0; r[rl]; rl++) { t[rl] = r[rl]; } }
   tl += rl;
   sl += ml;
   ml = 0;
  } else if (m[ml] != s[sl+ml]) {
   if (t) { t[tl] = s[sl]; }
   tl++;
   sl++;
   ml = 0;
  } else {
   ml++;
  }
 }
 if (t) {
  t[tl] = 0;
  return t;
 } else if (t = malloc(tl + 1)) {
  return repstr(s, m, r, t);
 } else {
  return 0;
 }

}

char *replace_string(char *str, char *match, char *replace)
{
 return repstr(str, match, replace, 0);

}

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Willem:

1. I wish I'd answered this before, and I apologize on behalf of the
slobs,thugs and creeps in this newsgroup for the lack of initial
response to your submission

2. I was, and remain, very impressed by your solution and as I
created the source file you see below I was rooting for you: for had
it ran with my test suite, I would have handed the "Olympic gold
medal" that I've awarded myself in the Spinoza challenge to you.

3. But "one of the great tragedies in life is the murder of a
beautiful theory by a brutal gang of facts" for your code has failed
many tests. Since it's late I may have made myself a clerical error in
assembling the test package.

4. Nonetheless, this has the potential to be the BEST solution. I
understand (unlike the regs and thugs here) that this is not a jobsite
and that we're all on different clocks and time schedules, and respect
your decision not to test the code. If you have the time, fix it, or,
I will see if I can tomorrow morning. Yes, we can use recursion, I
think, and this is a BRILLIANT insight. You see, I don't confuse code
monkeyshines with intelligence. Don't get me started...

5. Here is the source file I made and the console output.

#include <stdlib.h>
#include <stdio.h>

#define TESTER(resultPtr, master, target, replacement, expected) \
{ \
printf("Replace \"%s\" by \"%s\" in \"%s\"\n", \
(target), (replacement), (master)); \
printf("Expect \"%s\":\n \"%s\"\n", \
(expected), \
resultPtr = replace_string((master), \
(target), \
(replacement))); \
printf("\n\n"); \
free(resultPtr); \
}

char *repstr(char *s, char *m, char *r, char *t)
{
size_t sl = 0, ml = 0, tl = 0;
size_t rl;
if (!s || !m || !r || !*m) { return 0; }
while (s[sl]) {
if (m[ml] == 0) {
if (t) { for (rl = 0; r[rl]; rl++) { t[rl] = r[rl]; } }
tl += rl;
sl += ml;
ml = 0;
} else if (m[ml] != s[sl+ml]) {
if (t) { t[tl] = s[sl]; }
tl++;
sl++;
ml = 0;
} else {
ml++;
}
}
if (t) {
t[tl] = 0;
return t;
} else if (t = malloc(tl + 1)) {
return repstr(s, m, r, t);
} else {
return 0;
}
}

char *replace_string(char *str, char *match, char *replace)
{
return repstr(str, match, replace, 0);
}

int main()
{
char *ptrResult;
printf("\nReplace\n\n\n");
TESTER(ptrResult,
"1111123bbb1111123bbb11123bb11111231111112111111123",
"111123",
"ono",
"1onobbb1onobbb11123bb1ono1111112111ono")
TESTER(ptrResult,
"bbb1111123bbbbb",
"111123",
"ono",
"bbb1onobbbbb")
TESTER(ptrResult,
"a stupid error",
"stupid error",
"miracle",
"a miracle")
TESTER(ptrResult,
"a stupid error",
"stupid",
"miracle",
"a miracle error")
TESTER(ptrResult,
"the stupid error",
"the stupid error",
"a miracle",
"a miracle")
TESTER(ptrResult,
"the miracle",
"the",
"a",
"a miracle")
TESTER(ptrResult,
"a miraclsnirpKamunkle",
"snirpKamunkle",
"e",
"a miracle")
TESTER(ptrResult,
"a miraclesnirpKamunkle",
"a miracle",
"",
"snirpKamunkle")
TESTER(ptrResult,
" a miraclesnirpKamunkle",
"a miracle",
"",
" snirpKamunkle")
TESTER(ptrResult,
" a miraclesnirpKamunklea miraclea miracle",
"a miracle",
"",
" snirpKamunkle")
TESTER(ptrResult,
"a stupid errord",
"stupid error",
"miracle",
"a miracled")
TESTER(ptrResult,
"a stupid errod",
"stupid error",
"miracle",
"a stupid errod")
TESTER(ptrResult,
"a sstupid error",
"stupid error",
"miracle",
"a smiracle")
TESTER(ptrResult,
"a stupid errorstupid error",
"stupid error",
"miracle",
"a miraclemiracle")
TESTER(ptrResult,
"a stupid error stupiderror",
"stupid error",
"miracle",
"a miracle stupiderror")
TESTER(ptrResult,
"bbbbbbbbbb",
"b",
"a",
"aaaaaaaaaa")
TESTER(ptrResult,
"In the halls of R'yleh great %s lies dreaming",
"%s",
"Cthulu",
"In the halls of R'yleh great Cthulu lies dreaming")
TESTER(ptrResult,
"%s%s%s%s%s%s",
"%s",
"Cthulu",
"CthuluCthuluCthuluCthuluCthuluCthulu")
TESTER(ptrResult,
"banana",
"ana",
"oat",
"boatna")
TESTER(ptrResult,
" a stupid errorstupid errorHeystupid errors",
"stupid error",
"+",
" a ++Hey+s")
TESTER(ptrResult,
"foo barfoo barf",
"foo bar",
"bas",
"basbasf")
TESTER(ptrResult,
"abab",
"ba",
"ba",
"abab")
TESTER(ptrResult,
"abab",
"bab",
"boop",
"aboop")
TESTER(ptrResult,
"banana",
"ana",
"ono",
"bonona")
TESTER(ptrResult,
"a",
"x",
"b",
"a")
TESTER(ptrResult,
"x",
"x",
"b",
"b")
TESTER(ptrResult,
"egregious",
"egregious",
"egregious",
"egregious")
TESTER(ptrResult,
"egregious",
"egregious",
"x",
"x")
TESTER(ptrResult,
"",
"egregious",
"x",
"")
printf("\n\nTesting complete: check output carefully\n\n");
return 0;
}


Replace


Replace "111123" by "ono" in
"1111123bbb1111123bbb11123bb11111231111112111111123"
Expect "1onobbb1onobbb11123bb1ono1111112111ono":
"ono"


Replace "111123" by "ono" in "bbb1111123bbbbb"
Expect "bbb1onobbbbb":
"ono1"


Replace "stupid error" by "miracle" in "a stupid error"
Expect "a miracle":
"miracle"


Replace "stupid" by "miracle" in "a stupid error"
Expect "a miracle error":
"miracle"


Replace "the stupid error" by "a miracle" in "the stupid error"
Expect "a miracle":
"a miracle"


Replace "the" by "a" in "the miracle"
Expect "a miracle":
"a miracle"


Replace "snirpKamunkle" by "e" in "a miraclsnirpKamunkle"
Expect "a miracle":
"e miracl"


Replace "a miracle" by "" in "a miraclesnirpKamunkle"
Expect "snirpKamunkle":
"snirpKamunkle"


Replace "a miracle" by "" in " a miraclesnirpKamunkle"
Expect " snirpKamunkle":
" snirpKamunkle"


Replace "a miracle" by "" in " a miraclesnirpKamunklea miraclea
miracle"
Expect " snirpKamunkle":
" snirpKamunkle"


Replace "stupid error" by "miracle" in "a stupid errord"
Expect "a miracled":
"miracle"


Replace "stupid error" by "miracle" in "a stupid errod"
Expect "a stupid errod":
"a stupid errod"


Replace "stupid error" by "miracle" in "a sstupid error"
Expect "a smiracle":
"miracle"


Replace "stupid error" by "miracle" in "a stupid errorstupid error"
Expect "a miraclemiracle":
"miracle"


Replace "stupid error" by "miracle" in "a stupid error stupiderror"
Expect "a miracle stupiderror":
"miracle"


Replace "b" by "a" in "bbbbbbbbbb"
Expect "aaaaaaaaaa":
"a"


Replace "%s" by "Cthulu" in "In the halls of R'yleh great %s lies
dreaming"
Expect "In the halls of R'yleh great Cthulu lies dreaming":
"Cthulu halls of R'yleh great "


Replace "%s" by "Cthulu" in "%s%s%s%s%s%s"
Expect "CthuluCthuluCthuluCthuluCthuluCthulu":
"Cthulu"


Replace "ana" by "oat" in "banana"
Expect "boatna":
"oat"


Replace "stupid error" by "+" in " a stupid errorstupid errorHeystupid
errors"
Expect " a ++Hey+s":
"+a "


Replace "foo bar" by "bas" in "foo barfoo barf"
Expect "basbasf":
"bas"


Replace "ba" by "ba" in "abab"
Expect "abab":
"ba"


Replace "bab" by "boop" in "abab"
Expect "aboop":
"boop"


Replace "ana" by "ono" in "banana"
Expect "bonona":
"ono"


Replace "x" by "b" in "a"
Expect "a":
"a"


Replace "x" by "b" in "x"
Expect "b":
"b"


Replace "egregious" by "egregious" in "egregious"
Expect "egregious":
"egregious"


Replace "egregious" by "x" in "egregious"
Expect "x":
"x"


Replace "egregious" by "x" in ""
Expect "":
""




Testing complete: check output carefully
 
S

spinoza1111

"io_x" <[email protected]> ha scritto nel messaggio



; bcc32 -v  this.c  that.obj
#include <stdio.h>
#include <stdlib.h>
char* __stdcall  SostituisciPercS(char* fmt, char*  w);
int  main(void)
{char  *a1="FirstString", *p;
p=SostituisciPercS("this is fmt %%s =%s %s %s %s % s %s %s now end\n", a1);
if(p==0){printf("Errore\n"); return 0;}
printf("%s", p);
free(p);
return  0;
}
----------
this is fmt %s =FirstString FirstString FirstString FirstString % s
FirstString
FirstStrin
g now end
--------------
gotos, jumps, jn, jnz, jetc are ok
section _DATA use32 public class=DATA
global   SostituisciPercS
extern   _realloc
extern   _free
section _BSS  use32 public class=BSS
section _TEXT use32 public class=CODE
; 0k,4j,8i,12b,16ra, 20P_fmt, 24P_etc + 64
;                    84       88
; 0inString, 4sizeResult, 8NowArg
SostituisciPercS:
<b,i,j,k
s-=64
   i=^84|a=&*s+88|^8=a  ; 8 is for arg address
   j=0                  ; k=current arg, j array
   ^0=0 |^4=0|b=0|#.0   ; 0led_isstring b is index [len]
.e:   _free(j)
.e0:  a^=a|stc|##.z
.0: b>=^4!#.1
       c=b|c+=32|<c |_realloc(j,c)|s+=8|>c
       a==0#.e  |j=a|^4=c
.1: D*s==1!#.3
       a^=a|al=*k|a==0!#.2
                  ^0=0|#.0
.2:     B*j+b=al|++k,b|#.0
.3: a^=a|al=B*i
   a=='%'!#.5
        B*i+1=='%'!#.4
        B*j+b ='%'|++b |i+=2|#.0
.4:   B*i+1=='s'!#.5
       ^0=1|a=^8 |k=*a|i+=2|##.0
.ee: ##.e
.5: B*j+b=al|++i,b|a==0#.6  |##.0
.6: _realloc(j, b)|s+=8|a==0#.ee
   clc
.z:
s=&*s+64
ret  8

very good from the day one

I don't understand. If you claim this "beat" me, would you mind
posting a console output with the test suite in my program? I was
willing to concede to Willem and am willing to concede to you,
although I find your code "hard" to read. The readability of my code
is a readability to someone with a high level of English, therefore,
in resolving this "contest", we should just forget about
"readability", since we seem to have coders of all nations.

This is turning into the Vancouver Olympics...
 
W

Willem

spinoza1111 wrote:
) Willem:
)
) 1. I wish I'd answered this before, and I apologize on behalf of the
) slobs,thugs and creeps in this newsgroup for the lack of initial
) response to your submission
)
) 2. I was, and remain, very impressed by your solution and as I
) created the source file you see below I was rooting for you: for had
) it ran with my test suite, I would have handed the "Olympic gold
) medal" that I've awarded myself in the Spinoza challenge to you.
)
) 3. But "one of the great tragedies in life is the murder of a
) beautiful theory by a brutal gang of facts" for your code has failed
) many tests. Since it's late I may have made myself a clerical error in
) assembling the test package.

No, I made a simple mistake in the copy loop of the target string.

This:
if (t) { for (rl = 0; r[rl]; rl++) { t[rl] = r[rl]; } }
Should have been:
if (t) { for (rl = 0; r[rl]; rl++) { t[tl+rl] = r[rl]; } }
And that's the bug.

Basically, it was copying the replacement string to the beginning
of the target string instead of to the current position.

And then it passes all the tests.
Thanks for the test harness, it was a real help in checking the fix.

) 4. Nonetheless, this has the potential to be the BEST solution. I

Doubtful. The loop structure is a bit convoluted (in fact it's inside-out)
because that way all the loop structures fall into place. But it's
counter-intuitive.

Oh, and the recursion isn't actually recursion, but rather a sneaky way
to run the same loop twice.

A pity, though. Just once I would have enjoyed creating a piece of code
that worked exactly as planned on the first go.


However, I have to disappoint you: it's not really a recursive solution,
It will always recurse exactly once. A smart compiler could inline the
whole thing, so you get two copies of the loop, with all the if(t) checks
removed (i.e. one with, and one without the body of that if(t) ).


Perhaps as a new challenge one could write a truely recursive solution,
though. I wonder if it would be possible to do it elegantly.



SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
W

Willem

Willem wrote:
) However, I have to disappoint you: it's not really a recursive solution,
) It will always recurse exactly once. A smart compiler could inline the
) whole thing, so you get two copies of the loop, with all the if(t) checks
) removed (i.e. one with, and one without the body of that if(t) ).
)
)
) Perhaps as a new challenge one could write a truely recursive solution,
) though. I wonder if it would be possible to do it elegantly.

Without further ado: the really recursive solution:
This time, I have tested it.
(And again, to my dismay, there was one silly bug the first time 'round.)

Note: I do not recommend this function to anybody for reasons other
than academic interest. Note, for example, that the length of the
replacement is recalculated on every recursion but the first.

char *repstr(char *s, char *m, char *r, size_t tl)
{
char *t;
size_t sl = 0, ml = 0, rl;
while (s[sl]) {
if (m[ml] == 0) {
for (rl = 0; r[rl]; rl++) ;
if (t = repstr(s+sl+ml, m, r, tl+sl+rl)) {
while (rl--) { t[tl+sl+rl] = r[rl]; }
while (sl--) { t[tl+sl] = s[sl]; }
}
return t;
}
if (s[sl+ml] != m[ml]) {
sl++;
ml = 0;
} else {
ml++;
}
}
if (t = malloc(tl + sl + 1)) {
do { t[tl+sl] = s[sl]; } while (sl--);
}
return t;
}

char *replace_string(char *str, char *match, char *replace)
{
if (match[0] == 0) return 0; /* Sanity check */
return repstr(str, match, replace, 0);
}


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
S

spinoza1111

spinoza1111 wrote:

) Willem:
)
) 1.  I wish I'd answered this before, and I apologize on behalf of the
) slobs,thugs and creeps in this newsgroup for the lack of initial
) response to your submission
)
) 2.  I was, and remain, very impressed by your solution and as I
) created the source file you see below I was rooting for you: for had
) it ran with my test suite, I would have handed the "Olympic gold
) medal" that I've awarded myself in the Spinoza challenge to you.
)
) 3.  But "one of the great tragedies in life is the murder of a
) beautiful theory by a brutal gang of facts" for your code has failed
) many tests. Since it's late I may have made myself a clerical error in
) assembling the test package.

No, I made a simple mistake in the copy loop of the target string.

This:
  if (t) { for (rl = 0; r[rl]; rl++) { t[rl] = r[rl]; } }
Should have been:
  if (t) { for (rl = 0; r[rl]; rl++) { t[tl+rl] = r[rl]; } }
And that's the bug.

Basically, it was copying the replacement string to the beginning
of the target string instead of to the current position.

And then it passes all the tests.
Thanks for the test harness, it was a real help in checking the fix.

) 4.  Nonetheless, this has the potential to be the BEST solution. I

Doubtful.  The loop structure is a bit convoluted (in fact it's inside-out)
because that way all the loop structures fall into place.  But it's
counter-intuitive.

Oh, and the recursion isn't actually recursion, but rather a sneaky way
to run the same loop twice.

A pity, though.  Just once I would have enjoyed creating a piece of code
that worked exactly as planned on the first go.

However, I have to disappoint you: it's not really a recursive solution,
It will always recurse exactly once.  A smart compiler could inline the
whole thing, so you get two copies of the loop, with all the if(t) checks
removed (i.e. one with, and one without the body of that if(t) ).

Perhaps as a new challenge one could write a truely recursive solution,
though.  I wonder if it would be possible to do it elegantly.

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

This version works perfectly. I don't like its one character
identifiers, which make it a non-industrial solution, but as we can
see, trying to provide meaningful identifiers to aliterates is pearls
before swine.

Thanks! Great Code!

The mighty Willem thought a while, and from his Perch on High
Emitted a falsely Recursive routine with an Eagle's cry
The mighty Nilges tested it, alas it did not work
But Willem changed but a single line, and in the void and Murk
Shone a program which tho' not yet proven correct handled Nilges' test
cases with great ease
The mob ignored it but the Cognoscenti it did please.
 
S

spinoza1111

Willem wrote:

) However, I have to disappoint you: it's not really a recursive solution,
) It will always recurse exactly once.  A smart compiler could inline the
) whole thing, so you get two copies of the loop, with all the if(t) checks
) removed (i.e. one with, and one without the body of that if(t) ).
)
)
) Perhaps as a new challenge one could write a truely recursive solution,
) though.  I wonder if it would be possible to do it elegantly.

Without further ado: the really recursive solution:
This time, I have tested it.
(And again, to my dismay, there was one silly bug the first time 'round.)

Note: I do not recommend this function to anybody for reasons other
than academic interest.  Note, for example, that the length of the
replacement is recalculated on every recursion but the first.

char *repstr(char *s, char *m, char *r, size_t tl)
{
  char *t;
  size_t sl = 0, ml = 0, rl;
  while (s[sl]) {
    if (m[ml] == 0) {
      for (rl = 0; r[rl]; rl++) ;
      if (t = repstr(s+sl+ml, m, r, tl+sl+rl)) {
        while (rl--) { t[tl+sl+rl] = r[rl]; }
        while (sl--) { t[tl+sl] = s[sl]; }
      }
      return t;
    }
    if (s[sl+ml] != m[ml]) {
      sl++;
      ml = 0;
    } else {
      ml++;
    }
  }
  if (t = malloc(tl + sl + 1)) {
    do { t[tl+sl] = s[sl]; } while (sl--);
  }
  return t;

}

char *replace_string(char *str, char *match, char *replace)
{
  if (match[0] == 0) return 0; /* Sanity check */
  return repstr(str, match, replace, 0);

}

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

WE'RE NOT WORTHY WE'RE NOT WORTHY WE'RE SCUM


Replace


Replace "111123" by "ono" in
"1111123bbb1111123bbb11123bb11111231111112111111123"
Expect "1onobbb1onobbb11123bb1ono1111112111ono":
"1onobbb1onobbb11123bb1ono1111112111ono"


Replace "111123" by "ono" in "bbb1111123bbbbb"
Expect "bbb1onobbbbb":
"bbb1onobbbbb"


Replace "stupid error" by "miracle" in "a stupid error"
Expect "a miracle":
"a miracle"


Replace "stupid" by "miracle" in "a stupid error"
Expect "a miracle error":
"a miracle error"


Replace "the stupid error" by "a miracle" in "the stupid error"
Expect "a miracle":
"a miracle"


Replace "the" by "a" in "the miracle"
Expect "a miracle":
"a miracle"


Replace "snirpKamunkle" by "e" in "a miraclsnirpKamunkle"
Expect "a miracle":
"a miracle"


Replace "a miracle" by "" in "a miraclesnirpKamunkle"
Expect "snirpKamunkle":
"snirpKamunkle"


Replace "a miracle" by "" in " a miraclesnirpKamunkle"
Expect " snirpKamunkle":
" snirpKamunkle"


Replace "a miracle" by "" in " a miraclesnirpKamunklea miraclea
miracle"
Expect " snirpKamunkle":
" snirpKamunkle"


Replace "stupid error" by "miracle" in "a stupid errord"
Expect "a miracled":
"a miracled"


Replace "stupid error" by "miracle" in "a stupid errod"
Expect "a stupid errod":
"a stupid errod"


Replace "stupid error" by "miracle" in "a sstupid error"
Expect "a smiracle":
"a smiracle"


Replace "stupid error" by "miracle" in "a stupid errorstupid error"
Expect "a miraclemiracle":
"a miraclemiracle"


Replace "stupid error" by "miracle" in "a stupid error stupiderror"
Expect "a miracle stupiderror":
"a miracle stupiderror"


Replace "b" by "a" in "bbbbbbbbbb"
Expect "aaaaaaaaaa":
"aaaaaaaaaa"


Replace "%s" by "Cthulu" in "In the halls of R'yleh great %s lies
dreaming"
Expect "In the halls of R'yleh great Cthulu lies dreaming":
"In the halls of R'yleh great Cthulu lies dreaming"


Replace "%s" by "Cthulu" in "%s%s%s%s%s%s"
Expect "CthuluCthuluCthuluCthuluCthuluCthulu":
"CthuluCthuluCthuluCthuluCthuluCthulu"


Replace "ana" by "oat" in "banana"
Expect "boatna":
"boatna"


Replace "stupid error" by "+" in " a stupid errorstupid errorHeystupid
errors"
Expect " a ++Hey+s":
" a ++Hey+s"


Replace "foo bar" by "bas" in "foo barfoo barf"
Expect "basbasf":
"basbasf"


Replace "ba" by "ba" in "abab"
Expect "abab":
"abab"


Replace "bab" by "boop" in "abab"
Expect "aboop":
"aboop"


Replace "ana" by "ono" in "banana"
Expect "bonona":
"bonona"


Replace "x" by "b" in "a"
Expect "a":
"a"


Replace "x" by "b" in "x"
Expect "b":
"b"


Replace "egregious" by "egregious" in "egregious"
Expect "egregious":
"egregious"


Replace "egregious" by "x" in "egregious"
Expect "x":
"x"


Replace "egregious" by "x" in ""
Expect "":
""




Testing complete: check output carefully

Excellent work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,229
Latest member
GloryAngul

Latest Threads

Top