ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] ๋ฐฑ์ค€ 4358 ์ƒํƒœํ•™ (Counter)
    ๐Ÿ’ป Algorithm/Python 2022. 7. 8. 19:35

    ๐Ÿ“Œ ๋ฌธ์ œ

    ์ƒํƒœํ•™์—์„œ ๋‚˜๋ฌด์˜ ๋ถ„ํฌ๋„๋ฅผ ์ธก์ •ํ•˜๋Š” ๊ฒƒ์€ ์ค‘์š”ํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋‹น์‹ ์€ ๋ฏธ๊ตญ ์ „์—ญ์˜ ๋‚˜๋ฌด๋“ค์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ ์ข…์ด ์ „์ฒด์—์„œ ๋ช‡ %๋ฅผ ์ฐจ์ง€ํ•˜๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค.

    ์ž…๋ ฅ

    ํ”„๋กœ๊ทธ๋žจ์€ ์—ฌ๋Ÿฌ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ํ•œ ์ค„์— ํ•˜๋‚˜์˜ ๋‚˜๋ฌด ์ข… ์ด๋ฆ„์ด ์ฃผ์–ด์ง„๋‹ค. ์–ด๋–ค ์ข… ์ด๋ฆ„๋„ 30๊ธ€์ž๋ฅผ ๋„˜์ง€ ์•Š์œผ๋ฉฐ, ์ž…๋ ฅ์—๋Š” ์ตœ๋Œ€ 10,000๊ฐœ์˜ ์ข…์ด ์ฃผ์–ด์ง€๊ณ  ์ตœ๋Œ€ 1,000,000๊ทธ๋ฃจ์˜ ๋‚˜๋ฌด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

    ์ถœ๋ ฅ

    ์ฃผ์–ด์ง„ ๊ฐ ์ข…์˜ ์ด๋ฆ„์„ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜๊ณ , ๊ทธ ์ข…์ด ์ฐจ์ง€ํ•˜๋Š” ๋น„์œจ์„ ๋ฐฑ๋ถ„์œจ๋กœ ์†Œ์ˆ˜์  4์งธ์ž๋ฆฌ๊นŒ์ง€ ๋ฐ˜์˜ฌ๋ฆผํ•ด ํ•จ๊ป˜ ์ถœ๋ ฅํ•œ๋‹ค.

    ์˜ˆ์ œ ์ž…๋ ฅ 1

    Red Alder
    Ash
    Aspen
    Basswood
    Ash
    Beech
    Yellow Birch
    Ash
    Cherry
    Cottonwood
    Ash
    Cypress
    Red Elm
    Gum
    Hackberry
    White Oak
    Hickory
    Pecan
    Hard Maple
    White Oak
    Soft Maple
    Red Oak
    Red Oak
    White Oak
    Poplan
    Sassafras
    Sycamore
    Black Walnut
    Willow

    ์˜ˆ์ œ ์ถœ๋ ฅ 1

    Ash 13.7931
    Aspen 3.4483
    Basswood 3.4483
    Beech 3.4483
    Black Walnut 3.4483
    Cherry 3.4483
    Cottonwood 3.4483
    Cypress 3.4483
    Gum 3.4483
    Hackberry 3.4483
    Hard Maple 3.4483
    Hickory 3.4483
    Pecan 3.4483
    Poplan 3.4483
    Red Alder 3.4483
    Red Elm 3.4483
    Red Oak 6.8966
    Sassafras 3.4483
    Soft Maple 3.4483
    Sycamore 3.4483
    White Oak 10.3448
    Willow 3.4483
    Yellow Birch 3.4483

     


    ๐Ÿ“Œ ํ’€์ด

    ๐Ÿ’ฌ Code

    import sys
    from collections import Counter
    input = sys.stdin.readline
    
    trees = []
    cnt = 0
    
    while True:
        name = input().rstrip()
        if not name:
            break
        trees.append(name)
        cnt += 1
    
    trees = sorted(Counter(trees).items())
    
    for i in range(len(trees)):
        print(trees[i][0], "{:.4f}".format(trees[i][1]/cnt*100))

    ๐Ÿ’ก Solution

    ์ž…๋ ฅ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ชจ๋ฅผ ๋•Œ while-try-except๋ฌธ์„ ์ผ์—ˆ๋Š”๋ฐ ์™œ์ธ์ง€ ์ด ๋ฌธ์ œ์—์„œ๋Š” ๊ทธ๊ฒŒ ์•ˆ ๋จนํžˆ๊ธธ๋ž˜ ๊ตฌ๊ธ€๋ง์˜ ํž˜์„ ๋นŒ๋ ค while-if not ์„ธํŠธ๋กœ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”์ด์ƒ input์ด ๋“ค์–ด์˜ค์ง€ ์•Š์„ ๋•Œ break๋ฅผ ๊ฑธ์–ด์ค๋‹ˆ๋‹ค.

     

    input์œผ๋กœ ๋“ค์–ด์˜ค๋Š” ๋‚˜๋ฌด ์ด๋ฆ„๋“ค์„ trees ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ฃผ๊ณ , Counter(trees)๋กœ ๊ฐ ์ด๋ฆ„์˜ ๋นˆ๋„๋ฅผ ์„ธ์–ด์ค๋‹ˆ๋‹ค. ๋‹ค๋งŒ ์ถœ๋ ฅํ•  ๋•Œ ๋นˆ๋„์ˆœ์ด ์•„๋‹Œ ์ด๋ฆ„ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์ •๋ ฌ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    print(Counter(['hello', 'hello', 'goodbye', 'thanks']))
    print(sorted(Counter(['hello', 'hello', 'goodbye', 'thanks'])))
    
    # Counter({'hello': 2, 'goodbye': 1, 'thanks': 1})
    # ['goodbye', 'hello', 'thanks']

    ์ด๋ ‡๊ฒŒ Counter๋ฅผ ๋ฐ”๋กœ sorted ๊ตฌ์กฐ๋กœ ๋งŒ๋“ค์–ด ๋ฒ„๋ฆฌ๋ฉด key๋งŒ ์ •๋ ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์—

    print(Counter(['hello', 'hello', 'goodbye', 'thanks']).items())
    print(sorted(Counter(['hello', 'hello', 'goodbye', 'thanks']).items()))
    
    # dict_items([('hello', 2), ('goodbye', 1), ('thanks', 1)])
    # [('goodbye', 1), ('hello', 2), ('thanks', 1)]

    Counter.items()๋กœ key, value์Œ์„ ํ•จ๊ป˜ ๊ฐ€์ ธ์™€์„œ sorted ๊ตฌ์กฐ๋กœ ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผ key, value์Œ์„ ํ•จ๊ป˜ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


    Counter

    from collections import Counter
    print(Counter('hello world'))
    # Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})

    Counter๋ฅผ ์ด์šฉํ•˜๋ฉด ์ด์™€ ๊ฐ™์ด ๊ฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๋™์ž‘์„ ์‰ฝ๊ฒŒ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜, Counter ํด๋ž˜์Šค๋Š” ํŒŒ์ด์ฌ์˜ ๊ธฐ๋ณธ ์ž๋ฃŒ๊ตฌ์กฐ์ธ ์‚ฌ์ „(dictionary)๋ฅผ ํ™•์žฅํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์ „์—์„œ ์ œ๊ณตํ•˜๋Š” API๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


    ์†Œ์ˆ˜์  ํ‘œํ˜„๋ฒ•

    a = "example1 : {:.2f}".format(1.23456789)
    print(a)
    # example1 : 1.23
    
    b = "example2 : {:.2f} / {:.3f}".format(1.23456789, 3.456789)
    print(b)
    # example2 : 1.23 / 3.457
    
    c = "example3 : {0:.2f} / {1:.1f}".format(3.22521321, 10.123456)
    print(c)
    # example3 : 3.23 / 10.1
    
    d = "example4 : {1:.2f} / {0:.1f}".format(3.22521321, 10.123456)
    print(d)
    # example4 : 10.12 / 3.2

     

     

     

    ๋Œ“๊ธ€

Designed by Tistory.