This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular Expression to find a string included between two characters, while EXCLUDING the delimiters
I'm using a method called Interpreter.eval() of external library "beanshell" that allows to make math operations with Strings and return the solution.
It works fine, but if I want to raise to the second power or get a square root out of this I have to modify the original String that is like
String st ="x = 2+4*a*(b+c)^2"
I need to get the charaters between parentheses to replace "(b+c)^2" to "(b+c)*(b+c)" or "Math.pow((b+c),2)"
How can I do this?
Thanks in advance!
----edit----
Finally I found the solution.
Interpreter interpreter = new Interpreter();
String st = "x = 2+4*1*(2*(1+1)^2)^2 + (4+5)^2";
int index = -2;
char prev;
char post;
int openPar = -1;
if (st.contains("^")) {
index = st.indexOf("^");
while (index != -1) {
prev = st.charAt(index - 1);
post = st.charAt(index + 1);
if (prev == ')') {
int match = 0;
int i = index - 2;
char check = '0';
boolean contiunar = true;
while (contiunar) {
check = st.charAt(i);
if (check == ')')
match++;
if (check == '(') {
if (match == 0) {
openPar = i;
contiunar = false;
} else
match = match - 1;
}
i = i - 1;
}
String rep = st.substring(openPar + 1, index - 1);
String resultado = "";
try {
interpreter.eval("r= Math.pow(" + rep + "," + post
+ ")");
resultado = interpreter.get("r").toString();
} catch (EvalError e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
st = st.replace("(" + rep + ")^" + post, resultado);
} else {
st = st.replace(prev + "^" + post, prev + "*" + prev);
}
index = st.indexOf("^");
}
}
With this I modified the original String x = 2+4*1*(2*(1+1)^2)^2+(4+5)^2 (for example)
to x=2+4*1*64+81
first it search the first "^"
get previous char
and if there are ")"
search previous char while finds "(" But if finds ")" before of "(" has to find 2 "("; one to open internal parentheses and the second open the parentheses I want to pow.
This is in case of "(2+(3+4)+5)^2" ---> return "2+(3+4)+5" instead of "3+4)+5".
now replace to correct expresion Math.pow("result","2")" and calculate step by step
(1+1)^2 = 4
(2*4)^2 = 64
(4+5)^2 = 81
finally now I can calculate the retun with Interpreter.eval()
Thanks a lot for the answers!
try the following code to do your task
String originalString ="x = 2+4*a*(b+c)^2";
String stringToInsert = "Math.pow((b+c),2)";
int startIndex = originalString.indexOf("(");
int endIndex = originalString.indexOf(")");
String firstPortion = originalString.substring(0,startIndex);
String lastPortion = originalString.substring(endIndex+1);
String finalString = firstPortion + stringToInsert + lastPortion;
System.out.println("finalString : "+finalString);
You will need to parse the string. You could write a complete parser and go through it character by character.
Otherwise, if there is a specific flowchart you have in mind (using the operators), use the String.split(\"*(...) functionality where you can essentially create tokens based on whatever your delimeters are.
Hope this helps.
Of course you need to parse the String. You can convert it to intermediate state ( which you can execute after very quickly) . You can see the following link maybe it can help you http://en.wikipedia.org/wiki/Reverse_Polish_notation
Related
Encoding format: introduce * to indicate "repeat from beginning". Example. Input-{a,b,a,b,c,a,b,a,b,c,d} can be written as {a , b, * ,c, * , d}. Output:5; E.g 2: ABCABCE, output- 5.
Here * means repeat from beginning. For example if given String is ABCABCABCABC , it will return ABC**, another example is if String is ABCABCABC, it will return ABC*ABC.
I have the below code but this code assumes that the string will contain the repetitive pattern only and no other characters, I want to modify it to check :
1. Which pattern is repeating
2. Ignore non repeating patterns
2. encode that pattern according to the problem statement
import java.util.Scanner;
public class Magicpotion {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string:");
String str = sc.nextLine();
int len = str.length();
if (len != 0) {
int lenby3 = len / 3;
int starcount = ( int).(Math.log(lenby3) / Math.log(2));
int leftstring = (lenby3 - (int) Math.pow(2, starcount));
int resultlen = (1 * 3) + starcount + (leftstring * 3);
System.out.println("ResultLength: " + resultlen);
System.out.print("ABC");
for (int i = 0; i < starcount; i++) {
System.out.print("*");
}
for (int i = 0; i < leftstring; i++) {
System.out.print("ABC");
}
} else
System.out.println("ResultLength: " + 0);
}
}
Here my assumption is that ABC will always be repeating pattern , hence I have divided the length by 3. I want to generalise it such that I find the repeating pattern which can be a AB or BC or ABCD and proceed accordingly.
This looks like homework. So instead of a full solution just some hints:
You can process the input string character by character and encode as you go. If you have at some point already read k characters and the next k characters are exactly the same, output a * and advance to position 2k.
Otherwise, output the next input character and advance position to k+1.
As mentioned by dyukha this algorithm does not always result in the shortest possible encoding. If this is required some more effort has to be put into the search.
This problem can be solved using dynamic programming.
Assume that you processed your stay at some position i. You want to understand what it the minimal length of encoding of str[0..i]. Let's call it ans[i]. You have two options:
Just add i-th character to the encoding. So the length is ans[i-1] + 1.
You may write *, when possible. In this case the length is ans[i / 2] + 1 or something like this.
The final length is in ans[n-1]. You can store how you obtained ans[i] to recover the encoding itself.
Checking whether you can write * can be optimized, using some hashing (to obtain O(n) solution instead of O(n^2)).
The difference with Henry's solution is that he always applies * when it's possible. It's not clear to me that it results into the minimal length (if I understood correctly, aaaaaa is a counterexample), so I'm giving a solution I'm sure about.
/**
* #author mohamed ali
* https://www.linkedin.com/in/oo0shaheen0oo/
*/
public class Magic_potion_encoding
{
private static int minimalSteps( String ingredients )
{
StringBuilder sb = new StringBuilder(ingredients);
for(int i =0;i<sb.length();i++)
{
char startChar = sb.charAt(i);
int walkingIndex1=i;
int startIndex2 =sb.toString().indexOf(startChar,i+1);
int walkingIndex2=startIndex2;
while(walkingIndex2 !=-1 && walkingIndex2<sb.length() && sb.charAt(walkingIndex1) == sb.charAt(walkingIndex2) )
{
if(walkingIndex1+1==startIndex2)
{
String subStringToBeEncoded = sb.substring(i,walkingIndex2+1);//substring the string found and the original "substring does not include the last index hence the +1
int matchStartIndex = sb.indexOf(subStringToBeEncoded,walkingIndex2+1);// look for first match for the whole string matched
int matchEndeIndex= matchStartIndex+subStringToBeEncoded.length();
int origStartIndex=i;
int origEndIndex = i+subStringToBeEncoded.length();
if (matchStartIndex!=-1 )
{
if(origEndIndex==matchStartIndex)
{
sb.replace(matchStartIndex,matchEndeIndex,"*");
}
else
{
while(matchStartIndex!=-1 && matchEndeIndex<sb.length() && sb.charAt(origEndIndex) == sb.charAt(matchEndeIndex) )
{
if(origEndIndex==matchStartIndex-1)// if the index of the 2 strings are right behind one another
{
sb.replace(matchStartIndex,matchEndeIndex+1,"*");
}
else
{
origEndIndex++;
matchEndeIndex++;
}
}
}
}
sb.replace(startIndex2,walkingIndex2+1,"*");
break;
}
walkingIndex1++;
walkingIndex2++;
}
}
System.out.println("orig= " + ingredients + " encoded = " + sb);
return sb.length();
}
public static void main( String[] args )
{
if ( minimalSteps("ABCABCE") == 5 &&
minimalSteps("ABCABCEA") == 6 &&
minimalSteps("abbbbabbbb") == 5 &&
minimalSteps("abcde") == 5 &&
minimalSteps("abcbcbcbcd") == 6 &&
minimalSteps("ababcababce") == 6 &&
minimalSteps("ababababxx") == 6 &&
minimalSteps("aabbccbbccaabbccbbcc") == 8)
{
System.out.println( "Pass" );
}
else
{
System.out.println( "Fail" );
}
}
}
Given that the repetitions are from the beginning, every such repeating substring will have the very first character of the given string. [Every repetition needs to be represented by a "star". (i.e ABCABCABC ans = ABC** ) . If all sequential repetitions are to be represented with one "star". (i.e ABCABCABC and = ABC* ), a slight modification to (2) will do the thing (i.e remove the if case where the just a star is added)]
Divide the given string to substrings based on the first character.
Eg. Given String = "ABABCABD"
Sub Strings = {"AB", "ABC", "AB", "ABD"}
Just traverse through the list of substrings and get the required result. I've used a map here, to make the search easy.
Just a rough write up.
SS = {"AB", "ABC", "AB", "ABD"};
result = SS[0];
Map<string, bool> map;
map.put(SS[0],true);
for (i = 1; i < SS.length; i++){
if (map.hasKey(SS[i])){
result += "*";
}
else {
res = nonRepeatingPart(SS[i], map);
result += "*" + res;
map.put(SS[i], true);
}
}
String nonRepeatingPart(str, map){
for (j = str.length-1; j >= 0; j--){
if (map.hasKey(str.subString(0, j))){
return str.subString(j, str.length-1);
}
}
return throwException("Wrong Input");
}
string getCompressed(string str){
string res;
res += str[0];
int i=1;
while(i<str.size()){
//check if current char is the first char in res
char curr = str[i];
if(res[0]==curr){
if(str.substr(0,i)==str.substr(i,i)){
res += '*';
i+=i; continue;
}else{
res += curr;
i++; continue;
}
}else {
res += curr;
i++; continue;
}
}
return res;
}
int main()
{
string s = "ABCABCABC";
string res = getCompressed(s);
cout<<res.size();
return 0;
}
I have string like
String str = "This is john";
In that above string i need replace "is" with "was"
This is my code
class replaces
{
public static void main(String[] args)
{
String s = " This is john ";
String input = "is";
String old = "was";
String s1[] = s.split(" ");
for(int i=0;i<s1.length;i++)
{
if(s1[i]==input)
{
s1[i]=old;
}
}
for(int i=0;i<s1.length;i++)
{
System.out.println(s1[i]);
}
}
}
when i execute this its printing the same string -
This is john
If anybody can explain how to solve this if you have that code just post it.
Its useful for me.. Thank You
Assuming its Java code, your problem is that in your case, you need to use the equals() method comparison of strings, not the equality sign. Rather use s1[i].equals(input) instead of s1[i]==input
s = s.replace(" is ", " was ");
String method
First, let's solve with any reasonable API:
String source = "This is john";
String find = "is";
String replace = "was";
int index = source.indexOf(find);
// actually we don't have to change the first occurense (that is in the "thIS"),
// we want "is" to be a separate word i.e. either in the begining/ending of the string
// or started/followed by spaces
while (index >= 0)
if (((index == 0) ||
(source.charAt(index - 1) == ' ')) &&
((index + find.length() >= source.length()) ||
(source.charAt(index + find.length()) == ' ')))
break; // this occurence is OK
else
index = source.indexOf(find, index + 1); // next one, please
String result = index >= 0
? source.substring(0, index) + replace + source.substring(index + find.length())
: source;
then inspect which API used is allowed. I guess, that length(), charAt() have to be allowed;
as for indexOf() and substring() you can easily implement them.
I am new to regular expressions. I have a string named encryptId (does not contain |) and I want to append the | character after every 20 characters of this string, using the encryptId.replace/replaceAll(Regex,Pattern) function in Java. But it should never have \ at the end of the string.
Thanks for your help.
EDIT:
The reason I want to use replace and replaceAll functions particularly is because I have to use that in velocity template mananger. And there we can use common String functions but can't write whole java code.
My current solution is shown below
encryptId = encryptId.replaceAll("(.{20})","$1|");
if(encryptId.charAt(encryptId.length() - 1)=='|') {
encryptId = encryptId.substring(0,encryptId.length()-1);
}
I need to get rid of this if statement so that It would be just a string function.
You asked how to do it with replaceAll: I say don't. Regular expressions are not always the best approach to string manipulation problems.
You can efficiently build the new string by taking 20 character blocks from encryptId and appending them to a StringBuilder, optionally appending the pipe if it will not be at the end of the string:
String method(String encryptId) {
StringBuilder sb = new StringBuilder(encryptId.length() + encryptId.length() / 20);
for (int i = 0; i < encryptId.length(); i += 20) {
int end = Math.min(i + 20, encryptId.length());
sb.append(encryptId, i, end);
if (end != encryptId.length()) {
sb.append('|');
}
}
return sb.toString();
}
You can use String.toCharArray:
String s = "..."; //your string
int i = 0;
StringBuilder res = new StringBuilder("");
for (char c : s.toCharArray()){
res.append(c);
i++;
if (i % 20 == 0 && i != s.length()){
res.append("|");
}
}
System.out.println(res.toString());
res will have your first String with an | every 20 characters but not at the end of the String.
This can be done via regular expressions as follows
static String enterADelimiter(String str, String delimiter, int after) {
String regex = "(.{" + after +"})(?!$)";
String replacement = "$1" + delimiter;
return str.replaceAll(regex, replacement);
}
Just use
enterADelimiter(yourString, "|", 20)
This will return correct solution. Explantion
( Start group 1
. Match Anything
{after} after times
) End group 1
(?!$) Don't match if at end of String
Regex may complicate things more. You can also try to use StringBuilder for this:
String encryptId = "test";
StringBuilder builder = new StringBuilder(encryptId);
int insertAfter = 20;
for(int i = encryptId.length(); i > 0 ; i--) {
if (i % insertAfter == 0 && i != encryptId.length()) {
builder.insert(i, "|");
}
}
I am going through the Java CodeBat exercises. Here is the one I am stuck on:
Look for patterns like "zip" and "zap" in the string -- length-3, starting with 'z' and ending with 'p'. Return a string where for all such words, the middle letter is gone, so "zipXzap" yields "zpXzp".
Here is my code:
public String zipZap(String str){
String s = ""; //Initialising return string
String diff = " " + str + " "; //Ensuring no out of bounds exceptions occur
for (int i = 1; i < diff.length()-1; i++) {
if (diff.charAt(i-1) != 'z' &&
diff.charAt(i+1) != 'p') {
s += diff.charAt(i);
}
}
return s;
}
This is successful for a few of them but not for others. It seems like the && operator is acting like a || for some of the example strings; that is to say, many of the characters I want to keep are not being kept. I'm not sure how I would go about fixing it.
A nudge in the right direction if you please! I just need a hint!
Actually it is the other way around. You should do:
if (diff.charAt(i-1) != 'z' || diff.charAt(i+1) != 'p') {
s += diff.charAt(i);
}
Which is equivalent to:
if (!(diff.charAt(i-1) == 'z' && diff.charAt(i+1) == 'p')) {
s += diff.charAt(i);
}
This sounds like the perfect use of a regular expression.
The regex "z.p" will match any three letter token starting with a z, having any character in the middle, and ending in p. If you require it to be a letter you could use "z[a-zA-Z]p" instead.
So you end up with
public String zipZap(String str) {
return str.replaceAll("z[a-zA-Z]p", "zp");
}
This passes all the tests, by the way.
You could make the argument that this question is about raw string manipulation, but I would argue that that makes this an even better lesson: applying regexes appropriately is a massively useful skill to have!
public String zipZap(String str) {
//If bigger than 3, because obviously without 3 variables we just return the string.
if (str.length() >= 3)
{
//Create a variable to return at the end.
String ret = "";
//This is a cheat I worked on to get the ending to work easier.
//I noticed that it wouldn't add at the end, so I fixed it using this cheat.
int minusAmt = 2;
//The minus amount starts with 2, but can be changed to 0 when there is no instance of z-p.
for (int i = 0; i < str.length() - minusAmt; i++)
{
//I thought this was a genius solution, so I suprised myself.
if (str.charAt(i) == 'z' && str.charAt(i+2) == 'p')
{
//Add "zp" to the return string
ret = ret + "zp";
//As long as z-p occurs, we keep the minus amount at 2.
minusAmt = 2;
//Increment to skip over z-p.
i += 2;
}
//If it isn't z-p, we do this.
else
{
//Add the character
ret = ret + str.charAt(i);
//Make the minus amount 0, so that we can get the rest of the chars.
minusAmt = 0;
}
}
//return the string.
return ret;
}
//If it was less than 3 chars, we return the string.
else
{
return str;
}
}
The problem I'm trying to solve is given a string that may contain carriage returns, insert additional carriage returns such that each line does not exceed a set number of characters. Also it should try to keep a word intact if possible.
Is there a library in either Java or Scala that does what I need?
There is a BreakIterator class in the java.text package that can tell you where you could insert a line break, but it's a little complicated to use. A regular expression like this can do 80% of the job:
str += "\n"; // Needed to handle last line correctly
// insert line break after max 50 chars on a line
str = str.replaceAll("(.{1,50})\\s+", "$1\n");
The Apache commons lang library has a WordUtils class, which includes a wrap method, to wrap a long line of text to several lines of given length on word boundaries.
public static String addReturns(String s, int maxLength)
{
String newString = "";
int ind = 0;
while(ind < s.length())
{
String temp = s.substring(ind, Math.min(s.length(), ind+maxLength));
int lastSpace = temp.lastIndexOf(" ");
int firstNewline = temp.indexOf("\n");
if(firstNewline>-1)
{
newString += temp.substring(0, firstNewline + 1);
ind += firstNewline + 1;
}
else if(lastSpace>-1)
{
newString += temp.substring(0, lastSpace + 1) + "\n";
ind += lastSpace + 1;
}
else
{
newString += temp + "\n";
ind += maxLength;
}
}
return newString;
}
This will do the trick if you don't want to use regular expressions.
System.out.println(addReturns("Hi there, I'm testing to see if this\nalgorithm is going to work or not. Let's see. ThisIsAReallyLongWordThatShouldGetSplitUp", 20));
Output:
Hi there, I'm
testing to see if
this
algorithm is going
to work or not.
Let's see.
ThisIsAReallyLongWor
dThatShouldGetSplitU
p
I think you can start with something like that. Note that you will have to handle the special case when a word is more than the MAX_LINE_LENGTH.
package com.ekse.nothing;
public class LimitColumnSize {
private static String DATAS = "It was 1998 and the dot-com boom was in full effect. I was making websites as a 22 year old freelance programmer in NYC. I charged my first client $1,400. My second client paid $5,400. The next paid $24,000. I remember the exact amounts ā they were the largest checks Iād seen up til that point.\n"
+ "Then I wrote a proposal for $340,000 to help an online grocery store with their website. I had 5 full time engineers at that point (all working from my apartment) but it was still a ton of dough. The client approved, but wanted me to sign a contract ā everything had been handshakes up til then.\n"
+ "No prob. Sent the contract to my lawyer. She marked it up, sent it to the client. Then the client marked it up and sent it back to my lawyer. And so on, back and forth for almost a month. I was inexperienced and believed that this is just how business was done."
+ "Annoyed by my lawyering, the client eventually gave up and hired someone else.";
private static int MAX_LINE_LENGTH = 80;
private static char[] BREAKING_CHAR = {' ', ',', ';', '!', '?', ')', ']', '}'}; // Probably some others
public static void main(String[] args) {
String current = DATAS;
String result = "";
while (current.length() != 0) {
for (int i = (current.length() - 1) < MAX_LINE_LENGTH ? current.length() - 1 : MAX_LINE_LENGTH; i >= 0; i--) {
if (current.charAt(i) == '\n') {
result += current.substring(0, i);
current = current.substring(i + 1);
break;
} else if (isBreakingChar(current.charAt(i))) {
result += current.substring(0, i) + "\n";
current = current.substring(i + 1);
break;
} else if (i == 0 && (current.length() - 1) < MAX_LINE_LENGTH) {
result += current;
current = "";
} else {
// Line cannot be break, try to go to the right and find the next BREAKING_CHAR
}
}
}
System.out.println(result);
}
private static boolean isBreakingChar(char c) {
for (int i = 0; i < BREAKING_CHAR.length; ++i) {
if (c == BREAKING_CHAR[i]) {
return true;
}
}
return false;
}
}
If anybody is interested my final solution used Apache Commons WordUtils, thanks to Joni for pointing the WordUtils out to me.
private static String wrappify(String source, int lineLength, String eolMarker){
String[] lines = source.split(eolMarker);
StringBuffer wrappedStr = new StringBuffer();
for (String line : lines) {
if(line.length() <= lineLength){
wrappedStr.append(line + eolMarker);
}else{
wrappedStr.append(WordUtils.wrap(line, lineLength, eolMarker, true) + eolMarker);
}
}
return wrappedStr.replace(wrappedStr.lastIndexOf(eolMarker), wrappedStr.length(), "").toString();
}