# 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
• what language are you using here?
{{ DiscussionBoard.errors[4615342].message }}
• It's a version of Scheme (which is a Lisp)
Signature

{{ DiscussionBoard.errors[4628616].message }}
• Hi,
What is the benefit of this computing overall? Can I write the same program in C?
{{ DiscussionBoard.errors[4635526].message }}
•  Originally Posted by michalsemen 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 }}
• i like this stuff sanjay, but i just fried a fuse in my brain
Signature
I MARKET STUFF

{{ DiscussionBoard.errors[4636693].message }}
• 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 }}
• Facebook has some pretty fun coding challenges if anyone is interested.

{{ DiscussionBoard.errors[4639588].message }}
• thank you for sharing ideas how to hack godbless .
{{ DiscussionBoard.errors[5645862].message }}

• ### Russell Brunson Acquires Dan Kennedy's "Magnetic Marketing" Company

Tony M

Just seen this email in my inbox this morning... ---------- "A VERY IMPORTANT ANNOUNCEMENT!... I am here today to announce that ownership of Magnetic Marketing (formerly known as "GKIC") has ... [read more]

• ### How much does it cost to hire a Social Media marketing team for a blog?

Hi guys, I have a blog that isn't doing too well and I want to hire a social media team to promote its content on different social media channels to ... [read more]

• ### What Old Technology are You Still Buying

tagiscom

Is it Atari 2600, games, computers with Windows 95, or something else, you cannot live without? For me, my VHS, Video Recorder, (yeah, tape) recently, after chewing its way through ... [read more]

• ### Force Google search console indexing duplicate content?

shimon revivo in SEO

I have a WordPress website that uses a plugin to duplicate content from other websites using RSS feed. It copies the entire article and adds a Copywrite msg at the ... [read more]

• ### Love my hot and spicy pork skins....what snacks are you in to ?

discrat

Just sitting here relaxing and finishing off a bag of hot and spicy pork skins from HEB. My affinity for pork skins started in 92' when I started dating a ... [read more]