0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python Coding Test - easy

Posted at

##電話番号リスト (Level2)
####問題要約

  • phone_book:電話帳を配列します。
  • ひとつの番号が電話帳の違う番号の接頭語である場合はfalse、なければtrueをreturnします。

####テストケース
image.png

####解決

  • 電話帳リストの長さを基準に昇順に整列
  • 当該配列をfor文を戻し、後ろの番号と比較
  • 接頭語が存在したら、answerをFalse後、checkという変数にTrueと宣言して関数を終了
  • 接頭語が存在しない場合、for文が完了した後にTrueに返却
def solution(list):
     answer = True 
     check = False 
     list = sorted(list, key=len) 
     for i in range(0, len(list)): 
        if check: 
             break
        current = list[i]
        for j in range(i+1, len(list)): 
            comp = list[j] 
            if len(current)<len(comp) and current == comp[0:len(current)]: 
                answer = False 
                check = True 
                break 
    return answer
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?