47일차 문제

양진혁·2021년 12월 18일
0

문제 풀이

comp(a, b) returns true because in b 121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on. It gets obvious if we write b's elements in terms of squares:

a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [121, 14641, 20736, 361, 25921, 361, 20736, 361]

이 문제는 true이다 왜냐하면,
a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [11x11, 121x121, 144x144, 19x19, 161x161, 19x19, 144x144, 19x19]

b 원소들이 위에 있는 숫자들의 제곱이기 때문이다.

def comp(array1, array2):
  el=[]
  if array1 == None or array2 == None:
    return False
  for i in array1:
    el.append(i**2)
  if sorted(el) == sorted(array2):
    return True
  else:
    return False  

일단 array1이나 2가 None이면 False를 해주고 빈 리스트에 array1의 제곱의 수를 넣어준 후 el과 array2를 sorted를 통해 정렬한 후 같으면 true 아니면 false를 프린트한다.

두번째는
a1 = ["arp", "live", "strong"]

a2 = ["lively", "alive", "harp", "sharp", "armstrong"]

returns ["arp", "live", "strong"]

즉 위 리스트 내 인덱스가 두번째 리스트 내 인덱스 문자 안에 포함되면 그걸 리턴한다.

def in_array(array1, array2):
  el=[]
  for a2 in array2:
    for a1 in array1:
      if a1 in a2 and a1 not in el:
        el.append(a1)
  return sorted(el)     

이중 반복문을 통해서 만약 a1이 a2 안에 포함되고 a1이 빈 리스트 안에 들어있지 않다면 빈 리스트에 a1을 추가한 후 sorted를 통해서 오름차순으로 정렬한다.

0개의 댓글