if-statement

Multiple conditions in if statement

巧了我就是萌 提交于 2021-01-29 04:11:24
问题 No matter what number is generated here I always get the first option (penguins). I can't seem to see any problem with my code, anyone else see what's wrong? { srand(time(0)); prand = (rand() % 20); if (prand == 1,5,9,10,14,15,19,20){ entity = "penguins"; srand(time(0)); pquantity = (rand() % 8) + 2; } else if (prand == 2,6,11,16,18){ entity = "troll"; pquantity = 1; } else if (prand == 3,7,12,17){ entity = "goblin"; pquantity = 1; } else if (prand == 4,8,13){ entity = "wizard"; pquantity = 1

Redundant If Statement and Regex

纵然是瞬间 提交于 2021-01-29 00:29:14
问题 The following code is clearly redundant, but in my experience I use this pattern fairly often. Is there some better way to do this in python? if re.search("at (\d{1,2}):\d{2}", p): a=re.search("at (\d{1,2}):\d{2}",p).group(1) 回答1: Yes, it is redundant; you should assign the result of search() to a variable instead of calling it twice: m = re.search("at (\d{1,2}):\d{2}", p) if m: a = m.group(1) or maybe a = m.group(1) if m else some_default_value Also, if you're going to be using this pattern

Redundant If Statement and Regex

杀马特。学长 韩版系。学妹 提交于 2021-01-29 00:21:22
问题 The following code is clearly redundant, but in my experience I use this pattern fairly often. Is there some better way to do this in python? if re.search("at (\d{1,2}):\d{2}", p): a=re.search("at (\d{1,2}):\d{2}",p).group(1) 回答1: Yes, it is redundant; you should assign the result of search() to a variable instead of calling it twice: m = re.search("at (\d{1,2}):\d{2}", p) if m: a = m.group(1) or maybe a = m.group(1) if m else some_default_value Also, if you're going to be using this pattern

Redundant If Statement and Regex

こ雲淡風輕ζ 提交于 2021-01-29 00:17:54
问题 The following code is clearly redundant, but in my experience I use this pattern fairly often. Is there some better way to do this in python? if re.search("at (\d{1,2}):\d{2}", p): a=re.search("at (\d{1,2}):\d{2}",p).group(1) 回答1: Yes, it is redundant; you should assign the result of search() to a variable instead of calling it twice: m = re.search("at (\d{1,2}):\d{2}", p) if m: a = m.group(1) or maybe a = m.group(1) if m else some_default_value Also, if you're going to be using this pattern

Looping an “If Else Statement” java

我的未来我决定 提交于 2021-01-28 22:44:54
问题 I am attempting to make a simple program in java using Scanner that will allow the user to shoot craps (play dice). 1.This code asks the user to enter how much money they have. The code will ask user to input a bet. Using a random number generator, it will inform the user what they rolled and inform the user how much money they've won/lost. I have been able to successfully tell the computer to inform the user when they have won or lost, when they roll 2,3,7,11, or 12. I am not sure how to

How do I shift columns (left or right) in a matrix?

家住魔仙堡 提交于 2021-01-28 22:41:58
问题 I would like to non-circularly shift my matrix and then have zeros padded to either the left or right (depending on the shift) i.e. if the matrix shifts to the right, zeros would be padded to the left. I am using MATLAB 2019b and my code so far looks like this: %dummy data data = rand(5, 16); channelSink = 9; %this variable will either be >layerIV, <layerIV or =layerIV layerIV = 7; diff = layerIV - channelSink; for channel = 1:16 if channelSink > layerIV %shift columns to the left by ab(diff)

Google Sheets - Combine multiple IF Functions into one cell

一笑奈何 提交于 2021-01-28 21:26:07
问题 I'm trying to produce a SKU in Google Sheets for a product using the values of three variants (Title, Colour and Size) The product is 'Lightweight trainers' with colour variants of 'Red' and 'Blue', and the sizes range from 5 - 12. Link to spreadsheet https://docs.google.com/spreadsheets/d/1trq0X3MjR-n2THFnT8gYYlwKscnQavCeeZ8L-ifYaHw/edit?usp=sharing Aim I'm hoping to have a SKU that displays the product, the colour variant and the shoes size. Example: LW-1-8 (Lightweight trainer, colour Red,

Condition based where clause SQL Server stored procedure

ε祈祈猫儿з 提交于 2021-01-28 19:41:52
问题 Can someone suggest how to add a condition in WHERE clause of my stored procedure? CREATE Procedure getAllEmployeesByDeptAndFlag @Dept int, @sal int, @Flag int AS if @flag = 1 select * from employee where Department = @dept and @sal < 10000 else select * from employee where Department = @dept Is there any way to simplify above procedure? 回答1: You could use the or logical operator to unify both branches of the if statement: select * from employee where Department = @dept AND (@flag != 1 OR

Conditional redirection appending / overwriting

我的梦境 提交于 2021-01-28 19:33:57
问题 I have a condition, depending on which I want to either append or overwrite some file like this: if [ "${CONDITION}" -eq 1 ]; then echo "writing some info" echo "${INFO1}" > "${FILE1}" echo "${INFO2}" > "{$FILE2}" ... else echo "Appending some info" echo "${INFO1}" >> "${FILE1}" echo "${INFO2}" >> "{$FILE2}" ... fi The thing is that if and else blocks are relatively big (20 lines each) and are essentially identical except the redirection difference. This makes me feel really bad indeed. How

replace values with NA across multiple columns if a condition is met in R

a 夏天 提交于 2021-01-28 18:53:23
问题 I'm trying to replace values across values with NA across multiple columns if a condition is met. Here's a sample dataset: library(tidyverse) sample <- tibble(id = 1:6, team_score = 5:10, cent_dept_test_agg = c(1, 2, 3, 4, 5, 6), cent_dept_blue_agg = c(15:20), num_in_dept = c(1, 1, 2, 5, 100, 6)) I want the columns that contain cent_dept_.*_agg to be NA when num_in_dept is 1, so it looks like this: library(tidyverse) solution <- tibble(id = 1:6, team_score = 5:10, cent_dept_test_agg = c(NA,