extracting numeric characters at end of string - google-sheets

I am trying to extract a total $ amount from a string of imported characters in Google Sheets. The import (in column I) always looks something like this:
Rack Cards (Amount: 22.00 USD, Quantity: 50) Trifolds (Amount: 49.00 USD, Quantity: 100) Postcard Magnets (Amount: 19.00 USD, Quantity: 20) Subtotal: 90.00 Tax: 7.20
Total: 97.20
I'm using the formula =RIGHT(I2,(FIND(" ",I2)-1)) to return ONLY the total. In this particular case, I get the returned answer "97.20", which is correct.
HOWEVER, I also get a return of "l: 82.08" when the corresponding I column reads
Stickers (Amount: 74.62 USD, Quantity: 50) Subtotal: 74.62 Tax: 7.46
Total: 82.08
LIKEWISE, I get " 145.20" - including a space at the beginning - when the corresponding I column reads
Flyers (Amount: 19.50 USD, Quantity: 25) Trifolds (Amount: 112.50
USD, Quantity: 500) Subtotal: 132.00 Tax: 13.20
Total: 145.20
This is driving me batty. I can't figure out why this formula would return characters after the space character, and it seems arbitrary. Any help would be much appreciated!

According to your samples, try
=regexextract(index(split(A1,char(10)),1,2),"[0-9.]+")
I am using here a very simple extract expression as [0-9.]+ on the second line

Related

Lua loop shuffle list

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.

Sum of values with added percentage keeps giving wrong result

Ik keep getting wrong amounts when I want to sum prices and then add VAT to it.
I have the following situation:
Let's say I have 4 products and I want them show them on a invoice. Each products has the same price of 2.50.
I have the following code:
#products = Product.find([1,2,3,4])
On the invoice I have:
#products.each do |product|
%p
Price without VAT:
= number_to_currency(product.price) --> Gives € 2,50
%p
Price with 21% VAT:
= number_to_currency(product.price / 100 * 121) --> Gives € 3,03
This shows like this:
Price without VAT: € 2,50
Price with 21% VAT: € 3,03
Now I want to add a total line. I have tried something like this:
- sum = #products.sum( &:price ) --> Gives 10
%p
Total with 21% VAT:
= number_to_currency(sum / 100 * 121 ) --> Gives € 12,10 instead of € 12,12
What I try, I keep getting a total price including VAT on € 12.10 instead of € 12,12. (4 x € 3,03 = € 12,12)
I have the price in my database as:
t.decimal "price"
The price is stored as 2.5
Who can help me out with this?
One product's correct VAT amount:
(BigDecimal.new("2.5") / 100 * 121).to_f #=> 3.025
4 products correct VAT amount:
(BigDecimal.new("10") / 100 * 121).to_f #=> 12.10
Options:
don't round up and then you can calculate the total VAT from total sum
Round up, but then calculate total VAT as sum of VAT amounts per product
Applying what's said above, 1 product with VAT:
(product.price / 100 * 121).round(2)
all products with VAT:
products.sum { |p| (p.price / 100 * 121).round(2) }

Swift - Sort array element by range

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)]

generating series of number 0,3,5,8,10,13,15,18

i want to generate a series of number through looping.
my series will contain numbers like 0,3,5,8,10,13,15,18 and so on.
i try to take reminder and try to add 2 and 3 but it wont work out.
can any one please help me in generating this series.
You can just use an increment which toggles between 3 and 2, e.g.
for (i = 0, inc = 3; i < 1000; i += inc, inc = 5 - inc)
{
printf("%d\n", i);
}
It looks like the the sequence starts at zero, and uses increments of 3 and 2. There are several ways of implementing this, but perhaps the simplest one would be iterating in increments of 5 (i.e. 3+2) and printing two numbers - position and position plus three.
Here is some pseudocode:
i = 0
REPEAT N times :
PRINT i
PRINT i + 3
i += 5
The iteration i=0 will print 0 and 3
The iteration i=5 will print 5 and 8
The iteration i=10 will print 10 and 13
The iteration i=15 will print 15 and 18
... and so on
I was pulled in with the tag generate-series, which is a powerful PostgreSQL function. This may have been tagged by mistake (?) but it just so happens that there would be an elegant solution:
SELECT ceil(generate_series(0, 1000, 25) / 10.0)::int;
generate_series() returns 0, 25, 50, 75 , ... (can only produces integer numbers)
division by 10.0 produces numeric data: 0, 2.5, 5, 7.5, ...
ceil() rounds up to your desired result.
The final cast to integer (::int) is optional.
SQL Fiddle.

Hashfunction to map combinations of 5 to 7 cards

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)

Resources