How Do I Minus A Total From It's Next Highest Multiple Of 10 - highest

So I have a total, say 24, I need my code to find the nearest hightest multiple of 10. This would be 30 of course, so I need the code to calculate (30-24). If the number is 20 it would be 20 beucase it is equal to a highest muliple of 10. I then need to store the result for later use.

>>> def nh(val):
... return 9 - ((val + 9) % 10)
...
>>> nh(24)
6
>>> nh(20)
0

Related

How to find 90th% value?

I have this list:
def grades = [5,4,3,2,1,1]
Where index is a grade, and value is an occurrence of the grade:
Grade
Occurrence
0
5
1
4
2
3
3
2
4
1
5
1
How can I calculate the 90th percentile for the grades?
This gets me a whole grade. I was hoping to find an exact value of 90th percentile, but this will do for me.
def grades = [5,4,3,2,1,1]
sum=grades.sum()
per_grade=0
per_value=sum*0.9
grades.eachWithIndex { grade_count, grade ->
per_value-=grade_count
if (per_value<0){per_grade=grade-1}
if (per_value==0){per_grade=grade}
}
out.write(per_grade)
A total of 16 grades have been given. 90% are 14.4 grades, so discard the lowest 14 grades and take the smallest remaining (in your example it will be 4).
How to code? There are some ways:
You may count through the array you have got. Subtract 5 from 14 (= 9), then 4, then 3, then 2. Once you reach zero, you’re at the index of the 90th percentile.
Maybe easier to understand, but will require a few more code lines: Put all 16 grades into an array (or list): [0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 5]. Since the array is sorted, the median is found at index 14.

how to get a kind of "maximum" in a matrix, efficiently

I have the following problem: I have a matrix opened with pandas module, where each cell has a number between -1 and 1. What I wanted to find is the maximum "posible" value in a row that is also not the maximum value in another row.
If for example 2 rows has their maximum value at the same column, I compare both values and take the bigger one, then for the row that has its maximum value smaller that the other row, I took the second maximum value (and do the same analysis again and again).
To explain myself better consider my code
import pandas as pd
matrix = pd.read_csv("matrix.csv")
# this matrix has an id (or name) for each column
# ... and the firt column has the id of each row
results = pd.DataFrame(np.empty((len(matrix),3),dtype=pd.Timestamp),columns=['id1','id2','max_pos'])
l = len(matrix.col[[0]]) # number of columns
while next = 1:
next = 0
for i in range(0, len(matrix)):
max_column = str(0)
for j in range(1, l): # 1 because the first column is an id
if matrix[max_column][i] < matrix[str(j)][i]:
max_column = str(j)
results['id1'][i] = str(i) # I coul put here also matrix['0'][i]
results['id2'][i] = max_column
results['max_pos'][i] = matrix[max_column][i]
for i in range(0, len(results)): #now I will check if two or more rows have the same max column
for ii in range(0, len(results)):
# if two id1 has their max in the same column, I keep it with the biggest
# ... max value and chage the other to "-1" to iterate again
if (results['id2'][i] == results['id2'][ii]) and (results['max_pos'][i] < results['max_pos'][ii]):
matrix[results['id2'][i]][i] = -1
next = 1
Putting an example:
#consider
pd.DataFrame({'a':[1, 2, 5, 0], 'b':[4, 5, 1, 0], 'c':[3, 3, 4, 2], 'd':[1, 0, 0, 1]})
a b c d
0 1 4 3 1
1 2 5 3 0
2 5 1 4 0
3 0 0 2 1
#at the first iterarion I will have the following result
0 b 4 # this means that the row 0 has its maximum at column 'b' and its value is 4
1 b 5
2 a 5
3 c 2
#the problem is that column b is the maximum of row 0 and 1, but I know that the maximum of row 1 is bigger than row 0, so I take the second maximum of row 0, then:
0 c 3
1 b 5
2 a 5
3 c 2
#now I solved the problem for row 0 and 1, but I have that the column c is the maximum of row 0 and 3, so I compare them and take the second maximum in row 3
0 c 3
1 b 5
2 a 5
3 d 1
#now I'm done. In the case that two rows have the same column as maximum and also the same number, nothing happens and I keep with that values.
#what if the matrix would be
pd.DataFrame({'a':[1, 2, 5, 0], 'b':[5, 5, 1, 0], 'c':[3, 3, 4, 2], 'd':[1, 0, 0, 1]})
a b c d
0 1 5 3 1
1 2 5 3 0
2 5 1 4 0
3 0 0 2 1
#then, at the first itetarion the result will be:
0 b 5
1 b 5
2 a 5
3 c 2
#then, given that the max value of row 0 and 1 is at the same column, I should compare the maximum values
# ... but in this case the values are the same (both are 5), this would be the end of iterating
# ... because I can't choose between row 0 and 1 and the other rows have their maximum at different columns...
This code works perfect to me if I have a matrix of 100x100 for example. But, if the matrix size goes to 50,000x50,000 the code takes to much time in finish it. I now that my code could be the most inneficient way to do it, but I don't know how to deal with this.
I have been reading about threads in python that could help but it doesn't help if I put 50,000 threads because my computer doesn't use more CPU. I also tried to use some functions as .max() but I'm not able to get column of the max an compare it with the other max ...
If anyone could help me of give me a piece of advice to make this more efficient I would be very grateful.
Going to need more information on this. What are you trying to accomplish here?
This will help you get some of the way, but in order to fully achieve what you're doing I need more context.
We'll import numpy, random, and Counter from collections:
import numpy as np
import random
from collections import Counter
We'll create a random 50k x 50k matrix of numbers between -10M and +10M
mat = np.random.randint(-10000000,10000000,(50000,50000))
Now to get the maximums for each row we can just do the following list comprehension:
maximums = [max(mat[x,:]) for x in range(len(mat))]
Now we want to find out which ones are not maximums in any other rows. We can use Counter on our maximums list to find out how many of each there are. Counter returns a counter object that is like a dictionary with the maximum as the key, and the # of times it appears as the value.
We then do dictionary comprehension where the value is == to 1. That will give us the maximums that only show up once. we use the .keys() function to grab the numbers themselves, and then turn it into a list.
c = Counter(maximums)
{9999117: 15,
9998584: 2,
9998352: 2,
9999226: 22,
9999697: 59,
9999534: 32,
9998775: 8,
9999288: 18,
9998956: 9,
9998119: 1,
...}
k = list( {x: c[x] for x in c if c[x] == 1}.keys() )
[9998253,
9998139,
9998091,
9997788,
9998166,
9998552,
9997711,
9998230,
9998000,
...]
Lastly we can do the following list comprehension to iterate through the original maximums list to get the indicies of where these rows are.
indices = [i for i, x in enumerate(maximums) if x in k]
Depending on what else you're looking to do we can go from here.
Its not the speediest program but finding the maximums, the counter, and the indicies takes 182 seconds on a 50,000 by 50,000 matrix that is already loaded.

