Scripting Operators - Any way to make it shorter? - scripting

I'm not a newbie when it comes to scripting but I always wondered!
Let's say I want to check if "num" equals to 2 or equal to 4, 6 and 8.
if(num == 2 || num == 4 || num == 6 || num == 8)
But, is there any way to make it shorter? Something like
if(num == 2 || 4 || 6 || 8)
I know it doesn't work, but I'm just wondering.
Would be cool to learn something.
EDIT:
The above numbers are used for example. Math formulas won't do the work for me since I'd actually want to compare between "num" and variable names.

In PHP, you can do:
if (in_array($num, array(2, 4, 6, 8)) !== false) {
}
In jQuery you can do:
if ($.inArray(num, new Array(2, 4, 6, 8))) {
}
If such a function is not offered by language X, then you can write a function to loop through and do the checks until one succeeds, and then exit the loop with true, else return false.

Related

Find all numbers adding up to given number and range to use

This is not homework, but a real world application I'm getting stumped on.
The best way I can explain my problem is as follows:
Imagine you have 3 pig pens A, B, and C and 10 pigs to be kept.
The pens have restrictions in that each pen must contain at least 1 pig and A.pigs <= B.pigs <= C.pigs. List all the possible pen configurations.
The REAL application can have anywhere between 1 and 7 pens, and anywhere between numPens and numPens*30 pigs and no pen may contain more than 30 pigs. So essentially this translates into "what numbers between 1 and 30 (repeats allowed) add up to X using Y numbers"
This could be solved simply with nested loops, but thats a terrible inefficient solution especially knowing the scope of the real application:
var cnt = 0
val target = 10
for (a in 1..30) {
for (b in a..30) {
val c = target - a - b
cnt++
if (a <= b && b <= c && a + b + c == target) {
println("$a $b $c")
}
}
}
println(cnt)
output:
1 1 8
1 2 7
1 3 6
1 4 5
2 2 6
2 3 5
2 4 4
3 3 4
465
I'm sure there is a recursive solution to this problem. But I'm having trouble even finding the starting point for this one.
It seems easy to start with an array of [1, 1, 8] where each index represents a pen. Just put all the pigs in 1 pen and move them around 1 at a time while following the constraints.
Subtract from C, add to B as much as you can while keeping constraints gets us [1, 2, 7], [1, 3, 6], [1, 4, 5] but at that point I'm stuck code wise.
What I have currently:
fun main(vararg args: String) {
val list = arrayOf(1, 1, 8)
list.forEach { print("$it\t") }
println()
rec(list, list.lastIndex - 1)
}
fun rec(list: Array<Int>, index: Int) {
if (index == list.lastIndex || index < 0) {
return
}
while (list[index] + 1 <= list[index + 1] - 1) {
list[index]++
list[index + 1]--
list.forEach { print("$it\t") }
println()
}
}
Obviously I need to call rec somewhere within itself and probably need some conditionals to call rec correctly (possibly adding more parameters to it). The question is where and how? Normally not bad with recursion, but this one is stumping me.
Solution:
I got so hung up on the idea I needed recursion. This loop seems to work pretty well.
for (a in 1..30) {
// we know a and c must be at least b, so cap range
var b = a
while(b < min(target - a - b, 30)){
val c = target - a - b
cnt++
if (a <= b && b <= c && a + b + c == target) {
println("$a $b $c")
}
b++
}
}
Some inefficiencies to note:
Once you know a and b, the only value for c that could make a solution would be 10-a-b, so you don't need the innermost loop at all.
Since a<=b, the second loop should start at a, not 1.
You can also limit the top of the second loop's range based on the value of a

Compare value against two values with 'or' operator

I have a simple if () condition which needs to check if value is either 1 or 2 (for example). Let's say the value we are comparing against is not 'simple':
if(it.first().property.value == 1 || it.first().property.value == 2) {
// Do stuff
}
Is there a better way to perform this check (without typing the entire expression to get the actual value twice)? The only thing that comes to mind is
if(listOf(1, 2).contains(it.first().property.value)) {
// Do stuff
}
But I'm afraid it's more memory consuming since it has additional list introduced.
Your last suggestion is a good one in general, though it's usually better to use a predefined list (and the in operator):
// At the top level, or in a (companion) object:
val acceptableValues = listOf(1, 2)
// Then in the relevant part of the code:
if (it.first().property.value in acceptableValues)
// Do stuff
That only allocates the list once, and performance is about as good as any other option.  It's also very readable, and general.
(If the list doesn't naturally fit into a named property, you'd have to judge how often it might be needed, in order to trade a minor performance benefit against the conciseness of putting it directly in the condition.)
In fact, because you're looking for consecutive integers, there's a more concise option for this particular test:
if (it.first().property.value in 1..2)
// Do stuff
That would work whenever the acceptable values form an (uninterrupted) range.
Alternatively, if you're always checking against exactly two values, you could write a simple extension function:
fun <T> T.isEither(a: T, b: T) = this == a || this == b
(You could write a more general one using a vararg param, but that would create an array each time — very similar to the in listOf() case we started with.)
You can decide it using a when expression like in this example:
fun main() {
val number = 22
when (number) {
1, 2 -> println("${number} is 1 or 2")
in 10..20 -> println("${number} is between 10 and 20 (inclusively)")
else -> println("${number} is either negative, equals 0, 3, 4, 5, 6, 7, 8, 9, 21 or any number above")
}
}
The output here is
22 is either negative, equals 0, 3, 4, 5, 6, 7, 8, 9, 21 or any number above
You could define an extension function on the type of it to make it more readable:
if(it.isOneOrTwo()) {
// Do stuff
}
Not sure what's the type of your it, replace TYPEOFIT accordingly:
private inline fun TYPEOFIT.isOneOrTwo() = first().property.value == 1 || first().property.value == 2
To additionally improve the condition you could leverage when:
private inline fun TYPEOFIT.isOneOrTwo() = when(first().property.value) {
1,2 -> true
else -> false
}

