Version number comparison [duplicate] - python

This question already has answers here:
How do I compare version numbers in Python?
(16 answers)
Closed 5 years ago.
I am trying to compare android version numbers in my code. If any version is less than 4.1 I want that version number.
Should I use comparison on strings directly as below?
Examples:
"4.0.3" < "4.1" # should return.
"5.0" < "4.1" # should not return.

Try this
def compare_versions_greater_than(v1, v2):
for i, j in zip(map(int, v1.split(".")), map(int, v2.split("."))):
if i == j:
continue
return i > j
return len(v1.split(".")) > len(v2.split("."))
a = "2.0.3"
b = "2.1"
print(compare_versions_greater_than(a, b))
print(compare_versions_greater_than(b, a))
Output
False
True

You can transform the version string to float. And then compare them.
def version2float(version):
main, *tail = version.split('.')
temp = ''.join(tail)
return float('.'.join([main, temp]))

Related

Sort list a into ascending order by value using recursion instead of a loop in python [duplicate]

This question already has answers here:
A recursive function to sort a list of ints
(9 answers)
Closed 2 years ago.
How to sort a list into ascending order by value using recursion instead of a loop in python?
For example, to sort [2,0,1] to [0,1,2].
def sort(a):
pos = 0
if pos == 0 or a[pos] >= a[pos - 1]:
pos += 1
return sort(a)
else:
a[pos], a[pos-1] = a[pos-1], a[pos]
pos -= 1
return sort(a)
Here is what I wrote and I know it does not work because the pos is always equal to 0 at first.
How could I fix it?
I test the code below.
enter image description here
Based on this answer, Quick sort is an example of recursive sorting algorithm and can be implemented in Python like this:
def quick_sort(l):
if len(l) <= 1:
return l
else:
return quick_sort([e for e in l[1:] if e <= l[0]]) + [l[0]] +\
quick_sort([e for e in l[1:] if e > l[0]])

Assert that all values are negative or all values are non-negative [duplicate]

This question already has answers here:
Check if all numbers in a list are same sign in Python?
(4 answers)
Closed 4 years ago.
I need to assert that all numeric values in an array are either negative or non-negative.
I wrote this:
def check(arr):
return all([i < 0 for i in arr]) or all([i >= 0 for i in arr])
And this, which is slightly more efficient I suppose:
def check(arr):
temp = [i < 0 for i in arr]
return all(temp) or not any(temp)
I would like to know if there's a cleaner / more pythonic way, or perhaps some arithmetic trick which I can use instead.
One way is to use a set comprehension to derive a set of Boolean values. This set will be either {True}, {False} or {True, False}. Then test if your set has length equal to 1.
def check(arr):
return len({i < 0 for i in arr}) == 1

python swap two elements [duplicate]

This question already has answers here:
Python multiple assignment issue (list) [duplicate]
(3 answers)
Tuple unpacking order changes values assigned
(4 answers)
Closed 5 years ago.
I have the following code for a small program:
def get_different_number(arr):
if len(arr) == 0 or arr is None:
return 0
n = len(arr)
for i in range(n):
print (str(i))
temp = arr[i]
while (temp < n and temp != arr[temp]):
temp, arr[temp] = arr[temp],temp
for i in range(n):
if arr[i] != i:
return i
return n
test = [0,1,2,4,5]
get_different_number(test)
however, when it executes, it tells me there is a problem in the line where I swap temp and arr[temp], it gives me a list index out of range error at i is 3 (temp is 4).
When I change that line to arr[temp], temp = arr[temp], temp (reversed assign order), it worked fine. Why is this happening? I thought the a,b = b,a assignment in python assign both elements at the same time?

Checking if element is in tuple using if condition [duplicate]

This question already has answers here:
How to check if a tuple contains an element in Python?
(4 answers)
Closed 6 years ago.
I am trying to check if there is a certain digit inside my tuple using an if statement, but finding it hard. Whats wrong here?
def racaman(x):
y = x
w = (0,)
for i in range(y):
k = w[i]-x[i]
if k == i in w:
w = w + ((w[i]+x[i]),)
else:
w = w + ((w[i]-x[i]),)
You can replace 3 in the if condition to find a specific digit
def raceman(x):
#assuming x is tuple
if 3 in x:
print("found")
else:
print("not found")
raceman((1,2,3,4))
Please correct your question, paste code properly.
I am not sure what you are asking for but, I guess:
tupl = (1,2,3,4,5)
if 1 in tupl:
print('y')
else:
print('n')
I would recommend a list instead
def racaman(x):
w = [0]
for i in range(x):
k = w[i]-x[i]
if k in w: # fix this
w.append(w[i]+x[i])
else:
w.append(k) # already calculated
return w # did you want to return that?
This could simply be a matter of checking like so:
>>>n in t
Where n is the digit and t is the tuple, for example:
>>>2 in (1,2,3)
True
However it is not enough if you are looking for a digit and the elements are strings:
>>>2 in ('a1','a2','a3') #won't return desired output since digit '2' is part of a string
False
If so, you would need to resort to a more adaptive method, iterating over the elements of the tuple and testing each one with an appropriate regular expression (import re).

Count of a string in a list [duplicate]

This question already has answers here:
How do I count the occurrences of a list item?
(30 answers)
Closed 9 years ago.
I need help:
I want to write function that: takes a list as a input and return count of specific string.
If function input is x=[a,a,b,c] function need return 2 (a is two times in this list).
>>> def F(seq, string):
return seq.count(string)
>>> F(['a','a','b','c'], 'a')
2
Equivalent to:
def F(seq, string):
n = 0
for x in seq:
if x == string:
n += 1
return n

Categories