void func(int a[], int n, int m)
…
func(a[0], n, m);
void dumpArray(const Array2D& x)
from itertools import groupby
def squeeze(text):
return ''.join(key for key, group in groupby(text))
squeeze('aAaaabbccdcc') # => 'aAabcdc'
def squeeze(text):
for c1, c2 in zip(text[:-1], text[1:]):
if c1 != c2:
yield c1
yield c2
''.join(squeeze('aAaaabbccdcc')) # => 'aAabcdc'
from operator import ne
from itertools import compress, chain
def squeeze(text):
return ''.join(chain(compress(text, map(ne, text[:-1], text[1:])), text[-1]))
squeeze('aAaaabbccdcc') # => 'aAabcdc'
function nod(m, n) {
var mult = 0;
while (true) {
if (0 == m) {
return n << mult;
}
if (0 == n) {
return m << mult;
}
if (1 == m || 1 == n) {
return 1 << mult;
}
if (m == n) {
return m << mult;
}
if (0 == (m & 1) && 0 == (n & 1)) {
mult++;
m >>= 1;
n >>= 1;
} else if (0 == (m & 1)) {
m >>= 1;
} else if (0 == (n & 1)) {
n >>= 1;
} else if (m > n) {
m = (m-n) >> 1;
} else {
n = (n-m) >> 1;
}
}
}
function reduceFrac(numerator, denomerator) {
var divider = nod(numerator, denomerator);
return {n: numerator/divider, d: denomerator/divider};
}
function permut8(arr, prepend) {
var i, version, el, result = [];
prepend = prepend || [];
if(arr.length === 1) return [arr];
for( i=0; i<arr.length; i++) {
if( arr.length === 2) {
result.push( prepend.concat( [arr[i], arr[(i+1)%2]] ));
} else {
version = arr.slice();
el = version.splice(i,1);
result = result.concat( permut8( version, prepend.concat(el)));
}
}
return result;
}
var test = permut8( 'abcd'.split('') );
test.map( e=>e.join(' ')).join("\n")
/*
a b c d
a b d c
a c b d
a c d b
a d b c
a d c b
b a c d
b a d c
b c a d
b c d a
b d a c
b d c a
c a b d
c a d b
c b a d
c b d a
c d a b
c d b a
d a b c
d a c b
d b a c
d b c a
d c a b
d c b a
*/
["a", "a"]
получим два одинаковых [["a", "a"], ["a", "a"]]
function nextLexInPlace(arr){
var i, a = -1, b = -1;
for( i = 0; i < arr.length-1; i++) if(arr[i] < arr[1+i]) a = i;
if( !~a) return; // no more permutations
for( i = a + 1; i < arr.length; i++) if(arr[a] < arr[i]) b = i;
swap(arr, a, b);
a++;
b = arr.length - 1;
while( a < b) swap(arr, a++, b--);
return true;
}
function swap( arr, a, b) {
var xx = arr[a];
arr[a] = arr[b];
arr[b] = xx;
}
function allMutations( source) {
var result = [], arr = source.slice();
result.push( arr.sort().slice());
while( nextLexInPlace(arr)) result.push(arr.slice());
return result;
}
var test = ['a','c','c']; JSON.stringify( allMutations(test))
// [["a","c","c"],["c","a","c"],["c","c","a"]]
function* permutator(arr) {
var i, a, b;
function swap( arr, a, b) {
var xx = arr[a];
arr[a] = arr[b];
arr[b] = xx;
}
yield arr.slice();
while(true) {
a = -1, b = -1;
for( i = 0; i < arr.length-1; i++) if(arr[i] < arr[1+i]) a = i;
if( !~a) return;
for( i = a + 1; i < arr.length; i++) if(arr[a] < arr[i]) b = i;
swap(arr, a++, b);
b = arr.length - 1;
while( a < b) swap(arr, a++, b--);
yield arr.slice();
}
}
function allMutations( source) {
var all = [], result, G = permutator(source.slice().sort());
while(true) {
result = G.next();
if(result.done) break;
all.push( result.value);
}
return all;
}
var test = ['a','c','c']; JSON.stringify( allMutations(test))
// [["a","c","c"],["c","a","c"],["c","c","a"]]