Advertisement
eggggggggor

valid permutations

Apr 27th, 2024 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.10 KB | None | 0 0
  1. program permutations;
  2.  
  3. function isConsonant(ch: Char): Boolean;
  4. begin
  5.     ch := UpCase(ch);
  6.     isConsonant := not (ch in ['A', 'E', 'I', 'O', 'U']);
  7. end;
  8.  
  9. function isValidChain(chain: string): Boolean;
  10. begin
  11.     isValidChain := isConsonant(chain[1]) and
  12.                     (chain[2] in ['C', 'D', 'F']) and
  13.                     ((UpCase(chain[3]) in ['A', 'E', 'I', 'O', 'U']) or (isConsonant(chain[3]) and (chain[3] <> chain[2]))) ;
  14. end;
  15.  
  16. procedure generatePermutations(prefix, remaining: string; var counter: Integer);
  17. var
  18.     i: Integer;
  19. begin
  20.     if length(prefix) = 3 then
  21.     begin
  22.         if isValidChain(prefix) then
  23.             counter := counter + 1;
  24.         Exit;
  25.     end;
  26.  
  27.     for i := 1 to length(remaining) do
  28.         generatePermutations(prefix + remaining[i], Copy(remaining, 1, i - 1) + Copy(remaining, i + 1, length(remaining)), counter);
  29. end;
  30.  
  31. var
  32.     counter: Integer;
  33.     str: string;
  34. begin
  35.     str := 'ABCDEF';
  36.     counter := 0;
  37.     generatePermutations('', str, counter);
  38.     WriteLn('Количество подходящих цепочек: ', counter);
  39. end.
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement