Reduce input to zero by using given variables - reduce

I hope you can help me with my request.
I have an integer input from a client. Lets say 250. The goal is to calculate the input to exactly 0 by only using 3 variables. x = 30, y = 50 and z = 100 and output how often i used each of them.
The given rules are:
I have to use as less of them as possible. means, first the z's then the y's then the x's. So i have to use as much as possible of the variables with greater values.
Im allowed to use a maximum of 3 of each of them.
I have to count how often i used each of them.
If a combination is not possible, lets say 70 ( 70 - y = 20, 70 - x - x = 10 ), give a message to the client, "Invalid input!".
Few examples:
Input = 250
Output:
z = 2
y = 1
####################
Input = 170
Output: Invalid input!
Because i can only calculate to zero by using 4 x's and 1 y. See rule 2.
Other solutions are not possible.
i.e. Input - z - y = 20 => error
Input - y - y - y = also 20 => error
####################
Input = 90
Output:
x = 3
####################
Input = 120
Output: Invalid input!
Input - z = 20 => error
Input - 50 - 50 = 20 => error
Input - x - x - x - x => See rule 2.
####################
and so on....
And how it could be extended if a further value is added to the 3 variables. i.e. 40
So if thats the case it should be somehowe dynamically.
I tried to explain that as much as possible. If you miss infos, just let me know.
Thank you.
dagogi

Related

How to deduce left-hand side matrix from vector?

Suppose I have the following script, which constructs a symbolic array, A_known, and a symbolic vector x, and performs a matrix multiplication.
clc; clearvars
try
pkg load symbolic
catch
error('Symbolic package not available!');
end
syms V_l k s0 s_mean
N = 3;
% Generate left-hand-side square matrix
A_known = sym(zeros(N));
for hI = 1:N
A_known(hI, 1:hI) = exp(-(hI:-1:1)*k);
end
A_known = A_known./V_l;
% Generate x vector
x = sym('x', [N 1]);
x(1) = x(1) + s0*V_l;
% Matrix multiplication to give b vector
b = A_known*x
Suppose A_known was actually unknown. Is there a way to deduce it from b and x? If so, how?
Til now, I only had the case where x was unknown, which normally can be solved via x = b \ A.
Mathematically, it is possible to get a solution, but it actually has infinite solutions.
Example
A = magic(5);
x = (1:5)';
b = A*x;
A_sol = b*pinv(x);
which has
>> A
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
but solves A as A_sol like
>> A_sol
A_sol =
3.1818 6.3636 9.5455 12.7273 15.9091
3.4545 6.9091 10.3636 13.8182 17.2727
4.4545 8.9091 13.3636 17.8182 22.2727
3.4545 6.9091 10.3636 13.8182 17.2727
3.1818 6.3636 9.5455 12.7273 15.9091

idl radius of each pixel

I'm completely new to coding and have almost no idea what I'm doing. Currently I'm trying to find the radius of each pixel from the center of a galaxy in a fits file. I was told to try this by creating a blank array the same size as the fits file and I'm trying to use a for loop for each x value and another for each y. So far, I have the array and have attempted to create the for loops.
xcenter =249.8
ycenter =250.0
d=fltarr(500,500)
for i=0,499 do begin
d=d(i-xcenter,*)
endfor
for j=0,499 do begin
d=d(j-ycenter,*)
endfor
I know this look awful and I obviously have no idea what I'm doing. So if anyone can offer any help at all I'd be grateful.
Let's look at a simpler version first. Suppose you have 10 points on a line, and you want to calculate the distance of each point from some xref. IDL supports vector math, so you can do this:
xref = 5.4
x = dindgen(10)
distance = abs(x - xref)
I have used the DINDGEN command here, and you can look up the help. Now in a 2d case, you want 2-d arrays: one will be a 500 * 500 array containing the X coordinate of each pixel, the other containing the Y coordinate. So the arrays will have to be of the form,
0 1 2 3 ...
0 1 2 3 ...
0 1 2 3 ...
and
0 0 0 0 ...
1 1 1 1 ...
2 2 2 2 ...
We can generate them using the # operator. Note that IDL counts from 0 by default.
just_one = replicate(1d, 500) ; contains 1 1 1 1 ...
one_500 = dindgen(500) ; contains 0.0 1.0 2.0 ...
x = just_one # one_500
y = one_500 # just_one
Now you can calculate the distance as usual, d = sqrt(xx + yy), but using vector math again:
distance = sqrt( (x - xref) ^ 2 + (y - yref) ^ 2 )
This is a 500x500 array, which contains the distance of each pixel from your xref, yref points.

Outputs of a program

