Find palindromic numbers in both binary and ternary bases: Difference between revisions
Find palindromic numbers in both binary and ternary bases (view source)
Revision as of 02:20, 15 May 2019
, 5 years ago→Functional Python: (tidied top level)
m (→Functional Python: (tidied top level)) |
|||
Line 1,638:
def palinBoth():
'''Non finite stream of dually palindromic integers.'''
yield (0, '0', '0')
n = 2▼
yield ibt
while True:
yield
n = succ(v)▼
# isBoth :: (Int, String, String) -> Bool
def isBoth(
'''True if the
the ternary string is already known to be).
'''
b = bin(int(s + '1' + s[::-1], 3))[2:]▼
return b == b[::-1]
#
def
s = showBase3(n)▼
return (d, bin(int(pal, 3))[2:], pal)
Line 1,676 ⟶ 1,678:
def main():
'''Integers with palindromic digits in both binary and ternary'''
def label(k):▼
def go(sw):▼
return getattr(s, k)(w, ' ') + ' '▼
return lambda sw: go(sw)▼
xs = take(6)(palinBoth())
print(
fTable(
label('rjust')(('Decimal',
''.join(map(
label('center'),
[('Binary', bw), ('Ternary', tw)]
)) + '\n'
)(compose(str)(
lambda
)(identity)(xs)
)
# GENERIC -------------------------------------------------
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
'''Right to left function composition.'''
return lambda f: lambda x: g(f(x))
def fst(tpl):
'''First member of a pair.'''
return tpl[0]
# identity :: a -> a
def identity(x):
'''The identity function.'''
return x
# showIntAtBase :: Int -> (Int -> String) -> Int -> String -> String
Line 1,722 ⟶ 1,735:
return lambda toChr: lambda n: lambda rs: (
wrap(toChr, n, rs)
)▼
▲# succ :: Enum a => a -> a
▲def succ(x):
▲ '''The successor of a value.
)
Line 1,762 ⟶ 1,765:
# FORMATTING ----------------------------------------------
# label :: Method String -> (String, Int)
'''Stringifiction, using the named justification
method (ljust|centre|rjust) of the label,
and the specified amount of white space.
# fTable :: String -> (a -> String) ->
|