How to re-order units based on their degree of desirable neighborhood ? (in Processing)

The solution I propose to solve this challenge is based on repeating the algorithm several times while recordig valid solutions. As solution is not unique, I expect the algorithm to throw more than 1 solution. Each of them will have a score based on neighbours affinity. I’ll call an ‘attempt‘ to a complete run trying … Read more

Generate permutations of list with repeated elements

This web page looks promising. def next_permutation(seq, pred=cmp): “””Like C++ std::next_permutation() but implemented as generator. Yields copies of seq.””” def reverse(seq, start, end): # seq = seq[:start] + reversed(seq[start:end]) + \ # seq[end:] end -= 1 if end <= start: return while True: seq[start], seq[end] = seq[end], seq[start] if start == end or start+1 == … Read more

Secret Santa – Generating ‘valid’ permutations

What you’re looking for is called a derangement (another lovely Latinate word to know, like exsanguination and defenestration). The fraction of all permutations which are derangements approaches 1/e = approx 36.8% — so if you are generating random permutations, just keep generating them, and there’s a very high probability that you’ll find one within 5 … Read more

How to generate all permutations of an array in sorted order?

In C++ you can use std::next_permutation to go through permutations one by one. You need to sort the characters alphabetically before calling std::next_permutation for the first time: cin>>anagrama; int len = strlen(anagrama); sort(anagrama, anagrama+len); do { cout << anagrama << endl; } while (next_permutation(anagrama, anagrama+len)); Here is a demo on ideone. If you must implement … Read more

Code Golf: Countdown Number Game

Python Number of characters: 548 482 425 421 416 413 408 from operator import * n=len def C(N,T): R=range(1<<n(N));M=[{}for i in R];p=1 for i in range(n(N)):M[1<<i][1.*N[i]]=”%d”%N[i] while p: p=0 for i in R: for j in R: m=M[i|j];l=n(m) if not i&j:m.update((f(x,y),”(“+s+o+t+”)”)for(y,t)in M[j].items()if y for(x,s)in M[i].items() for(o,f)in zip(‘+-*/’,(add,sub,mul,div))) p|=l<n(m) return min((abs(x-T),e)for t in M for(x,e)in t.items())[1] … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)