Sorry if this is a general question I'm a bit of a beginner and I'm looking for a simple and uncomplicated library or way to do the following
I have some code that eventually depends on two parameters eg:
param1 = 12
param2 = 5
and at the end I get a variable that changes depending on these parameters
score = 5031
I want an easy way to loop over the code with randomized parameters until it finds the combination that gives the highest score
you can get it by looping for every index in the list and choosing the max out of it with max built in function
Related
UPDATED QUESTION AND CODE!30.01.2021
This question logically came up after this question - How to find a separate attribute among the records and form a separate collection on it?
I need to calculate the average score of each group.
In the code that the user #itprorh66 suggested, I added a formula for calculating the average score(it works) and a conditional search operator for the grade.
Mister #itprorh66
I decided to modify your code a bit to try to solve my problem myself. But so far unsuccessful.
This code block is working
def srtgr(self):
counthrees=0
countfours=0
countfives=0
midl_average=0
gl=dict() #Dictionary for the group
for s in studinfos:
sd=gl.pop(s.numbgroup,dict())
sd[s.surn]=[s.markgeometry,s.markalgebra,s.markinformatika]
gl[s.numbgroup]=sd
for k,v, in gl.items():
s=f'Group: {k}: \n'
for stdn,grds in v.items():
s+=f' {stdn}\t{grds[0]},{grds[1]},{grds[2]} \n'
print(s)
This is the second part in def srtgr. In this code block I trying to calculate the number of grades (3,4 and 5) in each group, and then find the average grade for each group. With the help of a loop, I start traversing the dictionary and with the help of the branch operator I start counting the scores in each group, but that's what I thought when I wrote this code. In reality, nothing starts and does not count
for grds in gl.items():
if grds.markgeometry==3:
counthrees+=1
if grds.markalgebra==3:
counthrees+=1
if grds.markinformatika==3:
counthrees+=1
if grds.markgeometry==4:
countfours+=1
if grds.markalgebra==4:
countfours+=1
if grds.markinformatika==4:
countfours+=1
if grds.markgeometry==5:
countfives+=1
if grds.markalgebra==5:
countfives+=1
if grds.markinformatika==5:
countfives+=1
midl_average = (counthrees+countfours+countfives)/3
for kk,grds1 in gl.items():
grds=f'Group: {kk}: {grds1}\n'
print(grds)
print(*sorted(gl, key = midl_average, reverse = True))
INPUT DATA
My reputation is litlle and I can not add the picture form my computer and I send the link,where my input data, what I add in class and how my object classes collection showing in the console. And the separated of groups
https://ibb.co/zV1B0HC
How to make it so that the average grade is displayed at the bottom after the list of students in each group?
Or just calculate the average score in each group and enter it into another dictionary?
I figured out the sorting of values in the dictionary.
I tried to make a 2nd dictionary and put the groups there, and then calculate it in a separate assessment unit - it didn't work.
Then I read that in Python you can add a dictionary to a dictionary.
As a result, I tried to add the existing dictionary to the new one, but did not understand how to proceed.
Because grades must be calculated anyway
You cannot use any helper classes, work with only one class.
dict.items() returns key value pairs in a dictionary. Based on the code the v dictionary has two kinds of data
key is a student number and value is a list of grades.
key is a student numbgroup and the value is a list with an empty list in it.
My best guess is you don't want the second type of thing in that dictionary. I can't tell what the redball stuff is supposed to do but currently you make it an empty list, put it in a dictionary that is carrying a totally different kind of info and then overwrite the variable with a float without ever messing with the earlier empty list.
As an aside, don't be afraid of making verbose variable names. It's harder to follow the code when we need to work out what k and v are. That we includes you by the way, even with your own code bad naming can make it harder to diagnose errors.
I would like to perform the following:
Given a total number of observations (in this case the variable 'total_models'), I would like to parse this for parallel processing by a given number of python sessions ('sessions' variable and 'by' variable). I figure it best to perform this task using a dictionary.
The desired results should can be found in the 'obs_dict' object. For any given input to 'total_models', 'sessions' and 'by'. Can you assist in creating the desired output in a dictionary object? If possible, I would like to see the answer using some sort of list or dictionary comprehension.
total_models=1000000
sessions=4
by=int(total_models/sessions)
### Desired Output.
obs_dict={1:'0:250000',2:'250001:500000',3:'500001:750000',4:'750001:1000000'}
obs = {i+1: str(i*by+1)+':'+str((i+1)*by) for i in range(sessions)}
Edited, Kyle:
For the odd models it would seem wrapping a ceil the division will ensure we we do not go over the 'total_models'
total_models=1000326
sessions=5
by=math.ceil(total_models/sessions)
obs = {i+1: str(i*by+1)+':'+str(min((i+1)*by,total_models)) for i in range(sessions)}
EDIT: When I say function in the title, I mean mathematical function not programming function. Sorry for any confusion caused.
I'm trying to create a function from randomly generated integers and operators. The approach I am currently taking is as follows:
STEP 1: Generate random list of operators and integers as a list.
STEP 2: Apply a set of rules to the list so I always end up with an integer, operator, integer, operator... etc list.
STEP 3: Use the modified list to create a single answer once the operations have been applied to the integers.
For example:
STEP 1 RESULT: [1,2,+,-,2,/,3,8,*]
STEP 2 RESULT: [1,+,2,-,2,/,3,*,8] - Note that I am using the operator command to generate the operators within the list.
STEP 3 RESULT: The output is intended to be a left to right read function rather than applying BODMAS, so in this case I'd expect the output to be 8/3 (the output doesn't have to be an integer).
So my question is: What function (and within what module) is available to help me combine the list as defined above. OR should I be combining the list in a different way to allow me to use a particular function?
I am considering changing the way I generate the list in the first place so that I do the sort on the fly, but I think I'll end up in the same situation that I wouldn't know how to combine the integers and operators after going through the sort process.
I feel like there is a simple solution here and I am tying myself up in knots unnecessarily!
Any help is greatly appreciated,
Dom
Why not create one list for the ints and one for the operators and them append from each list step by step?
edit: you can first convert your ints to strings then, create a string by using string=''.joint(list) after that you can just eval(string)
edit2: you can also take a look at the Sympy module that allows you to use symbolic math in python
I don't know if it's easier, but an elegant way would be to use a binary tree, where leaves are operands and other nodes are operators. You can directly generate it (without lists) by doing something like this (quick and dirty, probably wrong, but you get the idea) :
def generate(root, end_depth, depth):
root = random_operator()
right_child = random_operand()
if depth == end_depth:
left_child = random_operand()
else:
generate(left_child, end_depth, depth + 1)
Your example would like this :
*
/ \
div 8
/ \
- 3
/ \
+ 2
/ \
1 2
It's "backwards" because when you evaluate, you need to start by the bottom, where the 2 operands are known.
So, for those that are interested. I achieved what I was after by using the eval() function. Although not the most robust, within the particular loop I have written the inputs are closely controlled so I am happy with this approach for now.
I need help with this problem and I think the major reason I don't know how to do it is because I don't really understand what it is asking for.
The problem is asking to write a program that will find the curve of a class. It has to find the "cut off" values for A,B,C and D. 40% of the class will receive an A, as well as a B(40%), 10% will receive a C and the remaining 10% will get a D.
The grades have to be generated randomly between 20 - 100 and the number of student has to be an input. a tuple has to be returned with the cut off values of A,B,C and D.
It also gives me the hint to use list.sort() and min(list)
I honestly don't really get what this is asking for, so far I have this but I am not sure what I am doing.
import random
def Cutoffgrade():
students=float(input('How many students are in the class?'))
grades=list(range(20,101))
list.sort(grades)
Astudents=students*0.4
Bstudents=students*0.4
Cstudents=students*0.1
Dstudents=students*0.1
I also have this other function I did which I know is something similar to what I have to do, but I just don't get this problem.
Here is the other function I did for a different problem that is somewhat asking for a random list as well:
def createlist(v1,v2):
random.randint(v1,v2)
random.randrange(v1,v2)
numlist=[]
for i in range(5):
n=random.randrange(v1,v2)
numlist.append(n)
print(numlist)
So I have been trying to transform this function into what I would need for the new problem but don't know how.
Since this is a homework question and I imagine your professor isn't very likely to get back to you during the weekend I will give you some hints at how to tackle this:
First build a list of grades using random.randint(start, stop) and a list comprehension or standard for loop
Sort the list.
Determine how many students should be in each grade bracket.
Slice the list into the appropriate chunks and find the min() of each list.
Alright so I am trying to make a program that prompts for a square bin dimension using a list of lists to fill this list of lists with squared blocks. It also prompts for a text file for example: blockList.txt:
3 1 2 1 3
I have a function that splits that up into a list and tries to fill in the space of the lists using the First Fit descending algorithm. The problem is is that the function only fills the highest valued item in the list and then stops and prints the grid. Can someone help me figure out why it isn't looping correctly? All help would be much appreciated
Here is my code:
https://gist.github.com/anonymous/1ac55a8fcb350d0992a4
I'm not 100% on python syntax, but it seems you called your placement() function in your pack() function before you defined your placement() function. That could be messing you up.