Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/Happy-numbers/AppleScript/happy-numbers-1.applescript
Normal file
32
Task/Happy-numbers/AppleScript/happy-numbers-1.applescript
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
on run
|
||||
set howManyHappyNumbers to 8
|
||||
set happyNumberList to {}
|
||||
set globalCounter to 1
|
||||
|
||||
repeat howManyHappyNumbers times
|
||||
repeat while not isHappy(globalCounter)
|
||||
set globalCounter to globalCounter + 1
|
||||
end repeat
|
||||
set end of happyNumberList to globalCounter
|
||||
set globalCounter to globalCounter + 1
|
||||
end repeat
|
||||
log happyNumberList
|
||||
end run
|
||||
|
||||
on isHappy(numberToCheck)
|
||||
set localCycle to {}
|
||||
repeat while (numberToCheck ≠ 1)
|
||||
if localCycle contains numberToCheck then
|
||||
exit repeat
|
||||
end if
|
||||
set end of localCycle to numberToCheck
|
||||
set tempNumber to 0
|
||||
repeat while (numberToCheck > 0)
|
||||
set digitOfNumber to numberToCheck mod 10
|
||||
set tempNumber to tempNumber + (digitOfNumber ^ 2)
|
||||
set numberToCheck to (numberToCheck - digitOfNumber) / 10
|
||||
end repeat
|
||||
set numberToCheck to tempNumber
|
||||
end repeat
|
||||
return (numberToCheck = 1)
|
||||
end isHappy
|
||||
127
Task/Happy-numbers/AppleScript/happy-numbers-2.applescript
Normal file
127
Task/Happy-numbers/AppleScript/happy-numbers-2.applescript
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
---------------------- HAPPY NUMBERS -----------------------
|
||||
|
||||
-- isHappy :: Int -> Bool
|
||||
on isHappy(n)
|
||||
|
||||
-- endsInOne :: [Int] -> Int -> Bool
|
||||
script endsInOne
|
||||
|
||||
-- sumOfSquaredDigits :: Int -> Int
|
||||
script sumOfSquaredDigits
|
||||
|
||||
-- digitSquared :: Int -> Int -> Int
|
||||
script digitSquared
|
||||
on |λ|(a, x)
|
||||
(a + (x as integer) ^ 2) as integer
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
on |λ|(n)
|
||||
foldl(digitSquared, 0, splitOn("", n as string))
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
-- [Int] -> Int -> Bool
|
||||
on |λ|(s, n)
|
||||
if n = 1 then
|
||||
true
|
||||
else
|
||||
if s contains n then
|
||||
false
|
||||
else
|
||||
|λ|(s & n, |λ|(n) of sumOfSquaredDigits)
|
||||
end if
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
endsInOne's |λ|({}, n)
|
||||
end isHappy
|
||||
|
||||
--------------------------- TEST ---------------------------
|
||||
on run
|
||||
|
||||
-- seriesLength :: {n:Int, xs:[Int]} -> Bool
|
||||
script seriesLength
|
||||
property target : 8
|
||||
|
||||
on |λ|(rec)
|
||||
length of xs of rec = target of seriesLength
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
-- succTest :: {n:Int, xs:[Int]} -> {n:Int, xs:[Int]}
|
||||
script succTest
|
||||
on |λ|(rec)
|
||||
tell rec to set {xs, n} to {its xs, its n}
|
||||
|
||||
script testResult
|
||||
on |λ|(x)
|
||||
if isHappy(x) then
|
||||
xs & x
|
||||
else
|
||||
xs
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
{n:n + 1, xs:testResult's |λ|(n)}
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
xs of |until|(seriesLength, succTest, {n:1, xs:{}})
|
||||
|
||||
--> {1, 7, 10, 13, 19, 23, 28, 31}
|
||||
end run
|
||||
|
||||
|
||||
-------------------- GENERIC FUNCTIONS ---------------------
|
||||
|
||||
-- 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
|
||||
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
|
||||
-- splitOn :: String -> String -> [String]
|
||||
on splitOn(pat, src)
|
||||
set {dlm, my text item delimiters} to ¬
|
||||
{my text item delimiters, pat}
|
||||
set xs to text items of src
|
||||
set my text item delimiters to dlm
|
||||
return xs
|
||||
end splitOn
|
||||
|
||||
|
||||
-- until :: (a -> Bool) -> (a -> a) -> a -> a
|
||||
on |until|(p, f, x)
|
||||
set mp to mReturn(p)
|
||||
set v to x
|
||||
|
||||
tell mReturn(f)
|
||||
repeat until mp's |λ|(v)
|
||||
set v to |λ|(v)
|
||||
end repeat
|
||||
end tell
|
||||
return v
|
||||
end |until|
|
||||
|
|
@ -0,0 +1 @@
|
|||
{1, 7, 10, 13, 19, 23, 28, 31}
|
||||
Loading…
Add table
Add a link
Reference in a new issue