A Challenge
Moderator: General Moderators
-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
A Challenge
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.
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.
Last edited by spacebiscuit on Fri Feb 10, 2012 7:21 am, edited 1 time in total.
Re: A Challenge
Do it recursively:
Code: Select all
function reverse($string)
{
return $string
? reverse(substr($string, 1)) . $string[0]
: '';
}Re: A Challenge
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
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
-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
Re: A Challenge
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.
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
or not recursion at all
Code: Select all
function reverse_string($foo)
{
return implode('',array_reverse(str_split($foo)));
}Re: A Challenge
If array_reverse is permitted, that's even better.
-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
Re: A Challenge
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:
Where have I gone wrong?
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
{
'';
}Re: A Challenge
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 '';
}-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
Re: A Challenge
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.
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
Code: Select all
function reverse($string)
{
return ($string)
? reverse(substr($string, 1)) . $string[0]
: '';
}
reverse('hello');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.
-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
Re: A Challenge
Why does the concatenation occur in reverse order?
Also it looks like string is an array $string[0]
My head hurts!
Also it looks like string is an array $string[0]
My head hurts!
Re: A Challenge
A string basically is an array of characters. The concatenation doesn't occur in reverse order. Do you understand that returns ?
Code: Select all
reverse('hello');Code: Select all
reverse('ello') . 'h'-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
Re: A Challenge
I can see that we're shortening the sting yes but I'm not sure where the concatenation is taking place.
Re: A Challenge
Code: Select all
function reverse($string) +--- Concatenation
{ |
return $string v
? reverse(substr($string, 1)) . $string[0]
: '';
}