recursion

Why a recursion happens here?

假装没事ソ 提交于 2020-01-01 09:42:13
问题 Recently I read an interesting discussion on how to make a singleton in Python. One of the solutions was a tricky decorator defining a class inside its code as a substitute for decorated class: def singleton(class_): class class_w(class_): _instance = None def __new__(class2, *args, **kwargs): if class_w._instance is None: class_w._instance = super(class_w, class2).__new__(class2, *args, **kwargs) class_w._instance._sealed = False return class_w._instance def __init__(self, *args, **kwargs):

Why a recursion happens here?

倾然丶 夕夏残阳落幕 提交于 2020-01-01 09:41:58
问题 Recently I read an interesting discussion on how to make a singleton in Python. One of the solutions was a tricky decorator defining a class inside its code as a substitute for decorated class: def singleton(class_): class class_w(class_): _instance = None def __new__(class2, *args, **kwargs): if class_w._instance is None: class_w._instance = super(class_w, class2).__new__(class2, *args, **kwargs) class_w._instance._sealed = False return class_w._instance def __init__(self, *args, **kwargs):

Using recursion with map in python

喜夏-厌秋 提交于 2020-01-01 09:40:14
问题 I am trying to learn functional programming concepts. An exercise, Flatten a nested list using map/reduce. My code. lists = [ 1 , 2 , [ 3 , 4, 5], 6, [7, 8, 9] ] def flatten(lists): return map(lambda x: flatten(x) if isinstance(x,list) else x, lists) print flatten(lists) I get output same as input. What did i do wrong ? How recursion works with map() ? 回答1: Here's a solution that uses both map and reduce : def flatten(seq): return reduce(operator.add, map( lambda x: flatten(x) if isinstance(x

Applying `functools.lru_cache` to lambda

本小妞迷上赌 提交于 2020-01-01 09:18:18
问题 So I made a recursive lambda in Python for the Fibonacci sequence. I used recursion because it was easiest to implement with lambda. fib = lambda n: fib(n - 1) + fib(n - 2) if n > 1 else 1 Because using recursion, the same Fibonacci values were calculated many times, I thought using a cache decorator would help, and I knew that functools.lru_cache was an easy option. I know you can't apply the decorator to the function using @functools.lru_cache to a lambda like a normal function, but when I

Applying `functools.lru_cache` to lambda

血红的双手。 提交于 2020-01-01 09:18:13
问题 So I made a recursive lambda in Python for the Fibonacci sequence. I used recursion because it was easiest to implement with lambda. fib = lambda n: fib(n - 1) + fib(n - 2) if n > 1 else 1 Because using recursion, the same Fibonacci values were calculated many times, I thought using a cache decorator would help, and I knew that functools.lru_cache was an easy option. I know you can't apply the decorator to the function using @functools.lru_cache to a lambda like a normal function, but when I

PHP mkdir( $recursive = true ) skips last directory

百般思念 提交于 2020-01-01 08:35:21
问题 I've got the following piece of code on a PHP 5.2.4 (no safe_mode) linux server: mkdir( $path, 0777, true ); when I enter a path like: '/path/to/create/recur/ively/' all directories are created except for the last one... when I add another directory like: '/path/to/create/recur/ively/more/' again, all paths are created except for the last one... have tried both with and without trailing slashes Can any1 enlighten me here please? 回答1: Ok the solutions is as follows: there was no problem. I did

How can I express a factorial n! with an F# function, recursive or otherwise?

风格不统一 提交于 2020-01-01 06:45:11
问题 A factorial of a natural number (any number greater or equal than 0 ) is that number multiplied by the factorial of itself minus one, where the factorial of 0 is defined as 1 . For example: 0! = 1 1! = 1 * 0! 2! = 2 * 1! 3! = 3 * 2! 4! = 4 * 3! 5! = 5 * 4! Another way of writing this is to multiply all natural numbers between 1 and n for n! : 5! = 1 * 2 * 3 * 4 * 5 How can I express this with a recursive function in F#? And should I do it with a recursive function? //Factorials! let factorial

Parsing: Lazy initialization and mutually recursive monads in F#

谁都会走 提交于 2020-01-01 06:44:09
问题 I've been writing a little monadic parser-combinator library in F# (somewhat similar to FParsec) and now tried to implement a small parser for a programming language. I first implemented the code in Haskell (with Parsec) which ran perfectly well. The parsers for infix expressions are designed mutually recursive. parseInfixOp :: Parser String -> Parser Expression -> Parser Expression parseInfixOp operatorParser subParser = ignoreSpaces $ do x <- ignoreSpaces $ subParser do op <- ignoreSpaces $

Parsing: Lazy initialization and mutually recursive monads in F#

风流意气都作罢 提交于 2020-01-01 06:44:08
问题 I've been writing a little monadic parser-combinator library in F# (somewhat similar to FParsec) and now tried to implement a small parser for a programming language. I first implemented the code in Haskell (with Parsec) which ran perfectly well. The parsers for infix expressions are designed mutually recursive. parseInfixOp :: Parser String -> Parser Expression -> Parser Expression parseInfixOp operatorParser subParser = ignoreSpaces $ do x <- ignoreSpaces $ subParser do op <- ignoreSpaces $

Generating unique sorted partitions in Ruby

只谈情不闲聊 提交于 2020-01-01 06:14:41
问题 I'm trying to generate the set of sequences as shown below, not in any particularly order, but here its shown as a descending sequence. Note that each sequence also descends as I'm interested in combinations, not permutations. I'd like to store each sequence as an array..or the set of sequences as an array of arrays more preferably, but first things first. 6 5 1 4 2 4 1 1 3 3 3 2 1 3 1 1 1 2 2 2 2 2 1 1 2 1 1 1 1 1 1 1 1 1 1 Right now I am simply focusing on generating these sets and I'm