All Questions
18 questions
3
votes
1
answer
319
views
Efficiently calculate value of Pascal's Triangle using memoization and recursion
So reading about functional programming, applications to dynamic programming, and learning Scala. I figured I would try it out with a popular example, Pascal's Triangle, tests against known values ...
2
votes
0
answers
187
views
generic implementation approaches for a recursive function
Let's take the famous fibonacci problem as an example but this is a generic problem. Also, taking scala as the language as it's rather feature-rich. I want to know which solution you'd prefer.
we all ...
1
vote
1
answer
121
views
Find first repeating Char in String
Given a string, find the first repeating character in it.
Examples:
firstUnique("Vikrant") → None
...
4
votes
2
answers
450
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 ...
1
vote
1
answer
99
views
Check if list contains a pair which adds up to a given sum
Kindly review this scala code for given problem and suggest improvements.
Problem - Given an array of integers and a target sum, check if array contains a pair which adds up to sum.
Example -
<...
5
votes
1
answer
249
views
Bowling game Kata in Scala - pattern match
This is a functional approach to problem described by standard Bowling Game Kata - to implement the rules of calculating a score in a bowling game. The inspiration was the implementation by Uncle Bob: ...
0
votes
1
answer
2k
views
Cartesian product in Scala
I'm using this code to compute a cartesian product:
...
2
votes
1
answer
100
views
Building and querying an immutable phone book
I wanted to get your opinion on this immutable implementation of a Phone Book using recursion to process a stream. I am especially interested in ways to speed up the code execution.
Here is an ...
0
votes
2
answers
1k
views
Recursive Scala Case class Reduction
I have a case class that is recursive and looks like this:
...
2
votes
1
answer
105
views
Selecting subset of size k using recursion [closed]
Sometimes I need to implement recursive algorithms that pass a certain state from one recursive call to another. For example, a greedy subset selection: we have a set of candidate objects, and we ...
2
votes
3
answers
1k
views
Tail-Recursion to get a Map of word counts
I want to read a file and store the number of occurrences of each word in a Map, using tail recursion. I came up with the following; it seems to work; does it look like it's right?
...
3
votes
3
answers
131
views
Removing nested blocks from a string
I wrote this function in scala that uses tail recursion to remove nested blocks from a text.
Usage examples:
...
3
votes
3
answers
3k
views
Pascal triangle algorithm is good but fails
I have created a function which returns the number that corresponds to the given row and column from the pascal triangle. I have used the formula for this:
n! / r! * (r-n)!
The code:
...
10
votes
5
answers
22k
views
Pair matching elements from two lists
Here is a method that takes two lists (l1 and l2) and pairs up elements that "match". As you can see at the end of the code ...
4
votes
1
answer
3k
views
Recursive branch and bound Knapsack solution [closed]
I've got the imperative styled solution working perfectly.
What I'm wondering is how to make a recursive branch and bound.
This is my code below. Evaluate function returns the optimistic estimate, ...