1.Mexican Wave
In this simple Kata your task is to create a function that turns a string into a Mexican Wave. You will be passed a string and you must return that string in an array where an uppercase letter is a person standing up.
Example
wave("hello") => []string{"Hello", "hEllo", "heLlo", "helLo", "hellO"}
My solution:
def wave(str):
# Code here
result=[]
for i in range(len(str)):
s=list(str)
if s[i]==" ":
i=i+1
else:
s[i]=s[i].upper()
result.append(''.join(s))
return result
Clever solution NO.1:
def wave(str):
return [str[:i] + str[i].upper() + str[i+1:] for i in range(len(str)) if str[i].isalpha()]
2.Maximum subarray sum
The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers:
maxSequence([-2, 1, -3, 4, -1, 2, 1, -5, 4])
# should be 6: [4, -1, 2, 1]
My solution:
def maxSequence(arr):
# ...
max=0
for i in range(len(arr)):
for j in range(i+1,len(arr)+1):
if sum(arr[i:j])>max:
max=sum(arr[i:j])
return max
Clever solution NO.1:
def maxSequence(arr):
maximum, temp = 0, 0
for v in arr:
temp = max(temp + v, 0)
maximum = max(maximum, temp)
return maximum
此题只需要返回最大值,不需要返回总和为最大值的对应切片,因此降低了难度。
此题较为典型,可以引为范例。
3.First non-repeating character
Write a function named first_non_repeating_letter
that takes a string input, and returns the first character that is not repeated anywhere in the string.
For example, if given the input 'stress'
, the function should return 't'
, since the letter t only occurs once in the string, and occurs first in the string.
As an added challenge, upper- and lowercase letters are considered the same character, but the function should return the correct case for the initial letter. For example, the input 'sTreSS'
should return'T'
.
If a string contains all repeating characters, it should return an empty string (""
) or None
– see sample tests.
My solution:
def first_non_repeating_letter(string):
#your code here
if string!='':
for x in string:
if x.isalpha():
if x!=x.upper():
if string.count(x)+string.count(x.upper())==1:
return x
else:
if string.count(x)+string.count(x.lower())==1:
return x
else:
if string.count(x)==1:
return x
return ''
Clever solution NO.1:
def first_non_repeating_letter(string):
string_lower = string.lower()
for i, letter in enumerate(string_lower):
if string_lower.count(letter) == 1:
return string[i]
return ""
此题我的思路很繁琐,应该学习高分答案,思路清晰一些。另外enumerate()函数是可以简化代码的函数,需多加注意。
4.Tic-Tac-Toe Checker
If we were to set up a Tic-Tac-Toe game, we would want to know whether the board’s current state is solved, wouldn’t we? Our goal is to create a function that will check that for us!
Assume that the board comes in the form of a 3x3 array, where the value is 0 if a spot is empty, 1 if it is an “X”, or 2 if it is an “O”, like so:
[[0, 0, 1],
[0, 1, 2],
[2, 1, 0]]
We want our function to return:
-1 if the board is not yet finished (there are empty spots),
1 if “X” won,
2 if “O” won,
0 if it’s a cat’s game (i.e. a draw).
My solution:
def isSolved(board):
# TODO: Check if the board is solved!
if (board[0][0]==1 and board[1][1]==1 and board[2][2]==1) or (board[0][2]==1 and board[1][1]==1 and board[2][0]==1):
return 1
if (board[0][0]==2 and board[1][1]==2 and board[2][2]==2) or (board[0][2]==2 and board[1][1]==2 and board[2][0]==2):
return 2
for i in range(3):
if board[i]==[1,1,1]:
return 1
if board[i]==[2,2,2]:
return 2
for j in range(3):
if board[0][j]==1 and board[1][j]==1 and board[2][j]==1:
return 1
if board[0][j]==2 and board[1][j]==2 and board[2][j]==2:
return 2
for i in range(3):
if 0 in board[i]:
return -1
return
Clever solution NO.1:
def isSolved(board):
for i in range(0,3):
if board[i][0] == board[i][1] == board[i][2] != 0:
return board[i][0]
elif board[0][i] == board[1][i] == board[2][i] != 0:
return board[0][i]
if board[0][0] == board[1][1] == board[2][2] != 0:
return board[0][0]
elif board[0][2] == board[1][1] == board[2][0] != 0:
return board[0][0]
elif 0 not in board[0] and 0 not in board[1] and 0 not in board[2]:
return 0
else:
return -1
Clever solution NO.2:
import itertools
def isSolved(board):
b = list(itertools.chain(*board))
for p, q, r in [(0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6)]:
if b[p] == b[q] == b[r] != 0: return b[p]
return 0 if sum(b) == 13 else -1
该题我写的方法较为繁琐,反观第一个高分答案,虽然我俩思路大致一样,但是他巧妙的利用了返回值与矩阵内容的相同的特点,避免了一次for运算。至于第二个高分回答,他将所有可能成功的元素位置记载元组中并将所有元组存储在一个列表里,通过这种技巧性很高的方法,解决了此题,令我钦羡赞叹不已。