C# mod gives wrong answer not as python mod (%) [duplicate] - python

This question already has answers here:
modulo of a number - python vs c#
(3 answers)
Mod of negative number is melting my brain
(15 answers)
Closed 7 months ago.
I'm trying to get % result of ( -1%26 )
the result should be 25
in python it's right
but C# gives -1 as result
why and how to get 25 in C# not -1

You can use a different kind of modulus function, like this:
int Modulus(int a, int b) {
return ((a % b) + b) % b;
}
Console.WriteLine(Modulus(-1, 26)); // prints 25

Related

Python: C code for-loop that multiplies the loop variable [duplicate]

This question already has answers here:
Is there any method like divide by or multiply by in python range()?
(4 answers)
Closed 2 years ago.
The C code here:
for(i = 1; i <= 1000; i *= 3){
//task
}
How can I write this in python with a for loop?
So far I can only think of a solution using while loops:
i = 1
while i <= 1000:
#task
i *= 3
Is there a way I can write this with a for loop in python?
try this
step = 3
_i=1
for x in range(0,1000):
if x%_i!=0:
continue
_i*=step
# task
print(x)

Boolean operator precedence [duplicate]

This question already has answers here:
Python operators precedence
(2 answers)
Closed 2 years ago.
I have question about Python operator precedence.
print(not (8 < 4) or (10 == 5 * 2) and not (5 > 3))
The above code prints 'True'. But I think the result is 'False', with the following steps:
>>print(not F or T and not T)
>>print(T or T and F)
>>print(T and F)
>>print(F)
So I don't know why the result is 'True'
Maybe I'm missing the small detail.
and has higher precedence than or.
>>print(not F or T and not T)
>>print(T or T and F)
>>print(T or (T and F)) # Evaluated like this.
>>print(T or F)
>>print(T)
Adding the following (unnecessary) brackets will explain how it's getting evaluated:
print((not (8 < 4)) or ((10 == 5 * 2) and not (5 > 3)))
Since (not (8 < 4)) is evaluated to True (the other side of the or doesn't matter) this is the returned result

5-bit signed binary to int [duplicate]

This question already has answers here:
Two's Complement in Python
(20 answers)
Closed 6 years ago.
My code has to read 5-bit signed binary values. Let's say it reads a 11111, which would be two's complement -1. But int("0b11111", 2) returns 31 instead.
How can I parse the correct value?
Here's a possible solution testing all 5-length binary numbers of your future emulator:
import itertools
def two_complement(value, length):
if (value & (1 << (length - 1))) != 0:
value = value - (1 << length)
return value
opcodes_emulator = ["".join(seq) for seq in itertools.product("01", repeat=5)]
for my_string in opcodes_emulator:
print my_string, two_complement(int(my_string, 2), len(my_string))

To find the lcm of two numbers [duplicate]

This question already has answers here:
Division in Python 3 gives different result than in Python 2
(3 answers)
Closed 6 years ago.
I was given the task to compute lcm of any two numbers.I have coded in python.The problem is when i compiled it under python2.7, i got a result which is different from , when i compiled under python3.
import sys
def gcd(a,b):
if b == 0:
return a
remainder = a % b
return gcd(b,remainder)
def lcm(a, b):
return int( (a*b) / gcd(a,b))
if __name__ == '__main__':
input = sys.stdin.read()
a, b = map(int, input.split())
print(int(lcm(a, b)))
Input
226553150 1023473145
Output
46374212988031352 (under python3.5)
46374212988031350 (under python2.7)
Can someone help me ?
In Python 2 the division operator / will perform integer division, and not float division, when dealing with two integers. You can force the Python 3 behavior in Python 2 by importing division from __future__
>>> from __future__ import division
>>> 2/3
1.5

why does print 2++3 print 5 in python? [duplicate]

This question already has answers here:
Why does 1+++2 = 3?
(6 answers)
Closed 7 years ago.
I am new to python and I read that there is no ++ operator in Python but I am not able to understand the below code.
>>>print (2++3)
>>>5
+ and - act as unary as well as binary operators. So,
a ++ b is same as a + (+b)
a -+ b is same as a - (+b)
a -- b is same as a - (-b)
a +- b is same as a + (-b)
As can be seen below
>>> 2++3
5
>>> 2-+3
-1
>>> 2--3
5
>>> 2+-3
-1
This is not the ++ operator. Your code is interpreted as follows:
2 + (+3)
Now since +3 is 3, the final result is 5. For fun, try the following:
2++++++++++++++3
2+++++++++++++++++++++-8
See Expressions for details.

Categories

Resources