Author Topic: JavaBat / CodingBat Answers  (Read 221316 times)

Crashq3

  • Newbie
  • *
  • Posts: 6
    • View Profile
JavaBat / CodingBat Answers
« Reply #60 on: October 22, 2009, 09:08:01 AM »
int1to10

Code: [Select]
public boolean in1To10(int n, boolean outsideMode) {
 if (!outsideMode && n >= 1 && n <= 10)
    {
    return true;
    }
  if (outsideMode && n <= 1 || outsideMode && n >= 10)
    {
    return true;
    }
  else
    {
    return false;
    }
}


loneSum

Code: [Select]
public int loneSum(int a, int b, int c) {
  if (a != b && b != c && a != c) { return a + b + c; } if (a == b && a != c) { return c; } if (a == c && a != b) { return b; } if (b == c && b != a) { return a; } else { return 0; }
}


evenlySpaced

Code: [Select]
public boolean evenlySpaced(int a, int b, int c) {
  if (a - b == b - c || b - c == c - a || c - a == a - b)
    {
    return true;
    }
return false;
   
}


there ya go.

and if anyone could help me with a few in String-2 that would be awesome.

prefixAgain
xyzMiddle
getsandwich
samestarchar
zipzap
wordends

thanks guys.

lucky_9972

  • Newbie
  • *
  • Posts: 4
    • View Profile
JavaBat / CodingBat Answers
« Reply #61 on: October 22, 2009, 12:13:45 PM »
thanks guys.
can I get your email? Because I need to help with programming in the future, that's why i need to contact with you.

zlools

  • Administrator
  • Newbie
  • *****
  • Posts: 2275
    • View Profile
JavaBat / CodingBat Answers
« Reply #62 on: October 22, 2009, 02:17:41 PM »
Quote
and if anyone could help me with a few in String-2 that would be awesome.

prefixAgain
xyzMiddle
getsandwich
samestarchar
zipzap
wordends

thanks guys.




sameStarChar:
Code: [Select]
public boolean sameStarChar(String str) {
  boolean check = true;
  for(int i = 0; i < str.length(); i++) {
    if(i != str.length() - 1 && Character.toString(str.charAt(i)).equals("*")) {
      if(i != 0 && Character.toString(str.charAt(i - 1)).equals(Character.toString(str.charAt(i + 1)))) {
      } else if(i == 0) {
      } else {
        return false;
      }
    }
  }
  return true;
}


I really don't have time to do the rest right now, but when I do I will post them.

Crashq3

  • Newbie
  • *
  • Posts: 6
    • View Profile
JavaBat / CodingBat Answers
« Reply #63 on: October 26, 2009, 06:02:24 PM »
thanks, any luck on the rest of those?

they are do tomorrow and i cant seem to get em.

thanks

Crash.

lucky_9972

  • Newbie
  • *
  • Posts: 4
    • View Profile
