PATINDEX return wrong value? - return

Why does PATINDEX('%n%','nguyen') return value 6, not the first occurrence, but PATINDEX('%n%','nathan') return a value of 1, the first occurrence?

Related

Convert number input to characters in Google Sheets

How to convert a number entered in google sheets to some characters?
Example:
input in cell: “1234”
output format in cell: “ABCD”
So I’d like to have some logic that will loop through every digit and convert it to a corresponding character of my choice, and have that be what is displayed in the cell.
EDIT: The character <> number representation don't have to be sequential. So we could have 1:"A", 2:"Q", 3:"E" etc. And the conversion should happen in place then replace the input altogether, as if the string was entered initially and not the numbers. Not sure if that can be achieved without a script.
You'll need a script
You can take advantage of an onEdit trigger. This is something that allows a script to run every single time there is an edit on your sheet. Within the trigger, you can tell it to replace the contents of a cell if it meets certain criteria. For instance, if its in a certain range, or it contains a certain value.
Example:
const code = {
'A': 1,
'B': 2,
'C': 5,
'D': 6,
'E': 9,
'F': 8,
'G': 7,
'H': 3,
'I': 2,
'J': 45,
'K': 4,
'L': 32,
'M': 45,
'N': 5,
'O': 4,
'P': 3,
'Q': 6,
}
function onEdit(e) {
if (e.range.columnEnd === 1 &&
e.range.columnStart === 1) {
let letters = e.value.split("")
let numbers = letters.map(letter => code[letter])
let newValue = numbers.join("")
e.range.setValue(newValue)
}
}
Within the code dictionary, you need to define what you want the conversion to be.
Then within the onEdit function:
It checks if the change is in the first column.
If so, it takes the value (e.value) and splits it into an array. So if the value input into the cell was "ABC" then the resulting array would be ["A","B","C"].
Then it map the array using the code. This just goes through the array and returns whatever is in the dictionary. So now you have [1,2,5].
Then it join the resulting array into a continuous string "125"
Finally, it setValue using the e.range to the newValue.
Room for improvement
You need to make sure you have all the possible characters that someone will enter within the code object. Or you will need to handle the case of what to do with a character that is not in the object. For example, you could just return a space:
let numbers = letters.map(letter => {
if (code.hasOwnProperty(letter)) {
return code[letter]
} else {
return " "
}
You may have an issue with converting numbers to letters. Seeing as there are only 10 single digit numbers (0 - 9). For example, if you have A = 1 and B = 11. If the value in a cell was converted to 11 then you wouldn't know if the original value had been AA or B.
References and further reading
onEdit()
Range
setValue(value)
Event objects
try:
=INDEX(JOIN(, CHAR(64+REGEXEXTRACT(A1&"", JOIN("|", REPT("(.)", LEN(A1)))))))
suggested approach:
=INDEX(JOIN(, CHAR(64+SPLIT(A3, " "))))

Is there a Dart equivalent of Python's all() method?

I'm looking for a method that returns true if a function test, given an element, returns true for every element of an Iterable, or in my case a List, similar to Python's all() method.
I think what you are looking is the every method.
var numbers = [1, 2, 3, 4, 5];
var evenNumbers = [2, 4, 6, 8, 10];
print(numbers.every((n) => n.isEven)); //false
print(evenNumbers.every((n) => n.isEven)); //true
Iterable#every
Checks whether every element of this iterable satisfies test.
void main() {
print([1, 2, 3].every((i) => i > 0));
}

In Rails, how do I figure out if an array of objects contains specific attributes matching given values?

I'm using Ruby on Rails 5.0.1 with Ruby 2.4. I have an array of objects, stored in the array, "results." Each object has a numeric attribute
numeric_attr
I would like to know, given my array, how I can tell if I have exactly one object with a numeric attribute value of "1" and incrementing by one. Order is not important. So, for instance, if I have an array of three objects,
[MyObject(numeric_attr = 2), MyObject(numeric_attr = 1), MyObject(numeric_attr = 3)]
I want to know if I have exactly one object with numeric_attr = 1, another object with numeric_attr = 2, and another with numeric_attr = 3. So the above satisfies the condition. The below example does not
[MyObject(numeric_attr = 4), MyObject(numeric_attr = 1), MyObject(numeric_attr = 3)]
because although there is an object with numeric_attr = 1, there is no object with numeric_attr = 2. It is possible thet the numeric_attr field is nil. How can I figure this out?
This one-liner should work:
results.map(&:numeric_attr).sort == (1..results.count).to_a
Explanation:
results
#=> [#<MyObject:... #attr=2>, #<MyObject:... #attr=3>, #<MyObject:... #attr=1>]
results.map(&:attr)
#=> [2, 3, 1]
results.map(&:attr).sort
#=> [1, 2, 3]
(1..results.length).to_a
#=> [1, 2, 3]
# therefore:
results.map(&:attr).sort == (1..results.count).to_a
#=> true
If there is a chance that numeric_attr is nil:
results.map(&:attr).compact.sort == (1..results.count).to_a
Of course, if there is even a single nil value, the result is guaranteed to be false.
If the sequence could start at any number, not just 1:
results.map(&:attr).sort == results.count.times.to_a.
map { |i| i + results.map(&:attr).sort.first }
This is not very efficient though, as it sorts the numbers twice.
If they always start at 1 #Máté's solution works, if they can start at any arbitrary number then you could:
count = 0
array_objects.sort_by(&:numeric_attr).each_cons(2) {|a,b| count+=1 if a.numeric_attr==b.numeric_attr-1 }
count+1==array_objects.count
Not as elegant but handles a lot more situations

remove item from a list by specific index

I have this table:
local ls = {
["foo"] = {1, 2, 3, 4, 5},
["bar"] = {5, 4, 3, 2, 1}
}
I want to remove "foo" from list.
I tried this:
table.remove(ls, "foo")
but returns a error: "Only numbers"
Okay, but I can't input a number. This list isn't static, in my code a lot of indexes will be inserted in this list.
The question is, is there other way to do this or other function that fit my problem?
table.remove only works for a sequence. In your code, the table ls isn't one.
To remove an entry from a table, just assign the value of specific key to nil:
ls.foo = nil

Number of returned values

I've got a function which is returning a bunch of values and I can't know how many arguments are returned.
function ascii( value )
[...]
if type( value ) == "string" then
if #value > 1 then
local temp = {}
for i = 1, #value do
table.insert( temp , (value:byte(i)) )
end
return unpack( temp ) --<-- unknown number of return values
else
return value:byte(1)
end
end
[...]
end
How can I know how many values are returned?
My first idea was this:
return numberOfValues, unpack( temp )
But in most cases, the number of values is irrelevant.
Is there a way to get around that extra effort which is unnecessary in 95% of all cases?
Keep your function definition as it is and call it like this:
local values = {ascii(myString)}
local n = #values
Number of returned values is not the same as the number of elements in the table built from returned values. It works in the example you have, but only because the example doesn't include any nil values.
In the general case when the returned values may include nil, it's almost always better to use select('#'...) instead:
print(#{(function() return 1, nil, 2 end)()})
print(select('#', (function() return 1, nil, 2 end)()))
This code prints 1 3 in Lua 5.1, but 3 3 in Lua 5.2 and Lua 5.3. Adding one more nil to the return values changes this to 1 4 under all these versions of Lua.
These functions can be used as wrappers that returns the number of return values and also return a list or a table with the values:
function wrap(...) return select('#', ...), ... end
function wrapt(...) return select('#', ...), {...} end
Using one of these functions, print(wrap((function() return 1, nil, 2, nil end)())) prints 4 1 nil 2 nil as expected.

Resources