Day #5: Functional Programming with Python.

I am a Software Engineer from Hyderabad. Here to learn and share what I learn.
Search for a command to run...

I am a Software Engineer from Hyderabad. Here to learn and share what I learn.
No comments yet. Be the first to comment.
Hello. Today is the Day 6 of the #100DaysOfCodeChallenge. Received a problem previously asked by Google with a hard tag to it. The Question On Day #6: An XOR linked list is a more memory efficient doubly linked list. Instead of each node holding next...
Unveil Your Brilliance with DIY Portfolio – Your Personalized Showcase, One File Edit Away!

Building a backend server application using Node.js, Express and JDoodle API to execute scripts and return the output as response.

Learning how to create mind maps to understand what's needed for the project and setting up the project folder with required dependencies quickly.

A series of articles on how to build a simple web app which serves an online IDE using the MERN stack to run code in multiple languages.

Hello everyone. Today is the Day 8 of the #100DaysOfCodeChallenge. Received a problem previously asked by Google with an easy tag to it. The Question On Day #8: A unival tree (which stands for "universal value") is a tree where all nodes under it hav...

Hey. Hope you have been enjoying the series so far. Today is the Day 5 of the #100DaysOfCodeChallenge. Received a problem previously asked by Jane Street (refer the hyperlink for the company details) with a medium tag to it. This question is a true puzzle and can be solved in a minute if your functional programming understanding is strong.
You may get confused with the question, just like the way I did. I will try my best in putting the solution in the simplest way possible. You can always reach out to me if you need help.
cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last element of that pair. For example, car(cons(3, 4)) returns 3, and cdr(cons(3, 4)) returns 4.
Given this implementation of cons:
def cons(a, b):
def pair(f):
return f(a, b)
return pair
Implement car and cdr.
The question is a bit confusing but let us try to understand it clearly.
cons is a function which takes two inputs (consider integers). It returns a value which is an function defined inside itself, i.e. pair.pair is a function which takes a function f as input and returns the return value of its input function, i.e. the return value of the function f is returned by pair.f is a function which will return a value.f is the key to all our solution.pair function.Output 3 4
When car(cons(3,4)) is called,
car function is the pair function.car function returns the value returned by calling the pair function.pair function takes a function as input, so we pass the left function to it.pair function returns the value obtained by passing a, b to a a function f. So, our left is the function f, which takes a, b as input and returns a as output to the pair function.pair function to the car function, which will again return the same value of a.cdr.#The Python visualiser is very much needed to understand this logic more clearly, please refer it here.
I hope you were able to understand this problem. Feel free to reach out for any query clearance.
Thanks and cheers:)