Rozwiązuję sobie w wolnej chwili zadania ze SPOJ.com, i z tym: http://pl.spoj.com/problems/FR_05_02/ nie potrafię sobie poradzić. Napisałem taki kod w C:
#include <stdio.h>
#include <ctype.h>
#define MAX 1002
#define IS_UPPER 1
#define IS_LOWER 2
#define IS_DIGIT 4
#define IS_OTHER 8
void password_check(const char* pass) {
const char *p = pass;
int status = 0;
int length = 0;
while (!(status == 0xf && length > 7) && !iscntrl(*pass)) {
if(islower(*pass))
status |= IS_LOWER;
else if(isupper(*pass))
status |= IS_UPPER;
else if(isdigit(*pass))
status |= IS_DIGIT;
else
status |= IS_OTHER;
++pass;
++length;
}
if (length > 7 && status == 0xf) {
printf("%s", p);
}
}
int main(void) {
int n = 1;
char pass[MAX];
scanf("%d", &n);
fflush(stdin);
for(int i = 0; i < n; ++i) {
fgets(pass, MAX - 1, stdin);
password_check(pass);
}
return 0;
}
i taki w pythonie:
#-*- coding: utf-8 -*-
import sys
IS_DIGIT = 0x01
IS_UPPER = 0x02
IS_LOWER = 0x04
IS_OTHER = 0x08
def check_password(password):
if len(password) < 8:
return
else:
status = 0
for letter in password:
if letter.isdigit():
status |= IS_DIGIT
elif letter.isupper():
status |= IS_UPPER
elif letter.islower():
status |= IS_LOWER
elif letter.isprintable():
status |= IS_OTHER
if status == IS_DIGIT | IS_UPPER | IS_LOWER | IS_OTHER:
sys.stdout.write(password+'\n')
if __name__ == '__main__':
n = int(sys.stdin.readline())
for i in range(0, n, 1):
p = sys.stdin.readline()
check_password(p)
Ale w obu przypadkach dostaję błędną odpowiedź. Oczywiście, to że w obu przypadkach dostaję błąd, mnie dziwi, bo algorytmy wykorzystują te same filtry, ale próbowałem już różnych, i zawsze dostaję błąd.