Higher-order functions
-
Define a function
apply_twicethat takes a functionfand a valuex, and returns the result of applyingftoxtwo times.def sqr(x): return x * x def apply_twice(f, x): """apply f to x twice. >>> apply_twice(sqr, 3) 81 """ - Define a function
applierthat applies a given functionfto the argumentxforntimes.def sqr(x): return x * x def applier(f, x, n): """apply f to x n times. >>> applier(sqr, 3,2) 81 """ - Given an integer \(k>1\), compute the largest positive integer \(n\) smaller than \(k\) s.t. there is no other integer between 1 and \(k\) with a larger Collatz length. Use
proc_seqfrom here. Define two versions, one withwhilethe other withproc_seqfrom here. - Given an integer \(k>1\), compute the largest positive integer \(n\) smaller than \(k\) s.t. there is no other integer between 1 and \(k\) with a larger Collatz length. Use
proc_seqfrom here. - Define a function
argmax(f,start,end,step=1)that returns the smallest argument that maximizesfin the range[start,end]incremented insteps.