I am new to Scala and am implementing a Sudoku solver. I have a method which returns the set of all possible values a particular element int the grid can take and it works. However, I think that there is a much better way to do this. The problem arises when I try to check the values of other elements in the same block. Is there any other way (than the one shown below) I can find a relationship between the row, column and block to result in cleaner code?
Note that r and c are the row and column indices, respectively, and are given as parameters to the function.
val i=
if(r==0|r==1|r==2){
if(c==0||c==1||c==2)
0
else if(c==3|c==4|c==5)
1
else
2
} else if (r==3|r==4|r==5){
if(c==0||c==1||c==2)
3
else if(c==3||c==4||c==5)
4
else
5
} else {
if(c==0||c==1||c==2)
6
else if(c==3||c==4||c==5)
7
else
8
}
def i(r:Int,c:Int) = r/3*3 + c/3
Although you'll probably want to find a better name/ add comments, it's not really the most intuitive function...
Related
I have an array of 1 x 400, where all element values are above 1500. However, I have some elements that have values<50 which are wrong measures and I would like to have the mean of the elements before and after the wrong measured data points and replace it in the main array.
For instance, element number 17 is below 50 so I want to take the mean of elements 16 and 18 and replace element 17 with the new mean.
Can someone help me, please? many thanks in advance.
No language is specified in the question, but for Python you could work with List Comprehension:
# array with 400 values, some of which are incorrect
arr = [...]
arr = [arr[i] if arr[i] >= 50 else (arr[i-1]+arr[i+1])/2 for i in range(len(arr))]
That is, if arr[i] is less than 50, it'll be replaced by the average value of the element before and after it. There are two issues with this approach.
If i is the first or last element, then one of the two values will be undefined, and no mean can be obtained. This can be fixed by just using the value of the available neighbour, as specified below
If two values in a row are very low, the leftmost one will use the rightmost one to calculate its value, which will result in a very low value. This is a problem that may not occur for you in practice, but it is an inherent result of the way you wish to recalculate values, and you might want to keep it in mind.
Improved version, keeping in mind the edge cases:
# don't alter the first and last item, even if they're low
arr = [arr[i] if arr[i] >= 50 or i == 0 or i+1 == len(arr) else (arr[i-1]+arr[i+1])/2 for i in range(len(arr))]
# replace the first and last element if needed
if arr[0] < 50:
arr[0] = arr[1]
if arr[len(arr)-1] < 50:
arr[len(arr)-1] = arr[len(arr)-2]
I hope this answer was useful for you, even if you intend to use another language or framework than python.
I am doing some of CodeWars challenges recently and I've got a problem with this one.
"You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N. Write a method that takes the array as an argument and returns this "outlier" N."
I've looked at some solutions, that are already on our website, but I want to solve the problem using my own approach.
The main problem in my code, seems to be that it ignores negative numbers even though I've implemented Math.abs() method in scala.
If you have an idea how to get around it, that is more than welcome.
Thanks a lot
object Parity {
var even = 0
var odd = 0
var result = 0
def findOutlier(integers: List[Int]): Int = {
for (y <- 0 until integers.length) {
if (Math.abs(integers(y)) % 2 == 0)
even += 1
else
odd += 1
}
if (even == 1) {
for (y <- 0 until integers.length) {
if (Math.abs(integers(y)) % 2 == 0)
result = integers(y)
}
} else {
for (y <- 0 until integers.length) {
if (Math.abs(integers(y)) % 2 != 0)
result = integers(y)
}
}
result
}
Your code handles negative numbers just fine. The problem is that you rely on mutable sate, which leaks between runs of your code. Your code behaves as follows:
val l = List(1,3,5,6,7)
println(Parity.findOutlier(l)) //6
println(Parity.findOutlier(l)) //7
println(Parity.findOutlier(l)) //7
The first run is correct. However, when you run it the second time, even, odd, and result all have the values from your previous run still in them. If you define them inside of your findOutlier method instead of in the Parity object, then your code gives correct results.
Additionally, I highly recommend reading over the methods available to a Scala List. You should almost never need to loop through a List like that, and there are a number of much more concise solutions to the problem. Mutable var's are also a pretty big red flag in Scala code, as are excessive if statements.
I'm writing a program to play blackjack and one of the functions calculates the score. It takes in an input which is a structure array of cards and one of the attributes is value (for an ace the value is 11). My function is supposed to determine if the total of the values is over 21 and if 1 of the cards is an ace, then the ace's value is changed to 1. Can anyone help me figure this out please?
for index=1:length(input)
if(input(input).value == 11)
input(index).value = 1;
end;
end;
You're not actually summing the cards in your original snippet. You also seem to have a typo in input(input), I think this should be input(index). If you wanted to do it with a for loop like you have, you'd do something like this:
total = 0;
for index=1:length(input)
if(input(index).value == 11)
input(index).value = 1;
end;
total = total + input(index);
end;
The more MATLAB way of doing things would be to avoid loops by using the sum in-built command.
As simple as in title. I have nx1 sized vector p. I'm interested in the maximum value of r = p/foo - floor(p/foo), with foo being a scalar, so I just call:
max_value = max(p/foo-floor(p/foo))
How can I get which value of p gave out max_value?
I thought about calling:
[max_value, max_index] = max(p/foo-floor(p/foo))
but soon I realised that max_index is pretty useless. I'm sorry asking this, real beginner here.
Having dropped the issue to pieces, I realized there's no unique corrispondence between values p and values in my related vector p/foo-floor(p/foo), so there's a logical issue rather than a language one.
However, given my input data, I know that the solution is unique. How can I fix this?
I ended up doing:
result = p(p/foo-floor(p/foo) == max(p/foo-floor(p/foo)))
Looks terrible, so if you know any other way...
Once you have the index, use it:
result = p(max_index)
You can create a new vector with your lets say "transformed" values:
p2 = (p/foo-floor(p/foo))
and then just use find to find the max values on p2:
max_index = find(p2 == max(p2))
that will return the index or indices of p2 with the max value of that operation, and finally just lookup the original value in p
p(max_index)
in 1 line, this is:
p(find((p/foo-floor(p/foo) == max((p/foo-floor(p/foo))))))
which is basically the same thing you did in the end :)
I have this code, but there must be a better efficient to write it:
rt= RealTrans;
rtsize=size(rt);
rtrows=rtsize(1);
Relative_Axis_Moves=[rt(1,1) rt(1,2) rt(1,3) rt(1,4) rt(1,5);
rt(2:rtrows,1)-rt(1:rtrows-1,1) rt(2:rtrows,2)-rt(1:rtrows-1,2)
rt(2:rtrows,3)-rt(1:rtrows-1,3) rt(2:rtrows,4)-rt(1:rtrows-1,4)
rt(2:rtrows,5)-rt(1:rtrows-1,5)];
There are two rows in the matrix. The first row ends at rt(1,5).
I also have the following code:
p1size=size(p1);
p1rows=p1size(1);
flank_edge_point=[0 0 0; p1(2:p1rows,2)-p1(1:p1rows-1,2) xy(2:p1rows,1)-xy(1:p1rows-1,1) xy(2:p1rows,2)-xy(1:p1rows-1,2); 0 0 xy(p1rows,2)];
How do i get xy(p1rows,2) value in matlab without p1rows?
I also have the code below which relies on the number of rows:
RAMrow=size(Relative_Axis_Moves);
RAMrow=RAMrow(1);
for i=1:RAMrow
L(i)= norm(Relative_Axis_Moves(i,:));
end
L=L';
L(RAMrow+1)= 0;
Any way to write this code more succinctly and efficiently would be greatly appreciated.
Most likely, there will be more than two rows in Relative_Axis_Moves, since the differences in the second row evaluate to arrays.
Anyway, a compact way of writing this is
Relative_Axis_Moves = [RealTrans(1,1:5);diff(RealTrans(:,1:5),1,1)];