6

Python function to check the palindrome - codesd.com

 2 years ago
source link: https://www.codesd.com/item/python-function-to-check-the-palindrome.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python function to check the palindrome

advertisements

Given below is the code to check if a list is a palindrome or not. It is giving correct output for 983. Where am I going wrong?

def palindrome(num):
    flag=0
    r=num[::-1]
    for i in range (0, len(num)-1):
        if(r[i]==num[i]):
            flag=1
        else:
            flag=0
    return flag


You should return as soon as there is a mismatch. Also, you just need to iterate till half the length:

def function(...):
    ...
    for i in range (0, (len(num) + 1) / 2):
        if r[i] != num[i]:
            return False
    return True


BTW, you don't need that loop. You can simply do:

def palindrome(num):
    return num == num[::-1]

Related Articles

How to add a new column in Pandas by setting my own function that checks the values ​​in many columns?

So i have a very big data set, and i need to create a function that checks the value in the same row for multiple columns, obviously the values are different for each column i want to check. Then if all the given columns to check their values are tru

Python recursive function to check if Palindrome

This question already has an answer here: How do I test multiple variables against a value? 14 answers I need to define a recursive function to check if a string is a palindrome. Below's my code: def palindrome_recur(string): if len(string)==0 or 1:

I'm doing a python function to check if the input is a date

So as you can see from the title I'm making a function to check if user input is a date. This is what I have so far: import datetime def validate(date): try: datetime.datetime.striptime(date, %Y-%m-%d) expect ValueError: raise ValueError("incorrect d

A pythonic way to check the logic

What I have found so far in many cases python code for checking business logic or any other logic is some thing like below(simplified): user_input = 100 if user_input == 100: do_something() elif user_input > 100: do_sth_different() else: do_correct()

Writing a python function to cancel the values ​​of each identifier

Below is my json data. What I want to do is write a python function to loop out the values of each id. Would anybody please help me with this? [ { "id": "1", "code": "111", }, { "id": "2", "

Python script to check the output of Linux disk space

I am beginner in the python, I am writing a python script to verify the utilization of each mount point is above the threshold or not. I am able invoke the shell command and save the output to a variable. But I am not able to use the variable to spli

Preg_match function to check the value of the currency

I have some values like £523.22 9454 ££45523 741.03 etc I want to remove this £ from the value and check that whether it is valid vaue or not So that I can insert them into database I Create following expression with the help of preg_match() or simil

Hashing a python function to regenerate the output when the function is modified

I have a python function that has a deterministic result. It takes a long time to run and generates a large output: def time_consuming_function(): # lots_of_computing_time to come up with the_result return the_result I modify time_consuming_function

Translate the Python function to apply the mask to the image in Java

I'm trying to translate the following Python function, that applies a mask to an image, into Java: # Applies an image mask. def region_of_interest(img, vertices): #defining a blank mask to start with mask = np.zeros_like(img) #defining a 3 channel or

The Python function directly modifies the list, does not return it

[SOLVED] So the code goes like: >>> a = [1,3,2] >>> my_func(a) >>> a == [] True Where my_func alters the list without returning it. I know how to do this in C with pointers, but really confused over a python solution. Thanks in

Postgresql trigger function to check the date of a concert

So I have a problem with trigger function before insert on table performance. In table performance I have id_performance(PK) id_performer(foreign key) id_concert(FK) id_song(FK) role (it can be violine, sopran, alt etc) In table concert I have: id(PK

python - pythonic way to check the assignment and exit if no

I was looking for a python analogous of (C) if ((p = malloc(N * sizeof(*p))) == NULL) return 1; Something like user = getUser() or return does not work nor would if (user = getUser() is None) return - what's the pythonic way ?If you are looking for a

Rewrite the python function without inspecting the module

How would I write this introspective function without using the inspect module. import inspect def trace(cls): for name, m in inspect.getmembers(cls, inspect.ismethod): setattr(cls,name,log(m)) return cls The above is from this SO question/answer Edi

Python: How to check the data type of the key-value pair of an object?

I am creating a function to add the marks of each array(homework, quiz, test) inside an object(student1). There definitely arises errors as the code tries to add "Lloyd". I want to check the data-type of value of "name" so as to carry

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK