{ }codelper
membership testlogical operatorsP0098

Password rule checker

The first line gives the number of passwords n, and each of the next n lines gives one password.
The special characters are the eight characters !@#$%^&*.
Check the following six rules for each password. 1.
Its length is at least 8 and at most 16. 2.
It has at least one uppercase English letter (A–Z). 3.
It has at least one lowercase English letter (a–z). 4.
It has at least one digit (0–9). 5.
It has at least one special character. 6.
Every character is an English letter, a digit, or a special character. If it follows every rule, print "OK"; otherwise print the numbers of all broken rules, smallest first, separated by single spaces, on one line.

Input
1 ≤ n ≤ 100, a password is 1–30 characters with no spaces (ASCII codes 33–126)
Output
one line per password: "OK" or the broken rule numbers
Example 1 · stdin
4
Abcdef1!
abc
Pass_word1
Hello12345World!!
stdout
OK
1 2 4 5
5 6
1
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

Rules 2–5 ask "is there at least one" (any); rule 6 asks "are all of them" (all).
Write rule 6 with any and a password passes as soon as one character is allowed.
You must report every broken rule, so use six separate ifs, not an else if chain; a chain reports only the first broken rule.
For the length include both ends, like 8 <= len <= 16.
Asking about a range differs by language.
Only Python can chain "A" <= c <= "Z".
In C, C++ and JS, 'A' <= c <= 'Z' is read as ('A' <= c) <= 'Z'.
In C and C++ that is always true (recent clang rejects it outright), and in JS it compares a boolean with 'Z' as numbers, so it is always false.
TS, Java and C# refuse to compile it.
So write c >= 'A' && c <= 'Z'.
And, or and not are Python and/or/not and &&/||/! elsewhere, and everywhere and binds before or (a and b or c is (a and b) or c).
"Is this character in the special list?" is Python c in s, JS/TS s.includes(c), Java s.indexOf(c) >= 0, C# s.Contains(c), C++ s.find(c) != npos, C strchr(s, c).
Craykon uses SPECIAL.Contains(c), with &&/||/! for and/or/not.

