Commit fac85aa8 by 牛家玺

作业

parent b39ee352
"""
给定一个由空格分割单词的句子 S。每个单词只包含大写或小写字母。
我们要将句子转换为 “Goat Latin”(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。
山羊拉丁文的规则如下:
如果单词以元音开头(a, e, i, o, u),在单词后添加"ma"。
例如,单词"apple"变为"applema"。
如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma"。
例如,单词"goat"变为"oatgma"。
根据单词在句子中的索引,在单词最后添加与索引相同数量的字母'a',索引从1开始。
例如,在第一个单词后添加"a",在第二个单词后添加"aa",以此类推。
返回将 S 转换为山羊拉丁文后的句子。
示例 1:
输入: "I speak Goat Latin"
输出: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
示例 2:
输入: "The quick brown fox jumped over the lazy dog"
输出: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
说明:
S 中仅包含大小写字母和空格。单词间有且仅有一个空格。
1 <= S.length <= 150。
"""
def toGoatLatin(str):
vowel = ['a', 'e', 'i', 'o', 'u']
temp = str.split()
for i in range(len(temp)):
if temp[i][0].lower() in vowel:
temp[i] = temp[i] + 'ma' + 'a' * (i + 1)
else:
"""
goat o = 1 oatg
"""
temp[i] = temp[i][1:] + temp[i][0] + 'ma' + 'a' * (i + 1)
return " ".join(temp).lstrip(' ')
str = "I speak Goat Latin"
print(toGoatLatin(str))
str = "The quick brown fox jumped over the lazy dog"
print(toGoatLatin(str))
"""
冬季已经来临。 你的任务是设计一个有固定加热半径的供暖器向所有房屋供暖。
现在,给出位于一条水平线上的房屋和供暖器的位置,找到可以覆盖所有房屋的最小加热半径。
所以,你的输入将会是房屋和供暖器的位置。你将输出供暖器的最小加热半径。
说明:
给出的房屋和供暖器的数目是非负数且不会超过 25000。
给出的房屋和供暖器的位置均是非负数且不会超过10^9。
只要房屋位于供暖器的半径内(包括在边缘上),它就可以得到供暖。
所有供暖器都遵循你的半径标准,加热的半径也一样。
示例 1:
输入: [1,2,3],[2]
输出: 1
解释: 仅在位置2上有一个供暖器。如果我们将加热半径设为1,那么所有房屋就都能得到供暖。
示例 2:
输入: [1,2,3,4],[1,4]
输出: 1
解释: 在位置1, 4上有两个供暖器。我们需要将加热半径设为1,这样所有房屋就都能得到供暖。
"""
class Heater: class Heater:
def findRadius(self, houses, heaters): def findRadius(self, houses, heaters):
heaters.sort() heaters.sort()
...@@ -14,10 +40,7 @@ class Heater: ...@@ -14,10 +40,7 @@ class Heater:
print(radius) print(radius)
houses = [1, 2, 4, 9]
heaters = [-1, 1, 8, float('inf')]
Heater().findRadius(houses, heaters)
houses = [1, 2, 3] houses = [1, 2, 3]
heaters = [2] heaters = [2]
......
"""
要求:有一个sort方法,方法接收两个参数,一个参数是列表,另外一个参数是排序方法
例如:当传入bubble时,则调用冒泡排序的方法进行排序,并打印出每一步数字挪动的过程
当传入quick时,则调用快速排序的方法进行排序,并打印出每一步数字挪动的过程
外部不可以直接访问具体排序方法,只能调用sort方法
"""
class SortUtils:
def __init__(self, array, algorithm):
self.array = array
self.algorithm = algorithm
def sort(self):
if self.algorithm == 'bubble':
self.__bubble()
elif self.algorithm == 'quick':
self.__quick()
else:
raise RuntimeError("not support this algorithm")
return self.array
def __bubble(self):
for i in range(len(self.array) - 1):
for j in range(len(self.array) - 1 - i):
if self.array[j] > self.array[j + 1]:
self.array[j], self.array[j + 1] = self.array[j + 1], self.array[j]
def __quick(self):
left, right = 0, len(self.array) - 1
stack = []
stack.append(left)
stack.append(right)
while stack:
low = stack.pop(0)
high = stack.pop(0)
if high - low <= 0:
continue
x = self.array[high]
i = low - 1
for j in range(low, high):
if self.array[j] <= x:
i = i + 1
self.array[i], self.array[j] = self.array[j], self.array[i]
self.array[i + 1], self.array[high] = self.array[high], self.array[i + 1]
stack.extend([low, i, i + 2, high])
array = [1, 7, 8, 9, 30, 5, 6]
# self
sort1 = SortUtils(array, "bubble")
sort2 = SortUtils(array, "quick")
print(sort1 == sort2)
print(SortUtils(array, 'bubble').sort())
print(SortUtils(array, 'quick').sort())
...@@ -11,14 +11,9 @@ def quick_sort(array, l, r): ...@@ -11,14 +11,9 @@ def quick_sort(array, l, r):
# 越界检查 # 越界检查
if high - low <= 0: if high - low <= 0:
continue continue
# 取右值x作为基准
x = array[high] x = array[high]
i = low - 1 i = low - 1
# 从左边开始遍历
for j in range(low, high): for j in range(low, high):
# 是否小于等于基准值x
# 每次交换都放到左边 如果小于基准值
# 1, 7, 8, 9, 30, 5, 6
# x = 6 # x = 6
# i = -1 # i = -1
# j=0 i=0 1 < 6 j 和 i 交换 # j=0 i=0 1 < 6 j 和 i 交换
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment