Python 给定一个字符串,移动到所需字符的索引
关于 问题是: 给定一个长度为26的字符串键盘,指示 键盘(索引从0到25),最初您的手指位于索引0处。 要键入字符,必须将手指移到字符的食指上 理想的性格。将手指从食指i移动到食指i所需的时间 指数j为| i-j | 这是一个相当简单的问题,不应该花费太多时间。因此,我尝试了以下解决方案Python 给定一个字符串,移动到所需字符的索引,python,Python,关于 问题是: 给定一个长度为26的字符串键盘,指示 键盘(索引从0到25),最初您的手指位于索引0处。 要键入字符,必须将手指移到字符的食指上 理想的性格。将手指从食指i移动到食指i所需的时间 指数j为| i-j | 这是一个相当简单的问题,不应该花费太多时间。因此,我尝试了以下解决方案 def key_front(num, k, word, keyboard): for x in range(k): if word[num] == keyboard[x]:
def key_front(num, k, word, keyboard):
for x in range(k):
if word[num] == keyboard[x]:
return x
return 0
def calculateTime(keyboard, word):
k = len(keyboard)
count = 0
i = 0
w = len(word)
diff = 0
while i < w:
front = 0
front = key_front(i, k, word, keyboard)
print(front,diff,word[i],count)
if word[i] == word[i-1]:
diff = 0
diff = abs(front-diff)
count += diff
i = i + 1
return count
这似乎有效:
keyboard = "pyevcountbgjklxfqwimrdhazs"
word = "gxafwhexxykisc...jebxjfrfoxatxhi"
keys = {char: index for index, char in enumerate(keyboard)}
index = 0
s = 0
for char in word:
i = keys[char]
s += abs(index - i)
index = i
print(s) # 66042
我首先为键盘创建查找表键
,然后我在你的单词
的字符
上循环,更新索引
我当前所在的位置,并在运行的sums
中计算新的ald和旧的索引
之间的绝对差值
在您的解决方案中,
diff
始终指定给0
,并且永远不会是任何其他值:您只处理当前字母和以前字母相同的情况
另外,您似乎没有保留当前的索引。我对您的代码做了一点更改,但只是为了得到正确的结果。如果你想优化你的代码,@hiro progator的答案是一个很好的例子
# this function is working fine
def key_front(num, k, word, keyboard):
for x in range(k):
if word[num] == keyboard[x]:
return x
return 0
def calculateTime(keyboard, word):
k = len(keyboard)
count = 0
i = 0
w = len(word)
diff = 0
# I've added a new variable here to store "your finger's" previous position:
prev_front = 0
while i < w:
# there is no need to initialize "front" here
front = key_front(i, k, word, keyboard)
print(front,diff,word[i],count)
#if word[i] == word[i-1], front and prev_front would be the same, resulting diff == 0
diff = front - prev_front
# assigning our new variable prev_front
prev_front = front
# diff might be negative so we only need it's absolute value
count += abs(diff)
i = i + 1
return count
#此函数工作正常
def键前(数字、k、字、键盘):
对于范围(k)内的x:
如果字[num]==键盘[x]:
返回x
返回0
def计算时间(键盘、word):
k=len(键盘)
计数=0
i=0
w=len(字)
差异=0
#我在这里添加了一个新变量来存储“你的手指”以前的位置:
前面=0
而我
那么“长度为26的字符串键盘”呢?你说你在使用递归,但我在这里没有看到任何递归函数。您的程序中的代码是否比您向我们展示的代码多?请在hashmap[word[i]时提供一个链接以查看此循环。如果你说“我的函数是递归的,因为它有一个while循环”,那就不是递归。如果函数调用自身,则它是递归的calculateTime
从不调用calculateTime
,因此它不是递归的。@HiroProgator是的,它们是递归的。这不是原始问题的一部分,但更像是后续问题。假设字符是按顺序重复的,我知道了。但是你能告诉我我做错了什么吗?现在不是这样的front
是我们正在寻找的字符,diff
是前一个字符的索引。是的,我有。使用Dict很好。但我仍在尝试如何修改我的解决方案。谢谢你的帮助
keyboard = "pyevcountbgjklxfqwimrdhazs"
word = "gxafwhexxykisc...jebxjfrfoxatxhi"
keys = {char: index for index, char in enumerate(keyboard)}
index = 0
s = 0
for char in word:
i = keys[char]
s += abs(index - i)
index = i
print(s) # 66042
# this function is working fine
def key_front(num, k, word, keyboard):
for x in range(k):
if word[num] == keyboard[x]:
return x
return 0
def calculateTime(keyboard, word):
k = len(keyboard)
count = 0
i = 0
w = len(word)
diff = 0
# I've added a new variable here to store "your finger's" previous position:
prev_front = 0
while i < w:
# there is no need to initialize "front" here
front = key_front(i, k, word, keyboard)
print(front,diff,word[i],count)
#if word[i] == word[i-1], front and prev_front would be the same, resulting diff == 0
diff = front - prev_front
# assigning our new variable prev_front
prev_front = front
# diff might be negative so we only need it's absolute value
count += abs(diff)
i = i + 1
return count