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.

4
votes
1answer
86 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
35 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
1k 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, ...
1
vote
0answers
51 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
2answers
102 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
1answer
55 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 ...
3
votes
3answers
87 views

Sum of a sublist

The task: Given a list of numbers L, implement a method sum(i, j) which returns the sum from the sublist L[i:j] (including i, excluding j). For example, given L = [1, 2, 3, 4, 5], sum(1, 3) ...
3
votes
2answers
99 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 ...
3
votes
2answers
93 views

Given a pivot x, and a list lst, partition the list into three parts

The task: Given a pivot x, and a list lst, partition the list into three parts. The first part contains all elements in lst that are less than x The second part contains all elements in lst ...
3
votes
4answers
110 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 [...
0
votes
3answers
144 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 ...
-2
votes
1answer
50 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 ...
2
votes
2answers
55 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: ...
1
vote
1answer
41 views

Merging (not removing) duplicated elements in a vector

There's a struct named Player containing name and stat: ...
2
votes
1answer
97 views

Rust: Flattening nested struct to Vec<T>

I have a struct which nests other structs like following: ...
2
votes
1answer
50 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 ...
2
votes
2answers
56 views

RSS feed viewer in Clojure

This semester a classmate and I visited a course on functional programming in our university. For grading, we've to submit a small project which should be written in Clojure and make use of functional ...
3
votes
0answers
46 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
61 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
102 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 ...
1
vote
1answer
57 views

Turning simple Node.JS Postgres query and logging into most-functionally-pure-possible code

After reading "How to deal with dirty side effects in your pure functional JavaScript," I've decided to take something I do on a regular basis ("Original approach" below)—connect to a Postgres ...
2
votes
2answers
236 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
84 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
52 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/...
3
votes
1answer
70 views

React.js Nested Nav Bar

This was written for a coding challenge for a company I recently starting working for. I'm looking for any suggestions on how to clean up the code, as well as any issues anyone thinks may occur as it ...
4
votes
3answers
198 views

Uniformly generate a number from 0 to n-1 that isn't on a blacklist

The task: Given an integer n and a list of integers l, write a function that randomly generates a number from 0 to n-1 that isn't in l (uniform). Solution 1: ...
3
votes
1answer
104 views

Swap even and odd bits of unsigned 8-bit integer

The task: Given an unsigned 8-bit integer, swap its even and odd bits. The 1st and 2nd bit should be swapped, the 3rd and 4th bit should be swapped, and so on. For example, 10101010 should ...
0
votes
1answer
126 views

Return a sorted ordering of courses such that we can finish all courses

The task: We're given a hashmap associating each courseId key with a list of courseIds values, which represents that the prerequisites of courseId are courseIds. Return a sorted ordering of ...
1
vote
1answer
100 views

Find out whether string A can be shifted to get string B

The task: Given two strings A and B, return whether or not A can be shifted some number of times to get B. For example, if A is abcde and B is cdeab, return true. If A is abc and B is acb, ...
2
votes
2answers
72 views

Reach to the last index starting at index 0

The task: Given an integer list where each number represents the number of hops you can make, determine whether you can reach to the last index starting at index 0. For example, ...
2
votes
3answers
99 views

Contiguous elements of the list sum to K

The task: Given a list of integers and a number K, return which contiguous elements of the list sum to K. For example, if the list is [1, 2, 3, 4, 5] and K is 9, then it should return [2, ...
2
votes
2answers
317 views

Return the shortest substring containing all the characters in the set

The task: Given a string and a set of characters, return the shortest substring containing all the characters in the set. For example, given the string "figehaeci" and the set of characters ...
0
votes
0answers
16 views

QTP/UFT Prints the time in takes to complete every task in a Log using VBScript

This script is meant to log the time it takes to complete functions in order to find the locations where performance could be improved in QTP. If a duplicate log is attempted, the script changes the ...
0
votes
2answers
249 views

Find the length of the longest consecutive elements

The task: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, given [100, 4, 200, 1, 3, 2], the longest consecutive element ...
0
votes
2answers
55 views

Count Substrings for a binary string

Problem is taken from leet code. Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings ...
1
vote
0answers
46 views

KMP algorithm in scala

I implemented KMP pattern matching algorithm in Scala. My code works but it looks more imperative and a scala translation of C/C++ implementation. I am not able to figure out how to manage multiple ...
1
vote
0answers
91 views

Max heap in Haskell

I wrote a (max) heap in Haskell which is balanced after every (public) operation. Not based on any documentation. Focus is on: speed, that is the right time-complexities (not necessarily optimal); ...
0
votes
1answer
94 views

Managing locking for access

I've been working for a while on locking resources (state objects) so that the only way to access them is by acquiring a lock. This moves the responsibility for locking from the user of the state to ...
0
votes
1answer
72 views

Find first repeating Char in String

Given a string, find the first repeating character in it. Examples: firstUnique("Vikrant")None ...
2
votes
1answer
173 views

Execute promise tree in order of declaration

I have created a little framework for my work to manage loading and unloading plugins by using a modular tree structure that is based on Promises. I would like to be able to create the tree structure,...
0
votes
2answers
35 views

Expand spreadsheet ranges to lists of cells in Scala

Problem Spreadsheet cells are referenced by column and row identifiers. Columns are labeled with alphabetical characters, starting with "A", "B", "C", ...; rows are numbered from 1 in ascending ...
1
vote
0answers
44 views

Closest 3Sum in Scala

This question is taken from Leet code. Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three ...
4
votes
2answers
58 views

Replace array element with multiplication of neighbors in Scala

Given an array of integers, update the index with multiplication of previous and next integers, Input: 2 , 3, 4, 5, 6 Output: 2*3, 2*4, 3*5, 4*6, 5*6 Following ...
0
votes
1answer
68 views

Clockwise rotate a matrix in Scala

Given a matrix, clockwise rotate elements in it. Examples: ...
1
vote
2answers
95 views

Print matrix in spiral order in Scala

Problem: Given a square or rectangular matrix, print its element in spiral order. For example, if input matrix is this: ...
0
votes
1answer
53 views

Print words in decreasing order of frequency in Scala

Given a string print its words in decreasing order of frequency. Example: i/p - "aa bbb ccc aa ddd aa ccc" o/p - aa,ccc,ddd,bbb Scala: ...
1
vote
2answers
52 views

List of Happy Numbers in scala

Definition of Happy numbers taken from Wikipedia. A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits ...
2
votes
0answers
31 views

Finding first duplicated value from “circular” list with a recursive function

Background This year I've taken on the challenge of Advent of Code. The second challenge of the first day is to calculate a frequency for a special device based on a series of numbers. Here's a quote ...
2
votes
1answer
68 views

Game of Life with Scala and Functional Programming

I have been doing OOP and imperative programming my whole career but I wanted to dabble in Scala along with functional programming. Rather than writing Java like code in Scala I wanted to try a more ...