Looking for numbers in a string?

I am working on an algorithm, and I need to be able to pass to a List and see if there are four numbers in a string at any point in the list.

I tried my best to do it ... Here is the main idea. I would like the fourNumbersInARow () method to return true:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class Numbers {

    /**
     * @param args
     */
    public static void main(String[] args) {
        List<Integer> numbers = new ArrayList<Integer>();
        for(int i = 0; i<10; i++){
            numbers.add((new Random().nextInt()));
        }
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);

        System.out.println(fourNumbersInARow());

    }

    private static boolean fourNumbersInARow() {


    }

}
+3
source share
5 answers

An implementation in Java is implemented here.

static boolean fourNumbersInARow(List<Integer> list) {
    int last = 0xFACADE;  // can be any number
    int count = 0;        // important!
    for (int i : list) {
        if (i == last + 1) {
            if (++count == 4) return true;
        } else {
            count = 1;
        }
        last = i;
    }
    return false;
}

Unlike others, this resets the number of numbers in a line to 1when the sequence is interrupted (because the number itself is a number 1in the line). This allows us to simplify the processing of the first iteration, where technically there is no previous number.

+1
source

: last_value row_count. , , , last_value; , row_count, , reset 1. last_value . row_count 4, true. , false.

EDIT: , 1

+7

:

consecutiveCount = 1
lastNumber = firstElementInList(list)

for (number in list.fromSecondElement()):
  if (number - lastNumber == 1):
    consecutiveCount++
  else:
    consecutiveCount = 1

  if (consecutiveCount == 4):
    return true

  lastNumber = number

return false

, , , , , 1. , , lastNumber.

, , , , consecutiveCount.

, , true.

+1

, . . , , , , , .. , , true. , , ( ) .

0

Check this code, it will return true if there is a sequence of 4 digits, otherwise false otherwise

public class FindFourSequence {

    public boolean isFourinRow(ArrayList seqList) {

        boolean flag = false;
        int tempValue = 0;
        int tempValue2 = 0;
        int tempValue3 = 0;
        int tempValue4 = 0;
        Iterator iter = seqList.iterator();
        while(iter.hasNext()){
            String s1 = (String)iter.next();
            tempValue=Integer.valueOf(s1).intValue();
            if(!(iter.hasNext())){
                break;
            }
            String s2 = (String)iter.next();
            tempValue2=Integer.valueOf(s2).intValue();
            if(((tempValue2-tempValue)==1) || (tempValue-tempValue2)==1){
                if(!(iter.hasNext())){
                    break;
                }
                String s3 = (String)iter.next();
                tempValue3=Integer.valueOf(s3).intValue();
                if((tempValue3-tempValue2)==1 || (tempValue2-tempValue3)==1){
                    if(!(iter.hasNext())){
                        break;
                    }
                    String s4 = (String)iter.next();
                    tempValue4=Integer.valueOf(s4).intValue();
                    if((tempValue3-tempValue4==1) || (tempValue4-tempValue3)==1){
                        flag = true;
                        return flag;
                    }
                }
            }
        }

        return flag;
    }

    public static void main(String[] args) throws Exception {

        ArrayList aList = new ArrayList();
        boolean flag = false;
        FindFourSequence example = new FindFourSequence();
        Random random = new Random();
        for (int k = 0; k < 25; k++) {
            int number = random.nextInt(20);
            System.out.println(" the Number is :" + number);
            aList.add("" + number);
        }
/*      aList.add("" + 1);
        aList.add("" + 2);
        aList.add("" + 3);
        aList.add("" + 4);*/
        flag = example.isFourinRow(aList);
        System.out.println(" the result value is : " + flag);

    }
}
0
source

Source: https://habr.com/ru/post/1735765/


All Articles