Author |
Message |
|
johnbasedow
Newbie
Joined: 27 Sep 2007 Posts: 21
|
Posted: 27 Sep 2007 10:53:22 pm Post subject: |
|
|
I present you with the challenge of making the smallest program that will reverse a string.
The one I'm working on is 50 bytes including title, but I KNOW someone can go smaller. Much smaller.
Here's what I have so far:
PROGRAM:A
:" (one space)/->/Str1
:Input Str2
:For(X,length(Str2),1,-1
:Str1+sub(Str2,X,1/->/Str1
:End
Last edited by Guest on 27 Sep 2007 11:06:06 pm; edited 1 time in total |
|
Back to top |
|
|
Harrierfalcon The Raptor of Calcs
Super Elite (Last Title)
Joined: 25 Oct 2006 Posts: 2535
|
Posted: 27 Sep 2007 10:59:32 pm Post subject: |
|
|
Admin, someone, merge this into the 83 Basic Brain Teaser?
Anyways, I've come up with 50 bytes--input string is in Str1, output string in Ans. 50 bytes is the program minus the length of the title.
Try not to doublepost unless needed. Unless the topic is almost dead, just edit your post.
And, generally speaking, these challenges are made to be as small as possible--i.e. the input string probably should be in Ans, and the output probably should be in Ans as well. Don't try and add effects. That said, yours technically doesn't work, because there is a space preceding the output string.
Last edited by Guest on 27 Sep 2007 11:01:50 pm; edited 1 time in total |
|
Back to top |
|
|
johnbasedow
Newbie
Joined: 27 Sep 2007 Posts: 21
|
Posted: 27 Sep 2007 11:09:29 pm Post subject: |
|
|
Harrierfalcon wrote: That said, yours technically doesn't work, because there is a space preceding the output string.
[post="113415"]<{POST_SNAPBACK}>[/post]
yeah, but i didnt feel like throwing in a
sub(Str1,2,L
...it's comprehensible enough |
|
Back to top |
|
|
DarkerLine ceci n'est pas une |
Super Elite (Last Title)
Joined: 04 Nov 2003 Posts: 8328
|
Posted: 27 Sep 2007 11:16:50 pm Post subject: |
|
|
My current attempt is 43 bytes when you subtract the length of the title (input and output both in Ans)
Last edited by Guest on 27 Sep 2007 11:20:48 pm; edited 1 time in total |
|
Back to top |
|
|
Weregoose Authentic INTJ
Super Elite (Last Title)
Joined: 25 Nov 2004 Posts: 3976
|
Posted: 28 Sep 2007 12:09:14 am Post subject: |
|
|
DarkerLine wrote: My current attempt is 43 bytes when you subtract the length of the title (input and output both in Ans) I have the same (ignoring the second string's starting character, obviously). 43 bytes.
All my sentences are turning into questions backwards.
[EDIT]
Oh, I've got it. Use a space for the filler text instead! Genius.
[EDIT×2]
Hey, welcome to the forums, by the way.
Last edited by Guest on 28 Sep 2007 12:15:33 am; edited 1 time in total |
|
Back to top |
|
|
DarkerLine ceci n'est pas une |
Super Elite (Last Title)
Joined: 04 Nov 2003 Posts: 8328
|
Posted: 28 Sep 2007 10:22:45 am Post subject: |
|
|
Oh. I didn't use a second string. And I don't have a filler character.
Quote:
For(I,1,length(Ans)-1
sub(Ans,2I,1)+Ans
End
sub(Ans,1,I
(it's 42 bytes now, by the way)
Last edited by Guest on 23 Sep 2010 11:54:11 pm; edited 1 time in total |
|
Back to top |
|
|
thornahawk μολών λαβέ
Active Member
Joined: 27 Mar 2005 Posts: 569
|
Posted: 28 Sep 2007 12:30:54 pm Post subject: |
|
|
This particular one is almost twice the size, but it seems to dent the memory less if the string is long:
(highlight below)
Ans→Str0
length(Ans)–1→L
For(K,1,L
sub(Str0,K+1,1)+sub(Str0,1,K
If K<L
Ans+sub(Str0,K+2,L–K
Ans→Str0
End
Str0
thornahawk |
|
Back to top |
|
|
johnbasedow
Newbie
Joined: 27 Sep 2007 Posts: 21
|
Posted: 07 Oct 2007 12:53:12 am Post subject: |
|
|
Quote:
For(I,1,length(Ans)-1
sub(Ans,2I,1)+Ans
End
sub(Ans,1,I
can you walk me through this? it works, but i don't understand the code
Last edited by Guest on 23 Sep 2010 11:53:55 pm; edited 1 time in total |
|
Back to top |
|
|
DarkerLine ceci n'est pas une |
Super Elite (Last Title)
Joined: 04 Nov 2003 Posts: 8328
|
Posted: 07 Oct 2007 04:18:21 pm Post subject: |
|
|
suppose the string we start with is "HELLO".
We begin by adding the SECOND (I=1 so 2I=2) character of the string to the beginning, to get "EHELLO".
Then we add the fourth (I=2 so 2I=4) character of the current string, which was the THIRD character of the original string: now we have "LEHELLO"
Then the sixth (I=3 so 2I=6) character of the current string, which was the FOURTH character of the original string: now we have "LLEHELLO"
Finally, we add the 8th character, which was originally the fifth, to get "OLLEHELLO".
(In general, when we're adding the 2Ith character of the current string, we've already added I-1 characters to the front, so we're really adding the I+1th character of the original string)
The first 5 characters of the string are the original string backwards - "OLLEH", so we take those 5 characters and get our result. Notice that after we exit the loop, I is the next value we'd be on if we continued - the last value of I we took was 4, so I is 5 now, which is just the length of the string.
Does this help?
Last edited by Guest on 07 Oct 2007 04:19:25 pm; edited 1 time in total |
|
Back to top |
|
|
adriweb
Newbie
Joined: 28 Aug 2007 Posts: 22
|
Posted: 16 Oct 2007 06:20:00 am Post subject: |
|
|
it helped me !
Thanks !
(you're a so great programmer ^^ ) |
|
Back to top |
|
|
jwymbs23
Newbie
Joined: 19 Mar 2007 Posts: 14
|
Posted: 31 Oct 2007 07:32:24 pm Post subject: |
|
|
So in these uberoptimisations, do we not leave in the input functions? |
|
Back to top |
|
|
JoostinOnline
Active Member
Joined: 22 Aug 2007 Posts: 559
|
Posted: 31 Oct 2007 07:35:07 pm Post subject: |
|
|
You should, but people cheat sometimes and leave them out. |
|
Back to top |
|
|
DarkerLine ceci n'est pas une |
Super Elite (Last Title)
Joined: 04 Nov 2003 Posts: 8328
|
Posted: 31 Oct 2007 07:48:18 pm Post subject: |
|
|
Well, usually the idea is that you pass in the argument in Ans. I mean, you can't really optimize an "Input" or "Prompt" command, so it doesn't really matter. |
|
Back to top |
|
|
JoostinOnline
Active Member
Joined: 22 Aug 2007 Posts: 559
|
Posted: 31 Oct 2007 08:09:44 pm Post subject: |
|
|
DarkerLine wrote: Well, usually the idea is that you pass in the argument in Ans. I mean, you can't really optimize an "Input" or "Prompt" command, so it doesn't really matter.
[post="115214"]<{POST_SNAPBACK}>[/post]
I'm talking about when people compare program size |
|
Back to top |
|
|
DarkerLine ceci n'est pas une |
Super Elite (Last Title)
Joined: 04 Nov 2003 Posts: 8328
|
Posted: 31 Oct 2007 08:28:39 pm Post subject: |
|
|
That's never been an issue in those cases, as both people were using the same conventions every time.
Last edited by Guest on 31 Oct 2007 08:29:04 pm; edited 1 time in total |
|
Back to top |
|
|
|