i have an array with random nums
a = [1, 123, 155, 200]
i need to find that numbers that have at the end number 55 like 155, 255, 7055....
for i in a:
some code
....
some code
print(len(array_of_55))
but i have this example that`s not optimized i think
def checkSuffix(A, B):
# Convert numbers into strings
s1 = str(A);
s2 = str(B);
# Find the lengths of strings
# s1 and s2
n1 = len(s1)
n2 = len(s2)
# Base Case
if (n1 < n2):
return False;
# Traverse the strings s1 & s2
for i in range(n2):
# If at any index characters
# are unequals then return false
if (s1[n1 - i - 1] != s2[n2 - i - 1]):
return False;
# Return true
return True;
# Driver Code
# Given numbers
A = 12345
B = 45;
# Function Call
result = checkSuffix(A, B);
# If B is a suffix of A, then
# print "Yes"
if (result):
print("Yes")
else:
print("No")
how i can do it? It looks like your post is mostly code; please add some more details. It looks like your post is mostly code; please add some more details.
CodePudding user response:
We can use a list comprehension along with the modulus.
a = [1, 123, 155, 200]
output = [i for i in a if i % 100 == 55]
print(output) # [155]
CodePudding user response:
There is a simpler logic. Numbers that end with 55 leave the remainder 55 on division with 100. So code:
a = [1, 123, 155, 200]
b = []
for i in a:
if i0==55:
b.append(i)
print(b)
CodePudding user response:
This is not the best way but it works:
a = [1, 123, 155, 200]
output = []
for num in a:
num = str(num)
if len(num) > 1:
if num[-2:] == '55':
output.append(int(num))
else:
pass
print(output)
Output
[155]