rainwater_stillicide

Challenge: BASIC001

30 posts in this topic

Once again Perl is awesome.

[spoiler][code]
($a,$b) = ($b,$a);
[/code][/spoiler]

Share this post


Link to post
Share on other sites
[quote name='pix6ie' timestamp='1306087107' post='358312']
In python:

a = 10
b = 20
a = a*2
b = a/2

Is this too simplified??
[/quote]

It's not too simplified (it does take two variables and swap them) but it doesn't do it in a [i]general[/i] way, it will only work for certain numbers (i.e., where b = 2a).

Try and come up with a solution which can work for an arbitrary pair of numbers.

Share this post


Link to post
Share on other sites
[quote name='rainwater_stillicide' timestamp='1306091498' post='358314']
[quote name='pix6ie' timestamp='1306087107' post='358312']
In python:

a = 10
b = 20
a = a*2
b = a/2

Is this too simplified??
[/quote]

It's not too simplified (it does take two variables and swap them) but it doesn't do it in a [i]general[/i] way, it will only work for certain numbers (i.e., where b = 2a).

Try and come up with a solution which can work for an arbitrary pair of numbers.
[/quote]

It is just too easy to do in Python. Don't make it too hard on yourself. Here is the easiest way to do it in Python.
[spoiler][code]
a,b=b,a
[/code][/spoiler]

Share this post


Link to post
Share on other sites
Old thread but...

[CODE]
a = 2
b = 3
print a, b
a, b = b, a
print a, b
[/CODE]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now