Challenge: Recursive Recluse

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 14241 Views - Last Post: 16 September 2013 - 09:09 AM

#16 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 189
  • Joined: 17-February 13

Re: Challenge: Recursive Recluse

Posted 13 September 2013 - 05:40 AM

Technically it's a constant, not a variable ;). I could have declared it at the start of the function, but it seemed wasteful to declare it again for each recursive call. Having it as another argument though, is fine as well i suppose.

I guess i should have a explained a bit about how it works. See the spoiler.

Spoiler

This post has been edited by Witchking: 13 September 2013 - 05:45 AM

Was This Post Helpful? 0
  • +
  • -

#17 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Challenge: Recursive Recluse

Posted 13 September 2013 - 05:44 AM

My Implementation of the Matching Brace Position Challenge.

Spoiler


It maybe a constant but you reference it via a named variable Brackets which is external and not a parameter that is passed in. (As per the challenge)

This post has been edited by AdamSpeight2008: 13 September 2013 - 05:48 AM

Was This Post Helpful? 0
  • +
  • -

#18 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Challenge: Recursive Recluse

Posted 13 September 2013 - 06:06 AM

Is slightly tweaked the lambda.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#19 ybadragon  Icon User is offline

  • Home Owner
  • member icon

Reputation: 571
  • View blog
  • Posts: 2,647
  • Joined: 11-May 12

Re: Challenge: Recursive Recluse

Posted 13 September 2013 - 06:39 AM

@AdamSpeight2008

You failed your own challenge

Quote

So your challenge is to slay that beast by not only writing a recursive function, but also writing an informative explanation for a learner to understand it.


I'm a learner with no former knowledge of what a lambda expression is :P
Was This Post Helpful? 1
  • +
  • -

#20 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Challenge: Recursive Recluse

Posted 13 September 2013 - 02:00 PM

A variation of Witchking's method
Spoiler

Was This Post Helpful? 0
  • +
  • -

#21 andrewsw  Icon User is online

  • lashings of ginger beer
  • member icon

Reputation: 6337
  • View blog
  • Posts: 25,550
  • Joined: 12-December 12

Re: Challenge: Recursive Recluse

Posted 13 September 2013 - 02:08 PM

I'm with ybadragon. @Adam to meet your own challenge you have to provide an informative explanation :)
Was This Post Helpful? 0
  • +
  • -

#22 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Challenge: Recursive Recluse

Posted 13 September 2013 - 02:33 PM

It doing the exact same thing as Witchking's method. So why duplicate the explanation?
Only difference is how it calculate the next step.

Dim i = "} {".IndexOf(Code(Pos)) - 1
' Code(Pos) =
'             "}"  => i = -1
'             "{"  => i = +1


Calculating the index of the opposite brace, is handled by the following.
"] ["(-1 * i + 1)

 -1 * -1 ("}") =>  1 + 1 => 2 Index of matching brace
 -1 *  1 ("{") => -1 + 1 => 0 Index of matching brace



On subject of lambda expressions, is essentially using function as value (something that can be passed around and used.

If you look a the early example you'll see I'm embedding another function inside a function.

This post has been edited by AdamSpeight2008: 13 September 2013 - 02:36 PM

Was This Post Helpful? 0
  • +
  • -

#23 happyvisu  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 03-August 13

Re: Challenge: Recursive Recluse

Posted 16 September 2013 - 09:09 AM

Example: like scan a folder
Sub scanfolder(byval currdir as String)
   if interrupt Then Exit Sub
   Dim Dir As String
   Dim File As String
   Dim FI As FileInfo 
   Label1.Text = "Scanning " & currdir
   For Each File In Directory.GetFiles(currdir)
     FI = New FileInfo(File)
     FileList.Items.Add(FI.Name & vbTab & FI.Length & vbTab & FI.CreationTime)
   Next
   countFiles += Directory.GetFiles(currdir).Length
   Me.Text = "Scanning " & FolderList.Text & " "-Processed " & countFiles & "files in " & countFolders & "folders..."
   For Each Dir In Directory.GetDirectories(currdir)
     countFolders +=1
     Application.DoEvents()
     ScanFolder(Dir)
   Next
End Sub



Recursion function must exit explicitly
Examine the code:
Function DoSomething(n as Integer) As Integer
{other statements}
value=value-1
if value = 0 then Exit Function
newvalue = DoSomething(value)
{more statements}
End Function

Example: like scan a folder
Sub scanfolder(byval currdir as String)
if interrupt Then Exit Sub
Dim Dir As String
Dim File As String
Dim FI As FileInfo 
Label1.Text = "Scanning " & currdir
For Each File In Directory.GetFiles(currdir)
FI = New FileInfo(File)
FileList.Items.Add(FI.Name & vbTab & FI.Length & vbTab & FI.CreationTime)
Next
countFiles += Directory.GetFiles(currdir).Length
Me.Text = "Scanning " & FolderList.Text & " "-Processed " & countFiles & "files in " & countFolders & "folders..."
For Each Dir In Directory.GetDirectories(currdir)
countFolders +=1
Application.DoEvents()
ScanFolder(Dir)
Next
End Sub


This post has been edited by andrewsw: 16 September 2013 - 09:13 AM
Reason for edit:: Fixed code tags

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2