如何使用Python逻辑检查回文 [英] How to check for palindrome using Python logic

查看:107
本文介绍了如何使用Python逻辑检查回文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python检查回文.我拥有的代码非常消耗for循环.

I'm trying to check for a palindrome with Python. The code I have is very for-loop intensive.

在我看来,当人们从C转到Python时,最大的错误就是尝试使用Python实现C逻辑,这使事情运行缓慢,并且只是没有充分利用该语言.

And it seems to me the biggest mistake people do when going from C to Python is trying to implement C logic using Python, which makes things run slowly, and it's just not making the most of the language.

我在网站上看到.搜索"C-style for",即Python没有C-style for循环.可能已过时,但我将其解释为意味着Python为此具有自己的方法.

I see on this website. Search for "C-style for", that Python doesn't have C-style for loops. Might be outdated, but I interpret it to mean Python has its own methods for this.

我尝试环顾四周,但对此却找不到太多最新的建议(Python 3).如何在不使用for循环的情况下用Python解决回文挑战?

I've tried looking around, I can't find much up to date (Python 3) advice for this. How can I solve a palindrome challenge in Python, without using the for loop?

我已经在C类中完成了此操作,但我想在Python上基于个人进行此操作.问题出在 Euler Project 这个很棒的网站上.

I've done this in C in class, but I want to do it in Python, on a personal basis. The problem is from the Euler Project, great site By the way,.

def isPalindrome(n):
    lst = [int(n) for n in str(n)]
    l=len(lst)
    if l==0 || l==1:
        return True
    elif len(lst)%2==0:
        for k in range (l)
        #####
    else:
        while (k<=((l-1)/2)):
            if (list[]):
                #####   

for i in range (999, 100, -1):
    for j in range (999,100, -1):
        if isPalindrome(i*j):
            print(i*j)
            break

我在这里缺少很多代码.这五个哈希只是对自己的提醒.

I'm missing a lot of code here. The five hashes are just reminders for myself.

具体问题:

  1. 在C语言中,我将进行一个for循环,将索引0与索引max进行比较,然后将索引0 + 1与max-1进行比较,直到出现某种情况.如何在Python中做到最好?

  1. In C, I would make a for loop comparing index 0 to index max, and then index 0+1 with max-1, until something something. How to best do this in Python?

我的for循环(范围为(999,100,-1),这是在Python中执行此方法的不好方法吗?

My for loop (in in range (999, 100, -1), is this a bad way to do it in Python?

有人对我所处位置的人有任何好的建议,好的网站或资源吗?我不是程序员,我并不渴望成为一个程序员,我只是想学习足够多的知识,这样当我写学士学位论文(电气工程)时,在尝试尝试时不必同时学习适用的编程语言在项目中取得良好的结果.诸如此类的事情如何从基本的C到Python的出色应用".

Does anybody have any good advice, or good websites, or resources for people in my position? I'm not a programmer, I don't aspire to be one, I just want to learn enough so that when I write my bachelor's degree thesis (electrical engineering), I don't have to simultaneously LEARN an applicable programming language while trying to obtain good results in the project. "How to go from basic C to great application of Python", that sort of thing.

对这个问题做出很好解决的任何特定代码段也将受到赞赏,我需要学习良好的算法..我正在设想3种情况.如果该值为零或一位数字,则它的长度为奇数,并且为偶数长度.我打算写循环...

Any specific bits of code to make a great solution to this problem would also be appreciated, I need to learn good algorithms.. I am envisioning 3 situations. If the value is zero or single digit, if it is of odd length, and if it is of even length. I was planning to write for loops...

PS:问题是:找到两个也是回文的3位整数的最高乘积.

PS: The problem is: Find the highest value product of two 3 digit integers that is also a palindrome.

推荐答案

相对不直观的[::-1]语法的替代方法是:

An alternative to the rather unintuitive [::-1] syntax is this:

>>> test = "abcba"
>>> test == ''.join(reversed(test))
True

reversed函数返回test中字符的相反顺序.

The reversed function returns a reversed sequence of the characters in test.

''.join()将这些字符再次连接在一起,中间没有任何内容.

''.join() joins those characters together again with nothing in between.

这篇关于如何使用Python逻辑检查回文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