All Questions
Tagged with functional-programming programming-challenge
109
questions
2
votes
1answer
50 views
Project Euler 1 using functional programming in JS
So I am once again beating a dead horse, by solving the first Project Euler problem:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these ...
2
votes
0answers
73 views
Partial Function composability in Haskell
Below is my solution for the CTFP chapter 4 challenges which essentially involves composing partial functions (that don't have defined outputs for all possible inputs i.e. returning a Maybe).
The ...
1
vote
1answer
62 views
“Sequence full of colors” challenge on HackerRank
This is the challenge:
You are given a sequence of N balls in 4 colors: red, green, yellow and blue. The sequence is full of colors if and only if all of the ...
5
votes
2answers
88 views
sum of multiples by 3 or 5 using ranges
This program calculates the sum of all integers in the range \$[1, 1000)\$ which are multiples of either 3 or 5 or both.
Inspired by x86-64 Assembly - Sum of multiples of 3 or 5 the other day, and ...
2
votes
1answer
68 views
Extract values from English numerals, e.g. “nine million and one”
Though many have done it the other way around, I have not seen such code in many places. And, to be honest, I don't know why this cluster of if-statements, ...
2
votes
2answers
70 views
HackerRank - Filter huge list for elements that occur more than a given number of times
I'm trying to solve this problem on HackerRank, which basically requires the solution to read an input like this (comments are mine)
...
3
votes
2answers
126 views
Leap year check in Java (functional style)
I have done the leap year check on https://exercism.io already in a lot of languages. Today I came back to the exercise in Java and was playing around with some maybe more funny ways to do the check. ...
0
votes
1answer
163 views
Provide functional-programming style solution for 2D Array - DS challenge
Problem Statement:
Given a 6×6 2D Array, arr:
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
We define an hourglass in A to be a subset of ...
5
votes
2answers
217 views
Counting valleys traversed below sea level, given elevation changes
Problem Statement:
Gary is an avid hiker. He tracks his hikes meticulously, paying close
attention to small details like topography. During his last hike he
took exactly steps. For every step ...
6
votes
4answers
2k views
Find all letter Combinations of a Phone Number
The task
is taken from LeetCode
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A
mapping of digit to letters (just ...
2
votes
1answer
427 views
Given time intervals determine if a person could attend all meetings
The task is taken from LeetCode
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...
1
vote
1answer
194 views
Merge k Sorted Lists
The task is taken from LeetCode
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
Example:
...
8
votes
1answer
2k views
Merge Intervals in JavaScript
This is a task taken from Leetcode -
Given a collection of intervals, merge all overlapping intervals.
Example 1:
...
6
votes
1answer
2k views
Given a string containing just parentheses, determine if the input string is valid
The task
is taken from leetcode
Given a string containing just the characters '(', ')', '{', '}', '['
and ']', determine if the input string is valid.
An input string is valid if:
...
1
vote
1answer
1k 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
592 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
70 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
1answer
362 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
601 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 ...
1
vote
2answers
301 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
648 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
76 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 ...
2
votes
1answer
152 views
Find all pairs of unique indices in a list such that the concatenation of the two words is a palindrome
The task
Given a list of words, find all pairs of unique indices such that the
concatenation of the two words is a palindrome.
For example, given the list ...
1
vote
1answer
387 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, ...
1
vote
1answer
259 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.
...
1
vote
1answer
185 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, ...
4
votes
2answers
347 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
2k 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
113 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 ...
6
votes
2answers
342 views
Find duplicate in linear time and space
The task:
You are given an array of length n + 1 whose elements belong to the
set {1, 2, ..., n}. By the pigeonhole principle, there must be a
duplicate. Find it in linear time and space.
<...
4
votes
1answer
161 views
Return whether a string is a palindrome if you can delete at most k elements
The task:
Given a string which we can delete at most k, return whether you can
make a palindrome.
For example, given 'waterrfetawx' and a k of 2, you could delete f and
x to get '...
1
vote
0answers
151 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
0answers
233 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
474 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 ...
5
votes
1answer
509 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
294 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 ...
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, ...
1
vote
0answers
58 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
343 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 ...
2
votes
3answers
410 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
4answers
2k 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
3answers
659 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 ...
4
votes
5answers
1k 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
193 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
2answers
4k 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
172 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:
...
4
votes
1answer
501 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 ...
2
votes
3answers
158 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 ...
3
votes
2answers
592 views
Advent of Code 2018 day 1 part 2: find the first aggregated number that occures twice
I am using the coding puzzels of https://adventofcode.com for improving my F# skills.
Problem:
Day 1 Part 2:
Starting with frequency 0, a list of numbers should be added successive generating new ...