How to calculate height based on item count in swift - swift

How do I archive this result using a simple mathematical formula.
I have an initial offset = 100, and initial count = 0, that i want to increase the offset based on count value. I tried using the below code but it doesn't function correctly.
Example
When count is 0 to 3, then offset should be 100.
When count is
4 to 6, then offset should be 200.
When count is 7 to 9,
then offset should be 300.
When count is 10 to 12, then offset
should be 400.
Attempt
func getHeight(count: Int) ->CGFloat {
var index = 0
for i in 0..<count{
if(i % 2 != 0){
index += 1
}
}
return CGFloat(100 * index)
//return CGFloat(count + 3 / 9 * 100)
}
Testing
print("0 to 3 = \(self.getHeight(count: 0)), expected = 100")
print("0 to 3 = \(self.getHeight(count: 2)), expected = 100")
print("4 to 6 = \(self.getHeight(count: 4)), expected = 200")
print("7 to 9 = \(self.getHeight(count: 7)), expected = 300")
print("10 to 12 = \(self.getHeight(count: 12)), expected = 400")
Results
0 to 3 = 0.0, expected = 100
0 to 3 = 100.0, expected = 100
4 to 6 = 200.0, expected = 200
7 to 9 = 300.0, expected = 300
10 to 12 = 600.0, expected = 400

Formula with integer division:
let cnt = count != 0 ? count : 1
result = 100 * ((cnt + 2) / 3)

Related

Round a number to nearest 100 in flutter

How do I round down number nearest 100 with dart? e.g;
43 -> 100
153 -> 200
123 -> 200
450 -> 500
399 -> 400
1234 -> 1300
3456 -> 3500
int calculateNumber(int number) {
int a = number % 100;
if (a > 0) {
return (number ~/ 100) * 100 + 100;
}
return number;
}
Approach 1
Can Use integer division, which truncates the decimal portion of the quotient.
int result = ((number + 99) / 100 ) * 100;
Approach 2
(int) (Math.ceil(number/100.0))*100

Confusing Get Set behaviour

I am trying to understand Computed Properties mostly I have understood the concept but one output is confusing me
struct SomePrices {
var eighth: Double
var quarter: Double
var half: Double
var zip: Double {
get {
return half * 2 - 20
}
set {
eighth = newValue / 8 + 15
quarter = newValue / 4 + 10
half = newValue / 2 + 5
}
}
}
var gdp = SomePrices(eighth: 37.0, quarter: 73.0, half: 123.0)
gdp.eighth // 37
gdp.quarter // 73
gdp.half // 123
gdp.zip // 226
gdp.zip = 300
gdp.eighth // 52.5
gdp.quarter // 85
gdp.half // 155
gdp.zip // 290
Been trying to understand how did I get 290 when gdp.zip = 300
You set zip to 300 so half becomes (300 / 2 + 5) = 155.
half = newValue / 2 + 5
Then you get zip which is (155 * 2 - 20) = 290.
return half * 2 - 20

Create multiplication table with swift

im a beginner here. Ive been stuck on a problem for some time now. Practicing in playground and i need to make a multiplication table.
basically, if i input 3, i want the table to read
1 2 3
2 4 6
3 6 9
Im confused on the loop for this though. Any help please?
Code so far
var x = 3
var width = 1
for x in 1...x {
for width in 1...width {
print(x, width*2)
}
}
this code prints
1 2
2 2
3 2
You could do it like this.
func multiplicationTable(till limit: Int) {
for i in 1...limit {
for j in 1...limit {
print(i * j, terminator: "\t")
}
print("")
}
}
multiplcationTable(till: 5)
Output
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
If conciseness is paramount:
let x = 3
let range = 1...x
for i in range {
print(range.map { String(i * $0) }.joined(separator: "\t"))
}
You can store the multiplication table in a 2D array of Ints. First, you can populate the first row and first column with numbers from 1 to the size of the multiplication table. Then for each element in the remaining empty positions, you just need to multiply the first element of the same row and the first element of the same column that the element resides in.
func multiplicationTable(ofSize n:Int) -> [[Int]] {
var table = Array(repeating: Array(repeating: 0, count: n), count: n)
table[0] = Array(1...n)
for i in 1..<n {
table[i][0] = i+1
for j in 1..<n {
table[i][j] = table[i][0] * table[0][j]
}
}
return table
}
multiplicationTable(ofSize: 5).forEach { row in
print(row,"\n")
}
Output:
[1, 2, 3, 4, 5]
[2, 4, 6, 8, 10]
[3, 6, 9, 12, 15]
[4, 8, 12, 16, 20]
[5, 10, 15, 20, 25]

