Comparing two numbers without loops or branching in Python - python

I have to compare two numbers and write greater, then smaller, but I don't know how to get smaller one. I currently have this code:
a = int(input())
b = int(input())
c = round(((a + b) + abs(a - b)) / 2)
x = "should be smaller one"
print("{0}\n{1}".format(c, x))

smaller is : c = round(((a + b) - abs(a - b)) / 2)

If I right understand you:
In [1]: a , b = 3, 4
In [2]: smaller = a if a < b else b
In [3]: smaller
Out[3]: 3
Here you can replace < to other operator

Related

Simple addition of 1 to a large number does not work? (Python 3.9)

Note: I am not that experienced in Python, therefore my code may not be as good as it could/should be.
I am attempting to create a tool to facilitate calculating the algebraic factors of a certain form of number (see https://en.wikipedia.org/wiki/Aurifeuillean_factorization). This is mostly as a test/learning experience, however I have run into a problem when attempting to calculate the parameter "c", which is defined as 2^(2k+1)+1. The addition step does not work for me. I am simply getting the returned value as 2^129, instead of 2^129+1 as I am looking to get. Is this an issue with Python itself, or am I making some sort of mistake in this.
Code:
import math
def make_aurifeuille_factors(base, exponent):
if base == 2 and exponent % 4 == 2:
k = (exponent - 2) / 4
c = int(1 + 2 ** (2*k + 1))
d = int(2 ** (k + 1))
L = c + d
M = c - d
return int(k), int(c), int(d), int(L), int(M)
def gcd(a, b):
return int(math.gcd(a, b))
print(make_aurifeuille_factors(2, 258))
k = (exponent - 2) / 4 makes k a float, which means you potentially introduce numerical error in computations down the line. Use integer division to stay in int world from the start:
def make_aurifeuille_factors(base, exponent):
if base == 2 and exponent % 4 == 2:
k = (exponent - 2) // 4
c = 1 + 2 ** (2*k + 1)
d = 2 ** (k + 1)
L = c + d
M = c - d
return k, c, d, L, M

When I convert an expression using shortcut operators the output is wrong

I read that if I have the following expression:
variable = variable op expression
It can be simplified and shown as follows:
variable op= expression
For example: i = i + 2 * j --> i += 2 * j
However, I have used the previous example in a = a / 2 * b --> a /= 2 * b
Let a=6 and b=3. Using both values in a=a/2*b, the output is 9. However, in the case of a /= 2* b, the output is 1
This is because you are changing the order of operations.
a = a / 2 * b is interpreted as "Divide a by 2, then multiply by b, then store the result in a", or a = (a / 2) * b.
a /= 2 * b is interpreted as "Multiply 2 by b, then divide a by the result, and store the final result in a", or a = a / (2 * b).
I hope this helps.

Median of three, pivot

I'm looking for the median of three, using this for a pivot in a QuickSort. I would not like to import any statistics library because I believe it creates a bit of overhead which I would like to reduce as much as possible.
def median(num_list):
if (num_list[0] > num_list[len(num_list) - 1]) and (num_list[0] < num_list[int(len(num_list)//2)]):
return num_list[0]
elif (num_list[int(len(num_list)//2)] > num_list[len(num_list) - 1]) and (num_list[0] > num_list[int(len(num_list)//2)]):
return num_list[int(len(num_list)//2)]
else:
return num_list[len(num_list) - 1]
this seems to be returning the last else statement every time, I'm stumped...
In Quicksort you do not usually want just to know the median of three, you want to arrange the three values so the smallest is in one spot, the median in another, and the maximum in yet another. But if you really just want the median of three, here are two ways, plus another that rearranges.
Here's a short way to find the median of a, b, and c.
return a + b + c - min(a, b, c) - max(a, b, c)
If you want only comparisons, and to get what may be the quickest code, realize that three comparisons may need to be executed but you want to try for only two. (Two comparisons can handle four cases, but there are six arrangements of three objects.) Try
if a < b:
if b < c:
return b
elif a < c:
return c
else:
return a
else:
if a < c:
return a
elif b < c:
return c
else:
return b
If you want to rearrange the values so a <= b <= c,
if a > b:
a, b = b, a
if b > c:
b, c = c, b
if a > b
a, b = b, a
return b
Let Python do the work for you. Sort the three elements, then return the middle one.
def median(num_list):
return sorted([num_list[0], num_list[len(num_list) // 2], num_list[-1]])[1]
Using min and max:
>>> numlist = [21, 12, 16]
>>> a, b, c = numlist
>>> max(min(a,b), min(b,c), min(a,c))
16
>>>
Going out on a limb - I have a functional streak so here is the itertools equivalent, even though it means importing a module
>>> import itertools
>>> numlist = [21, 12, 16]
>>> z = itertools.combinations(numlist, 2)
>>> y = itertools.imap(min, z)
>>> max(y)
16

How to enter a Googol in Python?

I've heard that Python does not have any upper limit with integers. So I wanted to give a try:
a = 1e100
b = 1
c = a + b + a
c - 2 * a
> 0.0
Unfortunately I realized that writing 1e2 returns a float while 100 returns an int.
I've then tested with long('1' + '0' * 100) which works.
a = long('1' + '0' * 100)
b = 1
c = a + b + a
c - 2 * a
> 1L
Is this solution the only way to affect a Googol to a variable?
Subsequent question:
How to avoid confusion between floating point and fixed point during computations?
You can get a Googol like so:
10**100
I dont really understand your question but i think you are asking is there only this way to manipulate googol variable .
i just tried this on my python idle and got this
>>> a = 10 ** 100
>>> b = 1
>>> c = a + b + a
>>> c - 2 *a
1
>>>
You could use the power operator:
base**times
so 123 googols would be 123*10**100

python sympy simplify and Eq

This may not be a question, just an observation, but is sympy supposed to work this way.
I have a two complicated expression, A and E, and I am trying to find out if they are equivalent. If I simplify one, say E, and use Eq(A,E) it does not return True, but the two separated with "==". If would have expected that sympy would be smart enough to work out they are equal. Eq(simplify(A),E) returns True. Here is the code ...
from sympy import *
B = symbols('B')
C = symbols('C')
F = symbols('F')
G = symbols('G')
H = symbols('H')
A = (B - C)*(G*(B + C) - (B - C - F)*H)**2
D = 2*(B**2+B*F-C**2)**2
E = A/D
ED=simplify(E*D)
print("E*D= {0}").format(str(ED))
print("A = {0}").format(str(A))
print("0 = {0}").format(str(simplify(A-ED)))
print("T = {0}").format(Eq(A,ED))
print("T = {0}").format(Eq(simplify(A),ED))
and the output
E*D= (B - C)*(G*(B + C) + H*(-B + C + F))**2
A = (B - C)*(G*(B + C) - H*(B - C - F))**2
0 = 0
T = (B - C)*(G*(B + C) - H*(B - C - F))**2 == (B - C)*(G*(B + C) + H*(-B + C + F))**2
T = True
Note the -H versus +H in the last expression.
Equality does not do any simplification and two objects are identical only if they are structurally (not mathematically) zero. Proving mathematical equality (in general) is not a simple problem so if they aren't identical (as in this case) SymPy doesn't even begin chasing the "equality rabbit" to its hole :-). This is the expected behavior. If you want to let SymPy try some simplification on its own, try using the equals method:
>>> A.equals(simplify(E*D))
True

Categories

Resources