Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [scala]

Scala is a general purpose programming language principally targeting the Java Virtual Machine. Designed to express common programming patterns in a concise, elegant, and type-safe way, it fuses both imperative and functional programming styles.

-1
votes
1answer
27 views

Cleanest functional way to iterate a 2D Grid Co-ordinates in Scala? [closed]

A Solution using nested for loops is trivial. Here is my code to iterate over a 2D Grid in Scala using map ...
3
votes
3answers
103 views

Computing average duration per user from a large CSV file that doesn't fit in memory

We have a large log file which stores user interactions with an application. The entries in the log file follow the following schema: {userId, timestamp, actionType} where actionType is one of two ...
5
votes
1answer
492 views

Extracting numeric values from a string containing key:value pairs

I am writing function numericValues(text: String): List[Int] to extract patterns """([a-z]+)\s*:\s*(\d+)""" and return the list ...
1
vote
1answer
27 views

Page download with reattempts in Scala

I wrote the following code to deal with possible network problems. ...
2
votes
2answers
54 views

Solve membership binary mask problem in Scala

Imagine you have two string sequences: val s1 = Seq("c", "a", "b", "z", "a", "b") val s2 = Seq("a", "b") You need to come up with an algorithm that generates a ...
1
vote
0answers
37 views

Java classes to load configuration data from a JSON file, with Scala tests

I want to implement classes in a project that will support the loading of configuration data from a JSON file. The JSON file is in the following form. Please be as critical as you can be. I'm a junior ...
0
votes
2answers
52 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
39 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 ...
0
votes
1answer
67 views

Find first repeating Char in String

Given a string, find the first repeating character in it. Examples: firstUnique("Vikrant")None ...
0
votes
2answers
34 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 ...
0
votes
1answer
60 views

Minimum steps to reach target by a Knight In Scala

Given a chessboard of N size (square matrix), the position of Knight and position of a target, find out minimum steps (both count and exact steps) from start to target for a Knight. If it is not ...
1
vote
0answers
39 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
49 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
45 views

Clockwise rotate a matrix in Scala

Given a matrix, clockwise rotate elements in it. Examples: ...
1
vote
2answers
64 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
42 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
48 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
1answer
57 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 ...
1
vote
1answer
79 views

Calculate simple arithmetic string in Scala

Problem Given an arithmetic string like 2+2*3 calculate result 8. Assumptions you can make string will be always well formed (no error checking needs to be done) only operators passed in string ...
0
votes
1answer
38 views

find if one string is a valid anagram of another , in scala

Question is taken from leet code. Problem Given two strings s and t , write a function to determine if t is an anagram of s. Examples ...
1
vote
1answer
70 views

Longest Substring Without Repeating Characters in Scala

Question is taken from Leetcode and solution works for their test cases. I do see a scope of improvement and make it more functional (getting rid of vars and mutables data structures). Please suggest ...
0
votes
1answer
28 views

Efficiency of grouping a List of case classes by more than one field

I have the following input: ...
1
vote
1answer
53 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 - <...
1
vote
0answers
39 views

NQueen solution in scala

This is my solution for NQueen problem in scala. I just want to know how to make it more easy to read, modular and areas i need to improve. ...
0
votes
1answer
45 views

Find the combination of matches which are closest to each other

Problem - Given three sorted arrays find combinations which are closest to each other. example - ...
3
votes
1answer
49 views

Mark positions attacked by a queen on a chess board

Problem - Given a chess board and the position of only one queen piece in it, mark all the position where the queen can move with X. Kindly review this Scala code and suggest improvements. ...
0
votes
2answers
46 views
2
votes
1answer
131 views

Run two computations in parallel, and return the results together

I am reading the "Learning Concurrent Programming in Scala" and there are exercises in the end of each chapter. One of exercises is Implement a parallel method which takes two computation blocks a ...
1
vote
2answers
58 views

Project Euler - multiples of numbers

I'm trying to learn functional programming and Scala, following through Project Euler problems. I'm solving get the sum of multiples x & y in a list. So for a list ...
1
vote
1answer
66 views