From time products to numberic products

I have a list of products which is defined by time (half-hour). I would like to change from time to numbers without have to use 48 if functions:
From: To:
0000 1
0030 2
0100 3
0130 4
.... ...
2330 48
Do you guys have any smart shortcut to this?
You need to split the string into the hours and minutes parts. This can be done by slicing the string and then converting to integers.
From here, each hour represents 2 half-hours so we multiply by 2 and since you are saying the time 00:00 has one half-hour (?) we need to add 2 if there is 30 mins in the minutes parts, otherwise add just 1 to account for that initial offset. To check whether the minutes is 30, we could do ==30 but since the only other case is 0, we can just check the truthiness of the minutes value in a ternary.
So, the one liner:
def half_hour(t: str):
return int(t[:2]) * 2 + (2 if int(t[2:]) else 1)
and some tests:
>>> half_hour('0000')
1
>>> half_hour('0030')
2
>>> half_hour('0100')
3
>>> half_hour('0130')
4
>>> half_hour('2330')
48
You could do this with the datetime module if you wanted to. It's perhaps a little clumsy since time-only datetime instances are assumed to be on January 1, 1900, but it doesn't require that you do the string parsing yourself:
_epoch = datetime.datetime(1900, 1, 1)
_halfhour = datetime.timedelta(minutes=30)
def half_hour(time_string):
parsed_time = datetime.datetime.strptime(time_string, '%H%M')
delta = parsed_time - _epoch
return delta // _halfhour + 1

Different output in for loop

I made a simple program to generate prime numbers but It shows 10 as a prime number. In debugging the for loop incremented i from 2 to 4 in single step when number went from 9 to 10.
for i in range (2, int( number / 2) + 1):
if number % i == 0:
number += 1
i = 2
yield number
number += 1`

How can I use a while loop in python to add values in a list until they exceed a maximum value?

I am learning python V3 and have the following problem on a homework assignment:
In this exercise, your function will receive a list of numbers, and an integer. It will add the values in the list to a total as long as the total is less than or equal to the value of the second parameter. The sum of numbers in the list will always be larger than the second parameter's value.
I came up with the following solution:
def while11(nums,maxi):
i=0
total=0
while total<=maxi:
total+=nums[i]
return total
The test parameters are as follows:
[1,1,1,1,1,1,1,1,1], 6
[2,2,2,2,2,2,2,2,2], 6
range(10, 1, -1), 25
The my function returns 7 and 8, respectively, for the first two sets of parameters. However, it should return 27 for the third set, but it returns 30. It appears to be adding 11,10, and 9 as opposed to 10,9, and 8.
You need to increment i when you are performing your calculation.
def while11(nums,maxi):
i=0
total=0
while total<=maxi:
total+=nums[i]
i+=1
return total
Your function is simply taking the first value in a list and adding it until it is greater than the max value. You probably didn't notice this because in your first two cases, all values are the same. In your third case however, you have a list consisting of [10, 9, 8, 7, 6, 5, 4, 3, 2]. This should be taking 10 + 9 + 8 + 7... etc. until the max value, but your function is taking 10 + 10 + 10 + 10 .... etc. until the max value.
You need to change the value of i in your while loop:
def while11(nums,maxi):
i = 0
total = 0
while total <= maxi:
total += nums[i]
i += 1
return total
Otherwise it will always add the first value to total
You have forgotten to update the i variable on every iteration.
You keep adding the num[0] element

Resources