Monday Hacking Challenge (or Exercise)
Here is a simple problem to work on your hacking (coding) chops. You can use any language you like.
1. Take an arbitrary list of numbers
2. Generate a list of functions from these numbers that computes x^2+2y+1 where x is unknown and y is known
So passing 2 as y results in x^2+2*2+1 = x^2+5
3. If your list of numbers has 3 numbers, you generate 3 functions. If it has 10 you generate 10.
4. Positionally apply functions to numbers and add all together for a numerical result.
Example code in Racket (a version of scheme)
;;; <- This is a comment
;;; Declare a global variable with some numbers
(define list-of-numbers '(1 2 3 4 5 6 7 8 9 10))
;;; Create a function that returns a function
;;; return a function that computes x^2 + 2*y + 1 when y is known
;;; (function-generator 2) ==> x^2 + 2*2 + 1 ==> x^2 + 5
(define (function-generator y)
(lambda (x y)
(+ (* x x) (* 2 y) 1)))
;;; Generate functions on the fly and store in the list of functions global
;;; Generates as many functions as there are numbers in list-of-numbers
(define list-of-functions
(map function-generator list-of-numbers))
;;; take each function above and produce a result by passing by position each function
;;; from list-of-functions to each number in list-of-numbers.
;;; After they return add all the values together
;;; So from (f1 f2 .... f10) and (1 2 3 ... 10) Compute f1(1) + f2(2) + f3(3) .... +f10(10)
(apply +
(map (lambda (f x)
(f x 10)) list-of-functions list-of-numbers))
;;;=> This computes to 595 with the above variables
Hint: Use a dynamically typed language with higher order computation ability and you'll be happier
-
stcupp -
Thanks
{{ DiscussionBoard.errors[4615342].message }} -
-
sanjaypande -
Thanks
Signature{{ DiscussionBoard.errors[4628616].message }} -
-
michalsemen -
Thanks - 1 reply
{{ DiscussionBoard.errors[4635526].message }}-
sanjaypande -
Thanks
Signature{{ DiscussionBoard.errors[4636858].message }} -
-
-
imarketstuff -
Thanks
SignatureI MARKET STUFF{{ DiscussionBoard.errors[4636693].message }} -
-
imarketstuff -
Thanks
SignatureI MARKET STUFF{{ DiscussionBoard.errors[4636806].message }} -
-
unnatural -
Thanks
{{ DiscussionBoard.errors[4639588].message }} -
-
facemook -
Thanks
{{ DiscussionBoard.errors[5645862].message }} -