Tagged Questions
0
votes
2answers
18 views
Flattening a tree with parents/children and return all nodes
It's probably too late, but I can't sleep until it's solved:
I've got a tree with some parents, which have children, which have also children etc.
Now I need a function to get all nodes from the ...
0
votes
0answers
31 views
Leibniz determinant formula complexity
I wrote some code that calculates the determinant of a given nxn matrix using Leibniz formula for determinants.
(http://en.wikipedia.org/wiki/Leibniz_formula_for_determinants)
I am trying to figure ...
0
votes
4answers
61 views
Recursive numeric triangle in python
I'm trying to create a triangle like the following:
1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
Without using while, for in, lists, etc. Just "if-else" cases and recursive functions. I've just ...
4
votes
1answer
84 views
computing determinant of a matrix (nxn) recursively
I'm about to write some code that computes the determinant of a square matrix (nxn), using the Laplace algorithm (Meaning recursive algorithm) as written Wikipedia's Laplace Expansion.
I already have ...
0
votes
1answer
54 views
Trouble with recursive text splitting
trying to split text via text-defined boundary markers using recursion and create a list of lists and strings containing all of the organized parts of the original text file.
The split isn't ...
0
votes
1answer
58 views
Python TypeError: 'str' object is not callable when calling type function
I'm trying to split a wall of text into chunks of text and lists of texts based on headers set apart by key words. I figured the best way to do this would be recursion. Unfortunately I am getting the ...
1
vote
1answer
61 views
Recursive algorithm using memoization
My problem is as follows:
I have a list of missions each taking a specific amount of time and grants specific amount of points, and a time 'k' given to perform them:
e.g: missions = ...
1
vote
2answers
60 views
Recursive Parsing Python
I am trying to program a calculator which allows users to input an equation and receive an answer (order of operations apply). I will not be using the input() command to gather user input, but rather ...
-1
votes
1answer
76 views
How do I determine the time complexity of a recursive loop in Python?
What is the time complexity of generating all permutations of a list in Python? The following code is what needs to have its time complexity computed. How do I do this?
def permute(list):
...
1
vote
1answer
67 views
Printing a tree with branches recursively
I'm trying to print a tree recursively in Python. For some reason, the indentation isn't working (perhaps I'm too tired at this point to see an obvious flaw). Here's the structure / class definitions ...
3
votes
1answer
87 views
Overriding nested JSON encoding of inherited default supported objects like dict, list
I've set up some classes of my own that are subclassed from a dictionary to act like them. Yet when I want to encode them to JSON (using Python) I want them to be serialized in a way that I can decode ...
2
votes
3answers
68 views
Two versions of my input function, recursion vs while-loop?
I need to have a function which returns a guaranteed input of type float.
To implement this I came up with a recursive way, but only seconds later I realized I could use a while-loop just as well.
...
-1
votes
2answers
52 views
I need to make a recursive function power (x, n), which returns the result of raise x to the 9th power (xn)
def numPotencia(x, n):
if isinstance(x,int) and isinstance(n,int):
return aux_xPower(abs(x), abs(n));
else:
print("\n""Error: The number needs to be a integer");
def ...
4
votes
2answers
107 views
Can't figure out a recursive function
I have this piece of code to calculate first and second derivatives of a function at a given point
def yy(x):
return 1.0*x*x
def d1(func, x ,e):
x = x
y = func(x)
x1 = x + e
y1 = ...
0
votes
2answers
52 views
Using recursion to reverse a list, and count the number of occurances of an element within the list, using python
I'm trying to practice using recursion and lists, by writing a function which takes in a string, splits it into a list, and then uses recursion to produce a reversed version of the string, and count ...
2
votes
1answer
53 views
Using flood fill algorithm to determine map area with equal height
I have a list of lists where each element represents the average height in integers of a all square metres contained in the map (one number= one square metre). For example:
map=[
[1,1,1,1],
...
13
votes
7answers
765 views
Recursively decrement a list by 1
Very quick and easy homework question. I have it running ok but I think there's a better
way to do it. A more Pythonic way.
Here's my code to recursively decrement each element of a list by 1.
l = ...
-3
votes
0answers
46 views
recursion issues in labyrinth solver [closed]
Added:
I solved the problem by removing failed directions from the list of robot moves, thanks for your help, everyone! Here is the revised code:
def checkio(labyrinth):
move = labyrinth
move[10][10] ...
1
vote
4answers
92 views
What's wrong with my mergesort implementation?
On smaller lists of up to size N = ~1950 or so, I get the correct output... however, list sizes that are not much larger return an error rather than the expected result. My code:
def merge(left, ...
7
votes
2answers
104 views
Using print() inside recursive functions in Python3
I am following the book Introduction to Computing Using Python, by Ljubomir Perkovic, and I am having trouble with one of the examples in recursion section of the book. The code is as follows:
def ...
0
votes
2answers
105 views
Calculating subsets of a given list recursively
I want to build a recursive function that finds all the subsets of size 'k' of a given list with length n>=k>=0 and returns a list of those subsets.
example:
if the input list is [1,2,3,4] and k = 2 ...
0
votes
5answers
70 views
having trouble understanding this code
I just started learning recursion and I have an assignment to write a program that tells the nesting depth of a list. Well, I browsed around and found working code to do this, but I'm still having ...
0
votes
5answers
111 views
Formulation of a recursive solution (variable for loops)
Please consider the below algorithm:
for(j1 = n upto 0)
for(j2 = n-j1 upto 0)
for(j3 = n-j1-j2 upto 0)
.
.
for (jmax = n -j1 - j2 - j_(max-1))
...
0
votes
1answer
52 views
Is there a way to make a recursion method with unlimited depth in Python?
For example, when writing some algorithms on graphs like
def f(graph):
#graph is dictionary of pairs vertex_i:{set of edges (i,j)} for 1<=i,j<=n
def g(vertex):
for vertex1 in ...
4
votes
2answers
147 views
Big-O notation for two simple recursive functions
I have two recursive functions in Python and simply just want to know the Big O Notation for them. What is the Big O for each these?
def cost(n):
if n == 0:
return 1
else:
...
1
vote
0answers
63 views
How to Convert Recursion to Tail Recursion
Is it always possible to convert a recursion into a tail recursive one?
I am having a hard time converting the following Python function into a tail-recursive one.
def BreakWords(glob):
"""Break a ...
4
votes
2answers
121 views
Recursive “all paths” through a list of lists - Python [closed]
A peculiar problem, given a list of lists (nested at most one level here):
[['a', 'b'], 'c', ['d', 'e'], ['f', 'g'], 'h']
..find all the lists of length the same as given list and containing all ...
0
votes
0answers
34 views
What's wrong with my mergesort in python? [closed]
def msort(L):
if len(L) <= 1:
return L
if L[0] < L[-1]:
return msort(L[:(len(L)//2)]) + msort(L[(len(L)//2):])
else:
return msort(L[(len(L)//2):]) + ...
0
votes
0answers
36 views
Python- matplotlib maximum recurrsion error- Just stopped working
To start with, I have this code:
import numpy
import scipy
import scipy.fftpack
import matplotlib
import matplotlib.pyplot as plt
from scipy import pi
import pylab
from pylab import *
import cmath
...
0
votes
1answer
90 views
Tic Tac Toe with recursion (Python)
I can't figure out how to tie these functions together for a hard AI, in which it can never lose. I'm supposed to be using recursion in some form or fasion and these function names and contracts were ...
0
votes
1answer
73 views
Why does recursive sequence not work correctly?
I'm totally new to Python and I'm trying to print the solution for a recursive sequence:
#Input sequence variables
a = float(raw_input("type in a = "))
n0 = int(raw_input("type in n_0 = "))
n1 = ...
2
votes
2answers
89 views
Recursive list comprehension for permutation. Python
i have a function in python, which is programmed recursive in a list comprehension. But i don't understand it clearly what really happens in it!
def permut(s,l):
if l == []: return [[s]]
...
1
vote
1answer
78 views
Why does this recursion code work?
def permutation(li,result=[]):
print(li, result) # I added this print statement as a diagnostic.
if li == [] or li == None: # As coded, I expected each recursive call to
...
-2
votes
1answer
94 views
How can I make a recursive square root in python 3.3?
Hi I need some help with this I've got this code:
def root(x,n):
if n==0:
return x
else:
return 0.5**(x/root(x,n-1)+root(x,n-1))
but:
>>>root(4,2)
...
2
votes
1answer
61 views
python pickler - recursion depth exceeded
I'm trying to pickle instance of my cellular automata class, but I get this error:
RuntimeError: maximum recursion depth exceeded while calling a Python object
My cellular automata consist from ...
1
vote
1answer
50 views
python - traverse a graph (find all issues linked)
I'm bit stuck trying to solve a simple task of getting all linked issues. This is basically a graph task I guess - I take a jira issue, find all its links and then go to linked issues for their links ...
1
vote
3answers
85 views
Detecting infinite recursion
I'm creating a macro for Trac, and one of the things it does is to render a bit of wiki text, that can in turn use the same macro.
This can give rise to an infinite recursion if the inner macro is ...
1
vote
2answers
59 views
Explain how to use memory caches in Python
I have the following recursive solution for the nth fibonacci number:
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
x=input('which fibonnaci do ...
1
vote
1answer
36 views
how to eradicate 'None' from the end of a recursion
I have the following code:
n=input('How many disks?')
def MoveTower(n, source='A', dest='C', store='B'):
if n==1:
print source + '->' + dest
else:
MoveTower(n-1, source, ...
0
votes
2answers
48 views
Recursively append files to zip archive in python
In Python 2.7.4 on Windows, if I have a directory structure that follows:
test/foo/a.bak
test/foo/b.bak
test/foo/bar/c.bak
test/d.bak
And I use the following to add them to an existing archive such ...
2
votes
2answers
208 views
Algorithm for knight's tour on a 6x6 array only works on index [0, 0]
I must mention that this is my first time posting on this site, forgive me if I don't follow this site's guidelines to the tee.
My problem is probably simple but I can't understand it. My knight's ...
1
vote
3answers
95 views
Using recursion in Python
I'm trying to solve a problem using recursion that would be pretty verbose if I used 'if' statements. I'm looking to see how many times a CONST = 50 is in n. I want to return the number of occurrences ...
0
votes
3answers
72 views
Why is my recurssion not working? Warning: Project Euler spoiler
I'm not quite sure why my recursive algorithm is not working. I get the below error but, i guess in my mind it appears i have a termination point. I know I am forgetting something simple.
...
-3
votes
3answers
80 views
Factorial function producing wrong results
I know that there are more than one way of calculating the factorial of an integer as well as there is a math module. However I tried to put together a simple function that is returning a wrong ...
1
vote
3answers
63 views
recursive technique rather than lists technique in python
i have a function its name is positive_negative
def positive_negative(list_changes):
""" (list of number) -> (number, number) tuple
list_changes contains a list of float numbers. Return ...
1
vote
3answers
74 views
Recursive sequence addition Python
I am a bit stuck.
I am trying to calculate the distance to move gears of different sizes so they align next to each other. I think its some sort of recursive call, but I can't figure out how to write ...
0
votes
3answers
54 views
List function acting unexpectedly
def print_list(l):
for item in l:
if isinstance(item, list):
print_list(item)
else:
print(item)
I have written this function which uses recursion to ...
2
votes
3answers
118 views
JSON, lists and recursion in python
I am new to Python and have come across a problem I cannot solve.
I have decoded the following parse tree from JSON to the following list.
>>> tree
['S', ['NP', ['DET', 'There']], ['S', ...
-1
votes
1answer
67 views
Calling instance method recursively in python
I'm trying to call the instance method of objects inside a dictionary who have the objects themselves, all are from the same class. But i have a infinite recursion, i think that 'self' doesn't change ...
2
votes
1answer
65 views
Why is the counter not being reset to 0 in a recursive function?
I'm watching the MIT intro to ComScip and have to do a recursive call. I had trouble fitting in a counter nicely to count the number of string appearance. With a stroke of luck and playing with the ...