All Questions
50 questions
-1
votes
1
answer
82
views
Guide to improve/optimize my terrible code for is graph bipartite in scala
I wrote this code for https://leetcode.com/problems/is-graph-bipartite/
It works but I think the code is terrible. Issues:
How do I break when I find isBipartite=false
How do I only dfs(find if graph ...
2
votes
2
answers
207
views
Functional/Stream programming for the graph problem "Reconstruct Itinerary"
I am trying to solve the reconstruct itinerary problem (https://leetcode.com/problems/reconstruct-itinerary/) in Scala using functional approach. Java solution works but Scala doesn't. One reason I ...
5
votes
1
answer
285
views
Quantity redistribution logic - MapGroups with external dataset
I am working on a complex logic where I need to redistribute a quantity from one dataset to another dataset.
In the example we have Owner and Invoice - We need to subtract the quantity from the ...
1
vote
2
answers
103
views
Confusing call sequence in scala recursion
I am trying to trace recursion processing in scala. The following is the code sample:
def factorial(n: Int): Int =
if (n <= 1) 1
else {
println("Computing factorial of " + n + " - I first ...
0
votes
5
answers
157
views
Generate a custom pattern number sequence in one go
I'd like to generate the following number sequence in one go using a functional initialization construct:
Array(0, 0, 0, 0, 3, 3, 6, 6, 9, 9, ..., n*3, n*3)
One way is to do:
Array.fill[Int](2)(0) ++...
-2
votes
1
answer
405
views
How to convert into RPN(Reverse Polish notation) in Scala?
I tried to convert formula to RPN(Reverse Polish Notation) in Scala.
RPN: https://en.wikipedia.org/wiki/Reverse_Polish_notation
But I couldn't write any code.
object RPNCalclator {
def ...
3
votes
5
answers
346
views
How to retrieve the value of a nested map using the value of the first map?
I have a map of entries of type Map[String, String] and a lookup map of type Map[String, Map[String, String]]. My goal is to look in the first map for a match in the second map, once I have a key ...
2
votes
2
answers
2k
views
How to replace a value in one map with the value of another map, based on a pattern match?
I have a map that has a few key value pairs, and I would like a way to go through those pairs and attempt to match the keys with the value of another map. If there's a match the values are substituted ...
1
vote
2
answers
99
views
Merge sort not working for ascending order
I am studying a course on scala in which I am performing merge sort operation. Here is the code written for that
def merge(leftList:List[Int], rightList:List[Int]): List[Int] = {
val output = (0 ...
1
vote
1
answer
102
views
Performance issue in Scala code - O(n log n) faster than O(n)
I am just getting started with scala. I am trying to learn it by solving easy problems on leetcode. Here's my first (successful) attempt at LC #977:
def sortedSquares(A: Array[Int]): Array[Int] = {
...
2
votes
3
answers
109
views
Join elem with next one in a functional style
I'm trying to find a way to "join"/"groupby" 2 elements in a list as following :
List("a","b","c","d") -> List("ab","bc","cd")
With a functional style.
Would someone know how to do this?
Need ...
1
vote
3
answers
195
views
How to collapse 2D array by eliminating all empty/null entries
I have a 2D container with sample data as below:
NULL 1
NULL 2
3 NULL
NULL 4
5 NULL
And I wanna collapse upwards get rid of all the NULL entries on the way, and result to be like:
3 1
...
5
votes
2
answers
130
views
How to get a termination reason from a recursive function?
Suppose a function is looped to produce a numeric result. The looping is stopped either if the iterations maximum is reached or the "optimality" condition is met. In either case, the value from the ...
0
votes
0
answers
66
views
Scala generic functional way of segmenting a polyline
Working on an interesting problem of segmenting a line in space which is represented as Seq[Point]
abstract class Point[T] {
def dimensions: Double
def distance(rhs: T) : Double
def ...
0
votes
3
answers
1k
views
How can I speed up my Aho-Corasick Algorithm?
I am trying to solve a problem on HackerRank; "Determining DNA Health." I decided after looking at some of the discussions that the Aho-Corasick algorithm would be the best choice. The problem ...