recursion

How can I exclude directories using RecursiveDirectoryIterator

别说谁变了你拦得住时间么 提交于 2019-12-24 11:16:08
问题 I have the function below. Which goes through directories and recursively searches through them to grab a random image file and then attaches that to a post. What I want to do is exclude some files from the search. I have a comma separated list which I explode into an array, I tried using a filter but couldn't get this to work. Current function without filter is function swmc_get_imgs($start_dir, $ext, $exclude=array()){ $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(

How to unset elements using array_walk_recursive

本小妞迷上赌 提交于 2019-12-24 10:50:03
问题 I need to unset elements from arrays that are nested into another array, in a way that only the first N elements would be kept ( N being predefined). Only elements that have a numerical index should be affected. Input array: Array ( [0] => Array ( [a] => 'w' [b] => Array ( [0]=> 'x' [1]=> 'x' [2]=> 'x' ) ) [1] => Array ( [a] => 'y' ) [2] => Array ( [0] => 'z' [1] => 'z' [2] => 'z' ) ) Desired output (with N=2 ): Array ( [0] => Array ( [a] => 'w' [b] => Array ( [0]=> 'x' [1]=> 'x' ) ) [1] =>

Word Boundary Regular Expression Unless Inside HTML Tag

时光怂恿深爱的人放手 提交于 2019-12-24 10:48:12
问题 I have a regular expression using word boundaries that works exceedingly well... ~\b('.$value.')\b~i ...save for the fact that it matches text inside HTML tags (i.e. title="This is blue!" ). It's a problem because I'm doing text substitution on anything the regex matches, then making tooltips appear using those title tags. So, as you can imagine, it's substituting text inside the title and breaking the HTML of the tooltip. For example, what should be: <span class="blue" title="This is blue!"

State of an ArrayList in a recursive method

坚强是说给别人听的谎言 提交于 2019-12-24 10:46:38
问题 I have a recursive method like so: class MyClass { ArrayList<ArrayList<MyNode> allSeriesOfNodes = new ArrayList<ArrayList<MyNode>(); public void myMethod(MyNode currNode, ArrayList<MyNode> nodes) { // make sure currNode and nodes aren't null, blah.... nodes.add(currNode); for(MyNode n: otherNodeList) { for(listItem in n.getList()) { if(...) { myMethod(n, nodes); } } if(currNode is a leaf and nodes is > 1) { allSeriesOfNodes.add(nodes); } Debugging it I noticed when a stack frame pops and the

Function to return a table of all children of a node

╄→尐↘猪︶ㄣ 提交于 2019-12-24 10:45:16
问题 Let's say I've got the following table structure: | ID | ParentID | Name | I'd like to write a recursive PostgreSQL function for getting all child nodes of a node ID passed to it as a parameter. Here's my code so far (I only have one part of the function which gets all the children of the passed ID, and now I need the recursive part): CREATE OR REPLACE FUNCTION GetAllChildren(IN NodeID INTEGER) RETURNS INTEGER AS $$ DECLARE Crs CURSOR FOR SELECT ID, ParentID, Name FROM Tree WHERE ParentID

Binary Tree Recursive Function

大憨熊 提交于 2019-12-24 10:31:51
问题 I need to print out a binary tree that looks like this: --------x------- ----x-------x--- --x---x---x---x- -x-x-x-x-x-x-x-x xxxxxxxxxxxxxxxx Using recursion to print the left side of the line and the right side of the line with the exception of the first line. So the function would call a display function with parameters of the left starting point and the right ending point. Then it calls itself twice, on for the left side and one for the right. #include <stdio.h> #define LENGTH 16 void

reverse string recursive method

て烟熏妆下的殇ゞ 提交于 2019-12-24 10:14:03
问题 Hello Why my reverse method that uses recursion isn't working? The print statement shows that the operation is done correctly but at the end it seems like only the very ast char of the entire String is assigned to h. public static String reverse(String s,String h){ if(s.length()==0){ return s; } else { h+=s.charAt(s.length()-1); System.out.println(h);//FOR TEST s=s.substring(0,s.length()-1); reverse(s,h); return h; } } Any advice? 回答1: Use return reverse(s,h); instead of return h; i.e: public

Recursive JavaScript Method Scope

泪湿孤枕 提交于 2019-12-24 09:58:54
问题 I have this recursive function that is suppose to go through a JSON object to find all the subgroups. Here is what I have: var subgroupIds = []; this.getSubGroups = function (groupId) { this.getGroups("groupId="+groupId, function(groups) { if ($.isEmptyObject(groups)) { return subgroupIds; } else { $.each(groups, function(index,group) { subgroupIds.push(group.id); this.getSubGroups(group.id); }); } }); } ...where getGroups is an asynchronous function that returns all the groups. My problem is

Recursion - What does it do

情到浓时终转凉″ 提交于 2019-12-24 09:46:13
问题 I'm at my wit's end... I understand the easier examples of recursion, but I when it gets tricky I don't have a clue. Here is an example. I would be glad if someone can say what it does. What does the compiler do... public static char mystery(String s, int n, int m) { if (n==1) return s.charAt(m); char first = mystery(s, n/2, m*2); char second = mystery(s, n/2, m*2 +1); System.out.print(first + " " + second + " "); return first; } What is printed when the method is called with: mystery(

Java Recursion. Output for the following program

回眸只為那壹抹淺笑 提交于 2019-12-24 09:37:35
问题 I am looking for some brief explanations on how the below program or code works.Thanks public void Convert( int iNum ) { int m = 0; if (iNum == 1 ) System.out.print( iNum ); else { m = iNum % 2; Convert(iNum/2); System.out.print(m); } } 回答1: This program tries to convert a decimal number to binary using recursion. Lets take an example: Decimal 5 -> Binary 101 Convert(5): m = 5 %2 -> 1 Convert(2): m -> 2%2 -> 0 Convert(1) The first if is true: -> 1 Output: 101 回答2: It is a simple recursive