
Untitled
By: a guest on
May 17th, 2012 | syntax:
None | size: 1.77 KB | hits: 17 | expires: Never
Password checking with prolog/2
contains_letter(Password) :- wildcard_match('*[a-zA-Z]*', Password).
contains_number(Password) :- wildcard_match('*[0-9]*', Password).
contains_double_letter(Password) :-
(between(65, 90, Letter) ; between(97, 122, Letter)),
append([_, [Letter, Letter], _], Password),
!.
starts_with_letter(Password) :- wildcard_match('[a-zA-Z]*', Password).
long_enough(Password) :-
length(Password, Length),
Length >= 6.
check_everything(Password) :-
contains_letter(Password),
contains_number(Password),
contains_double_letter(Password),
starts_with_letter(Password),
long_enough(Password).
uniq([], []).
uniq([X], [X]).
uniq([X,X|L], R) :-
!,
uniq([X|L], R).
uniq([X,Y|L], [X|R]) :-
uniq([Y|L], R).
lengthWithDoubleLetters([],0).
lengthWithDoubleLetters([F,F|T],C) :-
lengthWithDoubleLetters(T,TC),
!,
C is TC + 1.
lengthWithDoubleLetters([H|T], C) :-
lengthWithDoubleLetters(T,TC),
C is TC + 1.
contains_letter(Password) :- wildcard_match('*[a-zA-Z]*', Password).
contains_number(Password) :- wildcard_match('*[0-9]*', Password).
starts_with_letter(Password) :- wildcard_match('[a-zA-Z]*', Password).
letter_start_and_number(Password) :-
wildcard_match('[a-zA-Z]*[0-9]*', Password).
length_double_letters([], Acc, yes, Acc).
length_double_letters([Char, Char|Password], Acc, _Contains, Length) :-
!,
NewAcc is Acc + 1,
length_double_letters(Password, NewAcc, yes, Length).
length_double_letters([_Char|Password], Acc, Contains, Length) :-
NewAcc is Acc + 1,
length_double_letters(Password, NewAcc, Contains, Length).
check_everything(Password) :-
letter_start_and_number(Password),
length_double_letters(Password, 0, no, Length),
Length >= 6.