check if array contains certain int BEFORE another int - java

I need to check if an array contains a 1 and then later in the array contains a 2.
What I have coded only checks if both are in there, not if one is before the other. How could I do this?
if(array[i] == 1)
count++;
else if(array[i] == 2)
count++;
}
if(count > 1)
System.out.print("true");
else
System.out.print("false");
Comparing the index of the values works!
if (nums[i] == 1)
value1 = i;
else if(nums[i] == 2)
value2 = i;
}
if (value2 > value1)
System.out.print("true");
else
System.out.print("false");

This oughta do it!
public void hasOneThenTwo(int[] a) {
bool hasOne = false;
for (int i = 0; i < a.length; ++i) {
if (!hasOne && a[i] == 1) {
hasOne = true;
} else if (hasOne && a[i] == 2) {
return true;
}
}
return false;
}

Related

How to check if a coordinate is adjacent to another?

I am writing a snake game. I want to write a method to check if the head (index 0) is next to any other body part. I am not sure what is going wrong, but this method does not do anything. I have it set so that if it returns true the game ends (for testing purposes)
Here is my code:
public boolean headNextToBody()
{
boolean xClose = false;
boolean yClose = false;
for(int i = 0; i < bodyParts; i++)
{
if( x[i] == (x[0] + 1) || x[i] == (x[0] - 1))
{
xClose = true;
}
if(y[i] == y[0] + 1 || y[i] == y[0] - 1)
{
yClose = true;
}
}
return (xClose && yClose);
}
Firstly, no need to include the head (at position 0) in the test, as it will always be false.
for(int i = 1; i < bodyParts; i++)
The conditions for adjacency are
(y[i] == y[0]) && (x[i] == (x[0] + 1) || x[i] == (x[0] - 1))
or
(x[i] == x[0]) && (y[i] == (y[0] + 1) || y[i] == (y[0] - 1))
If either of these are true for any non-head point on the snake you can return true immediately, otherwise return false.
You can combine the separate tests into a single if statement:
public boolean headNextToBody()
{
for(int i = 1; i < bodyParts; i++)
{
if(((y[i] == y[0]) && (x[i] == (x[0] + 1) || x[i] == (x[0] - 1))) ||
((x[i] == x[0]) && (y[i] == (y[0] + 1) || y[i] == (y[0] - 1)))
{
return true;
}
}
return false;
}
Alternatively you could shorten the test a little by using some math:
public boolean headNextToBody()
{
for(int i = 1; i < bodyParts; i++)
{
if(1 == ((x[i]-x[0])*(x[i]-x[0]) + (y[i]-y[0])*(y[i]-y[0]))
{
return true;
}
}
return false;
}

Finding values in multi dimensional array that sloped by number using recursion (Java)

Im trying to make a multi dimensional array that contains numbers and I need to find the number neighbors (down or right) that slope by num(variable)
example :
{7,5,3},{1,5,9}
the numbers 3,5,7 are sloped by 2 and the counter is 3 ( 3 numbers )
I need to find the longest slope which means that if there slope with 3 numbers and also after that slope with 6 numbers I need to return the 6 numbers..
I tried already for about 5 hours but im completely lost , here is my code until now :
private static int longestSlope (int [][] mat, int num , int i , int j , int count , int temp,int oldi,int oldj)
{
System.out.println("oldi " +oldi);
System.out.println("oldj " +oldj);
System.out.println("temp " +temp);
System.out.println("count "+count);
System.out.println("i "+i);
System.out.println("j "+j);
if(i < mat.length-1 && j < mat[0].length-1 )
{
if(j < mat[0].length-1 && mat[i][j] - num == mat[i][j+1] )
{
if(temp == 0)
{
oldj = j;
oldi = i;
}
if(j == mat[0].length-1)
{
return longestSlope(mat,num,i,j,count,temp+1,oldi,oldj);
}
else
{
temp = longestSlope(mat,num,i,j+1,count,temp+1,oldi,oldj);
}
}
else if(i < mat.length-1 && mat[i][j] - num == mat[i+1][j])
{
if(temp == 0)
{
oldj = j;
oldi = i;
}
temp = longestSlope(mat,num,i+1,j,count,temp+1,oldi,oldj);
}
else
temp = longestSlope(mat,num,i,j+1,count,temp,oldi,oldj);
}
else if(i < mat.length-1 && j == mat[0].length-1 && temp == 0)
{
temp = longestSlope(mat,num,i+1,0,count,0,oldi,oldj);
}
else if(temp > count)
{
count = temp;
System.out.println("nihnas "+count);
return longestSlope(mat,num,oldi,oldj+1,count,0,0,0);
}
else if(temp < count)
{
longestSlope(mat,num,oldi,oldj+1,count,0,0,0);
}
return 0;
}

Java - Roman Numeral validity

I am writing a program to add two roman numerals without converting to any bases. I have everything working except I am not sure how to check if my input String is a valid roman numeral or not.
These are the rules to check validity:
Five in a row of any digit is not allowed
Some digits are allowed in runs of up to 4. They are I,X,C, and M. The others (V,L,D) can only appear singly.
Some lower digits can come before a higher digit, but only if they appear singly. E.g. "IX" is ok but "IIIX" is not.
But this is only for pairs of digits. Three ascending numbers in a row is invalid. E.g. "IX" is ok but "IXC" is not.
A single digit with no runs is always allowed
I have not really been able to make much progress with this step and don't have anything working yet. Any help would be great!
Why not use regular expression:
boolean valid = word.matches("^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$");
Look at the post of paxdiablo:
How do you match only valid roman numerals with a regular expression?
Loop through each character in the string.
Use a if conditions to check which character it is.
Use the if conditions to check for the roman numeral rule violations in the adjacent characters to the selected character.
for(int i = 0; i < s.length(); i++){
if (s[i] == 'V'){
**Check if the character before of after is also 'V'. Then it is a violation
}
else if(s[i] == 'L'){
**Conditions for 'L' etc.
}
}
This is what I have come up with based on my rules. Any thoughts on refactoring this to make it simpler?
public static boolean checkValidity (String s1, HashSet<Character> romanNumerals){
HashSet<Character> alreadyContained = new HashSet<Character>();
if (s1.length() == 1 && romanNumerals.contains(s1.charAt(0))){
return true;
}
int i = 0;
while (i < s1.length()){
if (s1.charAt(i) == 'M'){
if (alreadyContained.contains('M')){
return false;
}
int count = 1;
i++;
while (s1.charAt(i) == 'M'){
i++;
count++;
}
alreadyContained.add('M');
if (count >= 5){
return false;
}
}
else if (s1.charAt(i) == 'D'){
if (alreadyContained.contains('D')){
return false;
}
alreadyContained.add('D');
if (!alreadyContained.contains('M')){
alreadyContained.add('M');
}
i++;
if ((i < s1.length()) && (s1.charAt(i) == 'D')){
return false;
}
}
else if (s1.charAt(i) == 'L'){
if (alreadyContained.contains('L')){
return false;
}
alreadyContained.add('L');
if (!alreadyContained.contains('M')){
alreadyContained.add('M');
}
if (!alreadyContained.contains('D')){
alreadyContained.add('D');
}
if (!alreadyContained.contains('C')){
alreadyContained.add('C');
}
i++;
if ((i < s1.length()) && (s1.charAt(i) == 'L')){
return false;
}
}
else if (s1.charAt(i) == 'V'){
if (alreadyContained.contains('V')){
return false;
}
alreadyContained.add('V');
if (!alreadyContained.contains('M')){
alreadyContained.add('M');
}
if (!alreadyContained.contains('D')){
alreadyContained.add('D');
}
if (!alreadyContained.contains('C')){
alreadyContained.add('C');
}
if (!alreadyContained.contains('L')){
alreadyContained.add('L');
}
if (!alreadyContained.contains('X')){
alreadyContained.add('X');
}
i++;
if ((i < s1.length()) && (s1.charAt(i) == 'V')){
return false;
}
}
else if (s1.charAt(i) == 'C'){
if (alreadyContained.contains('C')){
return false;
}
int count = 1;
i++;
if ((i < s1.length()) &&(s1.charAt(i) == 'M' || s1.charAt(i) == 'D')){
i++;
}
else if (i < s1.length() && s1.charAt(i) == 'C'){
while ((i < s1.length()) && (s1.charAt(i) == 'C')){
i++;
count++;
}
}
alreadyContained.add('C');
if (!alreadyContained.contains('M')){
alreadyContained.add('M');
}
if (!alreadyContained.add('D')){
alreadyContained.add('D');
}
if (count >= 5){
return false;
}
}
else if (s1.charAt(i) == 'X'){
if (alreadyContained.contains('X')){
return false;
}
int count = 1;
i++;
if ((i < s1.length()) && (s1.charAt(i) == 'D' || s1.charAt(i) == 'M')){
return false;
}
while ((i < s1.length()) && s1.charAt(i) == 'X'){
i++;
count++;
}
alreadyContained.add('X');
if (count >= 5){
return false;
}
}
else if (s1.charAt(i) == 'I'){
if (alreadyContained.contains('I')){
return false;
}
alreadyContained.add('I');
i++;
int count = 1;
if ((i < s1.length()) && (s1.charAt(i) != 'I' && s1.charAt(i) != 'X' && s1.charAt(i) != 'V')){
return false;
}
else if (i < s1.length() && s1.charAt(i) == 'I'){
while (i < s1.length() && s1.charAt(i) == 'I'){
i++;
count++;
}
if (count >= 4){
return false;
}
}
}
else if (!romanNumerals.contains(s1.charAt(i))){
return false;
}
}
return true;
}

Why doesn't this code work? FizzBuzz JAVA

I cant get "FizzBuzz". No matter what the input, the "FizzBuzz" code isn't running. What did I do wrong?
public String[] fizzBuzz(int start, int end) {
int diff = end-start;
String[] array = new String[diff];
for (int i = 0; i < diff; i++) {
if (start%3 == 0 && start%5 == 0) array[i] = "FizzBuzz";
if (start%3 == 0 || start%5 == 0) {
if (start%3 == 0) array[i] = "Fizz";
if (start%5 == 0) array[i] = "Buzz";
}
else {
array[i] = String.valueOf(start);
}
start++;
}
return array;
}
Logic in your if statements is a bit busted, using your code as the starting point, you'd have to do something like this.
if (start%3 == 0 && start%5 == 0) {
array[i] = "FizzBuzz";
}
else if (start%3 == 0 || start%5 == 0) {
if (start%3 == 0) array[i] = "Fizz";
if (start%5 == 0) array[i] = "Buzz";
}
else {
array[i] = String.valueOf(start);
}
String s = "" + i;
if ((i % 3) == 0) {
s += " Fizz";
}
if ((i % 5) == 0) {
s+= " Buzz";
}
System.out.println(s);
This code snippet placed in a loop will print Fizz, Buzz and Fizz Buzz on i divisible by 3, 5 and 15 respectively.
you should try this.
class FizzBuzz{
public static void main(String args[]){
int n = 100;
for(int i=0;i<=n;i++){
if((i % 3) == 0 && (i % 5) != 0){
System.out.println("Fizz");
}
else if((i % 5) == 0 && (i % 3) != 0){
System.out.println("Buzz");
}else if((i % 3) == 0 && (i % 5) == 0){
System.out.println("FizzBuzz");
}else{
System.out.println(""+i);
}
}
}
}

Java, return true if 'x' is next to 'y' in an array

This is, I imagine, a really simple problem to solve however I just can't figure it out.
An array contains a list of integers and I want to return true if every number 'x' in the array is followed by the number 'y'.
So arrays with {x,3,4,y} or {x,x,y,4,5} or {5,8,x,x} would be false.
Whereas arrays with {x,y,4,1} or {x,y,5,1,x,y} would be true.
This is what I have tried so far:
for (int i = 0; i < nums.length-1; i++)
{
if (nums[i] == x && nums[i+1] == y)
{
return true;
}
else
{
return false;
}
}
return false;
My code however will only work for the first two elements in the array (so 0 and 1). It won't detect any integers further down in the array, so how do I do this?
Thank you.
I want to return true if every number 'x' in the array is followed by the number 'y'.
You need to get rid of the else and modify the checks like so:
for (int i = 0; i < nums.length - 1; i++)
{
if (nums[i] == x && nums[i + 1] != y)
{
return false;
}
}
return true;
Caveats:
This returns true if x is not present in the array. It's unclear from the question whether this is the behaviour you want.
This does not check whether x is the last element of the array. Again, it's not entirely clear what you'd expect to happen if it is.
When the code reaches a return statement it stops executing the function and returns the specified value, that's why your for is only executed once.
Since you want the condition to apply to ALL the elements in the array, you have to return false when you find a 2 that isn't followed by a 3. If the 2 is followed by a 3, you just keep checking the next position.
Like this:
for (int i = 0; i < nums.length-1; i++)
{
if(nums[i] == 2 && nums[i+1] != 3)
{
return false;
}
}
return true;
for (int i = 0; i < nums.length-1; i++) {
if (nums[i] == 2 && nums[i+1] != 3) {
return false;
}
}
return true;
This code will return False iff the 'x' number is not followed by 'y'.
for (int i = 0; i < nums.length-1; i++)
{
if(nums[i] == 2 && nums[i+1] != 3) {
return false;
}
}
return true;
for (int i = 0; i < nums.length-1; i++)
{
if(nums[i] == 2 && nums[i+1] == 3)
{
return true;
}
}
return false;
Your Code is returning in the first iteration always. because you have return written there in both If and else block.
remove the else case or just the return; from else case if there is any other logic inside.
int x = 2;
int y=3;
for (int i = 0; i < nums.length-1; i++) {
if (nums[i] == x && nums[i+1] != y) {
return false;
}
}
return true;
If you do not have x in the array, it will return true. and if you have any x that does not have y after it, it will return false.
Try out Following
boolean flag = false;
for (int i = 0; i <= (nums.length-1); i++) {
if(nums[i] == x) {
if (i == (nums.length-1)) {
if(nums[i] == x) {
flag = false;
}
} else {
if(nums[i+1] == y) {
flag = true;
} else {
flag = false;
break;
}
}
}
}
return flag;
You can try out the following:
if(nums[nums.length - 1] == x)
{
return false;
}
else
{
boolean flag = false;
for(i = 0; i < nums.length - 1; i++)
{
if(nums[i] == x)
{
if(nums[i + 1] == y)
{
flag = true;
}
else
{
flag = false;
break;
}
}
}
return flag;
}

Categories