A Challenge

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

A Challenge

Post 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.
Last edited by spacebiscuit on Fri Feb 10, 2012 7:21 am, edited 1 time in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: A Challenge

Post by Celauran »

Do it recursively:

Code: Select all

function reverse($string)
{
    return $string
        ? reverse(substr($string, 1)) . $string[0]
        : '';
}
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: A Challenge

Post 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
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: A Challenge

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: A Challenge

Post by Celauran »

mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: A Challenge

Post by mikosiko »

or not recursion at all

Code: Select all

function reverse_string($foo)
{
  return implode('',array_reverse(str_split($foo)));
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: A Challenge

Post by Celauran »

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

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: A Challenge

Post 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 '';
}
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: A Challenge

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: A Challenge

Post 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.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: A Challenge

Post by spacebiscuit »

Why does the concatenation occur in reverse order?

Also it looks like string is an array $string[0]

My head hurts!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: A Challenge

Post by Celauran »

A string basically is an array of characters. The concatenation doesn't occur in reverse order. Do you understand that

Code: Select all

reverse('hello');
returns

Code: Select all

reverse('ello') . 'h'
?
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: A Challenge

Post by spacebiscuit »

I can see that we're shortening the sting yes but I'm not sure where the concatenation is taking place.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: A Challenge

Post by Celauran »

Code: Select all

function reverse($string)             +--- Concatenation
{                                     |
    return $string                    v
        ? reverse(substr($string, 1)) . $string[0]
        : '';
}
Post Reply