구현 algorithm

Ryu·2021년 7월 30일
0

Python

목록 보기
3/9
post-thumbnail

Python 구현 algorithm

상하좌우 문제

# N 입력받기
n = int(input())
x, y= 1, 1
plans = input().split()

# L,R,U,D에 따른 이동 방향
dx=[0,0,-1,1]
dy=[-1,1,0,0]
move_types=['L','R','U','D']

# 이동 계획을 하나씩 확인하기
for plan in plans:
  #이동 후 좌표 구하기
  for i in range(len(move_types)):
    if plan == move_types[i]:
      nx = x + dx[i]
      ny = y + dy[i]

  # 공간을 벗어나는 경우 무시
  if nx <1 or ny <1 or nx>n or ny>n:
    continue
  
  # 이동 수행
  x, y = nx, ny

print(x, y)

시각 문제 설명

  • 시간과 분, 초를 모두 반복문으로 돌면서 문자열로 바꿔 '3'이 들어있으면 count를 올리는 식으로 해결
  • # H 입력받기
    h = int(input())
    
    count=0
    for i in range(h+1):
      for j in range(60):
        for k in range(60):
          # 매 시각 안에 '3'이 포함되어 있다면 카운트 증가
          if '3' in str(i)+str(j)+str(k):
            count += 1
    
    print(count)

    왕실의 나이트

  • 방향벡터를 나타내는 함수를 만들고 이동을 시켰을때 특정 범위를 벗어나지않으면 카운트를 늘리는 식으로 해결
  • position = input()
    night_row = int(position[1])
    night_column = int(ord(position[0]))-int(ord('a'))+1
    
    # 나이트가 이동할 수 있는 8가지의 방향
    steps = [
      (-2,-1),
      (-1,-2),
      (1,-2),
      (2,-1),
      (2,1),
      (1,2),
      (-1,2),
      (-2,1)
      ]
    
    #8가지 방향에 대하여 각 위치로 이동이 가능한지
    result= 0
    for step in steps:
      # 이동하고자 하는 위치
      next_row = night_row + step[0]
      next_column = night_column + step[1]
      if next_row >= 1 and next_row <= 8 and next_column >=1 and next_column <=8:
        result += 1
    
    print(result)
    print(night_row,night_column)
    

    문자열 재정렬

  • 알파벳 리스트를 따로만들고 숫자를 더할 공간도 따로만들어서 둘을 마지막에 합친다.
  • string_data = input()
    result = []
    value = 0
    
    # 문자를 하나씩 확인
    for i in string_data:
      if i.isalpha():
        result.append(i)
      #숫자 따로 더하기
      else :
        value += int(i)
    
    # 알파벳 오름차순 정렬
    result.sort()
    
    # 숫자가 하나라도 존재하는 경우 가장뒤에 삽입
    if value != 0:
      result.append(str(value))
    
    # 최종 결과 출력(리스트를 문자열로 변환하여 출력)
    print(''.join(result))
    
    profile
    쓴다.노트.하는동안.공부

    0개의 댓글

    관련 채용 정보