Monday Hacking Challenge (or Exercise)

8 replies
So, I was feeling a little geeky today and have not done any serious programming for a few years now.

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
#challenge #exercise #hacking #monday
  • Profile picture of the author stcupp
    what language are you using here?
    {{ DiscussionBoard.errors[4615342].message }}
  • Profile picture of the author sanjaypande
    It's a version of Scheme (which is a Lisp)
    Signature

    {{ DiscussionBoard.errors[4628616].message }}
  • Profile picture of the author michalsemen
    Hi,
    What is the benefit of this computing overall? Can I write the same program in C?
    {{ DiscussionBoard.errors[4635526].message }}
    • Profile picture of the author sanjaypande
      Originally Posted by michalsemen View Post

      Hi,
      What is the benefit of this computing overall? Can I write the same program in C?
      The benefit is higher order functions -> functions that return or accept functions as an input. With syntax based languages like C, there is also a double learning - you need to understand both logic and syntax before you even start actually programming. Being too close to machine does not allow you to easily express abstract thoughts and algorithms without too much coding.

      Yes, you can write it in C with perhaps a few thousand lines of code.

      It is way more efficient to write it in a higher order language and generate C code before compiling it to machine. Most Lisp->C compilers generate more efficient code than hand-written C (sometimes orders of magnitude).

      Stalin a popular Lisp->C compiler generates 2x to 7x faster executing C code than hand coding a C program by an expert.

      Languages like Scheme, Lisp, Python, Smalltalk etc are also interactive which shortens development time (max expense on project) considerably.

      When you have very few developers and a very large project, then these languages really help and are used as secret weapons.

      Google uses lots of Python (which borrows a lot from Lisp and Peter Norvig, the director of search quality at Google is a well known Lisp expert).

      ITA Software uses Lisp and it powers Orbitz and a whole lot of other airfare search engines

      Ruby is used by many including some popular apps that many IMers use such as Basecamp and Kajabi.

      Many security applications use higher level languages internally.

      Take Care,

      Sanjay
      Signature

      {{ DiscussionBoard.errors[4636858].message }}
  • Profile picture of the author imarketstuff
    i like this stuff sanjay, but i just fried a fuse in my brain
    Signature
    I MARKET STUFF

    {{ DiscussionBoard.errors[4636693].message }}
  • Profile picture of the author imarketstuff
    so, we need to be able to dynamically create functions on the fly and call them, hmmm...
    Signature
    I MARKET STUFF

    {{ DiscussionBoard.errors[4636806].message }}
  • Profile picture of the author unnatural
    Facebook has some pretty fun coding challenges if anyone is interested.

    Engineering Puzzles | Facebook
    {{ DiscussionBoard.errors[4639588].message }}
  • Profile picture of the author facemook
    thank you for sharing ideas how to hack godbless .
    {{ DiscussionBoard.errors[5645862].message }}

Trending Topics