Created
January 5, 2013 16:16
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(Awesome, I created the gist when I wasn't logged in... :p)