Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
Thread Tools Display Modes
Share |
  #1  
Old 09-29-2008, 11:07 AM
imaginemn's Avatar
imaginemn imaginemn is offline
v7n Mentor
 
Join Date: 02-18-04
Location: Brownsville, Texas
Posts: 1,354
iTrader: 0 / 0%
Code Challenge...

Can anyone write php code segment to interchange the value of two integer variables without using a 3rd variable?

I'm trying to see how many ways this could be done using PHP.
 
Reply With Quote

Advertisement

Advertisement

  #2  
Old 09-29-2008, 02:52 PM
marc_gfx marc_gfx is offline
Contributing Member
 
Join Date: 07-01-06
Posts: 126
iTrader: 0 / 0%
I might be leaning far out... but what you are asking for is not possible.
you have $a and $b. If you set either one of them to the other value, you lose the value it had before. if you are thinking of some clever function swap($a,$b), then this would be easy, but would also require the variable $c to work.
 
Reply With Quote
  #3  
Old 09-29-2008, 03:17 PM
imaginemn's Avatar
imaginemn imaginemn is offline
v7n Mentor
 
Join Date: 02-18-04
Location: Brownsville, Texas
Posts: 1,354
iTrader: 0 / 0%
Sure there is. Here is what I came up with:

a=5
b=10

a=a+b (a is now 15)
b=a-b (b is now 5)
a=a-b (a is now 10)

The KEY is that you are NOT introducing another VARIABLE. Even though you are introducing another "number". I recently had this question on a test from a client to prove my qualification and found it interesting.
 
Reply With Quote
  #4  
Old 09-29-2008, 03:31 PM
marc_gfx marc_gfx is offline
Contributing Member
 
Join Date: 07-01-06
Posts: 126
iTrader: 0 / 0%
I just have to try this

a=7
b=3

a=a+b (a is now 10)
b=a-b (b is now 7)
a=a-b (a is now 3)

quite clever and simple.

I guess you can use */ +- -+ /* so at least 4 ways.
a=a*b (a is now 21)
b=a/b (b is now 7)
a=a/b (a is now 3)

but... this does not work for the entire range of numbers. say you have 8 bit unsigned. 0-255 are possible values.

a=200; b=200;
a=a+b=400 -> out of the valid number range

so you have a solution, but it might not always work. not sure how php handles large numbers though.
 
Reply With Quote
  #5  
Old 09-29-2008, 03:47 PM
ZunuZunu ZunuZunu is offline
Contributing Member
 
Join Date: 09-04-08
Posts: 363
iTrader: 3 / 100%
The solution is right. The logic is correct and it's your job to translate it into code.

See PHP Help to learn more.
 
Reply With Quote
  #6  
Old 09-29-2008, 04:07 PM
nasty.web nasty.web is offline
v7n Mentor
 
Join Date: 07-24-06
Posts: 688
iTrader: 1 / 100%
My try:

PHP Code:
    $a 15;
    
$b 99;
    
    echo 
"a: $a; b: $b <br />"# should print "a: 15; b: 99" 
    
    
list($a$b) = array($b$a);
    
    echo 
"a: $a; b: $b <br />"# should print "a: 99; b: 15" 
 
Reply With Quote
  #7  
Old 09-29-2008, 04:30 PM
nasty.web nasty.web is offline
v7n Mentor
 
Join Date: 07-24-06
Posts: 688
iTrader: 1 / 100%
Second try:

PHP Code:
    $a 15;
    
$b 99;
    
    echo 
"a: $a; b: $b <br />";  # should print "a: 15; b: 99"  

    
extract(array(=> $b=> $a));
    
    echo 
"a: $a; b: $b <br />"# should print "a: 99; b: 15" 
 
Reply With Quote
  #8  
Old 09-29-2008, 04:33 PM
nasty.web nasty.web is offline
v7n Mentor
 
Join Date: 07-24-06
Posts: 688
iTrader: 1 / 100%
Third try:

PHP Code:
    $a 15;
    
$b 99;
    
    echo 
"a: $a; b: $b <br />"# should print "a: 15; b: 99"   

    
eval ("\$a = $b; \$b = $a;");
    
    echo 
"a: $a; b: $b <br />"# should print "a: 99; b: 15" 
 
Reply With Quote
  #9  
Old 09-29-2008, 04:55 PM
nasty.web nasty.web is offline
v7n Mentor
 
Join Date: 07-24-06
Posts: 688
iTrader: 1 / 100%
Fourth try:

PHP Code:
    $a 15;
    
$b 99;
    
    echo 
"a: $a; b: $b <br />";

    
$a = array($a$b);    
    
$b $a[0];
    
$a $a[1];
    
    echo 
"a: $a; b: $b <br />"
Fifth:

PHP Code:
    $a 15;
    
$b 99;
    
    echo 
"a: $a; b: $b <br />";

    
$a $a $b;
    
$b $b $a;
    
$a $a $b;
    
    echo 
"a: $a; b: $b <br />"
 
Reply With Quote
  #10  
Old 09-29-2008, 05:37 PM
AnonymousUser AnonymousUser is offline
Contributing Member
 
Join Date: 09-27-08
Posts: 99
iTrader: 0 / 0%
Quote:
Originally Posted by nasty.web View Post
Third try:

PHP Code:
    $a 15;
    
$b 99;
    
    echo 
"a: $a; b: $b <br />"# should print "a: 15; b: 99"   

    
eval ("\$a = $b; \$b = $a;");
    
    echo 
"a: $a; b: $b <br />"# should print "a: 99; b: 15" 
Wat are you doing?
I think its been declared how to do it
Your 4 tries are all the same
 
Reply With Quote
  #11  
Old 09-29-2008, 05:39 PM
nasty.web nasty.web is offline
v7n Mentor
 
Join Date: 07-24-06
Posts: 688
iTrader: 1 / 100%
Quote:
Originally Posted by AnonymousUser View Post
I think its been declared how to do it
I guess, I missed something. Could you explain?
 
Reply With Quote
  #12  
Old 09-29-2008, 07:31 PM
nasty.web nasty.web is offline
v7n Mentor
 
Join Date: 07-24-06
Posts: 688
iTrader: 1 / 100%
Hey, I hate it when people leave me without explanation
 
Reply With Quote
  #13  
Old 09-29-2008, 07:53 PM
kos's Avatar
kos kos is offline
Coding Tiger
 
Join Date: 04-13-07
Location: .ro
Posts: 4,029
iTrader: 3 / 100%


...no worries...
 
Reply With Quote
  #14  
Old 09-30-2008, 06:02 AM
ZunuZunu ZunuZunu is offline
Contributing Member
 
Join Date: 09-04-08
Posts: 363
iTrader: 3 / 100%
nasty.web,

1. The solution is already given by imaginemn.

2. I didn't read all your solutions but your first solution is wrong. You are using third variable (array).

HTH.
 
Reply With Quote
  #15  
Old 09-30-2008, 06:48 AM
nasty.web nasty.web is offline
v7n Mentor
 
Join Date: 07-24-06
Posts: 688
iTrader: 1 / 100%
Quote:
Originally Posted by ZunuZunu View Post
nasty.web,

1. The solution is already given by imaginemn.

2. I didn't read all your solutions but your first solution is wrong. You are using third variable (array).

HTH.
1. Imaginemen asked
Quote:
I'm trying to see how many ways this could be done using PHP.
So I posted some alternatives. Is it wrong?

2. Well, list and array are language constructs like if, for or even brackets. Eval and extract are functions. AFAIK all variables in PHP start with dollar sign and can be modified runtime.
 
Reply With Quote
  #16  
Old 09-30-2008, 08:05 AM
nasty.web nasty.web is offline
v7n Mentor
 
Join Date: 07-24-06
Posts: 688
iTrader: 1 / 100%
Hey, could someone show me the light in that tricky PHP world? How can I treat the array definition like a variable itself?

Is it possible
PHP Code:
array() = (...) # some definition here 
like in JavaScript?
Code:
Array.prototype = ...
ZunuZunu? AnonymousUser?
 
Reply With Quote
  #17  
Old 09-30-2008, 09:56 AM
ZunuZunu ZunuZunu is offline
Contributing Member
 
Join Date: 09-04-08
Posts: 363
iTrader: 3 / 100%
Quote:
1. Imaginemen asked
Yep, but he posted the solution in his 2nd post.

Quote:
So I posted some alternatives. Is it wrong?
Posting an alternate solution is NOT wrong. But your solution is wrong. Why? Because it uses more resources.

Quote:
2. Well, list and array are language constructs like if, for or even brackets. Eval and extract are functions. AFAIK all variables in PHP start with dollar sign and can be modified runtime.
Well, arrays is function and list is language construct. See PHP help for others.

If you use either arrays or list to solve the problem then it will take more computer resources. So it's not a good method to get the required solution.
 
Reply With Quote
  #18  
Old 09-30-2008, 10:09 AM
nasty.web nasty.web is offline
v7n Mentor
 
Join Date: 07-24-06
Posts: 688
iTrader: 1 / 100%
ZunuZunu

list($arg1, $arg2) = array($arg2, arg1)

scales down to one instruction like xchg in assembler, while 3 mathematical operators scale down to three operators. What kind of "more resources" do you have in mind?
 
Reply With Quote
  #19  
Old 09-30-2008, 10:09 AM
ZunuZunu ZunuZunu is offline
Contributing Member
 
Join Date: 09-04-08
Posts: 363
iTrader: 3 / 100%
Quote:
Because it uses more resources.
You may wonder.. how?

I can explain it to you but I am running short on time. But you can search 'php array memory usage' in google.

HTH.
 
Reply With Quote
  #20  
Old 09-30-2008, 10:14 AM
nasty.web nasty.web is offline
v7n Mentor
 
Join Date: 07-24-06
Posts: 688
iTrader: 1 / 100%
btw: could you remind me where you saw the resources limit in the number swap requirements?
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Google Analytics old code vs new code m42 Google Forum 0 03-29-2008 12:04 AM
$28K Challenge phpAdNetwork Marketing Forum 2 03-27-2008 07:51 AM
A CHALLENGE for all you Web geniuses! blairtro Web Design Lobby 18 12-12-2006 03:21 PM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 05:04 AM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2018 VIX-WomensForum LLC