Last active
February 13, 2020 14:37
-
-
Save Sintrastes/ce3aacb7bf22481f999d to your computer and use it in GitHub Desktop.
golf.hs
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
-- Outputs the number of strings of length six with the digits 0-9 with only two numbers used | |
-- (e.a. "100110", "611661", "899889") in 119 characters of Haskell. Enjoy! | |
import Data.List | |
z x|length x<6=z("0"++x)|length x==6=x | |
main=print$length$filter((==2).length)$map(nub.z.show)[0..999999] |
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
import Data.List -- for nub | |
-- Converts integers into a string of six digits | |
sixstring :: Int -> String | |
sixstring x = zeroes (show x) | |
-- Helper function, appends zeroes to a string of digits until | |
-- the length of the string is 6. | |
zeroes :: String -> String | |
zeroes x | length x < 6 = zeroes ("0"++x) | |
| length x == 6 = x | |
-- Returns true if the set of unique characters used in the string | |
-- passed is exactly two. | |
exactlyTwoNumbers :: String -> Bool | |
exactlyTwoNumbers x = length (nub x) == 2 | |
-- Count the number of strings from 000000 to 999999 with exactly | |
-- two digits used. | |
n = length $ filter exactlyTwoNumbers $ map sixstring [0..999999] | |
main = print n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment