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

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)

Related

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

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

Range Function That Prints Even Numbers From 100 To 200 Inclusive [duplicate]

This question already has answers here:
how to make a range() only even numbers in python3.3?
(2 answers)
Closed 9 months ago.
I have to create a range function so that the code prints the even numbers between 100 and 200 inclusive.
The solution I tried is
num = 100
for x in range(num % 2, 200):
print(x)
range(start,end,step)
list(range(100,200,2))

Write Numeric Manipulation in Python in 1 line [duplicate]

This question already has answers here:
How can I clamp (clip, restrict) a number to some range?
(9 answers)
Closed 4 years ago.
i have a simple problem in Python where i need to return a value that is equal to floor if it is smaller than floor, and return ceiling if the value is larger than ceiling. I'm not sure if there is a simpler way to do so. What i will do is:
def floor_ceil(x):
floor = 0
ceil = 100
if x < floor:
return floor
elif x > ceil:
return ceil
return x
Are there simpler ways to do so? The code looks clunky
You could reuse the built-in max and min:
def floor_ceil(x):
return max(0, min(100, x))

Calculating catalan numbers with memoization [duplicate]

This question already has answers here:
calculating catalan numbers using memoization
(2 answers)
Closed 7 years ago.
I am trying to use memoization in order to calculate catalan numbers, but it just does not seem to work, what do I need to change?
def catalan_mem(n, mem = None):
if n==0:
return 1
if mem==None:
mem={}
sum=0
if n not in mem:
for i in range(0, n):
sum = sum + catalan_mem(i) * catalan_mem(n-i-1)
mem[n]=sum
return mem[n]
You need to pass the cache to your recursive calls:
sum = sum + catalan_mem(i, mem) * catalan_mem(n-i-1, mem)
Otherwise, every method call creates a new empty cache.

C++ syntax when declaring an integer [duplicate]

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 8 years ago.
I'm having a difficult time learning python syntax. I've been doing some algorithms for a merge sort and I've run into a bit of an issue.
def arrMerge(a):
for i in range(1,len(a), *2):
for j in range(0,len(a)-1,2*i):
end2 = (2*i < len(a) -j) ? 2*i : len(a) -j
this block in python any ideas on how I should go about executing it?
I assume that you are asking what is the Python equivalent syntax to C++ ternary operator. In Python you would use a conditional expression that has the syntax value if condition else other_value.
So your assignment would become:
end2 = 2 * i if 2 * i < len(a) - j else len(a) - j
It is usually better to use a plain if though:
if 2 * i < len(a) - j:
end2 = 2 * i
else:
end2 = len(a) - j

Categories

Resources