Longest Collatz sequence in Scala

I am trying to find the longest Collatz Sequence: Which starting number, ≤ N, produces the longest chain? If many possible such numbers are there print the maximum one. (1 ≤ N ≤ 5×106) I tried ...
0
votes
1answer
58 views

Count lines of scala code (omitting empty lines) in a directory, traversing any subdirectories

I need to simply count lines of Scala code in a project, which includes some package(i.e. directory) hierarchy. I'm not sure about the performance of the code below. Can you help improve it, if ...
1
vote
0answers
76 views

Add an attribute to specific XML Elements in Scala

supposed I have an XML like ...
0
votes
0answers
145 views

Generating test Spark dataframe with Scala

I need to create test dataframe of lat/lon points. I make it with scalanlp and it looks suitable for me. Next stage is creating ...
1
vote
1answer
133 views

Binary search in Scala

Another exercise in Scala in which the goal is to find the target position as fast as possible. The initial input provides the grid size and the initial position. Each turn, this code provide a new ...
4
votes
1answer
105 views

Find an ideal speed to cross all traffic lights when they are green

As a second exercise with Scala I tried to solve another puzzle. In this puzzle, we have to find the highest speed that allows a driver to cross all the traffic lights when they are green, without ...
2
votes
1answer
55 views

Finding the nearest defibrillator exercise from CodingGames

For my first attempt at Scala I decided to solve a simple exercise on codingame.com. Based on a string input containing the user location, the defibrillators count and a list of defibrillators ...
0
votes
1answer
93 views

Finding the number of inversions in an Array

I am doing a college assignment where I have to count the number of inversions in an array/list. An inversion is defined as a tuple where i<j and a[i]>a[j]. ...
1
vote
1answer
114 views

Fixed size double linked list in Scala

I have written a double linked list in Scala. The list should be fixed in size and items are always added to the head. if the list reaches max size then the tail is dropped. This is my code. I want ...
3
votes
0answers
57 views

Finite State Machine to segment Thai syllables

I am developing a FSM to segment Thai syllables with the following rules: Here are my util functions: ...
6
votes
2answers
175 views

Program to calculate some stats from Facebook messages

I've written a program that parses JSON downloaded from Facebook to calculate some stats from a conversation between two parties. It attemps to count these things: Messages grouped by day of week ...
0
votes
2answers
55 views

Retrieve value from item wrapped in a Future[Option[Item]] [closed]

I have a function like this def findById(id: UUID): Future[Option[Item]] case class Item(subject: String) Now I want to retrieve the ...
0
votes
0answers
345 views

Replace Every JSON Key's Instance with Value

Given the following using the Ammonite REPL: ...
3
votes
1answer
60 views

Readable unit test - lists of complex objects

Goal: Writing more readable tests. I have a couple of functions, which basically merge and converts two lists of Datasets together, written using Scala and Spark. Each of these Datasets has a lot of ...
2
votes
1answer
691 views

Solution to a simple Hackerrank problem: Compare the Triplets

Given two equally sized arrays of integers A and B, compute the number of times ...
1
vote
0answers
500 views

WebSocket example with akka-http

Akka-http supports WS connections like that: ...
1
vote
1answer
58 views

Create a collection of functions tupled with a collection of parameters, and execute the collection in order in Scala

I am trying to create a collection of Unit functions that have any arity and type, and will execute them in order. This is the working implementation that I have: ...
1
vote
1answer
192 views

Check Is Anagram Scala HashMap based implementation

Here is the HashMap based implementation of the function checking whether two strings are anagrams: Example: ...
1
vote
1answer
306 views

Scala tic-tac-toe no mutable state

The following was an exercise for me to try to understand how state can be passed down the stack without immutability. I'm used to writing java code and this is just a way for me to understand. I've ...
1
vote
1answer
220 views
1
vote
1answer
2k views

Joining Apache Spark data frames, with many conditional substitutions

I am joining two data frame in spark using scala . My code looks very ugly because of the multiple when condition . Can somebody please help me simplify my code? Here is my existing code . ...