Python permutations, variable survives recursion
I have a problem with my code. Variable "used" survives recursion (it doesn't return to before state, but stays the same). I understand this is common for default variable, however, "used" isn't default now, can somebody explain how does it happen?
newArray = []
def permutations(used, default=None):
if default is None:
default = used
used = []
if len(used) == len(default):
newArray.append(used)
return
else:
for i in range(len(default)):
if default[i] in used:
continue
used.append(default[i])
permutations(used, default)
print(permutations(['1', '2', '3']))
See also questions close to this topic

Odoo. How to run permanently running process(listener) in background?
We use a python library, that is a listener for external web events(pythontelegrambot).
So we wrote some functions, that can be called, make stuff and so.
So what is the proper way to start this job with odoo11(as worker?) without bunch of third party apps?

How can I retrieve titles of various articles from one website using python web scraping?
I am looking to get topic of all the articles available in this webpage " " using python web crawler. I am very new to html. This is the code I have so far which I got as a reference from different examples. Would somebody please help me undersand this and get the correct code?
from urllib2 import urlopen from urllib2 import HTTPError from urllib2 import URLError from bs4 import BeautifulSoup try: html = urlopen("https://query.nytimes.com/search/sitesearch/") except HTTPError as e: print(e) except URLError: print("Server down or incorrect domain") else: res = BeautifulSoup(html.read(),'html.parser') tags = res.findAll("h2", {"class": "widgettitle"}) for tag in tags: print(tag.getText())

Solved :Could not find a version that satisfies the requirement Flaskboostrap (from versions: )
Solved :Edit : that was a typo error . thanks for pointing the issue
I am trying to set employment management system project on python using flask virtual environment . i have configured the environment and imported SQLAlchemy ,flask_login,flask_migrate ,flask_bootstrap with compatible version .
on installing Flaskboostrap I am getting
Could not find a version that satisfies the requirement Flaskboostrap (from versions: ) No matching distribution found for Flaskboostrap
in my requirement.txt following version mentioned
appdirs==1.4.3 click==6.7 Flask==0.12.1 itsdangerous==0.24 Jinja2==2.9.6 MarkupSafe==1.0 packaging==16.8 pyparsing==2.2.0 six==1.10.0 Werkzeug==0.12.1 MySQLpython==1.2.5 FlaskSQLAlchemy==2.2 FlaskLogin==0.4.0 FlaskMigrate==2.0.3 FlaskWTF==0.14.2 FlaskBootstrap==3.3.7.1
i googled for older version , but not able to understand how to find matching version . please suggest

algorithm for drawing xFractal at differents depths in F#
the above is the xFractal mentioned in the title of this post. I need to implement a function which allows f#sharp to draw the fractal at differents dephts using an auxilliary img_Util module. I have been advised to use the following code which allows for drawing of the sierpinski triangle at differents depths as a template:
open ImgUtil let rec triangle bmp len (x,y) = if len < 33 then setBox red (x,y) (x+len,y+len) bmp else let half = len / 2 do triangle bmp half (x+half/2,y) do triangle bmp half (x,y+half) do triangle bmp half (x+half,y+half) do runSimpleApp "Sierpinski" 600 600 (fun bmp > triangle bmp 512 (30,30) > ignore)
but i tried a lot of different things with the above as template. ive tried drawing one square in the base case. ive alse tried letting the base case be one snowflake so to speak. I can make the recursion draw the innermost element for each new depth but all else i cannot get drawn. Can anyone help me? explain the algorithm to me?

Illustrate Tower of Hanoi with ASCII
I am familiar with the recursive function of the Hanoi Tower.
Now I need to visualize the movements, representing the discs with asterisks (I guess disc number = number of asterisks makes sense).
Does anyone have a hint or example for how to draw the disc movements step by step using just
* *** *** ***** ***** *   ...
or similar?
This is the sample code my professor provided and I do understand how the recursion works. But after one lecture in Python I must say that I'm somewhat overwhelmed with the visualization task.
def hanoi(n, p1, p2, p3): if n==1: print("move from %d to %d" %(p1, p3)) else: hanoi(n1, p1, p3, p2) print("move from %d to %d " %(p1, p3)) hanoi(n1, p2, p1, p3) return if __name__=='__main__': j=int(input('Input the number of disk to be moved:\n')) print('Number of disk to be move is %d \n'%j) hanoi(j, 1, 2, 3)
Help would be much appreciated!

Convert Recursion to Loop or another faster solution
Working on improvement part of the code. Will be another way to make this method faster? recursion:
public static int MOD = 11380; public static int[][][][] memo = new int[11][11][11][31]; static int ret; public static int solve(int L1, int L2, int L3, int D) { if (D < 0) { return 0; } if (L1 + L2 + L3 == 0) { return 1; } ret = memo[L1][L2][L3][D]; if (ret == 0) { // ret = 0; for (int i = 0; i < L1; ++i) { for (int j = 0; j <= L2; ++j) { for (int k = 0; k <= L3; ++k) { ret += solve(i, j, k, D  1) *solve(L1  1  i, L2  j, L3  k, D); ret %= MOD; } } } for (int i = 0; i < L2; ++i) { for (int j = 0; j <= L3; ++j) { ret += solve(0, i, j, D  1) * solve(L1, L2  1  i, L3  j, D); ret %= MOD; } } for (int i = 0; i < L3; ++i) { ret += solve(0, 0, i, D  1) * solve(L1, L2, L3  1  i, D); ret%= MOD; } } return ret; }
This is part of the code works very fast in c++, but for now, need to convert into Java, but straight converting into Java code led to taking more time... So, for now looking for improvement of existing code. Upd. D  depth, L1  amount of {}, L2  amount of [] and L3  amount of (). This is task from spoj competition here

Permutation Algorithm in Haskell
I read this permutation function example in a Haskell textbook and I was wondering if someone could help me understand what is happening. Also, assume that the function delete simply deletes the first occurrence of a value from a list and returns the list
perms [] = [[]] perms p = [x:xs  x <p, xs < perms (delete x p)]
I understand that an empty list equals a list with an empty list. For all other cases the head of the list is prepended to x and the numbers except the head recurses through the algorithm.
My question is how does this work, for example, my pseudocode understanding is
perms[1,2,3] x < 1 xs < [2,3] perms [2,3] x < 2 xs < 3 perms [3] x < 3 xs < []
this would produce the list [1,2,3] how does the algorithm produce the other list results.
An output of this code working is as follows:
>perms [1,2,3] [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

Permutation method generating results arbitrarily
I am using the
permutations()
function from theitertools
module. I have to compare two strings. The strings can be considered equal as long as one is a permutation of the other.The documentation for
itertools.permutations()
says:itertools.permutations(iterable[, r])
Return successive
r
length permutations of elements in the iterable.If r is not specified or is
None
, thenr
defaults to the length of the iterable and all possible fulllength permutations are generated.This is my code:
import itertools mystr = [] for i in range(0, 2): mystr.append(input()) plist = itertools.permutations(mystr[0]) for i in plist: print(i) if any(x == mystr[1] for x in plist): print("Yes") else: print("No")
But generating
plist
takes too long. And when I try to print elements ofplist
, the process eventually gets killed because it is making too many permutations. But it should make only the permutations of length of the string itself.My inputs are:
Dcoderplatform patlodercDmfro
I am expecting the output to be
Yes
. But I get the opposite output (No
).How can I improve my algorithm?

Find all permutations with a condition in c
I would like to generate, in c, all the arrays of length L where the total sum N of the elements is fixed. For example for L=3 and N=2 one should get
{0,0,2}, {0,1,1}, {0,2,0}, {1,0,1}, {1,1,0}, {2,0,0}
I have found several post on how to do it in Mathematica, but what is an efficient way to do it in c?