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

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

问题描述

我正在尝试使用 Python 检查回文.我拥有的代码非常for-loop 密集型.

在我看来,人们在从 C 到 Python 的过程中犯的最大错误是尝试使用 Python 实现 C 逻辑,这会导致运行缓慢,而且没有充分利用该语言.

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

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

我在课堂上用 C 完成了这个,但我想在个人基础上用 Python 完成.问题来自 Euler Project,顺便说一下,很棒的网站.

def isPalindrome(n):lst = [int(n) for n in str(n)]l=len(lst)如果 l==0 ||l==1:返回真elif len(lst)%2==0:对于范围内的 k (l)#####别的:而 (k<=((l-1)/2)):如果(列表[]):#####对于范围内的 i (999, 100, -1):对于范围内的 j (999,100, -1):如果是回文(i*j):打印(i*j)休息

我在这里遗漏了很多代码.这五个哈希值只是给我自己的提醒.

具体问题:

  1. 在 C 中,我会做一个 for 循环,比较索引 0 和索引最大值,然后索引 0+1 和 max-1,直到出现某些东西.如何在 Python 中最好地做到这一点?

  2. 我的 for 循环(在 (999, 100, -1) 范围内,这是在 Python 中执行此操作的糟糕方法吗?

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

  4. 任何可以很好地解决此问题的特定代码位也将不胜感激,我需要学习好的算法..我设想了 3 种情况.如果该值为零或一位数,如果它是奇数长度,如果它是偶数长度.我打算写 for 循环...

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

解决方案

替代相当不直观的 [::-1] 语法是这样的:

<预><代码>>>>测试 = "abcba">>>测试 == ''.join(反转(测试))真的

reversed 函数返回 test 中字符的反向序列.

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

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

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.

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.

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?

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.

Concrete questions:

  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?

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

  3. 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.

  4. 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: The problem is: Find the highest value product of two 3 digit integers that is also a palindrome.

解决方案

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

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

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

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

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

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