Skip to content

Instantly share code, notes, and snippets.

@Jagua
Created December 24, 2014 14:32
Show Gist options
  • Save Jagua/99372b934d55dfce1282 to your computer and use it in GitHub Desktop.
Save Jagua/99372b934d55dfce1282 to your computer and use it in GitHub Desktop.
scriptencoding utf-8
function! harem#now(list, n)
return harem#_loop(a:list, [], 1, a:n)
endfunction
function! harem#_loop(list, result, count, n)
if len(a:list) > 0
let result = harem#_loop(a:list[1:], a:result + ['.'], a:count, a:n)
if a:count <= a:n
let result += harem#_loop(a:list[1:], a:result + [a:list[0]], a:count + 1, a:n)
endif
return result
else
return [a:result]
endif
endfunction
" vim-prettyprint (by thinca)
echo PrettyPrint(harem#now(split('ABCDE', '\zs'), 2))
" =>
"[
" ['.', '.', '.', '.', '.'],
" ['.', '.', '.', '.', 'E'],
" ['.', '.', '.', 'D', '.'],
" ['.', '.', '.', 'D', 'E'],
" ['.', '.', 'C', '.', '.'],
" ['.', '.', 'C', '.', 'E'],
" ['.', '.', 'C', 'D', '.'],
" ['.', 'B', '.', '.', '.'],
" ['.', 'B', '.', '.', 'E'],
" ['.', 'B', '.', 'D', '.'],
" ['.', 'B', 'C', '.', '.'],
" ['A', '.', '.', '.', '.'],
" ['A', '.', '.', '.', 'E'],
" ['A', '.', '.', 'D', '.'],
" ['A', '.', 'C', '.', '.'],
" ['A', 'B', '.', '.', '.']
"]
echo len(harem#now(split('ABCDE', '\zs'), 2))
" => 16
" C(5,2)+C(5,1)+C(5,0)=10+5+1=16 ... Ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment