Skip to content

Instantly share code, notes, and snippets.

Created January 5, 2013 16:16
Show Gist options
  • Save anonymous/4462276 to your computer and use it in GitHub Desktop.
Save anonymous/4462276 to your computer and use it in GitHub Desktop.
#[email protected]: 16:28 < Tanooki> Weet iemand welk algoritme ik zou moeten gebruiken om het volgende te doen? Ik moet een nummer van 9 cijfers bruteforcen, waarvan ik 6 cijfers weet. Ik moet dus met dat algoritme een lijst maken van alle nummers die ik moet proberen (dus xxxxxx000, xxxxxx001, xxxxxx002, etc. maar ook 9xxxxxx11, 98xxx…
module Combinaties where
import Data.List
deleteFromHead :: Eq a => a -> [a] -> [a]
deleteFromHead x [] = []
deleteFromHead x (y:ys)
| x == y = ys
| otherwise = (y:ys)
generate :: Eq a => Int -> [a] -> [a] -> [[a]]
generate n values knowns = generate' n knowns
where
-- base cases
generate' 0 [] = [[]]
generate' 0 knowns = []
-- recursive cases
generate' n [] = [v : subsolution | v <- values, subsolution <- generate' (n-1) []]
generate' n knowns = let values' = nub (values ++ knowns) in
[v : subsolution | v <- values', subsolution <- generate' (n-1) (deleteFromHead v knowns)]
test = generate 9 ['0'..'9'] "783659"
-- length test == 64234 -- (216.20 secs, 250246530152 bytes)
@ShinNoNoir
Copy link

(Awesome, I created the gist when I wasn't logged in... :p)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment