Skip to main content

Posts

Generate the Series up to N and sum it all together! N=13 [+1-2+3-4+5-6+7-8+9-1+0-1+1-1+2-1+3]

Let's say N=13, then the we have to generate the series till the number(including N) and evaluate the entire expression as  [+1-2+3-4+5-6+7-8+9-1+0-1+1-1+2-1+3]. It can be solved in various way, among all i am sharing one of the approach to solve it. public class ExpressionEvaluator { public static void main(String[] args) { int temp; int rem; int sum=0; int number=10; // get the sequence of numbers starting from 1 to 13 int i=1; while(i<=number) { if(i<=9) { rem=i%10; if(rem%2==0) { rem=-(rem); sum=sum+rem; } else if(rem==1) { sum=sum+rem; } else { sum=sum+rem; } } else { rem=i%10; //+0     // +1    //+2  //3  // 0  // 1  //2 // 3 temp=i/10; // 1   //1     //1    // 1   //2  ...

Tokenize a string input except double quotes(")

Sample Input: abc def ghi "jkl mno" pqrs "tuv" wxyz Expected Output:  abc def ghi "jkl mno"  pqrs "tuv" wxyz 1. One of the easiest way to solve the problem statement is through Patters!! "([^\"]\\S*|\".+?\")\\s*" - is the pattern to parse the string except double quotes. Complete Program. // import the required packages import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; class TokenizeString {     public static void main(String [] args) {     // user input     System.out.println("Enter the string");    // Instantiate the Scanner class     Scanner scanner=new Scanner(System.in);   // store the entire line in the variable    String queryString=sc.nextLine();   // instantiating the ArrayList object    List<String> list = new ArrayList...