Page 1 of 2
A Challenge
Posted: Fri Feb 10, 2012 3:57 am
by spacebiscuit
Hi,
I need some help with a problem that has truly stumped me.
I need to write a php function that reverse a string. The function can only have one argument, cannot use any sorting functions such strrev and cannot use any looping constructs.
Without the loop I can't figure it out - oo approach perhaps?
Thanks in avance.
Re: A Challenge
Posted: Fri Feb 10, 2012 6:33 am
by Celauran
Do it recursively:
Code: Select all
function reverse($string)
{
return $string
? reverse(substr($string, 1)) . $string[0]
: '';
}
Re: A Challenge
Posted: Fri Feb 10, 2012 6:48 am
by mikosiko
the challenge is for you to read about STRING functions and ARRAY functions ... using just one in both groups you can solve your challenge in 30 seconds or less
String Functions
http://php.net/manual/en/ref.strings.php
Array Functions
http://php.net/manual/en/ref.array.php
good luck with your home work
Re: A Challenge
Posted: Fri Feb 10, 2012 6:54 am
by spacebiscuit
Looks like Celauran did it with one string function!
My thoughts were to also call the function recursively but I couldn't figure out how to read a character at a time.
Calauran - can I ask what the ':' and '?' characters do in your code. I'm not familiar with their use - or else what should I search for to read up on this.
Thanks - such a simple and elegant solution.
Re: A Challenge
Posted: Fri Feb 10, 2012 6:56 am
by Celauran
Re: A Challenge
Posted: Fri Feb 10, 2012 7:01 am
by mikosiko
or not recursion at all
Code: Select all
function reverse_string($foo)
{
return implode('',array_reverse(str_split($foo)));
}
Re: A Challenge
Posted: Fri Feb 10, 2012 7:05 am
by Celauran
If array_reverse is permitted, that's even better.
Re: A Challenge
Posted: Fri Feb 10, 2012 7:22 am
by spacebiscuit
No the challenge is to not use any pre-written functions to reverse the order.
The ternary shorthand makes sense in principle but I'm trying to re-write it so that I fully understand and I have come up with the following which is obviously incorrect:
Code: Select all
if(return $string)
{
reverse(substr($string, 1));
}
else
{
'';
}
Where have I gone wrong?
Re: A Challenge
Posted: Fri Feb 10, 2012 8:32 am
by Celauran
If you're trying to rewrite the ternary using if/else, it would be
Code: Select all
if ($string) // alternately if (strlen($string) > 0)
{
return reverse(substr($string, 1));
}
else
{
return '';
}
Re: A Challenge
Posted: Fri Feb 10, 2012 9:16 am
by spacebiscuit
Ok thanks.
I still can't see how this works though. Each recurssive call is made with one less charachter:
START : hello
1: ello
2: llo
3: lo
4: o
How or where are you reversing the string. It seems to me that we're just parsing from start to finish.
Re: A Challenge
Posted: Fri Feb 10, 2012 9:52 am
by Celauran
Code: Select all
function reverse($string)
{
return ($string)
? reverse(substr($string, 1)) . $string[0]
: '';
}
reverse('hello');
First step returns: reverse('ello') . 'h'
Second step returns: reverse('llo') . 'e'
Third step returns: reverse('lo') . 'l'
Fourth step returns: reverse('o') . 'l'
Fifth step returns: reverse('') . 'o'
Sixth step returns: ''
Working backwards, we can see that reverse('o') returns 'o'. Substituting that up one step, the fourth step returns 'ol'. The third step, therefore, returns 'oll'. Continue working backwards and you'll see that the string is being rebuilt in reverse.
Re: A Challenge
Posted: Fri Feb 10, 2012 10:03 am
by spacebiscuit
Why does the concatenation occur in reverse order?
Also it looks like string is an array $string[0]
My head hurts!
Re: A Challenge
Posted: Fri Feb 10, 2012 10:08 am
by Celauran
A string basically is an array of characters. The concatenation doesn't occur in reverse order. Do you understand that
returns
?
Re: A Challenge
Posted: Fri Feb 10, 2012 10:35 am
by spacebiscuit
I can see that we're shortening the sting yes but I'm not sure where the concatenation is taking place.
Re: A Challenge
Posted: Fri Feb 10, 2012 10:37 am
by Celauran
Code: Select all
function reverse($string) +--- Concatenation
{ |
return $string v
? reverse(substr($string, 1)) . $string[0]
: '';
}