Scala error: ';' expected but '=' found [closed] - scala

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Can someone help me understand why the following code causes the above error?
The code is for rotation of a 2D array by 90 degrees.
def rotate90(block: Array[Array[Int]]) = {
var size: Int = block.size
var i: Int = 0
var j: Int = size - 1
while (j >= 0) {
while (i <= j) {
val a = block[j][i]
val b = block[j - i][j]
block[j][i] = b
block[j - i][j] = a
i = i + 1
}
j = j - 1
}
return block
}

def rotate90(block: Array[Array[Int]]) = {
val copy: Array[Array[Int]] = Array.ofDim[Int](block.length, block(0).length)
for (w <- 0 until block(0).length;
h <- 0 until block.length) {
copy(h).update(w, block(block(0).length - 1 - w)(h))
}
copy
}
Above is scala solution for you problem. Maybe you better understand how arrays in scala works.

Related

how can I add a percentage to an array of salaries with a for in loop using swift [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have done this using a repeat while loop and it ran. How can I do this with a for in loop
var salaries = [45000.0, 100000.0, 54000.0, 20000.0]
var i = 0
repeat {
salaries[i] = salaries[i] + (salaries[i] * 0.1)
i += 1
} while (i < salaries.count)
print(salaries)
You can do it using higher order function map
var salaries = [45000.0, 100000.0, 54000.0, 20000.0]
salaries = salaries.map {$0 + ($0 * 0.1)}
print(salaries)
also do it using for in loop
var salaries = [45000.0, 100000.0, 54000.0, 20000.0]
for (index,salary) in salaries.enumerated() {
salaries[index] = salary + (salary * 0.1)
}
print(salaries)

Conway sequence in swift [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Can anyone help me out with this sequece.
I have tried multiple times but not getting solution. Some of the solution looks like as follows
1
11
21
1211
11121
But i need following solution. I want to print this sequece in console.
1
11
21
1211
3112
132112
311322
var last = [1, 3, 2, 1, 1, 2]
var next = [Int]()
func getNext() {
if last.count == 0 { return }
let first = last.first!
let firstCount = last.filter{ $0 == first }.count
next.append(firstCount)
next.append(first)
last.removeAll { $0 == first }
getNext()
}
getNext()
print(next)//[3, 1, 1, 3, 2, 2]

How to find the average of highest three inputted numbers [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
The goal here is to try and find the average of the three highest inputted grades, when four numbers are put in. I think I've come close but I'm running into a syntax error and I can't seem to find what is causing it.
I've got almost no experience with python so excuse the sloppy code but this is what I've gotten so far.
def highestThreeAvg(grade1 : float, grade2 : float, grade3 : float, grade4 : float) -> float:
"""Given the grades, grade1, grade 2, grade3, grade4, returns the average
of the three highest grades"""
return (((grade1 + grade2 + grade3 + grade4) - lowestGrade) / 3
grade1 = float(input("Enter first grade: "))
grade2 = float(input("Enter second grade: "))
grade3 = float(input("Enter third grade: "))
grade4 = float(input("Enter fourth grade: "))
def lowest(grade1, grade2, grade3, grade4):
if (grade1 < grade2) and (grade1 < grade3) and (grade1 < grade4):
lowestGrade = grade1
elif (grade2 < grade1) and (grade2 < grade3) and (grade2 < grade4):
lowestGrade = grade2
elif (grade3 < grade1) and (grade3 <grade2) and (grade3 < grade4):
lowestGrade = grade3
else:
lowestGrade = grade4
print(lowestGrade)
lowest(grade1, grade2, grade3, grade4)
print(highestThreeAvg)
The syntax error that I'm running into is at the start of line 6
grade1 = float(input("Enter first grade: "))
^
SyntaxError: invalid syntax
If you see any other issues with my code or where I can improve it feel free to let me know.
Placing an additional close bracket, as shown below, at the end of line 4 should solve the syntax issue.
return (((grade1 + grade2 + grade3 + grade4) - lowestGrade) / 3)
It seems that you have 3 (, but only 2 ) in the previous line !

Functional version of a typical nested while loop

I hope this question may please functional programming lovers. Could I ask for a way to translate the following fragment of code to a pure functional implementation in Scala with good balance between readability and execution speed?
Description: for each elements in a sequence, produce a sub-sequence contains the elements that comes after the current elements (including itself) with a distance smaller than a given threshold. Once the threshold is crossed, it is not necessary to process the remaining elements
def getGroupsOfElements(input : Seq[Element]) : Seq[Seq[Element]] = {
val maxDistance = 10 // put any number you may
var outerSequence = Seq.empty[Seq[Element]]
for (index <- 0 until input.length) {
var anotherIndex = index + 1
var distance = input(index) - input(anotherIndex) // let assume a separate function for computing the distance
var innerSequence = Seq(input(index))
while (distance < maxDistance && anotherIndex < (input.length - 1)) {
innerSequence = innerSequence ++ Seq(input(anotherIndex))
anotherIndex = anotherIndex + 1
distance = input(index) - input(anotherIndex)
}
outerSequence = outerSequence ++ Seq(innerSequence)
}
outerSequence
}
You know, this would be a ton easier if you added a description of what you're trying to accomplish along with the code.
Anyway, here's something that might get close to what you want.
def getGroupsOfElements(input: Seq[Element]): Seq[Seq[Element]] =
input.tails.map(x => x.takeWhile(y => distance(x.head,y) < maxDistance)).toSeq

Scala - Project euler #8

I'm currently learning Scala and I'm trying to solve some of the Euler Challenges with it.
I have some problems getting the response to the 8th challenge and I really don't know where is my bug.
object Product{
def main(args: Array[String]): Unit = {
var s = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
var len = 13;
var bestSet = s.substring(0,len);
var currentSet = "";
var i = 0;
var compare = 0;
for(i <- 1 until s.length - len){
currentSet = s.substring(i,i+len);
compare = compareBlocks(bestSet,currentSet);
if(compare == 1) bestSet = currentSet;
}
println(v1);
var result = 1L;
var c = ' ';
for(c <- v1.toCharArray){
result = result * c.asDigit.toLong;
}
println(result);
}
def compareBlocks(block1: String, block2: String): Int = {
var i = 0;
var v1 = 0;
var v2 = 0;
if((block1 contains "0") && !(block2 contains "0")) return 1;
if(!(block1 contains "0") && (block2 contains "0")) return -1;
if((block1 contains "0") && (block2 contains "0")) return 0;
var chars = block1.toCharArray;
for(i <- 0 until chars.length){
v1 = v1 + chars(i).asDigit;
}
chars = block2.toCharArray;
for(i <- 0 until chars.length)
{
v2 = v2 + chars(i).asDigit;
}
if(v1 < v2) return 1;
if(v2 < v1) return -1;
return 0;
}
}
My result is:
9753697817977 <- Digit sequence
8821658160 <- Multiplication
Using the Euler Project to challenge yourself and learn a new language is a pretty good idea, but just coming up with the correct answer doesn't mean that you're using the language well.
It's obvious from your code that you have yet to learn idiomatic Scala. Would it surprise you to learn that the desired product can be calculated from the 100-character input string with just one line of code? That one line of code will:
turn each input character into a digit (Int)
slide a fixed size (13-digit) window over all the digits
multiply all the digits within each window
select the maximum from all those products
There's a handy little web site that has solved Euler challenges in Scala. I recommend that every time you solve an Euler problem, compare your code with what's found on that site. (But be careful. It's too easy to look ahead at solutions that you haven't tackled yet.)