Excel Formula Question - excel

How do I write the formula that would mean this: If X<$999.00 then multiply by 0% If X >$1000.00 but < $1499.00 then multiply by 10% If X > $1499.00 but < $1999.00 then multiply by 15% If X > $1999.00 then multiply by $20%?

I think this meets the specifications of your question, but some things are ambiguous (your conditions exclude everything between $999.00 and $1000.00 inclusive, for example) so I guessed at what your real intent was.
This formula will return:
0 if X < $1000
10% of X if X >= $1000 and X < $1500
15% of X if X >= $1500 and X < $2000
20% of X in all other cases
=IF(X < 1000, 0, IF(X < 1500, X * 0.1, IF(X < 2000, X * 0.15, X * 0.2)))

Related

Cycle through a variable range with another variable

I need to loop through 2 variables and cycle through 1 variable from 2 variables (whichever is bigger) until the range of the 2nd (longest) last.
For example
x = 5 #input by user
y = 8 #input by user
for x_val, y_val in itertools.zip_longest(range(x), range(y), fillvalue='-'):
print(x_val)
print(y_val)
Expected output
0
0
1
1
2
2
3
3
4
4
0
5
1
6
2
7
tried
x = 5
x_cyc = itertools.cycle(range(x))
y = 8
for x_val, y_val in itertools.zip_longest(range(x), x_cyc):
print(x_val)
print(y_val)
but that didn't make much sense.
you dont need zip longest, you create an infinite cycle for the smaller of the two numbers and then normal range for the larger number. this way the min range will be infinite and max range will be the finite range.
You can simply use normal zip to go through them till you reach the end of the non infinite range.
from itertools import cycle
x = 8
y = 5
min_range = cycle(range(min(x, y)))
max_range = range(max(x, y))
for x_val, y_val in zip(min_range, max_range):
print(x_val)
print(y_val)
OUTPUT
0
0
1
1
2
2
3
3
4
4
0
5
1
6
2
7
UPDATE BASED ON COMMENTS
Now the x_val and y_val are bound to the x and y range and the lowest of x or y ints will be cycled in range.
from itertools import cycle
x = 8
y = 5
x_range = range(x)
y_range = range(y)
if x > y:
y_range = cycle(y_range)
elif y > x:
x_range = cycle(x_range)
for x_val, y_val in zip(x_range, y_range):
print(x_val)
print(y_val)
Note that the output will now differ when x is greater than y or when y is greater than x since x will always output first.
OUTPUT x=2, y=3
0
0
1
1
0
2
OUTPUT x=3 y=2
0
0
1
1
2
0

Loop function advice

I am currently doing a course on the fundamentals of python and I have a question regarding the below loop function:
x = 0
for index in range(5, 10):
x = x + 10
print("The value of X is {}".format(x))
When I print it I get the following result:
The value of X is 10
The value of X is 20
The value of X is 30
The value of X is 40
The value of X is 50
This is where I get confused, I was expecting a result like:
The value of X is 60
The value of X is 70
The value of X is 80
The value of X is 90
The value of X is 100
Am I misunderstanding something?
Here is value of x, index at each loop iteration
Iteration 1. x = 10 + 0 , index = 5
Iteration 2. x = 10 + 10 , index = 6
Iteration 3. x = 10 + 20 , index = 7
Iteration 4. x = 10 + 30 , index = 8
Iteration 5. x = 10 + 40 , index = 9
Thats how your loop will execute
What your function effectively does is to call
x = x + 10
five times. Since it is initialized with x = 0, the output makes sense to me.
The index variable in the loop is not used at all. The following loops would be equivalent:
for _ in range(5, 10):
...
for i in range(0, 5):
...
In your case, it is only about repeating a certain operation 5 times.
If you wanted to have an output as described in the questions, you would need to modify the loop as follows:
x = 10
for i in range(5, 10):
# This loop will be performed for values
# of i of [5, 6, 7, 8, 9], so it includes
# the first value (5) and excludes that last
# one (10)
print(x * i)
>> 50
>> 60
>> 70
>> 80
>> 90
Look at the code line by line:
First, you initialize the variable x to the value 0.
Then you set up a loop which will run five times.
You add 10 to the original value of x, and set that new value to x
You print the value of x.
Repeat 5 times.
x starts at 0, then you add 10, then 10 again, and so on, until the final value is 50. Does that make sense?
To produce the output you are expecting, this is a loop you could use:
>>> x = 0
>>> for index in range(6, 11):
... x = index * 10
... print("The value of X is {}".format(x))
The value of X is 60
The value of X is 70
The value of X is 80
The value of X is 90
The value of X is 100
The initial value of x is set to 0 and in each iteration of the loop you're adding 10 to it. That's why you get these values.
I believe you wanted it something like this.
x = 0
for index in range(6,11):
x = index*10
print("The value of X is {}".format(x))

How can I avoid duplicate digit in the cicle result

The result of the code includes separetly "min" and "max" values. Can't get how I can print only result of cicle?
For examle, whan I set min = 1, max = 3 the result is:
1
3
1
2
3
And I need 1 2 3 without first 1 3.
min = int(input())
max = int(input())
while min <= max:
print(min)
min = min + 1
That depends entirely on the ide or python environment you are using. May I know which environment you are using?
Because when I ran here https://ideone.com/lUmjKL everything seems fine.
min = int(input())
max = int(input())
while min <= max:
print(min)
min = min + 1
Let me know.

How to combination between digits?

For given number N(Normal number) you must output amount of N-digit numbers, such, that last digits of their square is equal to 987654321.
where 1<=N<=10^6
It may be simple combinatorics problem. I am not sure. I am trying to find algorithm for this problem. What is the best algorithm to solve this problem?
The question is:
Find the number of n-digit number x that satisfies x^2 mod 1000000000 = 987654321.
1. Solution for n <= 9
First, calculate the value of x that satisfies x^2 = 987654321, 0 <= x < 1000000000.
You can pre-count in your PC because you only have to check for 10^9 integers.
I calculated the value and the result was following:
Result: 111111111,119357639,380642361,388888889,611111111,619357639,880642361,888888889
Therefore, if n <= 8 the answer is zero, and if n = 9 the answer is 8.
2. Solution for n >= 10
You can prove this facts because x^2 mod 1000000000 is cycle with length 10^9.
There are 8 solutions in 0 <= x < 10^9.
There are 8 solutions in 10^9 <= x < 2*10^9.
There are 8 solutions in 2*10^9 <= x < 3*10^9.
There are 8 solutions in k*10^9 <= x < (k+1)*10^9 (k is an integer).
So, you can say these things.
There are 8 solutions in 0 <= x < 10^9.
There are 80 solutions in 0 <= x < 10^10.
There are 800 solutions in 0 <= x < 10^11.
Therefore,
There are 72 solutions in all 10-digit number.
There are 720 solutions in all 11-digit number.
There are 7200 solutions in all 12-digit number.
So, you only have to output "72" + (n-10 0's) if n > 9.
3. Conclusion
If n <= 8, the answer is 0.
If n = 9, the answer is 8.
If n >= 10, the answer is 72 * 10^(n-10).

Excel formula based on If logic

I'm trying to build a formula if the value is 15% then 0 to 5% * 2, 5% to 10% * 5, 10% to 15% * 10 and sum of the value needs to be shown as a result.
Assuming the following logic -
If value >= 15% then return 0
If value < 5% then return value * 2
If value is >= 5% and < 10% then return value * 5
If value is between >= 10% and < 15% then return value * 10
You could try -
=IF((percentage) >= 0.15, actualValue * 0,
IF((percentage) < 0.05, actualValue * 2,
IF(AND((percentage) >= 0.05, (percentage) < 0.1), actualValue * 5,
IF(AND(((percentage) >= 0.1), ((percentage) < 0.15)), actualValue * 10))))

Resources