I am trying to add an if condition; Idea is If I enter the input as print or text with 4 in it, else block should be called.
if ((turnContext.activity.text.indexOf('print') == -1) || (turnContext.activity.text.indexOf('4') == -1))
Now, it goes into the if block as opposed to else.
You have to use: &&. Otherwise when the text is print, the text is not 4 entering the if block.
if ((turnContext.activity.text.indexOf('print') == -1)
&& (turnContext.activity.text.indexOf('4') == -1)) {
// not 4 nor print
} else {
// print or 4
}
The execution goes into else block if text is 'print' or string contains 4 in it.
if (turnContext.activity.text !== 'print' && turnContext.activity.text.indexOf('4') === -1) {
}
Related
I am having a hard time understanding why my first if statement is executing if my string variable exam1ScoreKnown is assigned the string "YES" or "Y". I only want the if statement to execute if the string is anything other than "YES" or "Y". Also, I have checked to make sure the variable is assigned "YES" or "Y" right before the if statement.
if (exam1ScoreKnown != "YES" || exam1ScoreKnown != "Y") {
totalWeight = totalWeight - exam1Weight - exam2Weight - finalExamWeight;
}
else if (exam2ScoreKnown != "YES" || exam2ScoreKnown != "Y") {
totalWeight = totalWeight - exam2Weight - finalExamWeight;
}
else if (finalExamScoreKnown != "YES" || finalExamScoreKnown != "Y") {
totalWeight = totalWeight - finalExamWeight;
}
This Boolean expression
(exam1ScoreKnown != "YES" || exam1ScoreKnown != "Y")
is equivalent to
!(exam1ScoreKnown == "YES" && exam1ScoreKnown == "Y")
I think in this rewritten form it is easier to see that it is a tautology, ie it is true every which way. I think you need to replace the or by and.
Below are my codes for Leetcode 20. (Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.)
When the input is "(])" I still got true. Can anyone let me know what is wrong with my code? Thanks!
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for(char c: s.toCharArray()){
if(c == '(' || c == '[' || c == '{'){
stack.push(c);
}else{
if(stack.empty()){
return false;
}
if(c == ')' && stack.peek() == '('){
stack.pop();
}
if(c == ']' && stack.peek() == '['){
stack.pop();
}
if(c == '}' && stack.peek() == '{'){
stack.pop();
}
}
}return stack.empty();
}
}
On the second iteration of the for loop you have char ], it doesn't match the first conditional so it goes on to the else block. None of the other if statements match, therefor it doesn't know what to do and just starts on the 3rd iteration of the loop, where it sees ) and also sees ( on peek so returns empty. This is where the issue lies. You'll need to add an additional else inside your else block to catch anything that doest match the 4 ifs.
In order to fix this particular test, add a check for the ] character only. if you see that character and you havent seen any [s then return false
Hopefully that helps, if not, let me know and I can try to clarify more.
I'm trying to make a calculator in Haxe, it is almost done but have a bug. The bug is happening every time that some part of the equation result in 0.
This is how I concatenate the numbers and put i the array number, the cn is the variable used to receive the digit and transform in a number, the ci is a specific counter to make the while work well and the c is the basic counter that is increased to a background while used to read the array (input) items:
var cn = '';
var ci = c;
if (input[c] == '-') {
number.push('+');
cn = '-';
ci ++;
}
while (input[ci] == '0' || input[ci] == '1' || input[ci] == '2' || input[ci] == '3' || input[ci] == '4' || input[ci] == '5' || input[ci] == '6' || input[ci] == '7' || input[ci] == '8' || input[ci] == '9' || input[ci] == '.') {
if(ci == input.length) {
break;
}
cn += input[ci];
ci++;
}
number.push(cn);
c += cn.length;
This is the part of the code used to calculate the addition and subtraction
for (i in 0 ... number.length) { trace(number); if (number[c] == '+') { number[c-1] = ''+(Std.parseFloat(number[c-1])+Std.parseFloat(number[c+1])); number.remove(number[c+1]); number.remove(number[c]); }
else {
c++;
}
}
Example:
12+13-25+1: When my code read this input, it transform in a array ([1,2,+,1,3,-,2,5,+,1]), then the code concatenate the numbers ([12,+,13,-,25,+,1]) and for lastly it seeks for the operators(+,-,* and /) to make the operation (ex: 12+13), substituting "12" for the result of the operation (25) and removing the "+" and the "13". This part works well and then the code does 25-25=0.
The problem starts here because the equation then becomes 0+1 and when the code process that what repend is that the 0 vanish and the 1 is removed and the output is "+" when the expected is "1".
remove in this case uses indexOf and is not ideal, suggest using splice instead.
number.splice(c,1);
number.splice(c,1);
https://try.haxe.org/#D3E38
I am working on a project that wants me to check for multi comments in a text file and also to see if it is a non terminating block statements. Pretty much I am using get char to check each character and compare it to the multi comment symbols and use peek to see if the next character matches the other symbols. The first part is working but to know when there is no terminating block statements is confusing please help.
if (c == '#' && inFile.peek() == '|') {
char next = '\0';
multipleComment += c;
while (inFile.get(c)) {
next = inFile.peek();
multipleComment += c;
if (c == '\n')
lineNumber++;
if (c == '|' && next == '#')
{
multipleComment += next;
tokenTypes.push_back(multipleComment);
values.push_back("COMMENT");
lineNumbers.push_back(lineNumber);
multipleComment.clear();
break;
}
else {
values.push_back("UNDEFINED");
tokenTypes.push_back(text);
lineNumbers.push_back(lineNumber);
}
}
}
I have a problem to get last two digit from a string.
example :
String texter = "5793231309"
how to get '09' ?
so when Iprintln "texter : "+texter.
It will be Groovy<<09
I try split but it not successful ?
Use this to split your first String:
static main(args) {
String texter = "5793231309"
String texter2 = texter[-2..-1]
println(texter2)
}
Here's a one liner that's also a safe alternative:
assert "5793231309".reverse().take(2).reverse() == "09"
In groovy you can substring via negative indices.
String last2 = texter[-2..-1] // Last 2 symbols
Its an analogue of substring, and it uses Ranges.
http://groovy.codehaus.org/Collections see 'Slicing with the subscript operator'
Inspired by tim_yates:
It may be safer to use some function, to extract last n characters, as tim suggested. But I think his solution, with regexp is a big overhead, and may be hard to understand by novices.
There is an easier and faster way to do this, using size() check, and then range substring:
def lastN(String input, int n){
return n > input?.size() ? null : n ? input[-n..-1] : ''
}
assert lastN("Hello", 2) == 'lo'
assert lastN("Hello", 3) == 'llo'
assert lastN("Hello", 0) == ''
assert lastN("Hello", 13) == null
assert lastN(null, 3) == null
Be careful though, if your unit is less than 2 characters long, s[ -2..-1 ] will fail.
Might be better to do:
String lastN( String input, int n ) {
input == null ?
null :
( input =~ /^.+(\S{$n})$/ ).with { m -> m.matches() ?
m[ 0 ][ 1 ] :
null }
}
assert lastN( "5793231309", 2 ) == '09'
assert lastN( "5793231309", 3 ) == '309'
assert lastN( "5793231309", 0 ) == ''
assert lastN( '', 2 ) == null
assert lastN( null, 2 ) == null
Or:
String lastN( String input, int n ) {
if( input == null || input.length() < n ) null
else if( n == 0 ) ''
else input[ -n..-1 ]
}
assert lastN( "5793231309", 2 ) == '09'
assert lastN( "5793231309", 3 ) == '309'
assert lastN( "5793231309", 0 ) == ''
assert lastN( '', 2 ) == null
assert lastN( null, 2 ) == null
The most readable solution is probably just to drop() all but the last two characters:
def texter = "5793231309"
println texter.drop(texter.size() - 2) //result: "09"
Or as a reusable closure:
def lastTwo = { it.drop(it.size() - 2) }
println lastTwo("ABC") //result: "BC"
println lastTwo("AB") //result: "AB"
println lastTwo("A") //result: "A" (no exception thrown)
println lastTwo("") //result: "" (no exception thrown)
Fiddle with the code:
https://groovyconsole.appspot.com/script/5768158526832640
More examples of Groovy goodness:
http://mrhaki.blogspot.com/2011/09/groovy-goodness-take-and-drop-items.html
Another safe alternative using size() and substring() methods:
s?.size() < 2 ? s : s.substring(s.size() - 2)
Note the above takes care of nulls and strings that are less than two characters long.
In Java use
String texter = "5793231309";
String val=texter.substring(texter.length()-2,texter.length());
System.out.println("val-->"+val);
In Groovy you don’t need the above stuff just,
String value= texter[-2..-1]