EVERGENCY SHUTDOWN
EVERGENCY SHUTDOWN

Reputation: 1

How to check if repeated digits in a 4-digit index appear only in a row and no more than three times

The index consists of 4 digits, the digits can be repeated, but only in a row and no more than three times. That is, 3331 is correct, 1212 is incorrect. I need to add a check that the numbers are exactly in a row.

#include <QVector>
#include "examplevalidator.h"

ExampleValidator::ExampleValidator(QObject *parent)
    : QValidator(parent) { }

ExampleValidator::~ExampleValidator() { }

QValidator::State ExampleValidator::validate(QString &input, int &pos) const {
    QVector<int> chek(10, 0);
    int lenght = input.count();

    if (lenght > 4) {
        return QValidator::Invalid;
    }

    if (input.indexOf(QString("121"))) {
        return QValidator::Invalid;
    }

    for (int i = 0; i < input.count(); i++) {
        QChar current = input.at(i);

        if (current < QChar('0') and current > QChar('9')) {
            return QValidator::Invalid;
        } else {
            int digit = current.digitValue();
            chek[digit]++;
            if (chek[digit] > 3) {
                return QValidator::Invalid;
            }
        }
    }

    if (lenght < 4) {
        return QValidator::Intermediate;
    }

    return QValidator::Acceptable;
}

I tried to add a substring check (if input.indexOf...), but nothing worked.

Upvotes: -3

Views: 126

Answers (1)

sdf46
sdf46

Reputation: 84

//Maybe something like this?
/*
  The function tries to parse the
  string str as intended by the 
  topic author.
  The limit is the length of a 
  continuous sequence in a line.
  But this will not work with a 
  string like "1122" which contains 
  more than one sequence.
  This algorithm can be used in a 
  validator implementation.  
*/

bool foo(QString str, int limit)
{
    using type=QString::size_type;
    type first{-1}, last{-1};

    /*
      Looping through the string, 
      we remember the index of the 
      beginning and end (the first 
      one encountered) of a continuous 
      sequence of identical characters.          
      Then we check the presence of 
      a character from the found 
      sequence, before and after 
      its location in the line. 
    */

    for(type i=0; i<str.length()-1; i++){
        if(str.at(i)==str.at(i+1)){
            if(first== -1){
                first=i;
                --limit;
            }
            --limit;
            last=i+1;
        }else if(first!= -1){
            break;
        }
    }

    if(limit>=0 && first>=0 && last>0 && first!=last
        && !str.left(first).contains(str.at(first))
        && !str.last(str.size()-last-1).contains(str.at(first)))
        return true;
    else
        return false;
}



qDebug()<<foo("1234", 2);   //false
qDebug()<<foo("3341", 2);   //true
qDebug()<<foo("133334", 2); //false
qDebug()<<foo("13343", 2);  //false
qDebug()<<foo("31334", 2);  //false
qDebug()<<foo("3341", 2);   //true
qDebug()<<foo("4133", 2);   //true
qDebug()<<foo("3333", 3);   //false

Upvotes: 1

Related Questions