Extra Long Factorials(阶乘)
Function Description
Complete the extraLongFactorials function in the editor below. It should print the result and return.
extraLongFactorials has the following parameter(s):
- n: an integer
Solution
# Complete the extraLongFactorials function below.
def extraLongFactorials(n):
result = 1
for i in range(1, n+1):
result = result * i
print(result)
Climbing the Leaderboard(攀登积分榜)
在三个Example中测试Runtime超时,满分20,只拿了12分
Function Description
Complete the climbingLeaderboard function in the editor below. It should return an integer array where each element res[j] represents Alice's rank after the j game.
climbingLeaderboard has the following parameter(s):
- scores: an array of integers that represent leaderboard scores
- alice: an array of integers that represent Alice's scores
Solution
def climbingLeaderboard(scores, alice):
scores = list(set(scores))
scores.sort(reverse=True)
rankList = []
for i in range(len(alice)):
rank = 1
current_score = alice[i]
# 如果当前分数超过榜单最高分则无需for loop
if(max(scores)<current_score):
pass
else:
for j in range(len(scores)):
if(scores[j]>current_score):
rank = rank + 1
rankList.append(rank)
Organizing Containers of Balls(整理球筐)
本质上是求矩阵行列之和经过排序后是否相等,十分简单
Solution
def organizingContainers(container):
row = []
for line in container:
row.append(sum(line))
column = container[0]
for i in range(1, len(container)):
for j in range(len(container)):
column[j] = column[j] + container[i][j]
row.sort()
column.sort()
if row == column:
return 'Possible'
else:
return 'Impossible'
Encryption(加密)
Problem Description
字符串分割问题
Function Description
Complete the encryption function in the editor below. It should return a single string composed as described.
encryption has the following parameter(s):
- s: a string to encrypt
Solution
def encryption(s):
# 判断行列数
root = math.pow(len(s), 1/2)
if root == int(root):
row = column = int(root)
else:
row = int(root)
column = row + 1
if row * column < len(s):
row = row + 1
result = ""
temp = []
res = []
for i in range(row):
temp.append(s[column*i:column*i+column])
stop = len(temp[-1])
for j in range(stop):
word = ""
for i in range(row):
word = word + temp[i][j]
res.append(word)
for j in range(stop, column):
word = ""
for i in range(row-1):
word = word + temp[i][j]
res.append(word)
for i in range(len(res)):
result = result + res[i] + " "
return result
Bigger is Greater(越大越好)
Problem Link
字符比较大小的动态规划问题
Problem Description
将单词的字典序看作数值,本问题要求将给定单词通过较换字母(Swap character)转换为更大的新单词,且要求这个新单词是可转换的所有新单词中最小的一个
Function Description
Complete the biggerIsGreater function in the editor below. It should return the smallest lexicographically higher string possible from the given string or no answer.
biggerIsGreater has the following parameter(s):
- w: a string
Solution
def biggerIsGreater(w):
wList = []
for i in range(len(w)):
wList.append(w[i])
for i in range(2, len(w)+1):
tailcopy = wList[-i:].copy()
tailcopy.sort(reverse=True)
if not (wList[-i:] == tailcopy):
wTarget = wList[-i]
bigger = []
for x in tailcopy:
if x > wTarget:
bigger.append(x)
if bigger:
bigger.sort()
swap = bigger[0]
index = wList[-i:].index(swap)
wList[-i] = swap
wList[-i+index] = wTarget
tailcopy = wList[-i+1:].copy()
tailcopy.sort()
wList[-i+1:] = tailcopy
break
result = ""
for i in range(len(w)):
result = result + wList[i]
if result == w:
result = "no answer"
return result