03. Resources/Python
[Python 문제풀이] 4일차
해 콩
2020. 7. 12. 11:00
728x90
반응형
Day_4
Q9. allLongestStrings
Given an array of strings, return another array containing all of its longest strings.
def allLongestStrings(inputArray):
maxLen = 0
for word in inputArray:
if maxLen < len(word):
maxLen = len(word)
maxLenList = list()
for word in inputArray:
if len(word) == maxLen:
maxLenList.append(word)
return maxLenList
오늘부턴 코드 짜는 과정도 메모해둘 수 있으면 메모해서 올리자
# 문제 파악
# Given an array of strings, return another array containing all of its longest strings.
# Example
# For inputArray = ["aba", "aa", "ad", "vcd", "aba"], the output should be
# allLongestStrings(inputArray) = ["aba", "vcd", "aba"].
# 입출력 확인
# Input/Output
# [execution time limit] 4 seconds (py3)
# [input] array.string inputArray
# A non-empty array.
# Guaranteed constraints:
# 1 ≤ inputArray.length ≤ 10,
# 1 ≤ inputArray[i].length ≤ 10.
# [output] array.string
# Array of the longest strings, stored in the same order as in the inputArray.
# 개발 과정
inputArray = ["aba", "aa", "ad", "vcd", "aba"] # 제공된 입력 확인
print(inputArray)
print(inputArray[0])
# 최대 길이 구하기
maxLen = 0
for word in inputArray:
if maxLen < len(word):
maxLen = len(word)
print(maxLen)
# 최대 길이인 리스트 채워넣기
maxLenList = list()
for word in inputArray:
if len(word) == maxLen:
maxLenList.append(word)
print(maxLenList)
반응형