Jump to content


Photo

Challenge: BASIC001


  • Please log in to reply
29 replies to this topic

#1 rainwater_stillicide

rainwater_stillicide

    SUP3R 31337 P1MP

  • Agents of the Revolution
  • 282 posts
  • Location:Scotland

Posted 02 October 2009 - 09:36 PM

This is a pretty simple challenge but quite fun.

Using the language of your choice: Write a function in which two integer variables (a and b) have their values swapped without using any temporary variables.

#2 PurpleJesus

PurpleJesus

    Dangerous free thinker

  • Members
  • 1,578 posts
  • Gender:Male
  • Location:800

Posted 02 October 2009 - 09:57 PM

This is a pretty simple challenge but quite fun.

Using the language of your choice: Write a function in which two integer variables (a and b) have their values swapped without using any temporary variables.


neat.. Would be cool to see how people approach this.. Here's mine..

Spoiler


edit:
Oh damn, you said function. I'm gonna have to think about this a bit.

Edited by PurpleJesus, 02 October 2009 - 10:06 PM.


#3 SigFLUP

SigFLUP

    SUPR3M3 31337 Mack Daddy P1MP

  • Members
  • 367 posts
  • Gender:Not Telling

Posted 02 October 2009 - 10:33 PM

Spoiler


edit: there are also some processor that have a swap instruction.

Edit by Ohm: Hid answer with spoiler tags.

Edited by Ohm, 03 October 2009 - 10:47 AM.


#4 SigFLUP

SigFLUP

    SUPR3M3 31337 Mack Daddy P1MP

  • Members
  • 367 posts
  • Gender:Not Telling

Posted 02 October 2009 - 10:34 PM

So I don't get it, whoever the winner is do they choose the next challenge or something?

Edited by SigFLUP, 02 October 2009 - 10:40 PM.


#5 rainwater_stillicide

rainwater_stillicide

    SUP3R 31337 P1MP

  • Agents of the Revolution
  • 282 posts
  • Location:Scotland

Posted 02 October 2009 - 10:57 PM

So I don't get it, whoever the winner is do they choose the next challenge or something?


Anyone can add a challenge. Here are the rules for the challenges forum: http://www.binrev.co...58-forum-rules/

#6 PurpleJesus

PurpleJesus

    Dangerous free thinker

  • Members
  • 1,578 posts
  • Gender:Male
  • Location:800

Posted 02 October 2009 - 11:03 PM

okay, here it is as a function...
Spoiler


SigFlup, Could you explain how yours works. I'm curious to know what's happening there.

--btw: read the rules, Should this be posted as a new topic, but titled "question: BASIC001"? Little confusion here.

edit: posthumously added spoiler. opps.

Edited by PurpleJesus, 03 October 2009 - 07:08 AM.


#7 rainwater_stillicide

rainwater_stillicide

    SUP3R 31337 P1MP

  • Agents of the Revolution
  • 282 posts
  • Location:Scotland

Posted 02 October 2009 - 11:26 PM

--btw: read the rules, Should this be posted as a new topic, but titled "question: BASIC001"? Little confusion here.


I believe the solutions should be posted in a new topic called "Answer: BASIC001". I'm not sure whether the person who creates the Challenge thread is also meant to create the Answer thread, or whether it's create by the first person to come up with an answer.

Edited by rainwater_stillicide, 02 October 2009 - 11:27 PM.


#8 FLW_FTW

FLW_FTW

    HACK THE PLANET!

  • Members
  • 59 posts
  • Country:
  • Gender:Male
  • Location:San Francisco

Posted 03 October 2009 - 12:26 AM

This is a pretty simple challenge but quite fun.

Using the language of your choice: Write a function in which two integer variables (a and b) have their values swapped without using any temporary variables.


Yay for only using addition and subtraction!:
Spoiler


And folks, don't forget the spoiler tags. just surround your code tags with spoiler tags.

Edited by FLW_FTW, 03 October 2009 - 12:26 AM.


#9 DanielG

DanielG

    SUP3R 31337 P1MP

  • Members
  • 294 posts
  • Location:The Netherlands

Posted 03 October 2009 - 06:17 AM

This was one of the first things we got thought in programming class when we were talking about logical operations.

Pseudo code:
Spoiler


Also I wonder for the people using purely arithmetic functions (like purplejesus) what would happen is the second variable is zero.

//edit, WTF? every time I save changes it capitalizes my b in the code :question: :confused: :blink:

