>>1
@tail_call
def f(i, o=''):
if not i: return o
if i[0] in ['a', 'b', 'c']: return f(i[1:], o)
return f(i[1:], o + i[0])
Name:
Anonymous2007-05-11 17:10 ID:jHJmv6jQ
def oneWordForcedIndentationOfCodeThreadOver(string):
(oneWordForcedIndentationOfCodeThreadOver(string[1:]) if string[0] in "abc" else string[0]+oneWordForcedIndentationOfCodeThreadOver(string[1:])) if string else string
>>8
But then there'd be no FORCED INDENTATION. >>10
Then the teachers are goddamn idiots. HAVE FUN OVERFLOWING THE STACK ON MODERATELY LARGE STRINGS.
Name:
Anonymous2007-05-11 18:03 ID:LrEob0br
Since OP wanted something like C or Java...
Here's some pseudocode to get you started. Thought this up in about one minute, it might delete your pron folder or just fail it, although it probably won't.
Also, you may want to make the function take the unwanted characters as arguments.
string removeshit (string s){
if(we come across one a, b or c in s){
return removeshit(s with the one offending character removed);
}
return s;
}
OP here. It's for a comparative between iterative and recursive timings, memory consuption, etc... Which is all good, if it wasn't for the fact that I did... one recursive function in my all life (factorial).
Name:
Anonymous2007-05-11 18:05 ID:LrEob0br
>>12 here.
Oh, and just so you won't get the wrong idea, there's better ways to do this than massive recursion. Also cocks.
char *removeCharacters(char *string,char removeMe)
{
int i;
int length;
for(length = 0;string[length]!='\0';length++){}
char *newString = malloc(sizeof(char) * length);
for(length = 0;string[length]!='\0';length++)
{
if(string[length]==removeMe)
{
for
}
}
}
Name:
Anonymous2007-05-11 18:10 ID:bLm7QA6k
woops, I clicked reply I'll finish the code lol
Name:
Anonymous2007-05-11 18:11 ID:ze7o20Cd
>>13 one recursive function in my all life (factorial).
OMG. You need to read SICP. For real, I really recommend it. Just chapter 1, and you'll become an EXPERT PROGRAMMER of recursion. Also,
It's for a comparative between iterative and recursive timings, memory consuption, etc.
Who cares. Ah, you're in your OMG OPTIMIZED phase. Well, I can tell you about it right now:
1. The non-recursive function will be faster on almost all compilers or interpreters.
2. The non-recursive function will take less memory on all compilers or interpreters that don't do tail call/recursion optimization.
3. The above facts are irrelevant compared to development time and costs. Your time > your hardware resources. You can make far more money in the time you save, or just be lazy.
Name:
Anonymous2007-05-11 18:11 ID:TcFyHC4t
2 recursions for the price of one.
contains c [] = False
contains c (char:chars)
| c == char = True
| otherwise = contains c chars
remove bad chars = remove' bad chars []
remove' bad [] acc = reverse acc
remove' bad (char:chars) acc
| contains char bad = remove' bad chars acc
| otherwise = remove' bad chars (char:acc)
Name:
Anonymous2007-05-11 18:15 ID:QC7ohZ6D
char *removeCharacters(char *string,char removeMe)
{
int i;
int length;
for(length = 0;string[length]!='\0';length++){}
char *newString = malloc(sizeof(char) * length);
I must say.. all those temporary variables and the use of if-else make this code look like the stuff at The Daily WTF. Think a bit about what the code's doing and how C strings work. You could code an iterative solution by selectively incrementing and copying between two pointers. Not only would the resulting code be very short, it'd also look very hackish. A recursive solution could then be based on that.
It might also run quite fast since the compiler would just use one register for each pointer, and some other for temporary storage. The C source you've posted so far probably compiles into a dozen jump instructions and retarded fumbling with the stack (not enough registers to fit all the variables in). Not good for speed. Since you're coding in C, you might as well know this.