how do i correctly use >= and <= in code?

I have tried many thing involving this, >=, >==, =>, ==>.i can not find one that works. hey all return either primary expression needed or expected initializer before '>'. I am creating a IR receiver latch switch and thus have to create parameters for the code because the receiver is not constant in all conditions. Full code below. Any suggestions to fix the code please reply and don't DM me. Thank you.
code:
int LEDState = 0;
int LEDPin = 8;
int dt = 100;
int recieverOld ==> 500 and recieverOld ==< 2000;
int recieverNew;
int recieverPin = 12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LEDPin, OUTPUT);
pinMode(recieverPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
recieverNew = digitalRead(recieverPin);
if((recieverOld >== 0 && recieverOld <== 10) && (recieverNew >== 500 && recieverNew <== 2000) {
if(LEDState == 0) {
digitalWrite(LEDPin, HIGH);
LEDState = 1;
}
}
recieverOld = recieverNew;
delay(dt);
}
error:
expected initializer before '==' token
if one = used line 4 and related, return error expected primary-expression before '>' token
if > before = line 4 and related, return error expected initializer before '>=' token
Any solutions or suggestions welcome.
TL;DR
Operators that do no exist, and that you should NOT use:
==>, ==<, >==, <==
Operators that works and you can use them:
>= - MORE THAN OR EQUAL, compare operator, for example X >= 5
<= - LESS THAN OR EQUAL, compare operator, for example X <= 5
> - MORE THAN, compare operator, for example X > 5
< - LESS THAN, compare operator, for example X < 5
== - compare operator, when you want to compare values of the variables if they have the same value, for example X == 5, Y == X, 10 == 7
=== - equality operator, similar to compare operator ==, but aditionally checks the type of a variable. for example X === Y, '10' === 10
= - assign operator, when you want to assign something to the variable, for example X = 5
<> OR != - NOT EQUAL, compare operator, for example X != 5, Y <> 10
!== - similar to != or <>, but also checks the type of a value. For example 10 !== '10', and will return opposite result of the equality operator ===

while infinite loop in octave

I am writing a code to convert from decimal number to binary number without using library method. But using while loop. It seems it is stuck in a infinite loop.
In debugger mode I tried to see what is the value of x. But even when the value of x gets to 0 or 1, while loop does not exit. Is it a bug in octave or even Matlab has this problem (I don't have Matlab or in any position to use it)?
function ans = dec2bin(input)
x = int16(input);
y = [];
while (x!=1) || (x!=0)
y=[mod(x,int16(2)) y];
x=idivide(x,int16(2));
end
y=[mod(x,int16(2)) y];
ans = arrayfun(#(a) mat2str(a) ,y);
end
The condition (x!=1) || (x!=0) will always be true - if x is 1 it's not equal to 0, and vise-versa. You should use an && logical condition, not an ||:
while (x!=1) && (x!=0)

More efficient way to write conditionals with lots of OR operators

How can I (more) efficiently write out an if / else conditional that uses the same variable and a huge amount of OR operators?
I've been banging my head on the desk trying to think of a way around writing out all of these OR operators with the same comparison. For loops won't do the trick, and I'm pretty certain that a do / while loop wouldn't do it either. A switch / case might solve this, but I don't think it could condense the code or make it any less tedious to write out.
The example below illustrates my dilemma. The uicollectionview API has a protocol method which is called for each section in the collection and needs a return value for the number of items in that section. Essentially, the protocol method below is a fancy for loop.
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (section == 0 || section == 31) return 7;
else if (section == 1 || section == 26 || section == 27 || section == 28 || section == 29 || section == 30) return 6;
else if (section == 2 || section == 3 || section == 4 || section == 5 || section == 6 || section == 7 || section == 8 || section == 9 || section == 10 || section == 11 || section == 12 || section == 13 || section == 14 || section == 15) return 2;
else if (section == 16 || section == 17 || section == 18 || section == 19 || section == 20 || section == 21 || section == 22 || section == 23 || section == 24 || section == 25) return 4;
else return 7;
}
As you can see in the extremely tedious if / else conditional I've written, it checks for every single section. I feel like I'm missing something - that Objective-C provides a nifty way around this kind of tedious and inefficient conditional writing / checking?
A clean way is to use a switch statement. These are usually implemented with a branching array, so are quite efficient.
switch (section)
{
case 0:
case 31:
return 7;
...
}
And the most efficient of all (branchless) is a lookup-table:
const int nums[]= { 7, 6, 2, 2, 2... };
return nums[section];
switch is probably the right answer, but you can also use shift/mask, something like:
int sectionBit = 0x00000001 << section;
if (sectionBit & 0x80000001) return 7;
if (sectionBit & 0x7c000002) return 6;
etc
The disadvantage of this approach is that it's tedious and error-prone to define the bit constants, but one can sometimes develop the constants by using enums, etc (at the expense of long declaration strings).
A generic approach to this is to use NSArray / NSDictionary to build indexes, something like:
NSArray *sections = #[#3, #5, #27, ...];
if ([sections containsObject:#3]) ...
or
NSDictionary *sections = #{ #5: #27, #7: #23, ... };
int value = [sections[#(section)] intValue]