1. 비밀 지도


In [181]:
def secret_map(n, arr1, arr2):
    answer = []
    bin_matrix = zip(arr1, arr2)

    for dec_1, dec_2 in bin_matrix:
        bin_1 = bin(dec_1)[2:].zfill(n)
        bin_2 = bin(dec_2)[2:].zfill(n)

        result = ""
        for i in range(n):
            or_operator = bin(int(bin_1[i]) | int(bin_2[i]))[2:]

            if or_operator == '1':
                result = result + "#"
            else:
                result = result + " "
        answer.append(result)
    return answer

In [182]:
n = 5
arr1 = [9, 20, 28, 18, 11]
arr2 = [30, 1, 21, 17, 28]

secret_map(n, arr1, arr2)


Out[182]:
['#####', '# # #', '### #', '#  ##', '#####']

In [183]:
n = 6
arr1 = [46, 33, 33, 22, 31, 50]
arr2 = [27, 56, 19, 14, 14, 10]

secret_map(n, arr1, arr2)


Out[183]:
['######', '###  #', '##  ##', ' #### ', ' #####', '### # ']

2. 다트 게임


In [322]:
import re 
def dart_game_score(dart_result):
    non_digit = re.findall('\D+', dart_result)
    digit = re.findall('\d+', dart_result)
    score = []
    
    for i in range(3):
        score.append(int(digit[i]))
        if 'S' in non_digit[i]:
            pass
        elif 'D' in non_digit[i]:
            score[i] = score[i]**2
        elif 'T' in non_digit[i]:
            score[i] = score[i]**3

        if '#' in non_digit[i]:
            score[i] = score[i]*-1
        elif '*' in non_digit[i]:
            if i == 0:
                score[i] = score[i]*2
            else:
                score[i-1] = score[i-1]*2
                score[i] = score[i]*2
    total_score = sum(score)
    return total_score

In [323]:
dart_result_list = ['1S2D*3T', '1D2S#10S', '1D2S0T', '1S*2T*3S', '1D#2S*3S', '1T2D3D#', '1D2S3T*']
for i in dart_result_list:
    print(dart_game_score(i))


37
9
3
23
5
-4
59

3. 캐시


In [217]:
cache_size = [3, 3, 2, 5, 2, 0]
cities = [['Jeju', 'Pangyo', 'Seoul', 'NewYork', 'LA', 'Jeju', 'Pangyo', 'Seoul', 'NewYork', 'LA'],
          ['Jeju', 'Pangyo', 'Seoul', 'Jeju', 'Pangyo', 'Seoul', 'Jeju', 'Pangyo', 'Seoul'], 
          ['Jeju', 'Pangyo', 'Seoul', 'NewYork', 'LA', 'SanFrancisco', 'Seoul', 'Rome', 'Paris', 
           'Jeju', 'NewYork', 'Rome'] ,
          ['Jeju', 'Pangyo', 'Seoul', 'NewYork', 'LA', 'SanFrancisco', 'Seoul', 'Rome', 'Paris', 
           'Jeju', 'NewYork', 'Rome'],
          ['Jeju', 'Pangyo', 'NewYork', 'newyork'],
          ['Jeju', 'Pangyo', 'Seoul', 'NewYork', 'LA']]

In [218]:
def cache_change(cache_size, cities):
    cache = []
    time = 0
    length = len(cities)

    if cache_size != 0:
        for i in range(length):
            if len(cache) < cache_size:
                time += 5
                cache.append(cities[i].lower())
            else:
                if cache.count(cities[i].lower()) != 0:
                    time += 1
                else:
                    cache = cache[1:]
                    cache.append((cities[i].lower()))
                    time += 5
    else:
        for i in range(length):
            time += 5
    return time

In [223]:
for i, j in zip(cache_size, cities):
    print(cache_change(i, j))


50
21
60
52
16
25