Pythonmain.py · 25 lines
SPECIAL = "!@#$%^&*"  def broken_rules(pw):    broken = []    if not 8 <= len(pw) <= 16:        broken.append(1)    if not any("A" <= c <= "Z" for c in pw):        broken.append(2)    if not any("a" <= c <= "z" for c in pw):        broken.append(3)    if not any("0" <= c <= "9" for c in pw):        broken.append(4)    if not any(c in SPECIAL for c in pw):        broken.append(5)    # 6: 모든 글자가 영어 글자·숫자·특수 글자 가운데 하나    if not all(c.isascii() and c.isalnum() or c in SPECIAL for c in pw):        broken.append(6)    return broken  n = int(input())for _ in range(n):    broken = broken_rules(input().strip())    print(" ".join(map(str, broken)) if broken else "OK")
JavaScriptmain.js · 27 lines
const lines = require("fs").readFileSync(0, "utf8").split("\n");const n = Number(lines[0]);const SPECIAL = "!@#$%^&*";const isUpper = (c) => c >= "A" && c <= "Z";const isLower = (c) => c >= "a" && c <= "z";const isDigit = (c) => c >= "0" && c <= "9";const isSpecial = (c) => SPECIAL.includes(c); function brokenRules(pw) {  const chars = [...pw];  const broken = [];  if (!(pw.length >= 8 && pw.length <= 16)) broken.push(1);  if (!chars.some(isUpper)) broken.push(2);  if (!chars.some(isLower)) broken.push(3);  if (!chars.some(isDigit)) broken.push(4);  if (!chars.some(isSpecial)) broken.push(5);  // 6: 모든 글자가 영어 글자·숫자·특수 글자 가운데 하나  if (!chars.every((c) => isUpper(c) || isLower(c) || isDigit(c) || isSpecial(c))) broken.push(6);  return broken;} const out = [];for (let i = 1; i <= n; i++) {  const broken = brokenRules(lines[i].trim());  out.push(broken.length ? broken.join(" ") : "OK");}console.log(out.join("\n"));
TypeScriptmain.ts · 28 lines
import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const n: number = Number(lines[0]);const SPECIAL = "!@#$%^&*";const isUpper = (c: string): boolean => c >= "A" && c <= "Z";const isLower = (c: string): boolean => c >= "a" && c <= "z";const isDigit = (c: string): boolean => c >= "0" && c <= "9";const isSpecial = (c: string): boolean => SPECIAL.includes(c); function brokenRules(pw: string): number[] {  const chars: string[] = [...pw];  const broken: number[] = [];  if (!(pw.length >= 8 && pw.length <= 16)) broken.push(1);  if (!chars.some(isUpper)) broken.push(2);  if (!chars.some(isLower)) broken.push(3);  if (!chars.some(isDigit)) broken.push(4);  if (!chars.some(isSpecial)) broken.push(5);  // 6: 모든 글자가 영어 글자·숫자·특수 글자 가운데 하나  if (!chars.every((c) => isUpper(c) || isLower(c) || isDigit(c) || isSpecial(c))) broken.push(6);  return broken;} const out: string[] = [];for (let i = 1; i <= n; i++) {  const broken: number[] = brokenRules(lines[i].trim());  out.push(broken.length ? broken.join(" ") : "OK");}console.log(out.join("\n"));
JavaMain.java · 44 lines
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List; public class Main {    static final String SPECIAL = "!@#$%^&*";     static List<Integer> brokenRules(String pw) {        boolean upper = false, lower = false, digit = false, special = false, allAllowed = true;        for (char c : pw.toCharArray()) {            boolean u = c >= 'A' && c <= 'Z', l = c >= 'a' && c <= 'z', d = c >= '0' && c <= '9';            boolean s = SPECIAL.indexOf(c) >= 0;            upper |= u;            lower |= l;            digit |= d;            special |= s;            // 6: 모든 글자가 넷 중 하나 — 하나라도 아니면 거짓            if (!(u || l || d || s)) allAllowed = false;        }        List<Integer> broken = new ArrayList<>();        if (!(pw.length() >= 8 && pw.length() <= 16)) broken.add(1);        if (!upper) broken.add(2);        if (!lower) broken.add(3);        if (!digit) broken.add(4);        if (!special) broken.add(5);        if (!allAllowed) broken.add(6);        return broken;    }     public static void main(String[] args) throws IOException {        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));        int n = Integer.parseInt(in.readLine().trim());        StringBuilder out = new StringBuilder();        for (int i = 0; i < n; i++) {            List<Integer> broken = brokenRules(in.readLine().trim());            if (broken.isEmpty()) out.append("OK");            else for (int j = 0; j < broken.size(); j++) out.append(j > 0 ? " " : "").append(broken.get(j));            out.append('\n');        }        System.out.print(out);    }}
C#Main.cs · 36 lines
using System;using System.Collections.Generic;using System.Linq; class Program{    const string Special = "!@#$%^&*";     static bool IsUpper(char c) => c >= 'A' && c <= 'Z';    static bool IsLower(char c) => c >= 'a' && c <= 'z';    static bool IsDigit(char c) => c >= '0' && c <= '9';    static bool IsSpecial(char c) => Special.Contains(c);     static List<int> BrokenRules(string pw)    {        var broken = new List<int>();        if (!(pw.Length >= 8 && pw.Length <= 16)) broken.Add(1);        if (!pw.Any(IsUpper)) broken.Add(2);        if (!pw.Any(IsLower)) broken.Add(3);        if (!pw.Any(IsDigit)) broken.Add(4);        if (!pw.Any(IsSpecial)) broken.Add(5);        // 6: 모든 글자가 영어 글자·숫자·특수 글자 가운데 하나        if (!pw.All(c => IsUpper(c) || IsLower(c) || IsDigit(c) || IsSpecial(c))) broken.Add(6);        return broken;    }     static void Main()    {        int n = int.Parse(Console.ReadLine());        for (int i = 0; i < n; i++)        {            var broken = BrokenRules(Console.ReadLine().Trim());            Console.WriteLine(broken.Count == 0 ? "OK" : string.Join(" ", broken));        }    }}
C++main.cpp · 37 lines
#include <algorithm>#include <iostream>#include <string>#include <vector> const std::string SPECIAL = "!@#$%^&*"; bool is_upper(char c) { return c >= 'A' && c <= 'Z'; }bool is_lower(char c) { return c >= 'a' && c <= 'z'; }bool is_digit(char c) { return c >= '0' && c <= '9'; }bool is_special(char c) { return SPECIAL.find(c) != std::string::npos; } std::vector<int> broken_rules(const std::string& pw) {    std::vector<int> broken;    if (!(pw.size() >= 8 && pw.size() <= 16)) broken.push_back(1);    if (std::none_of(pw.begin(), pw.end(), is_upper)) broken.push_back(2);    if (std::none_of(pw.begin(), pw.end(), is_lower)) broken.push_back(3);    if (std::none_of(pw.begin(), pw.end(), is_digit)) broken.push_back(4);    if (std::none_of(pw.begin(), pw.end(), is_special)) broken.push_back(5);    // 6: 모든 글자가 영어 글자·숫자·특수 글자 가운데 하나    if (!std::all_of(pw.begin(), pw.end(), [](char c) { return is_upper(c) || is_lower(c) || is_digit(c) || is_special(c); }))        broken.push_back(6);    return broken;} int main() {    int n;    std::cin >> n;    for (int i = 0; i < n; i++) {        std::string pw;        std::cin >> pw;        std::vector<int> broken = broken_rules(pw);        if (broken.empty()) std::cout << "OK";        for (std::size_t j = 0; j < broken.size(); j++) std::cout << (j ? " " : "") << broken[j];        std::cout << '\n';    }}
Cmain.c · 40 lines
#include <stdio.h>#include <string.h> static const char *SPECIAL = "!@#$%^&*"; // c가 SPECIAL에 있나. strchr는 c == '\0'일 때도 끝의 '\0'을 찾아 주므로 c를 먼저 본다static int is_special(char c) { return c != '\0' && strchr(SPECIAL, c) != NULL; } int main(void) {    int n;    char pw[32];    scanf("%d", &n);    for (int i = 0; i < n; i++) {        scanf("%31s", pw);        int len = strlen(pw);        int upper = 0, lower = 0, digit = 0, special = 0, all_allowed = 1;        for (int j = 0; j < len; j++) {            char c = pw[j];            int u = c >= 'A' && c <= 'Z', l = c >= 'a' && c <= 'z', d = c >= '0' && c <= '9', s = is_special(c);            upper |= u;            lower |= l;            digit |= d;            special |= s;            if (!(u || l || d || s)) all_allowed = 0;        }        int broken[6], k = 0;        if (!(len >= 8 && len <= 16)) broken[k++] = 1;        if (!upper) broken[k++] = 2;        if (!lower) broken[k++] = 3;        if (!digit) broken[k++] = 4;        if (!special) broken[k++] = 5;        if (!all_allowed) broken[k++] = 6;        if (k == 0) puts("OK");        else {            for (int j = 0; j < k; j++) printf(j ? " %d" : "%d", broken[j]);            putchar('\n');        }    }    return 0;}
Craykonmain.cray · 66 lines
SPECIAL = "!@#$%^&*"; 어긴규칙(pw){    upper = false;    lower = false;    digit = false;    special = false;    allAllowed = true;    foreach (c in pw)    {        u = c >= "A" && c <= "Z";        l = c >= "a" && c <= "z";        d = c >= "0" && c <= "9";        s = SPECIAL.Contains(c);        upper = upper || u;        lower = lower || l;        digit = digit || d;        special = special || s;        // 6: 모든 글자가 넷 중 하나 — 하나라도 아니면 거짓        if (!(u || l || d || s))        {            allAllowed = false;        }    }    broken = [];    if (!(pw.Length >= 8 && pw.Length <= 16))    {        broken.Add(1);    }    if (!upper)    {        broken.Add(2);    }    if (!lower)    {        broken.Add(3);    }    if (!digit)    {        broken.Add(4);    }    if (!special)    {        broken.Add(5);    }    if (!allAllowed)    {        broken.Add(6);    }    return broken;} n = Console.ReadLine().ToNumber();for (i = 0; i < n; i++){    broken = 어긴규칙(Console.ReadLine().Trim());    if (broken.Count == 0)    {        Console.WriteLine("OK");    }    else    {        Console.WriteLine(broken.Join(" "));    }}