recursion

Simplifying nested loops using recursion

Simplifying nested loops using recursion Question: Let’s assume we have a three-dimensional array x. For each k, I want to have the mean of x[:, :, k] stored as out[k]. The task is simple if it is a 3D or 4D matrix, with the for loop like this: x = np.random.normal(0, 1, [4, 4, 3]) …

Total answers: 2

Error with recursion from call "random.shuffle()"

Error with recursion from call "random.shuffle()" Question: Im trying to create a very simple blackjack game, and I think I’m almost done, but when I try to play the game I get an error: Traceback (most recent call last): File "c:Usersnils.edstromgithub-classroomNTI-Gymnasiet-Nackafebruariprojektet-Nilleni2Main.py", line 176, in <module> dealer = Dealer(player1) File "c:Usersnils.edstromgithub-classroomNTI-Gymnasiet-Nackafebruariprojektet-Nilleni2Main.py", line 107, in __init__ self._dealer …

Total answers: 1

Checking if set of elements can jointly create parent element

Checking if set of elements can jointly create parent element Question: I have a dictionary with all elements and their children. For example, looking at the python dictionary below: product_dict = { ‘A’: {‘B’, ‘C’, ‘D’}, ‘B’: {‘E’, ‘F’}, ‘C’: {‘G’}, ‘D’: {}, ‘E’: {‘H’, ‘I’} } By the dict we can infer that, for …

Total answers: 1

Time Complexity of this program solving the coin change problem

Time Complexity of this program solving the coin change problem Question: I have created a program shown below, and I am confused as to how to figure out its time complexity. Is it O(ntarget/min(coins)) because a for loop is created each time the function is called, and the function is called target/min(coins) times? The program …

Total answers: 1

Time complexity of recursion of multiplication

Time complexity of recursion of multiplication Question: What is the worst case time complexity (Big O notation) of the following function for positive integers? def rec_mul(a:int, b:int) -> int: if b == 1: return a if a == 1: return b else: return a + rec_mul(a, b-1) I think it’s O(n) but my friend claims …

Total answers: 4

Is n power of three – Python

Is n power of three – Python Question: Leet-Code 326. Power of Three: Question Link: https://leetcode.com/problems/power-of-three/description/ My Code: class Solution: def isPowerOfThree(self, n: int) -> bool: print(n) if n % 3 == 0: if n == 3: return True return self.isPowerOfThree(n/3) else: return False I a getting the following error. Any help! RecursionError: maximum recursion …

Total answers: 1

How to handle return from recursion

How to handle return from recursion Question: I’m trying to make a polyomino generator of level N. I successfully made a function that connects a tile with a root in every possible way and returns all combinations. Now I need to extend this to level N. I’ve done my best, but still can’t handle recursion …

Total answers: 1

Recursion Question in Python. No Conditionals or Loops

Recursion Question in Python. No Conditionals or Loops Question: I am trying to figure out how to print the word "hello" 121 times in python. I need to use a function without conditionals or loops, no new lines, and no multiplying the string by an integer. I thinking something like: print_hello(): print(‘hello’) print_hello() print_hello() but …

Total answers: 12