While loop with double conditional and || logical operator in Swift - ios

I have a While Loop with a condition that limits it to repeat only 10 times, every time a cycle is repeated a constant D generates a random number in a range from 0 to 24, if D is 0, I change a variable dIsZero to true and prints the cycle where D is 0 for the first time.
var S = 0
var dIsZero = false
while S < 10 || dIsZero == false {
S += 1
let D = Int.random(in: 0...24)
if dIsZero == false && D == 0 {
dIsZero = true
print("D = 0 in a cycle \(S)/10")
}
}
My problem is that I want the While Loop can also end when D is 0 before the 10 cycles are completed. I already tried to put the logical operator || but it doesn't work and I get the following results:
10 cycles are exceeded until D is 0. For example: 84 cycles.
If D is 0 before 10 cycles, the loop does not stop until that 10 cycles
are reached.
I read about the logical operators and found the following:
The Swift logical operators && and || are left-associative, meaning
that compound expressions with multiple logical operators evaluate the
leftmost subexpression first.
What solution do you recommend?

You just need to break loop
while S < 10 {
S += 1
let D = Int.random(in: 0...24)
if D == 0 {
print("D = 0 in a cycle \(S)/10")
break
}
}

Related

Check for equality between more than 2 values in Lua

In Lua 5.3.3, equalities between two values work normally using the == operator.
However, equalities between more than two values don't work.
> print(1 == 1 == 1)
false
>
How can I check if more than 2 values are equal to one another in Lua?
You should compare 2 values at a time:
print((1 == 1) and (1 == 1))
If you have specific needs and if this approach is not acceptable, then you could also write a dedicated function:
function EqualsAll (Values, Number)
local Equals = true
local Index = 1
while (Equals and (Index <= #Values)) do
if Values[Index] == Number then
Index = Index + 1
else
Equals = false
end
end
return Equals
end
You could use it as following:
> EqualsAll({1, 2, 3}, 1)
false
> EqualsAll({1, 1, 1}, 1)
true

How to count the number of repetitions for every single number?

So I have a for loop that loops 100 times and each time it generates a random number from 1 to 100. For some statistics, I need to count how many times each number repeats. I have no idea how to make it other than manually.
One = 0
Two = 0
Three = 0
Four = 0
Five = 0
for i=1, 100 do
number = GetRandomNumber(1, 5, 1.5)
if number == 1 then
One = One + 1
elseif number == 2 then
Two = Two + 1
elseif number == 3 then
Three = Three + 1
elseif number == 4 then
Four = Four + 1
elseif number == 5 then
Five = Five + 1
end
end
This is how I currently count, but I don't want to manually type for every number. How can I make this simpler?
I do it as such:
number_counter, number = {}, 0
for i = 1, 100 do
number = GetRandomNumber(1, 5, 1.5)
if number_counter[number] then
number_counter[number] = number_counter[number] + 1
else
number_counter[number] = 1
end
end
This is, of course, assuming there are no half points (not sure what the 1.5 is for). Then you can just call number_counter[#] to see what its value is.

Lua #Table returning 0, despite Table containing 3 elements (tables)? [duplicate]

Sounds like a "let me google it for you" question, but somehow I can't find an answer. The Lua # operator only counts entries with integer keys, and so does table.getn:
tbl = {}
tbl["test"] = 47
tbl[1] = 48
print(#tbl, table.getn(tbl)) -- prints "1 1"
count = 0
for _ in pairs(tbl) do count = count + 1 end
print(count) -- prints "2"
How do I get the number of all entries without counting them?
You already have the solution in the question -- the only way is to iterate the whole table with pairs(..).
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
Also, notice that the "#" operator's definition is a bit more complicated than that. Let me illustrate that by taking this table:
t = {1,2,3}
t[5] = 1
t[9] = 1
According to the manual, any of 3, 5 and 9 are valid results for #t. The only sane way to use it is with arrays of one contiguous part without nil values.
You can set up a meta-table to track the number of entries, this may be faster than iteration if this information is a needed frequently.
The easiest way that I know of to get the number of entries in a table is with '#'. #tableName gets the number of entries as long as they are numbered:
tbl={
[1]
[2]
[3]
[4]
[5]
}
print(#tbl)--prints the highest number in the table: 5
Sadly, if they are not numbered, it won't work.
There's one way, but it might be disappointing: use an additional variable (or one of the table's field) for storing the count, and increase it every time you make an insertion.
count = 0
tbl = {}
tbl["test"] = 47
count = count + 1
tbl[1] = 48
count = count + 1
print(count) -- prints "2"
There's no other way, the # operator will only work on array-like tables with consecutive keys.
function GetTableLng(tbl)
local getN = 0
for n in pairs(tbl) do
getN = getN + 1
end
return getN
end
You're right. There are no other way to get length of table
You could use penlight library. This has a function size which gives the actual size of the table.
It has implemented many of the function that we may need while programming and missing in Lua.
Here is the sample for using it.
> tablex = require "pl.tablex"
> a = {}
> a[2] = 2
> a[3] = 3
> a['blah'] = 24
> #a
0
> tablex.size(a)
3
local function CountedTable(x)
assert(type(x) == 'table', 'bad parameter #1: must be table')
local new_t = {}
local mt = {}
-- `all` will represent the number of both
local all = 0
for k, v in pairs(x) do
all = all + 1
end
mt.__newindex = function(t, k, v)
if v == nil then
if rawget(x, k) ~= nil then
all = all - 1
end
else
if rawget(x, k) == nil then
all = all + 1
end
end
rawset(x, k, v)
end
mt.__index = function(t, k)
if k == 'totalCount' then return all
else return rawget(x, k) end
end
return setmetatable(new_t, mt)
end
local bar = CountedTable { x = 23, y = 43, z = 334, [true] = true }
assert(bar.totalCount == 4)
assert(bar.x == 23)
bar.x = nil
assert(bar.totalCount == 3)
bar.x = nil
assert(bar.totalCount == 3)
bar.x = 24
bar.x = 25
assert(bar.x == 25)
assert(bar.totalCount == 4)
I stumbled upon this thread and want to post another option. I'm using Luad generated from a block controller, but it essentially works by checking values in the table, then incrementing which value is being checked by 1. Eventually, the table will run out, and the value at that index will be Nil.
So subtract 1 from the index that returned a nil, and that's the size of the table.
I have a global Variable for TableSize that is set to the result of this count.
function Check_Table_Size()
local Count = 1
local CurrentVal = (CueNames[tonumber(Count)])
local repeating = true
print(Count)
while repeating == true do
if CurrentVal ~= nil then
Count = Count + 1
CurrentVal = CueNames[tonumber(Count)]
else
repeating = false
TableSize = Count - 1
end
end
print(TableSize)
end
seems when the elements of the table is added by insert method, getn will return correctly. Otherwise, we have to count all elements
mytable = {}
element1 = {version = 1.1}
element2 = {version = 1.2}
table.insert(mytable, element1)
table.insert(mytable, element2)
print(table.getn(mytable))
It will print 2 correctly

Check for divisibility by a three digit number in F#

I want a function isDiv to return true if a number x is divisible by some number i between 100 and 999, and x/i is between 100 and 999; I attempted to write the method below;
let isDiv x =
for i in 101..999 do
if x%i == 0 && x/i > 100 && x/i < 999
0
the F# Interactive panel tells me that this is incomplete - Where have I gone wrong?
Your if has no corresponding then and equality comparison is = not ==.
But if you use a for-loop you will have to use somewhere a mutable, you can use a while-loop but I would use instead Seq.exists:
let isDiv x = Seq.exists (fun i -> x%i = 0 && x/i > 100 && x/i < 999) {101..999}

How to get number of entries in a Lua table?

Sounds like a "let me google it for you" question, but somehow I can't find an answer. The Lua # operator only counts entries with integer keys, and so does table.getn:
tbl = {}
tbl["test"] = 47
tbl[1] = 48
print(#tbl, table.getn(tbl)) -- prints "1 1"
count = 0
for _ in pairs(tbl) do count = count + 1 end
print(count) -- prints "2"
How do I get the number of all entries without counting them?
You already have the solution in the question -- the only way is to iterate the whole table with pairs(..).
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
Also, notice that the "#" operator's definition is a bit more complicated than that. Let me illustrate that by taking this table:
t = {1,2,3}
t[5] = 1
t[9] = 1
According to the manual, any of 3, 5 and 9 are valid results for #t. The only sane way to use it is with arrays of one contiguous part without nil values.
You can set up a meta-table to track the number of entries, this may be faster than iteration if this information is a needed frequently.
The easiest way that I know of to get the number of entries in a table is with '#'. #tableName gets the number of entries as long as they are numbered:
tbl={
[1]
[2]
[3]
[4]
[5]
}
print(#tbl)--prints the highest number in the table: 5
Sadly, if they are not numbered, it won't work.
There's one way, but it might be disappointing: use an additional variable (or one of the table's field) for storing the count, and increase it every time you make an insertion.
count = 0
tbl = {}
tbl["test"] = 47
count = count + 1
tbl[1] = 48
count = count + 1
print(count) -- prints "2"
There's no other way, the # operator will only work on array-like tables with consecutive keys.
function GetTableLng(tbl)
local getN = 0
for n in pairs(tbl) do
getN = getN + 1
end
return getN
end
You're right. There are no other way to get length of table
You could use penlight library. This has a function size which gives the actual size of the table.
It has implemented many of the function that we may need while programming and missing in Lua.
Here is the sample for using it.
> tablex = require "pl.tablex"
> a = {}
> a[2] = 2
> a[3] = 3
> a['blah'] = 24
> #a
0
> tablex.size(a)
3
local function CountedTable(x)
assert(type(x) == 'table', 'bad parameter #1: must be table')
local new_t = {}
local mt = {}
-- `all` will represent the number of both
local all = 0
for k, v in pairs(x) do
all = all + 1
end
mt.__newindex = function(t, k, v)
if v == nil then
if rawget(x, k) ~= nil then
all = all - 1
end
else
if rawget(x, k) == nil then
all = all + 1
end
end
rawset(x, k, v)
end
mt.__index = function(t, k)
if k == 'totalCount' then return all
else return rawget(x, k) end
end
return setmetatable(new_t, mt)
end
local bar = CountedTable { x = 23, y = 43, z = 334, [true] = true }
assert(bar.totalCount == 4)
assert(bar.x == 23)
bar.x = nil
assert(bar.totalCount == 3)
bar.x = nil
assert(bar.totalCount == 3)
bar.x = 24
bar.x = 25
assert(bar.x == 25)
assert(bar.totalCount == 4)
I stumbled upon this thread and want to post another option. I'm using Luad generated from a block controller, but it essentially works by checking values in the table, then incrementing which value is being checked by 1. Eventually, the table will run out, and the value at that index will be Nil.
So subtract 1 from the index that returned a nil, and that's the size of the table.
I have a global Variable for TableSize that is set to the result of this count.
function Check_Table_Size()
local Count = 1
local CurrentVal = (CueNames[tonumber(Count)])
local repeating = true
print(Count)
while repeating == true do
if CurrentVal ~= nil then
Count = Count + 1
CurrentVal = CueNames[tonumber(Count)]
else
repeating = false
TableSize = Count - 1
end
end
print(TableSize)
end
seems when the elements of the table is added by insert method, getn will return correctly. Otherwise, we have to count all elements
mytable = {}
element1 = {version = 1.1}
element2 = {version = 1.2}
table.insert(mytable, element1)
table.insert(mytable, element2)
print(table.getn(mytable))
It will print 2 correctly

Resources