conditional-statements

Using bitwise operations

主宰稳场 提交于 2019-12-23 18:44:59
问题 How often you use bitwise operation "hacks" to do some kind of optimization? In what kind of situations is it really useful? Example: instead of using if: if (data[c] >= 128) //in a loop sum += data[c]; you write: int t = (data[c] - 128) >> 31; sum += ~t & data[c]; Of course assuming it does the same intended result for this specific situation. Is it worth it? I find it unreadable. How often do you come across this? Note: I saw this code here in the chosen answers :Why is processing a sorted

Python function optional arguments - possible to add as condition?

血红的双手。 提交于 2019-12-23 17:12:15
问题 Is it possible, somehow, to aassign a condtional statement toan optional argument? My initial attempts, using the following construct, have been unsuccessful: y = {some value} if x == {someValue} else {anotherValue} where x has assigned beforehand. More specifically, I want my function signature to look something like: def x(a, b = 'a' if someModule.someFunction() else someModule.someOtherFunction()): : : Many thanks 回答1: Sure, that's exactly how you do it. You may want to keep in mind that b

C++ Conditional operator performance

為{幸葍}努か 提交于 2019-12-23 15:48:41
问题 I've a conditional statement expensive_foo() which is false in 99.9% cases. And I have a conditional statement bar which is true in ~50% cases. And I want some action be done if both statements are true. So I almost certainly know that expensive_foo() is false and I want to check it only if bar is true. Will the code below check the expensive_foo() ONLY if bar is true? Or it will check expensive_foo() every time? if ( bar && expensive_foo() ) { ... } Or I need to make a structure like this:

Doctrine Query Builder nested orX and andX conditions with join

本秂侑毒 提交于 2019-12-23 14:02:56
问题 I have two entities User and CalendarEvent . My users are attached to a factory, and calendarEvent can be used to know if a user is "loaned" to a different factory of the one he belongs to... My two entities are like this : User : class User extends BaseUser { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var string * @ORM\Column(name="firstname", type="string", length=255, nullable=true) */ private $firstname; /** * @var string *

If first argument falls true in a condition with and operators will the next run?

老子叫甜甜 提交于 2019-12-23 09:48:24
问题 Lets say I've got a pice of code that looks like this: if( !isset($this->domainID) && !$this->getDomainID() ){ return false; } Will the 2nd statement run if the first one is true? Because performance wise it would be stupid to get the ID from the database if I've already got it and there's a lot of other situation where the same apply. If it doesn't I'd have to nest them, am I right? I don't know if there's an standard on how programming language work in these cases or if it's different in

Rails ActiveRecord: Is a combined :include and :conditions query possible?

天涯浪子 提交于 2019-12-23 09:31:35
问题 Imagine I have wiki Articles, with many Revisions. I'd like to do a query with ActiveRecord through the database, which only returns those Articles which have Revisions which are updated in the past 24 hours. Is such a thing possible? I'd imagine it'd be something like: Articles.find_all(:include => :revisions, :conditions => {:revision[updated_at] => 1.week.ago..Time.now.utc}) If this isn't possible, is there some type of raw SQL I could pass to find_by_SQL that'd do the trick? 回答1: If you

Javascript: <script src=jquery…only if condition is true

拟墨画扇 提交于 2019-12-23 09:01:16
问题 I'm developing in javascript and would like to insert a script only if a condition is verified. For example: var a = exampleVariable; if (a == conditionIwant) { // append to head: <script src="http://code.jquery.com/jquery-1.5.js"> </ script> }; //or something like this How can I insert jquery.js only if a condition is true? 回答1: This is really simple: if(somethingIsTrue) { var sc = document.createElement('script'); sc.src = 'http://code.jquery.com/jquery-1.5.js'; sc.type = 'text/javascript';

R - fastest way to select the rows of a matrix that satisfy multiple conditions

允我心安 提交于 2019-12-23 07:41:35
问题 This is an extension to the question on returning the rows of a matrix that meet a condition in R. Say I have the matrix: one two three four [1,] 1 6 11 16 [2,] 2 7 12 17 [3,] 3 8 11 18 [4,] 4 9 11 19 [5,] 5 10 15 20 [6,] 1 6 15 20 [7,] 5 7 12 20 I want to return all rows, where matrix$two == 7 AND matrix$three == 12 as fast as possible. This is the way I know to do it: out <- mat[mat$two == 7,] final_out <- out[out$three == 12, ] There should obviously be a method to get the contents of

CakePHP Conditions on deep model associations

我的梦境 提交于 2019-12-23 05:35:07
问题 Using CakePHP 1.3, I have a Find statement with a condition on the 3rd level deep association: Hotel > Room > RoomType > name I would like my Find statement to only return Hotels with RoomTypes that have the Name "Suite". I believe the following code should work, but it does not, stating an SQL syntax error: $this->Hotel->find('first', array( 'contain' => array('Room' => array('RoomType')), 'conditions' => array( 'Hotel.Room.RoomType.name' => 'Suite' ), )); Please note that a Hotel will have

PHP - if dropdown selection = something

旧时模样 提交于 2019-12-23 04:42:20
问题 How would I go about creating a conditional statement based on a dropdown list selection? Lets say we have a dropdown list with entries: Alpha Bravo Charlie Other I want to create a conditional statement where if Other is the currently selected entry from the dropdown, I want to echo a newline stating If Other, Please Specify . But if it isn't the currently selected entry, I don't want that string to populate. 回答1: In PHP <select id="conditions" name="conditions"> <option></option> <?php