Questions tagged [functional-programming]

Functional programming is a paradigm which attempts to solve computational problems by the chained evaluation of functions whose output is determined by their inputs rather than the program state. In this style of programming, side effects and mutable data are deprecated.

Filter by
Sorted by
Tagged with
1
vote
1answer
485 views

Find common characters (LeetCode)

The task is taken from leetcode Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). ...
3
votes
2answers
239 views

Robot Return to Origin

The task is taken from leetcode There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes ...
1
vote
2answers
60 views

Flipping an Image

The task is taken from leetcode Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip ...
1
vote
0answers
17 views

Combinable filters for Python Pandas dataframe structure

This is an extension of my other question here. I have applied suggestions from 200_success in real-life problem at hand that deals with modelling extensible and ...
7
votes
1answer
3k views

“Mars Rover” challenge

I have written the solution of the Mars Rover problem in JavaScript, with a sort of functional approach. I would like to know how I can improve it and if there are any additional edge cases I missed ...
1
vote
1answer
322 views

Rotate a N by N matrix by 90 degrees clockwise

The task: Given an N by N matrix, rotate it by 90 degrees clockwise. For example, given the following matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] you should return: [[7, 4, 1], [8, ...
3
votes
1answer
50 views

Utmost function that takes a comparator function

I'm working through Paul Graham's "ANSI Common Lisp". In Chapter 6 (p105), he introduces the idea of utility functions. He points out these are core to bottom-up programming and reusability. He ...
1
vote
0answers
78 views

Transform vcards into contacts using functional programming in JavaScript

Context: I'm building a React app using Ramda as a helper library for functional programming. Users can import contacts into the app. These contacts can be vCards. I've written a function that: ...
3
votes
1answer
91 views

Grouping dates by weeks using reduce()

I am trying to implement a function groupWeeks(dates) using reduce that allows me to group an array of objects ...
1
vote
1answer
221 views

Sort Array By Parity

The task is taken from leetcode Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A. You may return any ...
0
votes
1answer
39 views

Items also occurs in a set

The task You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how ...
2
votes
0answers
432 views

Determine whether there exists a one-to-one character mapping from one string to another

The task Determine whether there exists a one-to-one character mapping from one string s1 to another s2. For example, given s1 = abc and s2 = bcd, return true since we can map a to b, b to ...
4
votes
2answers
160 views

Roman Numbers - The Functional Way

Yet another exercise in converting values between the decimal and roman number systems. I have tried the functional way. Any comments are welcome - but I'm especially interested in answers focusing ...
1
vote
2answers
66 views

Sum of two numbers in array

The task You are given two non-empty arrays representing two non-negative integers. The digits are stored in reverse order and each of their items contain a single digit. Add the two numbers ...
1
vote
1answer
322 views

Find busiest period in building

The task You are given a list of data entries that represent entries and exits of groups of people into a building. An entry looks like this: {"timestamp": 1526579928, count: 3, "type": "...
0
votes
1answer
61 views

Find indices of two numbers such that they add up to a target

The task ...is taken from leetcode Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have ...
1
vote
1answer
121 views

Check whether string of brackets are well-formed

The task I asked similar questions here and here. Given a string of round, curly, and square open and closing brackets, return whether the brackets are balanced (well-formed). For example, ...
1
vote
1answer
188 views

Given a list and a number k return whether any two numbers from the list add up to k

The task: Given a list of numbers and a number k, return whether any two numbers from the list add up to k. For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17. ...
4
votes
2answers
190 views

Check whether string of braces, brackets, and parentheses is balanced

The Task is taken from codewars: Write a function that takes a string of braces, and determines if the order of the braces is valid. It should return true if ...
1
vote
2answers
774 views

Minimum number of parentheses to be removed to make a string of parentheses balanced

The task: Given a string of parentheses, write a function to compute the minimum number of parentheses to be removed to make the string valid (i.e. each open parenthesis is eventually closed). ...
1
vote
1answer
62 views

Return a new array where each element is the number of smaller elements to the right of that element in the original input

The task: Given an array of integers, return a new array where each element in the new array is the number of smaller elements to the right of that element in the original input array. For ...
1
vote
0answers
79 views

Rotate a list by k elements

The task: Write a function that rotates a list by k elements. For example, [1, 2, 3, 4, 5, 6] rotated by two becomes [3, 4, 5, 6, 1, 2]. Try solving this without creating a copy of the list. ...
2
votes
1answer
76 views

Executing multiple steps to handle a purchase using currying

Is doing something like this utilizing currying in a practical sense? ...
3
votes
1answer
43 views
4
votes
2answers
210 views

Parsing infix expressions in Clojure

I'm trying to teach myself Clojure by reading the Brave Clojure book online, and at the end of Chapter 7 there is an exercise to parse a mathematical expression written as a list in infix notation to ...
2
votes
0answers
124 views

Sort a list using a reverse helper function

The task: Given a list, sort it using this method: reverse(lst, i, j), which reverses lst from ...
1
vote
2answers
151 views

Find the smallest distance between any two given words in a string

The task: Find an efficient algorithm to find the smallest distance (measured in number of words) between any two given words in a string. For example, given words "hello", and "world" and a ...
0
votes
1answer
80 views

Pure methods for updating java objects

I've been reading about pure methods and immutability in Java. I have a current application and want to convert all the methods to pure methods. My app takes payment from users. I first create a ...
5
votes
1answer
235 views

Return the first recurring character in a string

The task: Given a string, return the first recurring character in it, or null if there is no recurring character. For example, given the string "acbbac", return "b". Given the string "...
4
votes
1answer
197 views

Determine whether any permutation of a string is a palindrome

The task: Given a string, determine whether any permutation of it is a palindrome. For example, "carrace" should return true, since it can be rearranged ...
2
votes
0answers
36 views

Dispatch different parameters to different classes with single interface [closed]

I have a set of classes that have similar funcionality but very different interfaces. I created two interface classes with polymorphic methods so I can operate over then. When creating calling the ...
10
votes
3answers
2k views

Find the majority element, which appears more than half the time

The task: Given a list of elements, find the majority element, which appears more than half the time (> floor(len(lst) / 2.0)). You can assume that such element exists. For example, ...
3
votes
2answers
171 views

Find the elements that appear only once

The task: Given an array of integers in which two elements appear exactly once and all other elements appear exactly twice, find the two elements that appear only once. For example, given ...
1
vote
0answers
56 views

Find the square root of n

The task: Given a real number n, find the square root of n. For example, given n = 9, return 3. My solution: ...
3
votes
3answers
984 views

Readability of array_* vs foreach in PHP for filtering and extracting

With functional programming all the rage, I feel I should be using PHP's array_* functions more. But when I compare these two identical implementations, I can't ...
1
vote
1answer
175 views

Find nearest k points from the central point

The task: Given a list of points, a central point, and an integer k, find the nearest k points from the central point. For example, given the list of points [(0, 0), (5, 4), (3, 1)], the ...
0
votes
3answers
172 views

Finding nth lowest value without any Math max or min methods

So I decided to make a more efficient solution to a Google interview question where they ask you to either find the lowest or second lowest value in an array without using the max or min methods. I ...
3
votes
4answers
533 views

Find index of the nearest larger number of a number

The task: Given an array of numbers and an index i, return the index of the nearest larger number of the number at index i, where distance is measured in array indices. For example, given [...
-2
votes
1answer
53 views

Functional vs non functional approach in python [closed]

This is not a task, but I want to know what the general consensus is on functional programming. Particularly functional programming in Python. The two snippets of code I am comparing are the ...
1
vote
1answer
43 views

Merging (not removing) duplicated elements in a vector

There's a struct named Player containing name and stat: ...
2
votes
2answers
56 views

Determine if 2 javascript objects have strictly the same properties

I need to compare 2 objects to determine if they have strictly the same properties. This is what I came up with so far: ...
2
votes
1answer
408 views

Rust: Flattening nested struct to Vec<T>

I have a struct which nests other structs like following: ...
2
votes
1answer
57 views

Function to filter comments and ratings, with pagination support

I have a web component that is rendering a list of items and I have this method for filtering these items based in a filterBy object that is being updated by other ...
3
votes
0answers
56 views

Generic Variable Elimination with multiple entry points

I have this algorithm, called Variable Elimination, which is used quite often in my code-base (library). It is an algorithm for factor graphs (a type of bipartite graphs), used to efficiently maximize ...
3
votes
0answers
84 views

Naive Implementation of A Least Recently Used (LRU) Cache Memoiser

I wrote code to (naively) perform lru memoisation. I tried to write it in a functional programming style, and so did not make use of any global variables. My Code ...
1
vote
1answer
31 views

Naive Implementation of Automatic Memoisation

I wrote code to (naively) perform automatic memoisation. I tried to write it in a functional programming style, and so did not make use of any global variables. My Code ...
5
votes
3answers
213 views

Currying a function

The goal is to have a function that can transform any python function into a curried version of this function. The code is available here, I'll only update the code on repl.it so everyone can make ...
2
votes
2answers
2k views

Given a set of closed intervals, find the smallest set of numbers that covers all the intervals

The Task: Given a set of closed intervals, find the smallest set of numbers that covers all the intervals. If there are multiple smallest sets, return any of them. For example, given the ...
4
votes
2answers
103 views

square the elements of a sorted list and give the output in sorted order

Given a sorted list of integers, square the elements and give the output in sorted order. For example, given [-9, -2, 0, 2, 3], return [0, 4, 4, 9, 81]. My solution 1: ...
2
votes
1answer
71 views

User-Interactive Data Cleaning Program in Python

I'm trying to develop a program in Python that allows the user to import datasets and perform operations on them using pandas and numpy so she/he can skip writing all the preprocessing code her/...

1 2 3
4
5
18