MATLAB compute length of an integer

I'm trying to compute the length of an integer.
For example:
a = 1.1234; b = 33; c = 100; d = -222;
e = lengthint([a,b,c,d])
Expected output:
e = 1 2 3 3
I tried using this:
e = max(ceil(log10(abs([a,b,c,d]))),1)
but this is the output:
e = 1 2 2 3
So there is a problem with numbers that are multiples of 10.
You can do something like this -
A = [a,b,c,d]
lens = floor(log10(abs(A)))+1
lens(lens<0) = 0 %// Assuming that 0.xx numbers to have zero lengths
Sample runs:
Case #1:
>> A = [0.00001234, 1.1234, 33, 10, -222];
>> lens = floor(log10(abs(A)))+1;
>> lens(lens<0) = 0
lens =
0 1 2 2 3
Case #2:
>> A = [-0.00001234, 1.1234 33, 10, -222, 0];
>> lens = floor(log10(abs(A)))+1;
>> lens(lens<0) = 0
lens =
0 1 2 2 3 0
Another option would be to convert them to strings and check the length:
cellfun(#(x)length(num2str(abs(fix(x)))),{a,b,c,d});
the only complication is that you need cells to keep your strings separate.
Output from #Divakar's example input:
>> a1 = 0.00001234; a2 = 1.1234; b = 33; c = 100; d = -222;
>> cellfun(#(x)length(num2str(abs(fix(x)))),{a1,a2,b,c,d})
ans =
1 1 2 3 3
so it will obviously not give 0 for the 1e-5 case.

Simplify the looping under two sets of conditions

I want to establish a pair of indices =[row col] where
row = 4 * (n-1) + i and col = 4 * (m-1) + i
Explanation for i, m and n:
For n = 1 and m = 2, 3, 4, loop i = 1 : 4.
For n = 2 and m = 1, loop i = 1 : 4.
For n = 3 and m = 5, loop i = 1 : 4.
The outcome should be:
row = [1 1 1 2 2 2 3 3 3 4 4 4 5 6 7 8 9 10 11 12]
col = [5 9 13 6 10 14 7 11 15 8 12 16 1 2 3 4 17 18 19 20]
That is, I want to establish pairs of indices under different sets of n-m conditions.
My trial:
row = []; col = [];
n = 1;
for i = 1 : 4
for m = [2 3 4]
row = [row 4 * (n - 1) + i];
col = [col 4 * (m - 1) + i];
end
end
n = 2; m = 1;
for i = 1 : 4
row = [row 4 * (n - 1) + i];
col = [col 4 * (m - 1) + i];
end
n= 3; m = 5;
for i = 1 : 4
row = [row 4 * (n - 1) + i];
col = [col 4 * (m - 1) + i];
end
This works but indeed I have many n-m conditions and the looping for i = 1 : 4 appeared repeatedly which seems that can be simplified.
May I know if there are any elegant solutions to finish my objective?
I appreciate for your help.
You can use a bsxfun based solution for all those three cases -
ii = 1:4
row = reshape(bsxfun(#(A,B) 4 * (B-1) + A,ii,n'),1,[]) %//'
col = reshape(bsxfun(#(A,B) 4 * (B-1) + A,ii,m'),1,[]) %//'
The inputs would be as listed next.
Case #1:
m = [2, 3, 4]
n = ones(1,numel(m))
Case #2:
n = 2
m = 1
Case #3:
n = 3
m = 5
I would create a Matrix with all parameters, then apply the math once:
M=[...n m i
ones(3,1) (2:4).' (1:3).';...
2*ones(4,1) ones(4,1) (1:4).';...
3*ones(4,1) 5*ones(4,1) (1:4).';...
];
row = (4 * (M(:,1) - 1) + M(:,3)).';
col = (4 * (M(:,2) - 1) + M(:,3)).';
%alternative:
%index=(4 * (M(:,[1:2]) - 1) + M(:,[3,3])).'