IF statement, number less than X but greater than Y - excel

Within my program I have a column(H) specifying hours:
(24.2, 23.5, 21.5, 25.0, 28.3, 23.1, 22.5, 17.9, 22.1, 16.2, 24.3, 23.8)
this continues for 600 or so more rows.
Max hours = 36.88348
Min hours = 16.15569
I'm trying to categorise the hours into four different numbers to later use for more accurate data than averages:
0 = 16-20,
1 = 21-25,
2 = 26-30,
3 = 31>.
So far I have came to this solution:
=IF($H4>=31,3,IF($H4<=20,0,IF($H4>=21<=25,1,IF($H4>=26,2))))
This works apart from the 21-25($H4>=21<=25,1).
If anybody could assist me, I believe it's something basic as my syntax.

Shorter still:
=MATCH(H4,{0,21,26,31})-1

Slightly shorter:
=LOOKUP(H4,{0,21,26,31},{0,1,2,3})

Try this:-
=IF($H4>=31,3,IF($H4<=20,0,IF(AND($H4>=21,$H4<=25),1,IF($H4>=26,2))))

Just start with the lowest value and work up:
=IF($H4<=20,0,(IF($H4<=25,1,(IF($H4<=30,2,3)))))

The following should work:
=IF($H4>=31,3,IF($H4>=26,2,IF($H4>=21,1,IF($H4<21,0))))

Related

Python setting Decimal Place range without rounding using truncate does not work for some numbers

I wish to take in a float variable,
e.g.
w = float(1.678)
and control how far out the float goes without round(), e.g.
x = 1.67
y = 1.6
z = 1
Which was already answered in Python setting Decimal Place range without rounding? and it worked well until one day this number 0.00225 popped up and it some how just doesn't work for this specific number
In [161]: truncate(0.00225, 5)
Out[161]: 0.00224
0.00224 and 0.00226 also gave similar issues.
Here's the output I got while testing from 0.00223 - 0.00227
In [159]: truncate(0.00223, 5)
Out[159]: 0.00223
In [160]: truncate(0.00224, 5)
Out[160]: 0.00223
In [161]: truncate(0.00225, 5)
Out[161]: 0.00224
In [162]: truncate(0.00226, 5)
Out[162]: 0.00225
In [163]: truncate(0.00227, 5)
Out[163]: 0.00227
Why does this happen for some numbers and how do I fix it?
(I mainly use this for a trading algo which requires inputs to have precise decimals)
This is not a simple problem and it has no simple solution. I would start by studying this article:
https://realpython.com/python-rounding/

How to print the fractional part of the number?

It's my first question
So, the problem is python rounding. I have seen it, but I dont really know how to get around it.
For example: i have the number 10.34 - I need to receive just fractional part, so 0.34
I had some ideas how to do that. One of this:
n = float(input())
print(n - int(n))
In case of 10.34 the code give me "0.33999999999999986" instead 0.34.
I have some ideas how to do it with help of strings or another tools, but the task assumes that I need just some basic tools
Use round:
res = n - int(n)
print(round(res, 10))
n = float(input()) n = n - int(n) n = round(n,2)
https://www.w3schools.com/python/ref_func_round.asp
The round() function returns a floating point number that is a rounded version of the specified number, with the specified number of decimals.
round(number, digits)
For your refrence

How to round a big number to 1000 position in numpy?