Edited by DanielG, 03 October 2009 - 08:52 AM.


#10 modusTollens

modusTollens

    I broke 10 posts and all I got was this lousy title!

  • Members
  • 13 posts
  • Gender:Male

Posted 03 October 2009 - 07:25 AM

In AVR assembly (I'd do something else for larger values):

Spoiler


#11 modusTollens

modusTollens

    I broke 10 posts and all I got was this lousy title!

  • Members
  • 13 posts
  • Gender:Male

Posted 03 October 2009 - 08:01 AM

Pretty much the same mechanism using Python, again, assuming smaller integer values:

Spoiler


#12 Ohm

Ohm

    I could have written a book with all of these posts

  • Members
  • 3,209 posts
  • Gender:Male
  • Location:Maine, USA

Posted 03 October 2009 - 10:46 AM

Just a note on the answer threads, I put those rules in place for larger challenges where there may be lengthy answers and a lot of people answering. Each answer would then have its own thread, so it can be discussed. Don't worry about spamming threads, that's what this forum is for. Create as many threads as you need/want.

I really intended to post IGOR001 last week, but I'm really sick. Every time I think I'm getting better and have time/energy to finish it up and write the post, it just doesn't happen.

Anyway, this challenge has a more interesting counterpart: How do you reverse a string without allocating a second string?

#13 FLW_FTW

FLW_FTW

    HACK THE PLANET!

  • Members
  • 59 posts
  • Country:
  • Gender:Male
  • Location:San Francisco

Posted 03 October 2009 - 02:55 PM

Just a note on the answer threads, I put those rules in place for larger challenges where there may be lengthy answers and a lot of people answering. Each answer would then have its own thread, so it can be discussed. Don't worry about spamming threads, that's what this forum is for. Create as many threads as you need/want.

I really intended to post IGOR001 last week, but I'm really sick. Every time I think I'm getting better and have time/energy to finish it up and write the post, it just doesn't happen.

Anyway, this challenge has a more interesting counterpart: How do you reverse a string without allocating a second string?


Is that a new challenge?

#14 Ohm

Ohm

    I could have written a book with all of these posts

  • Members
  • 3,209 posts
  • Gender:Male
  • Location:Maine, USA

Posted 03 October 2009 - 03:17 PM

Not really. The tricky part was already solved, now you just have to iterate over a string and do it.

#15 FLW_FTW

FLW_FTW

    HACK THE PLANET!

  • Members
  • 59 posts
  • Country:
  • Gender:Male
  • Location:San Francisco

Posted 03 October 2009 - 04:14 PM

Not really. The tricky part was already solved, now you just have to iterate over a string and do it.

OK, here we go:
Spoiler


#16 livinded

livinded

    Dangerous free thinker

  • Agents of the Revolution
  • 1,942 posts
  • Location:~/

Posted 03 October 2009 - 04:53 PM

Spoiler


#17 PurpleJesus

PurpleJesus

    Dangerous free thinker

  • Members
  • 1,578 posts
  • Gender:Male
  • Location:800

Posted 03 October 2009 - 06:02 PM

Also I wonder for the people using purely arithmetic functions (like purplejesus) what would happen is the second variable is zero.


It bombs horribly.. Good catch.

edit: easy fix though..
Spoiler

Edited by PurpleJesus, 03 October 2009 - 06:52 PM.


#18 bcrscahh198987

bcrscahh198987

    Mack Daddy 31337

  • Members
  • 211 posts
  • Location:Ur rektumm

Posted 25 October 2009 - 07:55 AM

EDIT : I fixed my code

Spoiler


this is much simpler

Edited by bcrscahh198987, 31 October 2009 - 02:18 AM.


#19 bcrscahh198987

bcrscahh198987

    Mack Daddy 31337

  • Members
  • 211 posts
  • Location:Ur rektumm

Posted 25 October 2009 - 08:29 AM

So.... Is everyone using C#??

I recognize that <studio.h> header

Edited by bcrscahh198987, 31 October 2009 - 02:16 AM.


#20 Wintermute21

Wintermute21

    DDP Fan club member

  • Members
  • 53 posts

Posted 31 October 2009 - 09:59 PM

So.... Is everyone using C#??

I recognize that <studio.h> header


um, No. most of it is in C

and it's <stdio.h> the Standard C library I/O functions,




BinRev is hosted by the great people at Lunarpages!