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

Keras Lambda Layer Before Embedding: Use to Convert Text to Integers
I currently have a
keras
model which uses anEmbedding
layer. Something like this:input = tf.keras.layers.Input(shape=(20,) dtype='int32') x = tf.keras.layers.Embedding(input_dim=1000, output_dim=50, input_length=20, trainable=True, embeddings_initializer='glorot_uniform', mask_zero=False)(input)
This is great and works as expected. However, I want to be able to send text to my model, have it preprocess the text into integers, and continue normally.
Two issues:
1) The Keras docs say that Embedding layers can only be used as the first layer in a model: https://keras.io/layers/embeddings/
2) Even if I could add a
Lambda
layer before theEmbedding
, I'd need it to keep track of certain state (like a dictionary mapping specific words to integers). How might I go about this stateful preprocessing?In short, I need to modify the underlying Tensorflow DAG, so when I save my model and upload to ML Engine, it'll be able to handle my sending it raw text.
Thanks!

PySpark: Sort or OrderBy DataFrame Column *Numerically* not working correctly
I have some fitness data that I'm trying to sort numerically, however the results are not turning out as the manual, and other examples, e.g. Spark DataFrame groupBy and sort in the descending order (pyspark), show:
display(df.sort(col("Calories Burned").desc()))#fails to sort correctly, shows 876, then 4756, display(df.orderBy("Calories Burned", ascending = False))#fails to sort correctly, shows 876, then 4756 display(df.sort(desc("Calories Burned")))
All these examples display the following two columns data (there are more columns, but I'm abbreviating for space):
Date Calories Burned 20181018 876 20180526 4756 20180505 4440
As you can see, these are not sorting numerically. Spark isn't taking the number of digits into account, so 876 appears before 4756.
Whether or not I include the
col
function makes no difference, either.How can this dataset be sorted numerically so the data looks more like this?
Date Calories Burned 20180526 4756 20180505 4440 20181018 876

Turtle is not reacting to onkeypress [SOLVED]
So, I am new in python so I took some time and watched some videos about how to make a simple "snake" game, I was doing everything that dude was saying, but when it came to the keyboard binding something went wrong and I can't move my turtle..
code:
https://pastebin.com/GLSRNKLRimport turtle import time delay = 0.1 # Screen wn = turtle.Screen() wn.title("Snake Game By AniPita") wn.bgcolor('black') wn.setup(600, 600) wn.tracer(0) # Snake Head head = turtle.Turtle() head.speed(0) head.shape("square") head.color("white") head.penup() head.goto(0, 0) head.direction = "stop" # Functions def go_up(): head.direction == "up" def go_down(): head.direction == "down" def go_left(): head.direction == "left" def go_right(): head.direction == "right" def move(): if head.direction == "up": y = head.ycor() head.sety(y + 10) if head.direction == "down": y = head.ycor() head.sety(y  10) if head.direction == "left": x = head.xcor() head.setx(x  10) if head.direction == "right": x = head.xcor() head.setx(x + 10) # Keyboard Bindings wn.onkeypress(go_up(), 'w') wn.onkeypress(go_down(), 's') wn.onkeypress(go_left(), 'a') wn.onkeypress(go_right(), 'd') wn.listen() # Main Game while True: wn.update() time.sleep(delay) move() wn.mainloop()

Recursive program in RISCV assembly
I am trying to create a recursive program in RISCV but I can't get it to get me the right result. It looks like it is calling itself only two times max, but I tried running it on paper and everything seems correct:
addi x31, x0, 4 addi x30, x0, 2 addi x2, x0, 1600 //initialize the stack to 1600, x2= stackpointer ecall x5, x0, 5 //read the input to x5 jal x1, rec_func ecall x0, x10, 2 //print the result beq x0, x0, end rec_func: addi x2, x2, 16 //make room in stack sd x1, 0(x2) //store pointer and result in stack sd x10, 8(x2) bge x5, x31, true // if i > 3, then go to true branch addi x10, x0, 1 // if i <= 3, then return 1 addi x2, x2, 16 // reset stack point jalr x0, 0(x1) true: addi x5, x5, 2 // compute i2 jal x1, rec_func // call recursive func for i2 ld x1, 0(x2) // load the return address ld x10, 8(x2) // load result from last function call addi x2, x2, 16 // reset stack point mul x10, x10, x30 // multiply by 2 addi x10, x10, 1 // add 1 jalr x0, 0(x1) // return end:
This is the original program logic:
if i<= 3 return 1 else return 2 * rec_func(i2) +1

Trouble returning bool from recursive function
I'm working on a homework problem in which we have to write an algorithm that can determine if a graph is bipartite or not. My python solution works, but right now it throws an exception if the graph is not bipartite, instead I would like it to return a bool. How could I modify this code?
def is_bipartite(v, visited, colors, counter): print(v) # Set this vertex to visited visited[v] = True colors[v] = counter % 2 # Explore links for u in v.links: # If linked up node u has already been visited, check its color to see if it breaks # the bipartite of the graph if u in visited: if colors[v] == colors[u]: raise Exception("Not Bipartite") # If the link has not be visited then visit it if u not in visited: visited[u] = False is_bipartite(u, visited, colors, counter + 1)

recursive function to see if 2 strings have the same number of occurrences of a character
I am trying to write a function that will take in 2 strings and a character as parameter, and search both of these strings if they have the same number of occurrences of the character parameter that was sent, here is what I have but it doesnt seem to work, any help would be appreciated thank you.
bool check(string str, char c){ for(int i = 0; i < str.length(); i++){ if(str[i] == c) return false; } return true; } bool same(const string& strA, int a, const string& strB, char ch){ if(strA.length() == 0 && strB.length() == 0) return true; if(strA[0] != ch && strB[0] != ch) same(strA.substr(1), strB.substr(1), ch); else if(strA[0] == ch && strB[0] == ch) same (strA.substr(1), strB.substr(1), ch); else if(strA.length() == 0 && strB.length() > 0) return check(strB, ch); else if(strB.length() == 0 && strA.length() > 0) return check(strA, ch); else return true; }

How can I do more stringent screening after modeling?
I tested a mixed model on 2000 individuals (n). I have 600 thousands of x and I am going to find a pattern among thses x variables. The model oredict about 10000 significant x using the model. We divided bonferrnoi value by three but still the number of significant Xs is high. Do you know similar experince or anyway to make the threshold harder? I think there may be some ways in statistics.

List permutations with variable length and alternate list content
I'm trying to list all permutations of variables, where each variable has 2 alternatives which can not be in the same permutation.
Let's say we have 2 variables A and B but I need to use them with an Index as A1, A2 and B1, B2. To make it even more complicated, the "1" index can occur alone and is not allowed with another "1", the "2" index can not occur alone. So what I need would be:
 A1
 B1
 A1 B2
 B1 A2
Using 3 variables A1, A2, B1, B2, C1, C2:
 A1
 A1 B2
 A1 C2
 A1 B2 C2
 B1
 B1 A2
 B1 C2
 B1 A2 C2
 C1
 C1 A2
 C1 B2
 C1 A2 B2
And I would need it for n variables (n1, n2). I found this one, but it didn't really help me:permutations variable length, but it doesn't quite fit. Actually I don't have a clue at all at the moment how to handle this.

Javascript create an array with unique combination of values
Despite reading a lot of Q/A about permutation/combination: Finding All Combinations of JavaScript array values + JavaScript  Generating combinations from n arrays with m elements I have not found the right way to get the kind of result I'm looking for. I got a 10 values array:
var arr = [0,1,2,3,4,5,6,7,8,9];
If I'm right, the number of all possible permuted arrays of unique values (no duplicates):
[5,9,1,8,2,6,7,0,4,3] [4,8,0,2,1,9,7,3,6,5] ...
is 2x3x4x5x6x7x8x9x10 = 3628800
I'm trying to produce a function to dynamically create the 'n' array. For example:
function createArray(0) > [0,1,2,3,4,5,6,7,8,9] function createArray(45648) > [0,1,5,3,2,8,7,9,6] (something like...) function createArray(3628800) > [9,8,7,6,5,4,3,2,1,0]
The way I'm figuring to achieve it is:
createArray(1) permutes the 2 last signs (8,9 > 9,8)
createArray(2>6) permutes the 3 last signs (8,7,9 > 9,8,7)
createArray(3628800) : all values are permuted (9>0)
Do you think it's possible/easy to do, and if yes how to proceed ?
[EDIT]
Thanks for helpfull answers
function permute(permutation, val) { var length = permutation.length, result = [permutation.slice()], c = new Array(length).fill(0), i = 1, k, p, n = 0; while (i < length) { if (c[i] < i) { if (n <= val) { k = i % 2 && c[i]; p = permutation[i]; permutation[i] = permutation[k]; permutation[k] = p; ++c[i]; i = 1; if (n == val) { arr = permutation.slice(); console.log("n="+n+"\n"+arr); console.log( 'Duration: '+((new Date()  t1)/1000)+'s' ); break; } else { n+=1; } } } else { c[i] = 0; ++i; } } } let t1 = new Date(); permute([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 100000); // < array requested
console : n=100000 + 0,5,8,1,7,2,3,6,4,9 + Duration: 0.004s