This question already has answers here:
How to calculate a mod b in Python?
(7 answers)
Closed 1 year ago.
I want to make the number begin from zero if it is more than twenty-five. For example,
x = 24 + 10 must be 8. How can I do that?
You can use the modulus operator, or %. a % b finds the remainder when you divide a by b. If you want to keep the number at 25 or lower, you should take the mod with 26.
def add_and_wrap(n1, n2, wrap_at):
return (n1 + n2) % (wrap_at + 1)
Related
This question already has answers here:
How to make sure the input is a multiple of 16, and if not add padding to it
(2 answers)
Closed 12 months ago.
def asn1_integer(i):
if i >= 0:
i = decimal_to_binary(i)
if len(i) < 8:
i = ('0'* (8 - len(i))) + i
I want the length of i to be a multiple of 8. So I need to have a function that adds '0's to the beginning of a bit string, relative to the 8k - len(i) = number_of_additional_bits equation.
P.s. bin, hex, str, int, bytearray, divmod functions are prohibited for this task.
decimal_to_binary(i) converts a decimal to binary equivalent by using the bitwise operators.
It's trivial to figure out the next multiple of 8 that's greater than your string's length. Use divmod to divide the current length by 8. If there's a remainder, your final string needs to be of a length (quotient + 1) * 8. If there isn't a remainder, you just need quotient * 8
i = "11001"
str_len = len(i)
req_len, remainder = divmod(str_len, 8)
if remainder: req_len += 1
req_len *= 8
Then, pad your string to req_len:
i = f'{i:>0{req_len}}' # '00011001'
The f'...' construct is called a f-string and is an easy way to do string interpolation. More info
This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Python 3 integer division [duplicate]
(1 answer)
Closed 2 years ago.
Why isn't q equal to b as the formula for modulo is: a % b = a - (a/b)*b ?
x = (int(time.time())*100)
q = x % 360
b = x - (x/360)*360
print(x)
print(q)
print(b)
This question already has answers here:
Find the division remainder of a number
(13 answers)
Closed 2 years ago.
In python, after I have divided to variables (k and h) How do I then find the remainder of the equation? This is the code I have:
h = int(input())
print(k / h)
Simple math does the trick:
h = int(input())
print(k % h)
This question already has answers here:
Python:How to make the sum of the values of a while loop store into a variable?
(4 answers)
Closed 3 months ago.
Basically i want to sum up the result of the expression k=5x+17 but with different x, like (k=53+17) + (k=5*4+17) and so on... so far my code looks like the following.The result needs to be Σ which goes from the range (3,9).
for x in range(3,9):
k=5*x+17
k+=k
print(k)
you're overwriting k at each iteration, so in the end the result is just (5*8+17)*2
To perform such a sum, with x varying between 3 and 8 (9 is not included) so it in a generator comprehension and pass the result to sum, you'll avoid the nasty side-effects like you just created.
result = sum(5*x+17 for x in range(3,9))
(of course if you want to include 9 you have to range from 3 to 10), so depending on the upper boundary, you get 267 or 329
You can also do that without using sum at all using the n*(n+1)//2 formula for sum of integers from 1 to n and adapting it a la project euler to reduce complexity:
start = 3
end = 9 # inclusive
result = ((end*(end+1))//2 - ((start-1)*(start))//2)*5 + (end-start+1)*17
Remembering that the sum of integers between 1 and n is n*(n+1)/2 and using some basic summation equalities, you can calculate the result directly:
>>> (3 + 9 - 1) * (9 - 3) // 2 * 5 + 17 * (9 - 3)
267
For a range from i to j and an expression a*x+b, you can do:
a*(j-i)*(i+j-1)//2 + b*(j-i)
Because what you want is:
Σax+b = aΣx + Σb
#Use this simple code
l=[]
for x in range(3,9):
y = lambda x :5*x+17
l.append(y(x))
l =sum(l)
print l
This question already has answers here:
What is the best way to get all the divisors of a number?
(18 answers)
Prime factorization - list
(17 answers)
Closed 5 years ago.
I just started to open for myself programming in python. I know how to find factors for example of 30. I will get [1,2,3,5,6,10,15,30].
But I need this output 30 = 2 * 3 * 5. Thank you in advance!
Since you're trying to find all prime, unique factors, I'd use the following function:
def factor(numberToFactor, arr=list()):
i = 2
maximum = numberToFactor / 2 + 1
while i < maximum:
if numberToFactor % i == 0:
return factor(numberToFactor/i,arr + [i])
i += 1
return list(set(arr + [numberToFactor]))
print(factor(59511555)) # [3, 5, 1747, 757]