回文验证

A_byte_of_Python-v192-for_Python_3.0-中文版.pdf 中的一道题:

练习题:
检测一个文本是否为回文应该忽略标点,空格和大小写。
例如”Rise to vote,
sir.”同样是一个回文,但是我们当前的例子无法识别它。你能
改善这个例子让它做都这点吗?

代码有点挫,莫笑:

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

def reverse(text):

    return
text[::-1]

def is_huiwen(text):

    return
text ==
reverse(text)

while
True:

    somethine =
input(‘Enter text:‘)

    somethine =
somethine.lower()

    alist =
[]

    

    for
oneword in
somethine:

        if(oneword.isalpha()):

            alist.append(oneword)

        else:

            continue

            

    if
(is_huiwen(alist)):

        print
("YES,it‘s a huiwen string")

    else:

        print("No,it‘s not a huiwen string")

  

回文验证,码迷,mamicode.com

时间: 2024-08-12 04:35:29

回文验证的相关文章

[LeetCode] Palindrome Number 验证回文数字

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

Validate Palindrome 验证回文字符串

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "race a car" is not a palindrome. Note:Have you consider that the string might be empty? This is a good question to ask du

LeetCode(125):验证回文串

Easy! 题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a canal: Panama" 输出: true 示例 2: 输入: "race a car" 输出: false 解题思路: 验证回文字符串是比较常见的问题,所谓回文,就是一个正读和反读都一样的字符串,比如"level"或者"n

125. 验证回文串

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a canal: Panama" 输出: true 示例 2: 输入: "race a car" 输出: false 思路:从头尾开始同时遍历比较 目前 beat 95% class Solution { public boolean isPalindrome(String s) {

LeetCode 第125题 验证回文串

(一)题目描述 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a canal: Panama" 输出: true 示例 2: 输入: "race a car" 输出: false (二)算法思路 1 先将字符串转换成字符数组 用到的函数 toCharArray() 2 从两端开始判断每一个字符是否为字母或数字,虽然题目说的只考虑字

125验证回文串

Algorithm [leetcode]125验证回文串 https://leetcode.com/problems/valid-palindrome/ 1)problem 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a canal: Panama" 输出: true 示例 2: 输入: "race a car" 输出: fal

Leetcode 680.验证回文字符串

验证回文字符串 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca" 输出: True 解释: 你可以删除c字符. 注意: 字符串只包含从 a-z 的小写字母.字符串的最大长度是50000. 1 class Solution { 2 public boolean isPalindromeRange(String s, int i, int j) { 3 for (in

力扣(LeetCode)验证回文串 个人题解

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a canal: Panama" 输出: true 示例 2: 输入: "race a car" 输出: false 这题是验证回文串的一个变种,因为里面有干扰的字符串存在,不能直接判断,不过也很好解决,直接添加过滤的语句,遇到不是数字且不是字母的直接跳过,只有两边都是合法的,才进行比

力扣(LeetCode)125. 验证回文串

给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a canal: Panama" 输出: true 示例 2: 输入: "race a car" 输出: false java知识点 Java 字符串拼接,推荐使用StringBuilder String 本身没有反转函数 ,但是StringBuilder有 reverse() Str