recursion

On Finding the Maximum Depth of an Arbitrarily Nested List

安稳与你 提交于 2021-02-19 01:17:35
问题 I'm currently working with a recursive function in Python, and I've run into a wall. As titled, the problem is to return the maximum depth of an arbitrarily nested list. Here is what I have so far: def depthCount(lst): 'takes an arbitrarily nested list as a parameter and returns the maximum depth to which the list has nested sub-lists.' var = 0 if len(lst) > 0: if type(lst[0]) == list: var += 1 depthCount(lst[1:]) else: depthCount(lst[1:]) else: return var I feel that the problem is with my

On Finding the Maximum Depth of an Arbitrarily Nested List

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-19 01:17:26
问题 I'm currently working with a recursive function in Python, and I've run into a wall. As titled, the problem is to return the maximum depth of an arbitrarily nested list. Here is what I have so far: def depthCount(lst): 'takes an arbitrarily nested list as a parameter and returns the maximum depth to which the list has nested sub-lists.' var = 0 if len(lst) > 0: if type(lst[0]) == list: var += 1 depthCount(lst[1:]) else: depthCount(lst[1:]) else: return var I feel that the problem is with my

Find images with certain extensions recursively

隐身守侯 提交于 2021-02-18 22:33:50
问题 I am currently trying to make a script that will find images with *.jpg / *.png extensions in directories and subdirectories. If some picture with one of these extensions is found, then save it to an array with path, name, size, height and width. So far I have this piece of code, which will find all files, but I don't know how to get only jpg / png images. class ImageCheck { public static function getDirectory( $path = '.', $level = 0 ){ $ignore = array( 'cgi-bin', '.', '..' ); // Directories

Find images with certain extensions recursively

佐手、 提交于 2021-02-18 22:33:11
问题 I am currently trying to make a script that will find images with *.jpg / *.png extensions in directories and subdirectories. If some picture with one of these extensions is found, then save it to an array with path, name, size, height and width. So far I have this piece of code, which will find all files, but I don't know how to get only jpg / png images. class ImageCheck { public static function getDirectory( $path = '.', $level = 0 ){ $ignore = array( 'cgi-bin', '.', '..' ); // Directories

How do you generate a list of all possible strings given a generator of characters and a length?

旧时模样 提交于 2021-02-18 18:00:24
问题 For example given ['a', 'b'] (as a generator) and 2 as a length the function would output a generator that would yield: '', 'a', 'b', 'ab' 'ba' 'aa' 'bb' or given ['a'] and a length of 3: '', 'a', 'aa', 'aaa', As you could imagine this set would get a lot larger if more letters were added or length was increased, it should list all permutations of the given characters up until the length 回答1: Here's a fairly self-explanatory solution. //Returns all permuations of a certain length. function

Implement javascript function using tail recursion

六眼飞鱼酱① 提交于 2021-02-18 16:56:32
问题 I have got a flat array representing a tree, and I want to build a nested object using tail recursion. I've got the following code to run and generate the desired output, but I am not sure if it is a proper implementation of tail recursion. Please advice :) const myArray = [ { id: 'root' }, { id: 0, parent: 'root' }, { id: 1, parent: 'root' }, { id: 2, parent: 0 }, { id: 3, parent: 1 }, { id: 4, parent: 2 }, { id: 5, parent: 1 }, { id: 6, parent: 4 }, { id: 7, parent: 0 }, { id: 8, parent: 0

How to recursively Omit key from type

只愿长相守 提交于 2021-02-18 12:51:16
问题 I want to write a type utility that omits fields recursively. Something that you would name and use like that OmitRecursively<SomeType, 'keyToOmit'> I've tried to do it using mapped types + conditional typing but I stuck on the case when all required fields got typed correctly (hence field disappeared from nested type), but optional fields are ignored with that approach. // This is for one function that removes recursively __typename field // that Appolo client adds type Deapolify<T extends {

Displaying nested dictionary in Jinja2

一世执手 提交于 2021-02-18 08:16:25
问题 I have the following Jinja2 template: {% block body %} {% for key in tree recursive %} {% set outer_loop = loop %} {% for subkey in tree[key] %} {% if subkey == 'R' %} {{ tree[key][subkey] }} {% else %} {{ outer_loop(dict([(subkey, tree[key][subkey])])) }} {% endif %} {% endfor %} {% endfor %} {% endblock body %} where tree is a Python dictionary such as: tree = {"A": {"R": [1, 2, 3], "B": {"R": [4, 5, 6]}}} and dict() is the Python library function. The issue is that the template displays

Counting the number of characters inside double quotation marks

蓝咒 提交于 2021-02-17 06:30:07
问题 I want to find the number of characters inside a double quotation mark. For example : Case 1 "Hello World" , "Some output : Error // missing quotation marks after Some Case 2 "Hello Word" , "Some" output : 14 // All quotation marks are complete I have written the program for counting the total characters, the index of the first quotation mark and the total number of quotations marks using recursion. What approach shall I use to solve this problem? 回答1: Here is a working version using

Place positive numbers before negative

梦想与她 提交于 2021-02-17 05:13:51
问题 So I have this code which I found on the internet which takes an array of negative and positive numbers and rearranges the array so all negative numbers are before the positive ones. But the position of appearance of each number has to remain the same. So for example if i have -2 5 -9 ..... , in the organized array -2 still has to be the first number of the negative ones and -9 has to be the second of the negative ones, also 5 has to be the first number of the positive ones. Example: Input: