Is There a Shorter Way to Write Compound 'If' Conditions

Is there a shorter way to write compound 'if' conditions?

strchr() can be used to see if the character is in a list.

const char* list = "ABCXZ";
if (strchr(list, ch)) {
// 'ch' is 'A', 'B', 'C', 'X', or 'Z'
}

Is there a way to shorten multiple conditions such as below?

I think what you're looking for is the keyword in. The following is probably the most "pythonic" way.

if letter.lower() in ['a','b','c']:

That should do it for you. Although, if you're looking for speed, it would be better to create a dictionary or set and do lookups from that.
As the complexity of the in operator is average O(n) for arrays and strings but average O(1) and worst case O(n) for sets and dictionaries.

For example,

chars = set(['a','b','c'])
if letter.lower() in chars:

If you're interested in reading a little more about the differences in complexities, here's a Python Time Complexity document that's very useful to know!

Shorthand If/Else statement with two statements inside IF

No you cannot do that. The "small version" of the if/else is called the conditional operator. It is the only operator in c++ taking three operands and commonly also simply called "the ternary operator". From here:

Exp1 ? Exp2 : Exp3;

where Exp1, Exp2, and Exp3 are expressions. Notice the use and
placement of the colon. The value of a ? expression is determined like
this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and
becomes the value of the entire ? expression. If Exp1 is false, then
Exp3 is evaluated and its value becomes the value of the expression.

And for some clarification what is an expression see this question. break is not an expression, but a statement, hence you cannot use it inside a ternary.

Anyhow I would advise you not to hide the break within more stuff in a single line. In a loop a break is something extremely important and it makes sense to make it stand out from the "normal" buissness that happens inside the loop. If I dont immediately see a break (or a return) in a loop then I assume that it does its full iteration. Overlooking a break can cause mayor confusion and misunderstanding.

Is there a better way to write nested if statements in python?

While @Aryerez and @SencerH.'s answers work, each possible value of numeral_sys_1 has to be repeatedly written for each possible value of numeral_sys_2 when listing the value pairs, making the data structure harder to maintain when the number of possible values increases. You can instead use a nested dict in place of your nested if statements instead:

mapping = {
'Hexadecimal': {'Decimal': 1, 'Binary': 2},
'Binary': {'Decimal': 3, 'Hexadecimal': 5},
'Decimal': {'Hexadecimal': 4, 'Binary': 6}
}
def convert_what(numeral_sys_1, numeral_sys_2):
return mapping.get(numeral_sys_1, {}).get(numeral_sys_2, 0)

Alternatively, you can generate the pairs of values for the mapping with the itertools.permutations method, the order of which follows that of the input sequence:

mapping = dict(zip(permutations(('Hexadecimal', 'Decimal', 'Binary'), r=2), (1, 2, 4, 6, 3, 5)))
def convert_what(numeral_sys_1, numeral_sys_2):
return mapping.get((numeral_sys_1, numeral_sys_2), 0)

How to have multiple conditions for one if statement in python

I would use

def example(arg1, arg2, arg3):
if arg1 == 1 and arg2 == 2 and arg3 == 3:
print("Example Text")

The and operator is identical to the logic gate with the same name; it will return 1 if and only if all of the inputs are 1. You can also use or operator if you want that logic gate.

EDIT: Actually, the code provided in your post works fine with me. I don't see any problems with that. I think that this might be a problem with your Python, not the actual language.

Easier way to write this if function c++

Try this:

char c;
while (fd >> c)
{
if ((c < '`') || (c > 'c'))
{
k[i] = c;
++i;
}
}

Usually, using character constants is more readable than their ASCII decimal equivalents.

Also, using eof in a loop is considered wrong, so place the input operation in the while expression.

Since your values are contiguous, you can use the < and > operators to reduce the number of comparisons.

Edit 1:
Another alternative is to place the valid letters into a string and search the string.

const std::string invalid_letters = "@.abcdefghijklmnopqrstuvwxyz";
while (fd >> c)
{
if (invalid_letters.find(c) == std::string::npos)
{
k[i] = c;
++i;
}
}

You can also use islower to detect lowercase letters.

How to specify multiple conditions in an 'if' statement in JavaScript

Just add them within the main bracket of the if statement like:

if ((Type == 2 && PageCount == 0) || (Type == 2 && PageCount == '')) {
PageCount = document.getElementById('<%=hfPageCount.ClientID %>').value;
}

Logically, this can be rewritten in a better way too! This has exactly the same meaning:

if (Type == 2 && (PageCount == 0 || PageCount == '')) {


Related Topics



Leave a reply



Submit