I want to round a big numer like 15698 to the lowest, nearest 1000-th value like this:
num = 15698
num_round = function(num)
>> num_round = 15000
How can I do this? I only found np.round where I can set the decimals but haven't found a similar function for my case.
Can someone help me?
Something like this:
def round(num, factor=1000):
return (num//factor)*factor

problem with rounding in calculating minimum amount of coins in change (python)

I have a homework assignment in which I have to write a program that outputs the change to be given by a vending machine using the lowest number of coins. E.g. £3.67 can be dispensed as 1x£2 + 1x£1 + 1x50p + 1x10p + 1x5p + 1x2p.
However, I'm not getting the right answers and suspect that this might be due to a rounding problem.
change=float(input("Input change"))
twocount=0
onecount=0
halfcount=0
pttwocount=0
ptonecount=0
while change!=0:
if change-2>=0:
change=change-2
twocount+=1
else:
if change-1>=0:
change=change-1
onecount+=1
else:
if change-0.5>=0:
change=change-0.5
halfcount+=1
else:
if change-0.2>=0:
change=change-0.2
pttwocount+=1
else:
if change-0.1>=0:
change=change-0.1
ptonecount+=1
else:
break
print(twocount,onecount,halfcount,pttwocount,ptonecount)
RESULTS:
Input: 2.3
Output: 10010
i.e. 2.2
Input: 3.4
Output: 11011
i.e. 3.3
Some actually work:
Input: 3.2
Output: 11010
i.e. 3.2
Input: 1.1
Output: 01001
i.e. 1.1
Floating point accuracy
Your approach is correct, but as you guessed, the rounding errors are causing trouble. This can be debugged by simply printing the change variable and information about which branch your code took on each iteration of the loop:
initial value: 3.4
taking a 2... new value: 1.4
taking a 1... new value: 0.3999999999999999 <-- uh oh
taking a 0.2... new value: 0.1999999999999999
taking a 0.1... new value: 0.0999999999999999
1 1 0 1 1
If you wish to keep floats for output and input, multiply by 100 on the way in (cast to integer with int(round(change))) and divide by 100 on the way out of your function, allowing you to operate on integers.
Additionally, without the 5p, 2p and 1p values, you'll be restricted in the precision you can handle, so don't forget to add those. Multiplying all of your code by 100 gives:
initial value: 340
taking a 200... new value: 140
taking a 100... new value: 40
taking a 20... new value: 20
taking a 20... new value: 0
1 1 0 2 0
Avoid deeply nested conditionals
Beyond the decimal issue, the nested conditionals make your logic very difficult to reason about. This is a common code smell; the more you can eliminate branching, the better. If you find yourself going beyond about 3 levels deep, stop and think about how to simplify.
Additionally, with a lot of branching and hand-typed code, it's very likely that a subtle bug or typo will go unnoticed or that a denomination will be left out.
Use data structures
Consider using dictionaries and lists in place of blocks like:
twocount=0
onecount=0
halfcount=0
pttwocount=0
ptonecount=0
which can be elegantly and extensibly represented as:
denominations = [200, 100, 50, 10, 5, 2, 1]
used = {x: 0 for x in denominations}
In terms of efficiency, you can use math to handle amounts for each denomination in one fell swoop. Divide the remaining amount by each available denomination in descending order to determine how many of each coin will be chosen and subtract accordingly. For each denomination, we can now write a simple loop and eliminate branching completely:
for val in denominations:
used[val] += amount // val
amount -= val * used[val]
and print or show a final result of used like:
278 => {200: 1, 100: 0, 50: 1, 10: 2, 5: 1, 2: 1, 1: 1}
The end result of this is that we've reduced 27 lines down to 5 while improving efficiency, maintainability and dynamism.
By the way, if the denominations were a different currency, it's not guaranteed that this greedy approach will work. For example, if our available denominations are 25, 20 and 1 cents and we want to make change for 63 cents, the optimal solution is 6 coins (3x 20 and 3x 1). But the greedy algorithm produces 15 (2x 25 and 13x 1). Once you're comfortable with the greedy approach, research and try solving the problem using a non-greedy approach.

How do I round an input variable to a certain number

I am using python 3+ and I want to round a variable up to 500 and if the input is higher than 500 then it rounds up to 1000. Is there a way I could math.ceil() or round() ?
I have done this so far but I'm not sure whether I have come across it in the right way.
import math
x = int(input("how much data did you use this month? "))
math.ceil(x / 500.0) * 500.0
print(x)
I want to round x up to 500 whatever the number is but if it is higher (e.g - 600) I want it to round it to 1000. The last line does not work and only prints what the user inputted.
math.ceil() returns the value that you expect but you haven't assignet it to anything.
Simply assign that value to your variable.
This is the solution:
import math
x = int(input("how much data did you use this month? "))
x = math.ceil(x / 500.0) * 500.0
print(x)
Have you tried:
x = math.ceil(x / 500.0) * 500.0
to update your x variable before printing your x variable?

Resources