본문 바로가기
CodeSignal/Arcade

CodeSignal [51/60] deleteDigit

by 벤진[Benzene] 2020. 5. 28.

Problem:

Given some integer, find the maximal number you can obtain by deleting exactly one digit of the given number.

 

Example

  • For n = 152, the output should be
    deleteDigit(n) = 52;
  • For n = 1001, the output should be
    deleteDigit(n) = 101.

-Summary-

1. 현재 index에서 next index가 큰 값인 경우만 지워주면 항상 제일 큰 숫자가 되므로, 해당되는 index를 찾아준다.

2. 찾은후에는 그 index만 지우고 return. 만약에 loop을 다 돈 이후에도, 지운 숫자가 없다면 마지막 숫자만 지워준다.

 

다른 사람들이 푼 것을 보니, generator expression을 이용하여 다음과 같이 simple 하게 풀었다. 오늘도 사람들의 submission을 보며 배운다.

https://wiki.python.org/moin/Generators

 

Generators - Python Wiki

Generator functions allow you to declare a function that behaves like an iterator, i.e. it can be used in a for loop. Simplified Code The simplification of code is a result of generator function and generator expression support provided by Python. To illus

wiki.python.org

def deleteDigit(n):
    n = str(n)
    return max(int(''.join(n[:i]+n[i+1:])) for i in range(len(n)))

모든 문제에 대한 저작권은 CodeSignal 회사에 있습니다. [Copyright © 2020 BrainFights Inc. All rights reserved]

https://codesignal.com/

'CodeSignal > Arcade' 카테고리의 다른 글

CodeSignal [53/60] validTime  (0) 2020.05.28
CodeSignal [52/60] longestWord  (0) 2020.05.28
CodeSignal [50/60] chessKnight  (0) 2020.05.28
CodeSignal [49/60] lineEncoding  (0) 2020.05.27
CodeSignal [48/60] isDigit  (0) 2020.05.27

댓글