I'm currently working with the "Charts" pod.
My app shows a bar chart of athletes results, with:
X Axis: number of reps / time / rounds / weight
Y Axis: number of athletes
I would like to gather the number of reps in different groups.
Something that would be like: 10 < x < 20, 20 < x < 30, etc...
Rather than the real total of reps.
Something like that:
What would be the best way to do so? I though about some approaches:
Round the number of reps to transform 19 and 15 to 10 and 10 for example (for the 10 < x < 20 category)
The problem with that method is that I don't know if I can do the same for the "time (seconds)
Create a new array with dictionnaries inside, something like:
[["10-20": 15, 17, 19], ["20-30": 21, 22, 22, 24], etc..]
But I don't know how to achieve that...
What would be the best way?
You can use Dictionary's init(grouping:by:) initializer to create such a dictionary:
let array = [15,17,19,22,24,24,27]
let dict = Dictionary(grouping: array, by: { $0 / 10 })
// dict is [2: [22, 24, 24, 27], 1: [15, 17, 19]]
If I understood you correctly, you probably have a bunch of Athletes and they have a reps property. You can group by $0.reps / 10 instead:
Dictionary(grouping: athletes, by: { $0.reps / 10 })
And then map the keys and values to this:
.map { ("\($0.key * 10) - \(($0.key + 1) * 10)", $0.value.count) }
// now you have this:
// [("20 - 30", 4), ("10 - 20", 3)]
Related
i have a problem with my Script if i try to loop thought my list the output is completly random shuffled
minimal Code:
list = {
numbers = {
number1 = 1,
number2 = 2,
number3 = 3,
number4 = 4,
number5 = 5,
number6 = 6,
number7 = 7,
}
}
for k, numbers in pairs(list) do
for k, number in pairs(numbers) do
print(number)
end
end
output:
5
7
2
3
4
6
1
the only fix i figured out is to remove the variables number1 to number7
and just enter the numbers
Lua tables do not have an order.
In addition to that you're using pairs which internally uses next.
From the Lua manual:
The order in which the indices are enumerated is not specified, even
for numeric indices. (To traverse a table in numerical order, use a
numerical for.)
In your case the keys have a numeric component so you could simply create them in a numeric loop.
local numbers = {
number1 = 1,
number2 = 2,
number3 = 3,
number4 = 4,
number5 = 5,
number6 = 6,
number7 = 7,
}
for i = 1, 7 do
print(numbers["number"..i])
end
For other non-numeric keys you would have to use a second table that lists the keys in an ordered sequence:
local numbers = { bob = 1, bill = 3, john = 2}
local orderedKeys = { "bob", "john", "bill"}
for k,v in ipairs(orderedKeys) do
print(numbers[v])
end
A numeric loop will always work for any integer keys.
local numbers = {
[0] = 0,
[5] = 5,
[3] = 3,
[1] = 0,
}
for i = 0, 5 do
if numbers[i] then
print(numbers[i])
end
end
Read through this carefully:
A table with exactly one border is called a sequence. For instance,
the table {10, 20, 30, 40, 50} is a sequence, as it has only one
border (5). The table {10, 20, 30, nil, 50} has two borders (3 and 5),
and therefore it is not a sequence. (The nil at index 4 is called a
hole.) The table {nil, 20, 30, nil, nil, 60, nil} has three borders
(0, 3, and 6) and three holes (at indices 1, 4, and 5), so it is not a
sequence, too. The table {} is a sequence with border 0. Note that
non-natural keys do not interfere with whether a table is a sequence.
Things like ipairs, the length operator #, table.sort, table.concat and others only work with sequences.
Keys that do not contribute to the sequence are ignored by those functions. You can only loop over all keys of a table with next or pairs respectively. But then order is not guaranteed.
I'm currently working with the "Charts" pod.
My app shows a bar chart of athletes results, with:
X Axis: number of reps / time / rounds / weight
Y Axis: number of athletes
I would like to gather the number of reps in different groups.
Something that would be like: 10 < x < 20, 20 < x < 30, etc... Rather than the real total of reps / time / whatever.
Something like that:
Depending on the difference between the Max() and Min() value, I want to change the granularity.
This is what I have right now:
let resultTime = [2458, 3500, 3600] // Fake data -> 41min, 58min, 60 min
let dict = Dictionary(grouping: resultTime, by: { $0 / 60 }).map { ($0.key, $0.value.count) }
This way, I can have a dictionary grouped by minute. I got this:
[(58, 1), (40, 1), (60, 1)]
I would like to group these data 2 by 2, to have something like that instead:
[(60, 2), (40, 1)]
Is there a possibly to play with the granularity with groupBy?
As #Alexander said, the solution is to round the by function
You can do that with this little function:
func roundWithCoef(_ x : Double, coef: Int) -> Int {
return coef * Int(round(x / Double(coef)))
}
let dict = Dictionary(grouping: resultTime, by: { roundWithCoef(Double($0 / 60), coef: 4) }).map { ($0.key, $0.value.count) }
This way you can play with the coef number
I need to count amount of time in specified range. For example, I have range (let's call it peak hours) 12:00-14:00. And i have another range(visit time), that might change, for ex 9:00-15:00. How do I get intersected hours for these 2 ranges?
As result I would like to get something like: {peak_hours: 2, regular_hours: 4}
Here peak_hours value is 2 as that many peak hours overlap with regular hours. And, regular_hours value is 4 as that many regular hours do not overlap with peak hours.
I'm kinda stuck with solution. I tried to use time ranges, but that didn't work for me. This is the code
peak_hours_range = Time.parse(peak_hour_start)..Time.parse(peak_hour_end)
session_range = visit_start..visit_end
inters = session_range.to_a & peak_hours_range.to_a
But this throws me type error
Here is one way to do it, we find the total hours in both ranges included, and then remove the peak hours from it to get effective regular hours.
require "time"
peak_hour_start = "12:00"
peak_hour_end = "14:00"
regular_hour_start = "9:00"
regular_hour_end = "15:00"
ph = (Time.parse(peak_hour_start).hour...Time.parse(peak_hour_end).hour).to_a
#=> [12, 13]
rh = (Time.parse(regular_hour_start).hour...Time.parse(regular_hour_end).hour).to_a
#=> [9, 10, 11, 12, 13, 14]
total = (ph + rh).uniq
#=> [12, 13, 9, 10, 11, 14]
r = {peak_hours: (ph - rh).size, regular_hours: (total - ph).size}
#=> {:peak_hours=>2, :regular_hours=>4}
You can always try to find the intersection yourself.
inters = nil
intersection_min = [peak_hour_start, visit_start].max
intersection_max = [peak_hour_end, visit_end].min
if intersection_min < intersection_max
inters = [intersection_min, intersection_max]
end
inters
Of course this can be cleaned up by extracting it out into it's own method.
I want show numbers in dropdown like 0,10,20,30,40,60,70
For it I write
0..70
But it generate all numbers from 0 to 70 .I want only multiples of 10
Take a look at Numeric.step.
0.step(70,10).to_a
In addition to Numeric.step
0.step(70,10).to_a
you can also use Range#step:
Range.new(0,70).step(10).to_a
(0..70).step(10).to_a
You could create a new Enumerator for this:
multiples_of_10 = Enumerator.new do |y|
x = 0
loop do
y << x
x += 10
end
end
multiples_of_10.take(8) # => [0, 10, 20, 30, 40, 50, 60, 70]
Referring to the original problem: Optimizing hand-evaluation algorithm for Poker-Monte-Carlo-Simulation
I have a list of 5 to 7 cards and want to store their value in a hashtable, which should be an array of 32-bit-integers and directly accessed by the hashfunctions value as index.
Regarding the large amount of possible combinations in a 52-card-deck, I don't want to waste too much memory.
Numbers:
7-card-combinations: 133784560
6-card-combinations: 20358520
5-card-combinations: 2598960
Total: 156.742.040 possible combinations
Storing 157 million 32-bit-integer values costs about 580MB. So I would like to avoid increasing this number by reserving memory in an array for values that aren't needed.
So the question is: How could a hashfunction look like, that maps each possible, non duplicated combination of cards to a consecutive value between 0 and 156.742.040 or at least comes close to it?
Paul Senzee has a great post on this for 7 cards (deleted link as it is broken and now points to a NSFW site).
His code is basically a bunch of pre-computed tables and then one function to look up the array index for a given 7-card hand (represented as a 64-bit number with the lowest 52 bits signifying cards):
inline unsigned index52c7(unsigned __int64 x)
{
const unsigned short *a = (const unsigned short *)&x;
unsigned A = a[3], B = a[2], C = a[1], D = a[0],
bcA = _bitcount[A], bcB = _bitcount[B], bcC = _bitcount[C], bcD = _bitcount[D],
mulA = _choose48x[7 - bcA], mulB = _choose32x[7 - (bcA + bcB)], mulC = _choose16x[bcD];
return _offsets52c[bcA] + _table4[A] * mulA +
_offsets48c[ (bcA << 4) + bcB] + _table [B] * mulB +
_offsets32c[((bcA + bcB) << 4) + bcC] + _table [C] * mulC +
_table [D];
}
In short, it's a bunch of lookups and bitwise operations powered by pre-computed lookup tables based on perfect hashing.
If you go back and look at this website, you can get the perfect hash code that Senzee used to create the 7-card hash and repeat the process for 5- and 6-card tables (essentially creating a new index52c7.h for each). You might be able to smash all 3 into one table, but I haven't tried that.
All told that should be ~628 MB (4 bytes * 157 M entries). Or, if you want to split it up, you can map it to 16-bit numbers (since I believe most poker hand evaluators only need 7,462 unique hand scores) and then have a separate map from those 7,462 hand scores to whatever hand categories you want. That would be 314 MB.
Here's a different answer based on the colex function concept. It works with bitsets that are sorted in descending order. Here's a Python implementation (both recursive so you can see the logic and iterative). The main concept is that, given a bitset, you can always calculate how many bitsets there are with the same number of set bits but less than (in either the lexicographical or mathematical sense) your given bitset. I got the idea from this paper on hand isomorphisms.
from math import factorial
def n_choose_k(n, k):
return 0 if n < k else factorial(n) // (factorial(k) * factorial(n - k))
def indexset_recursive(bitset, lowest_bit=0):
"""Return number of bitsets with same number of set bits but less than
given bitset.
Args:
bitset (sequence) - Sequence of set bits in descending order.
lowest_bit (int) - Name of the lowest bit. Default = 0.
>>> indexset_recursive([51, 50, 49, 48, 47, 46, 45])
133784559
>>> indexset_recursive([52, 51, 50, 49, 48, 47, 46], lowest_bit=1)
133784559
>>> indexset_recursive([6, 5, 4, 3, 2, 1, 0])
0
>>> indexset_recursive([7, 6, 5, 4, 3, 2, 1], lowest_bit=1)
0
"""
m = len(bitset)
first = bitset[0] - lowest_bit
if m == 1:
return first
else:
t = n_choose_k(first, m)
return t + indexset_recursive(bitset[1:], lowest_bit)
def indexset(bitset, lowest_bit=0):
"""Return number of bitsets with same number of set bits but less than
given bitset.
Args:
bitset (sequence) - Sequence of set bits in descending order.
lowest_bit (int) - Name of the lowest bit. Default = 0.
>>> indexset([51, 50, 49, 48, 47, 46, 45])
133784559
>>> indexset([52, 51, 50, 49, 48, 47, 46], lowest_bit=1)
133784559
>>> indexset([6, 5, 4, 3, 2, 1, 0])
0
>>> indexset([7, 6, 5, 4, 3, 2, 1], lowest_bit=1)
0
"""
m = len(bitset)
g = enumerate(bitset)
return sum(n_choose_k(bit - lowest_bit, m - i) for i, bit in g)