recursion

Abstract Inheritance in Django Model, causing MAX recursion depth error

只谈情不闲聊 提交于 2020-01-03 08:53:13
问题 I'm trying to implement abstract inheritance in Django with the following code, but it produces a MAX recursion depth error. I'm trying to override a model's save method. class BaseModel(models.Model): class Meta: abstract = True def save(self, *args, **kwargs): #i'm doing something here #i think the problem is in the return statement specifically because of the #self.__class__ expression. return super(self.__class__, self).save(*args, **kwargs) class MyModel(BaseModel): p = models.CharField

Abstract Inheritance in Django Model, causing MAX recursion depth error

亡梦爱人 提交于 2020-01-03 08:53:09
问题 I'm trying to implement abstract inheritance in Django with the following code, but it produces a MAX recursion depth error. I'm trying to override a model's save method. class BaseModel(models.Model): class Meta: abstract = True def save(self, *args, **kwargs): #i'm doing something here #i think the problem is in the return statement specifically because of the #self.__class__ expression. return super(self.__class__, self).save(*args, **kwargs) class MyModel(BaseModel): p = models.CharField

N-Tree Traversal with Scala Causes Stack Overflow

眉间皱痕 提交于 2020-01-03 07:24:11
问题 I am attempting to return a list of widgets from an N-tree data structure. In my unit test, if i have roughly about 2000 widgets each with a single dependency, i'll encounter a stack overflow. What I think is happening is the for loop is causing my tree traversal to not be tail recursive. what's a better way of writing this in scala? Here's my function: protected def getWidgetTree(key: String) : ListBuffer[Widget] = { def traverseTree(accumulator: ListBuffer[Widget], current: Widget) :

Recursion: Sum of series of n terms

一曲冷凌霜 提交于 2020-01-03 06:36:08
问题 Recursive function needed Series is : 1 + 2*3 + 3*4*5 + 4*5*6*7 + .... Find the sum of the series for n recursively. I am not able to think of what parameters should I pass in function. My approach I thought that I should pass n, number of terms to be multiplied but what I am not able to think of is how should I + and * in same function and what will my return statement is? 回答1: function F(n, nmax, prod): Int //prod = (prod div n) * (2 * n) * (2 * n + 1) simpler: prod = prod * 2 * (2 * n + 1)

Recursion: Sum of series of n terms

孤人 提交于 2020-01-03 06:36:05
问题 Recursive function needed Series is : 1 + 2*3 + 3*4*5 + 4*5*6*7 + .... Find the sum of the series for n recursively. I am not able to think of what parameters should I pass in function. My approach I thought that I should pass n, number of terms to be multiplied but what I am not able to think of is how should I + and * in same function and what will my return statement is? 回答1: function F(n, nmax, prod): Int //prod = (prod div n) * (2 * n) * (2 * n + 1) simpler: prod = prod * 2 * (2 * n + 1)

How to return a value from a recursive Python function?

随声附和 提交于 2020-01-03 05:56:09
问题 This question seems a bit specific, and for that I'm sorry, but it has me stumped. I'm writing myself a password generator, one that takes a string (aka the URL of a website) and processes it into a secure password that can't be backtracked based on the name of the website. In part of the code, I created a recursive function that looks like this: def get_number(n = 0, nums = ''): for i in range(0, len(url)): #both n and nums are changed if len(nums) < num_length: get_number(n, nums) else:

Way to go from recursion to iteration

我只是一个虾纸丫 提交于 2020-01-03 05:55:11
问题 I've used recursion quite a lot on my many years of programming to solve simple problems, but I'm fully aware that sometimes you need iteration due to memory/speed problems. So, sometime in the very far past I went to try and find if there existed any "pattern" or text-book way of transforming a common recursion approach to iteration and found nothing. Or at least nothing that I can remember it would help. Are there general rules? Is there a "pattern"? 回答1: Usually, I replace a recursive

Recursive functions for partitions, stirling numbers, and chebyshev polynomials of the first

梦想的初衷 提交于 2020-01-03 05:44:08
问题 So I'm working on a homework assignment and I need to create recursive functions for partitions, Stirling numbers(first and second kind), and Chebyshev polynomials of the first. My program should be able to have a user input a positive integer n, and then create files named Partitions.txt, Stirling1.txt, Stirling2.txt, and Chebyshev.txt, that creates a table of all values f(k,m) for 1<=k<=n and 1<=m<=n. I'm struggling just to start off the assignment and feel like I have no understanding of

missed logic in finding sum using recurssion ,got segfault

拟墨画扇 提交于 2020-01-03 05:19:08
问题 I tried to achieve following using recurssion but im getting segfault pls correct me?? i tried solving problem using permutation of set {1,,3,5,7} but failed to output the required result print all compositions of a number in odd parts, i.e. for n = 8: 7 + 1 5 + 3 5 + 1 + 1 + 1 3 + 3 + 1 + 1 3 + 1 + 1 + 1 + 1 + 1 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 sumto8(a,start,sum1) is://choosing element of concern sumto8(a,start,sum)is ://choosing element of concern #include<iostream> #include<stdio.h> int

Extract files to same folder as archive using recursive to search all directories

僤鯓⒐⒋嵵緔 提交于 2020-01-03 05:19:07
问题 I'm working on an automation task in PowerShell that extracts the contents of several .tar archives to their respective subfolders using recursion and the 7z.exe utility. Im running into an issue where the output dumps in my working directory instead of the subdirectory gci -r found the original tarball. So far I have: $files=gci -r | where {$_.Extension -match "tar"} foreach ($files in $files) { c:\7z.exe e -y $file.FullName } Advice on setting the working directory within the loop or 7z