Results 1 to 25 of 25

Thread: Are you smart enough?

  1. #1

    Thread Starter
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593
    Most highly trained and skilled professors, programmers, text books, etc. claim that to swap two variables, A and B, so that A contains B's value and B contains A's value, you need a third temporary variable, C.

    Code:
    Dim a As Integer
    Dim b As Integer
    Dim c As Integer
    
    Let a = 10
    Let b = 20
    
    '
    ' Swap them...
    '
    Let c = a  ' c is 10
    Let a = b  ' a is 20
    Let b = c  ' b is 10
    Now, if you guys are so smart, then let me say this: There is a way to swap the two variables WITHOUT a third variable. Can anyone figure out this little riddle?

    If you want a hint, wait for a few days, and I will post it.

    Let's see how clever you all are.

  2. #2
    Hyperactive Member CyberSurfer's Avatar
    Join Date
    Aug 2000
    Location
    Old London Town
    Posts
    425

    Talking

    Probably not a very elegant solution, but here goes.

    dim a as string
    dim b as string

    a="Hello"
    b="There"

    open "c:\test.txt" for random as #1
    put #1, 1, a
    close #1

    a = b

    open "c:\test.txt" for random as #1
    get #1, 1, b
    close #1

    et voila!!!

  3. #3
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    I GOT IT AFTER LIKE 1 HOUR:

    Code:
    'I give credit to oetje for helping me with the positive and negative stuff
    Private Sub Form_Load()
    Dim a As Integer
    Dim b As Integer
    
    Let a = -753
    Let b = 233
    
    'Let b = a  ' 10 is 10
    'Let a = b  ' b is 20
    
    If b > a Then
    b = b - a
    a = a + b
    b = b - a
    '****help from oetje
    b = b - (2 * b)
    '*******************
    Else
    b = b - a
    a = a + b
    b = b - a
    '****help from oetje
    b = Abs(b)
    '*******************
    End If
    
    
    Label1.Caption = a
    Label2.Caption = b
    
    End Sub
    add 2 labels and run it to see for your self
    NXSupport - Your one-stop source for computer help

  4. #4
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    now do I get a nobel prize award for it? or a million dollars? or even better... A Lolli POP
    NXSupport - Your one-stop source for computer help

  5. #5
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Use Xor

    For some pairs of variables, Xor can be used to swap values.
    Code:
    A = A Xor B 
    B = A Xor B '(A Xor B) Xor B = A
    A = B Xor A 'A Xor (A Xor B) = B
    I would not be sure that the above would work if A, B are different length or different types of variables.


    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  6. #6
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'Label1,Label2,Text1,Text2,Command!,Form1
    'the only sticky part was using double to avoid overflow
    'of course if you are moving words that is another story
    'for another time
    
    Private Sub Command1_Click()
    
        Dim a As Double
        Dim b As Double
        
        
        a = Text1.Text
        b = Text2.Text
        a = CDbl(a)
        b = CDbl(b)
        Label1.Caption = "a = " & a & "   " & "b =  " & b
        b = b * a
        a = b / a
        b = b / a
        
        Label2.Caption = "a = " & a & "   " & "b =  " & b
    End Sub
    
    Private Sub Form_Load()
        Text1.Text = ""
        Text2.Text = ""
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  7. #7
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    thats aalmost the same thing that I did except I didn't add textboxes
    NXSupport - Your one-stop source for computer help

  8. #8

    Thread Starter
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593

    Thumbs up GUV is smart

    Yeah man.

    GUV, you've got it right. The correct answer is use XOR.

    Code:
    Dim a As Integer
    Dim b As Integer
    
    Let a = 'anything
    Let b = 'anything
    
    Let a = a Xor b
    Let b = a Xor b
    Let a = a Xor b
    I really like dimawa's solution and also the one that came from CyberSurfer.

    However, although this problem has been solved: What would you do if you could NOT use Xor and no external storages, including but not limited to (heard that somewhere), clipboard, files, variables, web servers, haha.

    You can still use logical operators such as AND, OR, but you CAN'T use XOR.

  9. #9
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    I didn't use any other stuff like that, no Xor and no external storages, including but not limited to (heard that somewhere), clipboard, files, variables, or web servers
    NXSupport - Your one-stop source for computer help

  10. #10

    Thread Starter
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593

    Unhappy Dear dima

    I am not gonna judge you...

    I haven't tried out your solution, but does it work with ANY value for a and b???

    I know this was not explicitly an initial requirement, but is now, haha

    Please don't hate me too much

  11. #11
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    1. I didn't get the inslut
    2. yes it does woerk with any numerical value
    NXSupport - Your one-stop source for computer help

  12. #12

    Thread Starter
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593

    dimawa

    OK, I admit I have still not tried it out, mainly because of time pressure, but I will.

    If it turns out your solution works, then poor Guv will have to share the victory with you. (The first one), the second victory would go to you. But, I gotta check it out first. So, tomorrow, is the big day.

    All you guys, keep those suggestions coming in. Surely there must be more than one simple solution to such a simple problem.

  13. #13
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    A+B?

    It occurred to me that for numeric variables, the following would work if roundoff/truncation does not throw a monkey wrench into the works.
    Code:
    A = A + B
    B = A - B '(A+B)-B = A
    A = A - B '(A+B)-A = B
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  14. #14
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    what do you mean?
    NXSupport - Your one-stop source for computer help

  15. #15
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Construct Xor?

    If construction of Xor from And, Or, & Not is allowed, you can do just that.

    (A Or B) And Not(A And B) is equivalent to Xor.
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  16. #16
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    well what you are saying is that what I made is the same thing as xor?
    NXSupport - Your one-stop source for computer help

  17. #17
    Guest

    another riddle

    what is the fastest way to reverse a 30,000 character string?

    this should keep you occupied.

    hint, it can be done in 0.07 of a second on a P200MMX PC.

    Have fun

  18. #18
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    wait wait wait, let hax soft say that I'm right first
    NXSupport - Your one-stop source for computer help

  19. #19

    Thread Starter
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593

    Talking Hi dima

    dimava, You're right.

    You are just fabulous and smart, haha

  20. #20
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    thanks, I KNEW I WAS CORRECT
    NXSupport - Your one-stop source for computer help

  21. #21

    Thread Starter
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593
    That string reversing question is very interesting ... are we working with or without Windows API calls?

  22. #22
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    Xor leaps in battle again.

    I think the following might work (I did not test it).
    Code:
    Private Sub Reverser(LongStr As String)
    Dim N As Integer
    Dim M As Integer
    Dim A As Integer
    Dim Z As Integer
    
    N = Len(LongStr)
    M = N / 2
    
    Z = N
    For A = 1 to M
    	Mid(LongStr, A, 1) = Mid(LongStr,  A, 1) _
    				Mid(LongStr, Z, 1)
    	Mid(LongStr, Z, 1) = Mid(LongStr,  A, 1) _
    				Mid(LongStr, Z, 1)
    	Mid(LongStr, A, 1) = Mid(LongStr,  A, 1) _
    				Mid(LongStr, Z, 1)
    	Z = Z - 1
         Next A
    
    End Sub
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  23. #23
    Member
    Join Date
    May 2000
    Location
    Sweden
    Posts
    50

    Talking

    I think the easyest way would be:

    StringB = StrReverse(StringA)

    Wolf

    VB6 SP5

  24. #24
    Guest

    Talking Another brainteaser

    How do you store the structure of a whole hard drive in a treeview without using a recursive loop?
    No controls, type libraries, dos commands are allowed.
    BTW and FYI, I don't have the answer

  25. #25
    Guest

    Question

    I dont think its possible without using a recursive loop, unless the FileSystemObject has a method to do it. Why would you constrain yourself to not using recursion? that would only become a problem if you had thousands of nested directories, and then call stack space would fill up fast.



    [Edited by wossname on 08-27-2000 at 06:31 AM]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width