QBasic => QB Projects => Topic started by: lrcvs on September 17, 2008, 02:43:01 PM

Title: Capicua
Post by: lrcvs on September 17, 2008, 02:43:01 PM
'Este programa comprueba si un numero / texto es capicua
'This program verifies whether a number / reads capicua

'lrcvs 15.09.08

'Examples /Ejemplos:

'"1234554321" / "abcddcba" = Yes / Si  =  capicua

'"1279256486" / "dfsafdas" = Not / No <>  capicua

'z$ = "0123456789876543210" 'Number$ / text to verify
INPUT "Text / Number to verify = "; z$
l = LEN(z$)
FOR n = 1 TO l
a$ = MID$(z$, n, 1)
b$ = MID$(z$, (l + 1) - n, 1)
'PRINT a$, b$
IF a$ <> b$ THEN PRINT "No es capicua / Not is capicua": BEEP: GOTO 20
PRINT "Si, es capicua / Yes, is capicua"
20 :

Title: Re: Capicua
Post by: Neo on September 24, 2008, 05:50:28 AM
I see you've been posting your snippets all over the place the past few weeks. I thought I'd comment on your most recent one.

First of all, your code seems to determine whether a string is a palindrome or not. In that aspect it works, but your code does some things that I count as excessive.

I would do something like the following.



  DIM pointer AS LONG, mirrorpointer AS LONG
  DIM halfway AS INTEGER

  isPalin = 1

  L = LEN(Z)
  page = VARSEG(Z)
  DEF SEG = page

  pointer = SADD(Z)
  mirrorpointer = pointer + L - 1

  halfway = L \ 2
  FOR i = 1 TO halfway
    IF PEEK(pointer) <> PEEK(mirrorpointer) THEN
      isPalin = 0
      EXIT FOR
    END IF
    pointer = pointer + 1
    mirrorpointer = mirrorpointer - 1
  NEXT i


  IsPalindrome = isPalin

Note that since I wrote this down without hardly any thinking, I can't guarantee its integrity  8) Note that it behaves the same as your code in the aspect that it is case sensitive.

Title: to : Neo
Post by: lrcvs on September 24, 2008, 06:21:17 PM
Hi, Neo:

Yes, your program is very perfect.!!!

My program is simple.

The two programs do the same thing.
I think your program is perfect, but excessive.
My program is this:
INPUT "Text / Number to comp. = "; z$
l = LEN(z$)
FOR n = 1 TO l
a$ = MID$(z$, n, 1)
b$ = MID$(z$, (l + 1) - n, 1)
IF a$ <> b$ THEN PRINT "No es capicua / Not is capicua": BEEP: GOTO 20
PRINT "Si, es capicua / Yes, is capicua"
20 :
Aprox. 10 Lines.
I think that the development must be to proportional the expected result.

Your program is completely perfect.!!!

Greetings from Spain

Thanks for your interest!!!

Title: Re: Capicua
Post by: Ralph on September 25, 2008, 01:10:48 AM
The following is written, with all due respect to professional programmers.

Since I am but an amateur programmer at best, I am all for the simplest programming possible.  Like not doing the DIMs and functions and such in a short program.  Why would anybody want to add more code to a short program?  Now, if we were setting out o write a very large program, I could see the merit in DINning the variables.  And, if we were planning on using a function any number of times, a function would be very useul; but, for once, only?  Or, if we were trying to teach someone who will write large programs, it would probably be fine, too. 

But, I must say that I like the simple, direct approach that Luis (;rcvs) employed.  I like to see code so simple that it feels as if I were thinking the thing through without a lot of planning and trying to code "by the rules of 'good' programming".

Title: Re: Capicua
Post by: roy on September 25, 2008, 07:10:27 AM
I got the impression that Neo was just showing off.

Title: Re: Capicua
Post by: Neo on September 25, 2008, 07:22:01 AM
I think you both missed the point of my post.

First of all @Ralph only:
This mindset is all fine when you're a not-professional amateur programmer as you say you are, but you did fail to see the importance of me adding those things you condemn. It is true that they may have little effect in such a small code, but on larger code you will notice it.

Let's start with why I put it in a function... Most short programs, like this one, are pretty useless in themselves, and commonly are meant to be employed in some bigger structure. So, I already incorporated the fact that some user may want to use my code in their huge program, and in that case, code like lrcvs's is not practical. Hence: I put it in a function, and a separate sourcefile, to be turned into some module or compiled into a library that this user's huge program uses.

Now on to the point of adding dims and such for anything you use... it improves readability. If you have a large code where on every line a new variable may be created, you will have a hard time keeping track of which variables were already declared and which were not. So yeah, it helps with reading code. There are many more factors that determine the readability of your code though, like indentation (which lrcvs's code also lacks) and many more.

And yes, I am currently a professional programmer (although parttime during school), though not in qbasic but C++ (which really shows in my code, lol).

Anyway, on to the next topic.

@lrcvs & @Ralph:

As I said at the complete start of this post, you missed the point of me posting a different snippet. I just noticed that lrcvs's code could be made a lot faster by a few simple alterations. This by no means means that my code is hyper-optimized, on the contrary even. But you will see the difference with a small benchmark:

The benchmark executes 1 million evaluations of a non-palindrome words and palindrome words of 23 letters, both in interpreter form and compiled exe form.
Result on non-palindromes:
Mine: 2.53 sec / exe: 0.61 sec
Yours: 2.96 sec / exe: 1.70 sec
Yours(optimized): 2.97 sec / exe: 1.65 sec
Most of this is probably function overhead, since the actual evaluation loop is very short.
Result on 23-letter palindrome:
Mine: 5.65 sec / exe: 0.82 sec
Yours: 21.70 sec / exe: 12.96 sec
Yours(optimized): 10.93 sec / exe: 6.59 sec
Yours(optimized) is lrcvs's code, but only looping till half of the string and not the whole thing.

You should be able to see the difference now. I can by no means guarantee how much of this benchmark is correct, since I do not know the caching behaviour of windows, nor do I know how BC.EXE compiles all this code.

But yeah, I suggest you (lrcvs) at the very least make your code loop only half of the string instead of the whole thing, you win a lot of time with that, according to the test results.

Title: Re: Capicua
Post by: lrcvs on September 25, 2008, 02:36:53 PM
Hello everyone:

First, this is an absurd conversation.

This is a program for children and not need a conversation absurd as this.

Second, I am not a programmer.

I do programs for fun or experimentation.

I just like to make programs in Qbasic, Ok !!!

And only a littles programs in Qbasic.

I believe it is not important that a program such as this the time = 1 Sec or time = 0.0000005, Is absurd.

Yes..., Neo your program is better.

Thanks for your assesorament, I learning too!!!

Title: Re: Capicua
Post by: Ralph on September 26, 2008, 01:13:39 AM
I agree with Meo's thinkint 100% ... for a professional programmer.  But, I disagree with the insistance of making amateur programmers do things as if they were professionals ... or even as if they want to become professionals.  I think that if someone wants o program, and it works, fine.  Let's not ask perfection, unless the programmer asks for it!  Then, yes, it will be welcome!