JavaBat / CodingBat Answers
« Reply #64 on: October 27, 2009, 02:35:33 AM »
same me too :(

i need to figure it out for while loop problems in javabat:

xyzMiddle
sameStarChar
starOut
wordEnds

can anyone help me with this?

Crashq3

  • Newbie
  • *
  • Posts: 6
    • View Profile
JavaBat / CodingBat Answers
« Reply #65 on: November 09, 2009, 04:37:59 PM »
any luck on those string 2?

zlools

  • Administrator
  • Newbie
  • *****
  • Posts: 2275
    • View Profile
JavaBat / CodingBat Answers
« Reply #66 on: November 10, 2009, 12:26:49 AM »
Quote from: "Crashq3"
any luck on those string 2?

When are they due?

Crashq3

  • Newbie
  • *
  • Posts: 6
    • View Profile
JavaBat / CodingBat Answers
« Reply #67 on: November 11, 2009, 08:13:58 PM »
a few days ago haha,and had no luck, i suck with strings :/... but ive finished almost all array 3 lol

Crashq3

  • Newbie
  • *
  • Posts: 6
    • View Profile
JavaBat / CodingBat Answers
« Reply #68 on: November 11, 2009, 09:31:31 PM »
sorry for double post, but i still need em done incase that wasnt clear.

zlools

  • Administrator
  • Newbie
  • *****
  • Posts: 2275
    • View Profile
JavaBat / CodingBat Answers
« Reply #69 on: November 11, 2009, 11:42:31 PM »
xyzMiddle String - 2
Code: [Select]
public boolean xyzMiddle(String str) {
 for(int i = 0; i < str.length(); i++) {
   if(i + 3 > str.length()) {
   } else {
    if(str.substring(i, i + 3).equals("xyz") &&
       (i == str.length() - (i + 3) ||
       i - 1 == str.length() - (i + 3) ||
       i + 1 == str.length() - (i + 3))) {
      return true;
    }
   }
 }
  return false;
}


wordEnds - String 2
Code: [Select]
public String wordEnds(String str, String word) {
    String result = "";
 for(int i = 0; i < str.length(); i++) {
   if(word.length() == str.length()) {
     return "";
   } else if(i + word.length() == str.length() && str.substring(i, i + word.length()).equals(word)) {
     result += Character.toString(str.charAt(str.length() - word.length() - 1));
     return result;
   } else if(i + word.length() < str.length() && str.substring(i, i + word.length()).equals(word)) {
      if(i == 0) {
      } else {
        result += Character.toString(str.charAt(i - 1));
      }
     
      result += Character.toString(str.charAt(i + word.length()));
     
    }
  }
  return result;
}


prefixAgain - String 2
Code: [Select]
public boolean prefixAgain(String str, int n) {
   String check = str.substring(0, n);
   
   if(str.length() == 2 && Character.toString(str.charAt(0)).equals(Character.toString(str.charAt(1)))) {
     return true;
   }
   
   for(int i = n; i + n < str.length(); i++) {
     if(str.substring(i, i + n).equals(check)) {
       return true;
     }
   }
  return false;
}


getSandwich - String 2
Code: [Select]
public String getSandwich(String str) {
  String result = "";
  boolean first = false, bread = false;
  int lengthb = 0;
 
  if(str.length() > 10  && str.substring(0, 5).equals(str.substring(str.length() - 5, str.length()))) {
    return str.substring(5, str.length() - 5);
  }
 
  for(int i = 0; i + 5 < str.length(); i++) {
    if(first) {
      if(str.substring(i, i + 5).equals("bread")) {
        lengthb = result.length();
        bread = true;
      }
     
      result += Character.toString(str.charAt(i));
    } else {
      if(str.substring(i, i + 5).equals("bread")) {
        i += 4;
        first = true;
      }
    }
  }
  if(bread) {
   result = result.substring(0, lengthb);
  }
  return result;
}


zipZap - String 2
Code: [Select]
public String zipZap(String str) {
  String original = str;
  boolean found = false;
  if(str.length() == 3) {
    if(Character.toString(str.charAt(0)).equals("z") && (Character.toString(str.charAt(2)).equals("p"))) {
      return "zp";
    }
  } else if(str.length() < 3) {
    return str;
  }
 
  for(int i = 0; i + 3 < str.length(); i++) {
    if(Character.toString(str.charAt(i)).equals("z") && Character.toString(str.charAt(i + 2)).equals("p")) {
     str = str.substring(0, i) + Character.toString(str.charAt(i)) + Character.toString(str.charAt(i + 2)) + str.substring(i + 3, str.length());
     found = true;
    } else {
    }
  }
 
  if(Character.toString(str.charAt(str.length() - 3)).equals("z") && Character.toString(str.charAt(str.length() - 1)).equals("p")) {
      return str = str.substring(0, str.length() - 3) + Character.toString(str.charAt(str.length() - 3)) + Character.toString(str.charAt(str.length() - 1));  
   }
 
  if(found) {
return str;
  } else {
    return original;
  }
}


There you go. Sorry about taking so long, but I've had a ton of homework because of college. I hope I didn't cause you to get a bad grade or anything :(. Anyways, enjoy and good luck :D.

Crashq3

  • Newbie
  • *
  • Posts: 6
    • View Profile
JavaBat / CodingBat Answers
« Reply #70 on: November 12, 2009, 07:08:24 PM »
not a problem m8. and no grades an A xD.
its wierd, i found those harder then string 3 lol. think u could help me out with the last 2 array problems i got?
if u can thay would be:

MaxMirror and SquareUp.

all the other problems are done lol

hkkm

  • Newbie
  • *
  • Posts: 3
    • View Profile
JavaBat / CodingBat Answers
« Reply #71 on: November 14, 2009, 11:07:24 PM »
Can anyone know how to do this Javabat plusout problem. I am not allowed to use Startwith code.

String where all chars have been replaced by pluses ("+"), except for appearances of the word string which are preserved unchanged.

plusOut("12xy34", "xy") → "++xy++"
plusOut("12xy34", "1") → "1+++++"
plusOut("12xy34xyabcxy", "xy") → "++xy++xy+++xy"

I work hard but fail to get all right.

public String plusOut(String str, String word)
{
String result="";
for(int i=0; i<str.length()-word.length()+1; i++)
if(str.substring(i,i+word.length()).equals(word))
{
result += word;
i++;
}
else
{
result += "++" ;
i++;
}
return result;
}

zlools

  • Administrator
  • Newbie
  • *****
  • Posts: 2275
    • View Profile
JavaBat / CodingBat Answers
« Reply #72 on: November 14, 2009, 11:42:54 PM »
plusOut - String 2
Code: [Select]
public String plusOut(String str, String word)
{
  String result = "";
  for(int i = 0; i < str.length(); i++)
  {
    if(i + word.length() > str.length()) {
      break;
    }
   
    if(str.substring(i, i + word.length()).equals(word))
    {
      result += word;
      i += (word.length() - 1);
    }
    else
    {
      result += "+" ;
    }
  }
  if(result.length() != str.length()) {
     while(result.length() != str.length()) {
       result += "+";
     }
  }
  return result;
}


Please not that this is NOT a very well done version of this result. There are better ways of doing this, but this is the only solution I have time to formulate at the moment.

hkkm

  • Newbie
  • *
  • Posts: 3
    • View Profile
JavaBat / CodingBat Answers
« Reply #73 on: November 15, 2009, 03:13:37 PM »
Thanks a lot! Though I don't really get the last if-while loop, it get all right.

zlools

  • Administrator
  • Newbie
  • *****
  • Posts: 2275
    • View Profile
JavaBat / CodingBat Answers
« Reply #74 on: November 15, 2009, 05:05:18 PM »
Quote from: "hkkm"
Thanks a lot! Though I don't really get the last if-while loop, it get all right.

That's why it's not the best solution. For some reason the first loop was leaving out the last few characters of every answer, so I made it so if the result string isn't the same length of the input string then it goes back through and adds the correct values until they are same length.