I am new to programming and I would like to know how to solve questions like this. I was told to expect questions like this on the exam. Can someone please tell me how I would go about solving something like this? Thanks.
x = 0
for num in range(5):
if num % 2 == 0:
x = x + 2
else:
x = x + 1
print(x)
You need to work on a skill which is to "be the compiler", in the sense that you should be able to run code in your head. Step through line by line and make sure you know what is happening. In you code example, you have
for num in range(5) means you will be iterating with num being 0,1,2,3 and 4. Inside the for loop, the if statement num % 2 == 0 is true when num/2 does not have a remainder (how % mods work). So if the number is divisible by 2, x = x+2 will execute. The only numbers divisible by 2 from the for loop are 0,2 and 4. so x=x+2 will execute twice. The else statement x = x +1 runs for all other numbers (1,3) which will execute 2 times.
Stepping through the for loop:
num = 0 //x=x+2, x is now 2
num = 1 //x=x+1, x is now 3, print(x) prints 3
num = 2 //x=x+2, x is now 5
num = 3 //x=x+1, x is now 6, print(x) prints 6
num = 4 //x+x+2, x is now 8
Therefore the answer is that 3 and 6 will be printed
In my opinion,
Whatever language you are using, you need to learn some common elements of the modern programming languages, such as flow-control (if...else in your case), loop(for, in your case)
Some common used functions, in your case, you need to what does range do in Python,
docs.python.org is a good place for you.
As you are new to programming, you can go with the flow in you mind or draw it on the paper.
Using x to store our final result
loop through every item in [0, 1, 2, 3, 4] <- range(5)
a. if
the number is divisible by 2
then increase x by adding 2 to it.
b. else
increase x by adding 1 and print it out
So the result would be :
3
6

octave: using find() on cell array {} subscript and assigning it to another cell array

This is an example in Section 6.3.1 Comma Separated Lists Generated from Cell Arrays of the Octave documentation (I browsed it through the doc command on the Octave prompt) which I don't quite understand.
in{1} = [10, 20, 30, 40, 50, 60, 70, 80, 90];
in{2} = inf;
in{3} = "last";
in{4} = "first";
out = cell(4, 1);
[out{1:3}] = find(in{1 : 3}); % line which I do not understand
So at the end of this section, we have in looking like:
in =
{
[1,1] =
10 20 30 40 50 60 70 80 90
[1,2] = Inf
[1,3] = last
[1,4] = first
}
and out looking like:
out =
{
[1,1] =
1 1 1 1 1 1 1 1 1
[2,1] =
1 2 3 4 5 6 7 8 9
[3,1] =
10 20 30 40 50 60 70 80 90
[4,1] = [](0x0)
}
Here, find is called with 3 output parameters (forgive me if I'm wrong on calling them output parameters, I am pretty new to Octave) from [out{1:3}], which represents the first 3 empty cells of the cell array out.
When I run find(in{1 : 3}) with 3 output parameters, as in:
[i,j,k] = find(in{1 : 3})
I get:
i = 1 1 1 1 1 1 1 1 1
j = 1 2 3 4 5 6 7 8 9
k = 10 20 30 40 50 60 70 80 90
which kind of explains why out looks like it does, but when I execute in{1:3}, I get:
ans = 10 20 30 40 50 60 70 80 90
ans = Inf
ans = last
which are the 1st to 3rd elements of the in cell array.
My question is: Why does find(in{1 : 3}) drop off the 2nd and 3rd entries in the comma separated list for in{1 : 3}?
Thank you.
The documentation for find should help you answer your question:
When called with 3 output arguments, find returns the row and column indices of non-zero elements (that's your i and j) and a vector containing the non-zero values (that's your k). That explains the 3 output arguments, but not why it only considers in{1}. To answer that you need to look at what happens when you pass 3 input arguments to find as in find (x, n, direction):
If three inputs are given, direction should be one of "first" or
"last", requesting only the first or last n indices, respectively.
However, the indices are always returned in ascending order.
so in{1} is your x (your data if you want), in{2} is how many indices find should consider (all of them in your case since in{2} = Inf) and {in3}is whether find should find the first or last indices of the vector in{1} (last in your case).

How can I prove the correctness of the following algorithm?

Consider the following algorithm min which takes lists x,y as parameters and returns the zth smallest element in union of x and y.
Pre conditions: X and Y are sorted lists of ints in increasing order and they are disjoint.
Notice that its pseudo code, so indexing starts with 1 not 0.
Min(x,y,z):
if z = 1:
return(min(x[1]; y[1]))
if z = 2:
if x[1] < y[1]:
return(min(x[2],y[1]))
else:
return(min(x[1], y[2]))
q = Ceiling(z/2) //round up z/2
if x[q] < y[z-q + 1]:
return(Min(x[q:z], y[1:(z - q + 1)], (z-q +1)))
else:
return(Min(x[1:q], B[(z -q + 1):z], q))
I can prove that it terminates, because z keeps decreasing by 2 and will eventually reach one of the base cases but I cant prove the partial correctness.
Your code is not correct.
Consider the following input:
x = [0,1]
y = [2]
z = 3
You then get q = 2 and, in the if clause that follows, access y[z-q+1], i.e. y[2]. This is an array bounds violation.