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.

Filter by
Sorted by
Tagged with
2
votes
2answers
35 views

Scala app to transpose columns into rows

This is the first application or really any Scala I have every written. So far it functions as I would hope it would. I just found this community and would love some peer review of possible ...
1
vote
1answer
180 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
0answers
24 views

Create JIRA ticket using REST API

Objective: The objective of the program is to create a JIRA ticket using a REST API. It's working perfectly fine without any issue. However I am not sure if it's up to good Scala standards. I would ...
1
vote
1answer
88 views

Return all anagrams for a word matched against a list of words in a file

The exercise description: Given a ‘words.txt’ file (attached) containing a newline-delimited list of dictionary words, please implement a small command line application capable to return all ...
3
votes
1answer
57 views

Counting bigrams in XML files in a directory tree

I've written this code in Scala that extracts bigram statistics from the Reuters dataset. It puts the statistics in a Map[String, SortedMap[String, Int]]. For ...
3
votes
0answers
1k views

Apache spark compaction script to handle small files in hdfs

I have some use cases where I have small parquet files in Hadoop, say, 10-100 MB. I would to compact them so as to have files at least say 100 MB or 200 MB. The logic of my code is to: * find a ...
0
votes
1answer
51 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 ...
0
votes
1answer
52 views

Imperative and functional-programming solutions for Jumping on the Cloud challenge

Problem Statement Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud ...
3
votes
2answers
85 views

Count character 'a' in first n characters of indefinitely repeating string s

Problem statement: Lilah has a string, s, of lowercase English letters that she repeated infinitely many times. Given an integer, n, find and print the number of letter a's in the first n ...
4
votes
2answers
85 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 ...
3
votes
0answers
2k views

Adding columns in Spark dataframe based on rules

I have a dataframe df, which contains below data: ...
0
votes
2answers
64 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 ...
4
votes
1answer
52 views

Print a Connect 4 grid based on some rules for each cell

In a game of Connect4: we start with an empty grid two players place pieces X and O on the grid the first player to achieve 4 pieces in a line wins! this is a text based console game Here is the ...
-1
votes
1answer
44 views

Perform KS-test on Random Number Generator [closed]

I've just started practicing Functional Programming in Scala in some days. This is the code I used to perform KS-test on a random number generator. ...
0
votes
1answer
64 views

Is it possible to DRY these 2 methods of a tailrec Node Tree?

I have a Tree structure composed by Node class. I have to @tailrec method that are visiting up to the root. Those 2 methods ...
5
votes
1answer
78 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 ...
1
vote
1answer
53 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 - ...
2
votes
1answer
251 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 ...
3
votes
3answers
129 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
558 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
41 views

Page download with reattempts in Scala

I wrote the following code to deal with possible network problems. ...
2
votes
2answers
68 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
45 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 ...
1
vote
1answer
77 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
62 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 ...
4
votes
2answers
70 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
80 views

Find first repeating Char in String

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

Clockwise rotate a matrix in Scala

Given a matrix, clockwise rotate elements in it. Examples: ...
0
votes
1answer
83 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
129 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: ...
4
votes
1answer
439 views

Heap Sort in Scala

Trying to implement Heap Sort in Scala ...
6
votes
3answers
1k views

Connecting to RabbitMQ

I would like a review for this connection to RabbitMQ. I just developed it and seems to be working well, but I would like another set of eyes on it before putting this on the sever. ...
1
vote
2answers
60 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 ...
0
votes
1answer
30 views
2
votes
1answer
74 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
119 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 ...
7
votes
1answer
249 views

Functional K-way merge (Merge k sorted sequences)

I've implemented a functional k way-merge algorithm to merge k sorted sequences of any type T. I used scalaz's Heap since the Scala standard library does not provide an immutable heap. Any suggestions ...
0
votes
1answer
49 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
54 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
42 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. ...
3
votes
1answer
66 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. ...
1
vote
2answers
60 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
74 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 ...
4
votes
1answer
791 views

Determining the HTTP status code to return for a request

I have a piece of code that I would like to refactor but can't figure how to do it. This is a method from a Play controller where I have to verify many precondition and respond different status for ...
1
vote
0answers
520 views

Scalaz state monad exercise to model sampling of coloured balls

Here is a self imposed exercise and solution to learn Scalaz state monad. How can I refactor it to take it to the next level? So far I've only studied the state monad so would welcome suggestions on ...
0
votes
1answer
77 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 ...