Is there a better way of comparing functions within another function? How can I handle too many conditions without having ugly code?
#Function to Convert string to integer
def input(X):
if type (X) == str:
X = int(X)
return X
#Function to check input equality
def equality(A,B,C):
if input(A) == input(B) or input(B) == input(C) or input(C) == input(A):
return True
else:
return False
This will help you..
def input(X):
if type(X) == str or type(X) == int:
return int(X)
def equality(*argv):
tmp_list = []
for arg in argv:
tmp_list.append(input(arg))
return not len(tmp_list) == len(set(tmp_list))
I changed the function to work with *args and now you can check more than three at once.
>>> equality(10,11,20,20)
True
>>> equality(10,'11',20,'20')
True
>>> equality('10','20',30,'40')
False
Related
This is my data list
data_list = [
'https://img2.doubanio.com/view/photo/s_ratio_poster/public/p2372307693.jpg', 'https://img2.doubanio.com/view/photo/s_ratio_poster/public/p2616355133.jpg',
'http://42.194.197.95:8001/poison_img_url',
'https://img2.doubanio.com/view/photo/s_ratio_poster/public/p480747492.jpg', 'https://img2.doubanio.com/view/photo/s_ratio_poster/public/p2578474613.jpg', 'https://img9.doubanio.com/view/photo/s_ratio_poster/public/p457760035.jpg', 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p524964039.jpg', 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p511118051.jpg', 'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2557573348.jpg', 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2561716440.jpg',
'http://42.194.197.95:8001/poison_img_url',
'https://img2.doubanio.com/view/photo/s_ratio_poster/public/p492406163.jpg',
'http://42.194.197.95:8001/poison_img_url'
]
I want to remove string in the list,but,My solution is too time consuming,i need some efficient way
def make_url(data_list,remove_str):
img_array = []
#print(result)
for index,x in enumerate(data_list):
if(remove_str == x):
data_list.append(x)
print(url_list)
if __name__ == "__main__":
remove_str = 'http://42.194.197.95:8001/poison_img_url'
t1 = time.time()
make_url(data_list,remove_str)
t2 =time.time()
print(t2-t1)//7s too slowly
use list comprehension to keep items not equal to x
def make_url(data_list, remove_str):
data_list[:] = [x for x in data_list if remove_str != x] # this will change the original list
if you want to return a new list then use return statement inside your function.
return [x for x in data_list if remove_str != x]
You could use the filter method:
value_to_be_removed = 'http://42.194.197.95:8001/poison_img_url'
result = list(filter(lambda val: val != value_to_be_removed, url_list))
My perfect number function is not working as intended :(. It prints false even though it should print true :(
def perfect_check(number):
z = []
for i in range(1, number):
if number % i == 0:
z.append(i)
if sum(z) == number:
return True
else:
return False
print(perfect_check(6))
def perfect_check(number):
z = []
for i in range(1, number):
if number % i == 0:
z.append(i)
if sum(z) == number:
return True
else:
return False
print(perfect_check(6))
You have put the if-else statement inside your for loop. It should be outside the for loop. Then, your code will work correctly.
Whenever i try print the number reverser function i always get an infinite loop,instead of my expected output 54321. Can someone help find the problem? Thanks.
def order(num):
x=str(num)
if x==False:
return None
else:
return order(x[1:])+(x[0])
print (order(12345))
Welcome to your community.
There are some problems with your code:
First:
A string never will be equal to False.
for instance:
'0' is not equal to False.
'' is not equal to False.
Second:
You cannot add a String to None.
This Error will be thrown: TypeError: can only concatenate str (not "NoneType") to str
Modify your code like this:
def order(num):
x=str(num)
if x=='':
return ''
else:
return order(x[1:])+(x[0])
print (order(12345))
Tip 1: A string can be equal to '' (empty string).
In your function, you compare the string x with the boolean False. This is not a correct way to test whether string x is an empty string or not.
In addition, if string x is empty, then you shouldn't return None, but an empty string: reversing an empty string should logically return an empty string.
Here I present two ways to fix your function, which I implemented under the names reverse0 and reverse1. I also present a few other alternatives to achieve the same result using python features, under the names reverse2 to reverse6. Finally I present three other ways to reverse nonnegative integers, under the names reverse7 to reverse9.
def reverse0(num):
x = str(num)
if len(x) == 0:
return ''
else:
return reverse0(x[1:]) + x[0]
def reverse1(num):
x = str(num)
if not x:
return ''
else:
return reverse1(x[1:]) + x[0]
def reverse2(num):
return str(num)[::-1]
def reverse3(num):
return ''.join(reversed(str(num)))
def reverse4(num):
x = str(num)
return ''.join(x[len(x)-1-i] for i in range(len(x)))
def reverse5(num):
x = str(num)
return ''.join(x[-i] for i in range(1, len(x)+1))
def reverse6(num):
y = ''
for c in str(num):
y = c + y
return y
# reverse7 only works for nonnegative integers
def reverse7(num):
if num < 10:
return str(num)
else:
return str(num % 10) + reverse7(num // 10)
# reverse8 only works for nonnegative integers
def reverse8(num):
l = []
while num > 9:
l.append(num % 10)
num = num // 10
l.append(num)
return ''.join(str(d) for d in l)
# reverse9 only works for nonnegative integers
# reverse9 returns an int, not an str
def reverse9(num):
y = 0
while num > 0:
y = 10 * y + (num % 10)
num = num // 10
return y
Relevant documentation:
builtin reversed;
str.join;
An informal introduction to string slices such as x[::-1].
this code is for checking if a word is a palindrome or not, and the decorator check if the word is a string
def check(func):
def string(w):
if w != str(w):
print('no')
else:
print('yes')
func(w)
return string
#check
def palindrome(w):
print(w)
inverse = ''
inverse = w[::-1]
if inverse == w:
print('1')
return True
else:
print('2')
return False
print(palindrome("test"))
After running it:
yes
test
2
None
At this point, everything should be ok, but I don't know why it is returning None instead of True or False.
Your check decorator ignores return value from decorated function. You need explicit return in wrapper function:
def check(func):
def string(w):
if w != str(w):
print('no')
else:
print('yes')
return func(w)
return string
I have this code for checking if a number is a palindrome and for some reason it returns false for number=1 even though it is palindrome. Why is that? The code works for other cases such as 12321.
def palindrome_integer(number):
if number != int:
return False
elif str(number) == str(number)[::-1]:
return True
else:
return False
If you want to check if number is integer, you should use isistance.
def palindrome_integer(number):
if not isinstance(number, int):
return False
elif str(number) == str(number)[::-1]:
return True
else:
return False
The rest of your code seems to work fine.
One-liner:
return isinstance(n, int) and str(n) == str(n)[::-1]
Or slightly more contrived:
import re
x = str(n)
return re.match(r”\d+“, x) and x == x[::-1]
solution without string
def palindrome_integer(num):
copy = num
rev = 0
while num!= 0:
rev = rev*10+num%10
num = num//10
return rev == copy
def palindrome_integer(number):
return type(number) == int and str(number)[::-1] == str(number)
Don't hesitate to comment if you have problem in understanding the solution.