[Python] CodeSignal 문제 풀이 (28~30)
2020. 3. 12. 21:16ㆍPython/CodeSignal Algorithm
28. Given a string, your task is to replace each of its characters by the next one in the English alphabet; i.e. replace a with b, replace b with c, etc (z would be replaced by a).
[Example]
For inputString = "crazy", the output should be alphabeticShift(inputString) = "dsbaz". |
[Solution]
#<My Code>
def alphabeticShift(inputString):
os = inputString
ns = ""
for i in range(len(os)):
if os[i] == 'z': ns += "a"
else: ns += chr(ord(os[i]) + 1)
return ns
#<Best Code>
def alphabeticShift(s):
return "".join(chr((ord(i)-96)%26+97) for i in s)
29. Given two cells on the standard chess board, determine whether they have the same color or not.
[Example]
[Solution]
#<My Code>
def chessBoardCellColor(cell1, cell2):
c1, c2 = cell1, cell2
#ord 값이 홀수: A, C, E... / 짝수: B, D, F...
if ord(c1[0]) % 2 == ord(c2[0]) % 2:
if(int(c1[1]) % 2 == int(c2[1]) % 2): return True
else: return False
else:
if(int(c1[1]) % 2 != int(c2[1]) % 2): return True
else: return False
#<Best Code>
def chessBoardCellColor(cell1, cell2):
return (ord(cell1[0])+int(cell1[1])+ord(cell2[0])+int(cell2[1]))%2==0
30. Consider integer numbers from 0 to n - 1 written down along the circle in such a way that the distance between any two neighboring numbers is equal (note that 0 and n - 1 are neighboring, too).
Given n and firstNumber, find the number which is written in the radially opposite position to firstNumber.
[Example]
[Solution]
#<My Code>
def circleOfNumbers(n, firstNumber):
return (int(n / 2) + firstNumber) % n
#<Best Code> = <My Code>
'Python > CodeSignal Algorithm' 카테고리의 다른 글
[Python] CodeSignal 문제 풀이 (33~35) (0) | 2020.03.21 |
---|---|
[Python] CodeSignal 문제 풀이 (31~33) (0) | 2020.03.12 |
[Python] CodeSignal 문제 풀이 (25~27) (0) | 2020.03.12 |
[Python] CodeSignal 문제 풀이 (22~24) (0) | 2020.03.12 |
[Python] CodeSignal 문제 풀이 (19~21) (0) | 2020.03.12 |