![]() So now, we just need to know how to call the function. Handling these cases is simple, if we got the actual solution, we just want to return that up the chain and if we got a fail ( False), we just want to return False And that will indicate to the node above us that we did not succeed and tell it to continue its search. So, after calling it, the function will return one of two things, either False indicating that the last node did not find the password (so this would occur in the case where something like ab reached the end in our search for bob so returned False after no solution was found), or, the call could return the actual solution. And we are at the easy case again and already know what to do! Now that we have called this function, it will run through subtracting one from the length at each of the subsequent calls it makes until it eventually reaches the case where length = 0. It already has the string (as this was the result of concatenating the next character in the chars string) and the length is just going to be one less as we just added one to the string through the concatenation and the goal is clearly going to be the same - we are still searching for the same password. Well, to handle these, we just need to think what the next function needs to know. We also need to know what to do with the length and goal variables when calling the next function. This will create a tree like affect where every string up to the original length is checked. In these cases, the aim is to take the string that we have been called with and loop through all of the characters in chars, each time calling the brute_force function (recursive) with the result of the concatenation of the string we were called with and that character ( c). We have now finished the case where length = 0 and now need to handle the other cases. We return either the solution or False to indicate to the function which called us (the call above in the stack) that we found the right password (or not). If it matches, then we return the string, otherwise we return False. This is the case when we already have a string that is the length of the goal and we just want to check whether it is correct. In the main body of the function, we need to first check the case where length is 0 (done by checking not length as 0 evaluates to False). The next variable goal is the correct password which we just pass through and is compare against. ![]() The next variable length holds how many characters there are to go till the string is the right length. ![]() The variable string holds the current guess up to this point, so in the first example, string will be everything up to bob such as ab, bo etc. We always call each function with the three variables: string, length and goal. Which you can call with syntax like: > brute_force('', 3, 'bob') S = brute_force(string + c, length - 1, goal) You can use the following recursive function: def brute_force(string, length, goal):
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |