ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 10384 ํŒฌ๊ทธ๋žจ with Python
    PS 2022. 3. 3. 19:20
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 10384 ํŒฌ๊ทธ๋žจ

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ํŒฌ๊ทธ๋žจ์€ ๋ชจ๋“  ์•ŒํŒŒ๋ฒณ์„ ์ ์–ด๋„ ํ•œ ๋ฒˆ์”ฉ์„ ์‚ฌ์šฉํ•œ ์˜์–ด ๋ฌธ์žฅ์„ ๋งํ•œ๋‹ค.

    2. ๋”๋ธ” ํŒฌ๊ทธ๋žจ์€ ๋ชจ๋“  ์•ŒํŒŒ๋ฒณ์„ ์ ์–ด๋„ ๋‘ ๋ฒˆ์”ฉ์€ ์‚ฌ์šฉํ•œ ๋ฌธ์žฅ์„ ๋งํ•˜๊ณ , ํŠธ๋ฆฌํ”Œ ํŒฌ๊ทธ๋žจ์€ ๋ชจ๋“  ์•ŒํŒŒ๋ฒณ์„ ์ ์–ด๋„ ์„ธ ๋ฒˆ์”ฉ์€ ์‚ฌ์šฉํ•œ ๋ฌธ์žฅ์„ ๋งํ•œ๋‹ค.

    3. ์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ์ค„์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋“ค๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.
      ์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์˜ ์ˆ˜ n์ด ์ฃผ์–ด์ง„๋‹ค.
      ๊ฐ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋Š” ์˜์–ด ์†Œ๋ฌธ์ž์™€ ๋Œ€๋ฌธ์ž, ํŠน์ˆ˜๊ธฐํ˜ธ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.

    4. ํŒฌ๊ทธ๋žจ์ด ์•„๋‹ ๊ฒฝ์šฐ - Not a pangram
      ํŒฌ๊ทธ๋žจ์ผ ๊ฒฝ์šฐ - Pangram!
      ๋”๋ธ” ํŒฌ๊ทธ๋žจ์ผ ๊ฒฝ์šฐ - Double pangram!!
      ํŠธ๋ฆฌํ”Œ ํŒฌ๊ทธ๋žจ์ผ ๊ฒฝ์šฐ - Triple pangram!!!

    5. ๊ตฌํ˜„ ์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin
    
    for i in range(1, int(stdin.readline()) + 1):
        res = 'Not a pangram'
        string = stdin.readline().rstrip()
    
        alphabet = [[False] * 26 for _ in range(3)]
    
        for j in string:
            idx = 0
            if not j.isalpha():
                continue
            else:
                while 1:
                    if not alphabet[idx][ord(j.lower()) - 97]:
                        alphabet[idx][ord(j.lower()) - 97] = True
                        break
                    else:
                        idx += 1
    
                    if idx > 2:
                        idx = 0
                        break
    
        cnt = 0
        for data in alphabet:
            if False in data:
                break
            else:
                cnt += 1
    
        if cnt == 1:
            res = 'Pangram!'
        elif cnt == 2:
            res = 'Double pangram!!'
        elif cnt == 3:
            res = 'Triple pangram!!!'
    
        print('Case {}: {}'.format(i, res))

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    3
    The quick brown fox jumps over a lazy dog.
    The quick brown fox jumps over a laconic dog.
    abcdefghijklmNOPQRSTUVWXYZ-zyxwvutsrqpon   2013/2014      MLKJIHGFEDCBA

    ์‹คํ–‰๊ฒฐ๊ณผ

    Case 1: Pangram!
    Case 2: Not a pangram
    Case 3: Double pangram!!

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ, ๊ฒฐ๊ณผ๊ฐ’์„ ์ €์žฅํ•  res ๋ณ€์ˆ˜๋ฅผ 'Not a pangram'์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

    2. string์— ์–ด๋–ค ๊ฒ€์‚ฌํ•  ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›๊ณ , ์•ŒํŒŒ๋ฒณ์„ ๋ช‡๋ฒˆ ์ผ๋Š”์ง€ ๊ฒ€์‚ฌํ•  alphabet ์ด๋ผ๋Š” 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.

    3. string์„ ์ˆœํšŒํ•˜๋ฉด์„œ ์•ŒํŒŒ๋ฒณ์ด ์•„๋‹ ๊ฒฝ์šฐ continue, ์•ŒํŒŒ๋ฒณ์ผ ๊ฒฝ์šฐ ํ•ด๋‹น ์•ŒํŒŒ๋ฒณ์„ True๋กœ ์ฒดํฌํ•ด์ฃผ๋ฉด๋œ๋‹ค.

    4. ์ตœ๋Œ€ ํŠธ๋ฆฌํ”Œ ํŒฌ๊ทธ๋žจ๊นŒ์ง€ ๋ฐ–์— ์—†์œผ๋‹ˆ idx ๋ฅผ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ 2๊นŒ์ง€ ๋Š˜๋ ค์ฃผ๊ณ , ๋‹ค์‹œ 0์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์ž‘์—…์„ ํ†ตํ•ด์„œ ์•ŒํŒŒ๋ฒณ์ด ์‚ฌ์šฉ๋˜์—ˆ๋Š”์ง€ ์ฒดํฌํ•œ๋‹ค.

    5. alphabet ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๋ฆฌ์ŠคํŠธ์— False๊ฐ€ ์—†๋‹ค๋ฉด cnt + 1, ์žˆ๋‹ค๋ฉด break๋ฅผ ํ•œ๋‹ค.

    6. cnt์— ๋”ฐ๋ผ ๊ฒฐ๊ณผ๊ฐ’ res๋ฅผ ๊ฐฑ์‹ ํ•˜๊ณ  ์ถœ๋ ฅ์–‘์‹์— ๋งž๊ฒŒ ์ถœ๋ ฅํ•œ๋‹ค.

    ๐Ÿ’พ ๋Š๋‚€์ 

    1. dict๋กœ๋„ ํ’€์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ? ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.