본문 바로가기
Algorithm

Python - For Loops Assignment

by Adam92 2020. 5. 29.

Assignment (wecode)

Input 으로 주어진 리스트에서 오직 한번만 나타나는 값 (unique value)을 가지고 있는 요소는 출력해주세요.

예를 들어, 다음과 같은 리스트가 주어졌다면:

[1, 2, 3, 4, 5, 1, 2, 3, 7, 9, 9, 7]

 

다음과 같이 출력되어야 합니다.

4
5

 


내가 작성한 코드

 

my_list = [s for s in input().split()]

list = []
count = 0

for i in range(0, len(my_list)):
  
  for j in range(0, len(my_list)):
    if (i != j) and (my_list[i] == my_list[j]):
      count += 1
  if count < 1:
    list.append(my_list[i])
    count = 0
  else:
    count = 0
    

print(list)

 

  • 중첩 for문을 활용하여 문제를 풀었다.

 

WeCode 답안

my_list = [s for s in input().split()]
current_index = 0

for element in my_list:
  is_unique = True
  list_without_current_element = my_list[0:current_index] + my_list[current_index+1:]

  for element2 in list_without_current_element:
    if element == element2:
      is_unique = False
      break

  if is_unique:
    ,
    print(element)

  current_index += 1

 

이해하기가 어려워 내방식대로 이해해보았다.

 

'Algorithm' 카테고리의 다른 글

Code Kata : day 1  (0) 2020.06.08
Python 문제  (0) 2020.06.05