Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
223
Task/Population-count/AppleScript/population-count-1.applescript
Normal file
223
Task/Population-count/AppleScript/population-count-1.applescript
Normal file
|
|
@ -0,0 +1,223 @@
|
|||
--------------------- POPULATION COUNT ---------------------
|
||||
|
||||
-- populationCount :: Int -> Int
|
||||
on populationCount(n)
|
||||
-- The number of non-zero bits in the binary
|
||||
-- representation of the integer n.
|
||||
|
||||
script go
|
||||
on |λ|(x)
|
||||
if 0 < x then
|
||||
Just({x mod 2, x div 2})
|
||||
else
|
||||
Nothing()
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
integerSum(unfoldr(go, n))
|
||||
end populationCount
|
||||
|
||||
|
||||
--------------------------- TEST ---------------------------
|
||||
on run
|
||||
set {evens, odds} to partition(compose(even, populationCount), ¬
|
||||
enumFromTo(0, 59))
|
||||
|
||||
unlines({"Population counts of the first 30 powers of three:", ¬
|
||||
tab & showList(map(compose(populationCount, raise(3)), ¬
|
||||
enumFromTo(0, 29))), ¬
|
||||
"", ¬
|
||||
"First thirty 'evil' numbers:", ¬
|
||||
tab & showList(evens), ¬
|
||||
"", ¬
|
||||
"First thirty 'odious' numbers:", ¬
|
||||
tab & showList(odds)})
|
||||
end run
|
||||
|
||||
|
||||
------------------------- GENERIC --------------------------
|
||||
|
||||
-- Just :: a -> Maybe a
|
||||
on Just(x)
|
||||
-- Constructor for an inhabited Maybe (option type) value.
|
||||
-- Wrapper containing the result of a computation.
|
||||
{type:"Maybe", Nothing:false, Just:x}
|
||||
end Just
|
||||
|
||||
|
||||
-- Nothing :: Maybe a
|
||||
on Nothing()
|
||||
-- Constructor for an empty Maybe (option type) value.
|
||||
-- Empty wrapper returned where a computation is not possible.
|
||||
{type:"Maybe", Nothing:true}
|
||||
end Nothing
|
||||
|
||||
|
||||
-- compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
on compose(f, g)
|
||||
script
|
||||
property mf : mReturn(f)
|
||||
property mg : mReturn(g)
|
||||
on |λ|(x)
|
||||
mf's |λ|(mg's |λ|(x))
|
||||
end |λ|
|
||||
end script
|
||||
end compose
|
||||
|
||||
|
||||
-- enumFromTo :: Int -> Int -> [Int]
|
||||
on enumFromTo(m, n)
|
||||
if m ≤ n then
|
||||
set lst to {}
|
||||
repeat with i from m to n
|
||||
set end of lst to i
|
||||
end repeat
|
||||
lst
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end enumFromTo
|
||||
|
||||
|
||||
-- even :: Int -> Bool
|
||||
on even(x)
|
||||
0 = x mod 2
|
||||
end even
|
||||
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to |λ|(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
-- The list obtained by applying f
|
||||
-- to each element of xs.
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
|
||||
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
|
||||
on mReturn(f)
|
||||
-- 2nd class handler function lifted into 1st class script wrapper.
|
||||
if script is class of f then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- partition :: (a -> Bool) -> [a] -> ([a], [a])
|
||||
on partition(f, xs)
|
||||
tell mReturn(f)
|
||||
set ys to {}
|
||||
set zs to {}
|
||||
repeat with x in xs
|
||||
set v to contents of x
|
||||
if |λ|(v) then
|
||||
set end of ys to v
|
||||
else
|
||||
set end of zs to v
|
||||
end if
|
||||
end repeat
|
||||
end tell
|
||||
{ys, zs}
|
||||
end partition
|
||||
|
||||
-- raise :: Num -> Int -> Num
|
||||
on raise(m)
|
||||
script
|
||||
on |λ|(n)
|
||||
m ^ n
|
||||
end |λ|
|
||||
end script
|
||||
end raise
|
||||
|
||||
|
||||
-- integerSum :: [Num] -> Num
|
||||
on integerSum(xs)
|
||||
script addInt
|
||||
on |λ|(a, b)
|
||||
a + (b as integer)
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
foldl(addInt, 0, xs)
|
||||
end integerSum
|
||||
|
||||
|
||||
-- intercalate :: String -> [String] -> String
|
||||
on intercalate(delim, xs)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, delim}
|
||||
set s to xs as text
|
||||
set my text item delimiters to dlm
|
||||
s
|
||||
end intercalate
|
||||
|
||||
|
||||
-- showList :: [a] -> String
|
||||
on showList(xs)
|
||||
"[" & intercalate(",", map(my str, xs)) & "]"
|
||||
end showList
|
||||
|
||||
|
||||
-- str :: a -> String
|
||||
on str(x)
|
||||
x as string
|
||||
end str
|
||||
|
||||
|
||||
-- unfoldr :: (b -> Maybe (a, b)) -> b -> [a]
|
||||
on unfoldr(f, v)
|
||||
-- A list derived from a simple value.
|
||||
-- Dual to foldr.
|
||||
-- unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10
|
||||
-- -> [10,9,8,7,6,5,4,3,2,1]
|
||||
set xr to {v, v} -- (value, remainder)
|
||||
set xs to {}
|
||||
tell mReturn(f)
|
||||
repeat -- Function applied to remainder.
|
||||
set mb to |λ|(item 2 of xr)
|
||||
if Nothing of mb then
|
||||
exit repeat
|
||||
else -- New (value, remainder) tuple,
|
||||
set xr to Just of mb
|
||||
-- and value appended to output list.
|
||||
set end of xs to item 1 of xr
|
||||
end if
|
||||
end repeat
|
||||
end tell
|
||||
return xs
|
||||
end unfoldr
|
||||
|
||||
|
||||
-- unlines :: [String] -> String
|
||||
on unlines(xs)
|
||||
-- A single string formed by the intercalation
|
||||
-- of a list of strings with the newline character.
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, linefeed}
|
||||
set s to xs as text
|
||||
set my text item delimiters to dlm
|
||||
s
|
||||
end unlines
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
on popCount(n)
|
||||
set counter to 0
|
||||
repeat until (n is 0)
|
||||
set counter to counter + n mod 2
|
||||
set n to n div 2
|
||||
end repeat
|
||||
|
||||
return counter div 1
|
||||
end popCount
|
||||
|
||||
-- Task code:
|
||||
-- Get the popcounts of the first 30 powers of 3.
|
||||
set list1 to {}
|
||||
repeat with i from 0 to 29
|
||||
set end of list1 to popCount(3 ^ i)
|
||||
end repeat
|
||||
|
||||
-- Collate the integers from 0 to 59 according to the evenness or oddness of their popcounts.
|
||||
-- In any even number of consecutive integers, exactly half are "evil" and half "odious". Thus thirty of each here.
|
||||
set lists2and3 to {{}, {}}
|
||||
repeat with i from 0 to 59
|
||||
set end of item (popCount(i) mod 2 + 1) of lists2and3 to i
|
||||
end repeat
|
||||
|
||||
-- Arrange the results for display.
|
||||
set astid to AppleScript's text item delimiters
|
||||
set AppleScript's text item delimiters to space
|
||||
set {list1, list2, list3} to {list1 as text, beginning of lists2and3 as text, end of lists2and3 as text}
|
||||
set AppleScript's text item delimiters to linefeed
|
||||
set output to {"Popcounts of 1st thirty powers of 3:", list1, "1st thirty evil numbers:", list2, "1st thirty odious numbers:", list3} ¬
|
||||
as text
|
||||
set AppleScript's text item delimiters to astid
|
||||
return output
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
"Popcounts of 1st thirty powers of 3:
|
||||
1 2 2 4 3 6 6 5 6 8 9 13 10 11 14 15 11 14 14 17 17 20 19 22 16 18 24 30 25 25
|
||||
1st thirty evil numbers:
|
||||
0 3 5 6 9 10 12 15 17 18 20 23 24 27 29 30 33 34 36 39 40 43 45 46 48 51 53 54 57 58
|
||||
1st thirty odious numbers:
|
||||
1 2 4 7 8 11 13 14 16 19 21 22 25 26 28 31 32 35 37 38 41 42 44 47 49 50 52 55 56 59"
|
||||
Loading…
Add table
Add a link
Reference in a new issue