Python中文
首页
教程
问答
标签
搜索
登录
注册
如何更正比较包含按键的两个字符串的算法?
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>如果两个字符串相等,这就是返回true的算法。字符串可能包含类似backspace的按键。代码使用光标和指针遍历字符串中的每个字母,如果找到按键,则跳过2个位置(即.\b)</p> <pre><code>#!/usr/bin/env python import argparse import __builtin__ # Given two different strings, one with backspaces (keypresses), find if they are equivalent or not def main(): parser = argparse.ArgumentParser(description="Enter two strings without or without backspaces") parser.add_argument("s1", type=str, help="The first string.") parser.add_argument("s2", type=str, help="The second string.") args = parser.parse_args() print(compare(args.s1, args.s2)) def compare(s1, s2): BACKSPACE = '\b' cursor = 0; pointer1 = 0; pointer2 = 0; # current position in backspaced string. canon_len1 = len(s1); canon_len2 = len(s2); # length of the canonical string num_diff = 0 while True: if s1[pointer1] == BACKSPACE or s2[pointer2] == BACKSPACE: # decrement the cursor and undo the previous compare cursor -= 1; if s1[cursor] != s2[cursor]: num_diff -= 1 # decrement the canonical lengths appropriately canon_len1 -= 2 if s1[pointer1] == BACKSPACE else 0 canon_len2 -= 2 if s2[pointer2] == BACKSPACE else 0 else: if s1[pointer1] != s2[pointer2]: num_diff += 1 cursor += 1 # increment the pointers, making sure we don't run off then end pointer1 += 1; pointer2 += 1; if pointer1 == len(s1) and pointer2 == len(s2): break if pointer1 == len(s1): pointer1 -= 1 if pointer2 == len(s2): pointer2 -= 1 return num_diff == 0 and canon_len1 == canon_len2 if __name__ == "__main__": main() </code></pre> <pre><code>#!/usr/bin/env python import compare_strings import unittest class compare_strings_test(unittest.TestCase): def test_01(self): raised = False try: compare_strings.compare('Toronto', 'Cleveland') except: raised = True self.assertFalse(raised, 'Exception raised') def test_02(self): equivalent = compare_strings.compare('Toronto', 'Cleveland') self.assertEquals(equivalent, False) def test_03(self): equivalent = compare_strings.compare('Toronto', 'Toroo\b\bnto') self.assertEquals(equivalent, False) def test_04(self): equivalent = compare_strings.compare('Toronto', 'Torooo\b\bntt\bo') self.assertEquals(equivalent, True) if __name__ == "__main__": unittest.main() </code></pre> <pre><code>...F ====================================================================== FAIL: test_04 (__main__.compare_strings_test) ---------------------------------------------------------------------- Traceback (most recent call last): File "compare_strings_test.py", line 26, in test_04 self.assertEquals(equivalent, True) AssertionError: False != True ---------------------------------------------------------------------- Ran 4 tests in 0.001s </code></pre> <p>测试4失败,但是“Toronto”和“Torooo\b\bntt\bo”应该是相等的减去backspaces</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>最好事先使用如下函数从字符串中删除退格:</p> <pre><code>def normalize(s): result = [] for c in s: if c == '\b': result.pop() // A try-catch block could be added here else: result.append(c) return "".join(result) </code></pre> <p>然后比较。你知道吗</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
Python中两个字典的交集
9 回答
python中两个字符串上的异或操作数?
8 回答
Python中两个字符串中的类似句子
3 回答
Python中两个字符串之间的Hamming距离
4 回答
python中两个字符串之间的匹配模式
8 回答
python中两个字符串之间的按位或
6 回答
python中两个字符串之间的数据(字节)切片
9 回答
python中两个字符串之间的模式
4 回答
python中两个字符串作为子字符串的区别
8 回答
Python中两个字符串元组的比较
1 回答
Python中两个字符串列表中的公共字符串
3 回答
python中两个字符串的Anagram测试
5 回答
Python中两个字符串的正则匹配
2 回答
python中两个字符串的笛卡尔乘积
2 回答
Python中两个字符串相似性的比较
9 回答
python中两个字符串语义相似度的求法
1 回答
Python中两个字符置换成固定长度的字符串,每个字符的数目相等
4 回答
Python中两个对数方程之间的插值和平滑数据
7 回答
Python中两个对象之间的And/Or运算符
3 回答
python中两个嵌套字典中相似键的和值
3 回答