Python mining algorithm
Publish: 2021-05-12 11:26:49
1.
[1] proct function calculates the proct of list elements
[2] the subcombine function enumerates the whole subset of the list element combination
[3] for statement block verifies whether the combination meets the conditions
2.
1题:
Staff.txt员工信息单
14000
25000
36000
47000
57000
68000
710000
812000
915000
1020000
运行脚本:
#!/usr/bin/envpython
classTax:
def__init__(self,name,wage):
self.name=name
self.wage=float(wage)
deftax(self):
ifself.wage<=3000:
printself.name,'taxis0'
elifself.wage<=6000:
printself.name,'taxis',self.wage*0.05
elifself.wage<=10000:
printself.name,'taxis',self.wage*0.10
elifself.wage<=20000:
printself.name,'taxis',self.wage*0.20
elifself.wage<=100000:
printself.name,'taxis',self.wage*0.40
else:
printself.name,'taxis',self.wage*0.45
withopen('Staff.txt')asf:
d=f.readlines()
foriind:
count=Tax(i.split()[0],i.split()[1])
count.tax()
执行结果:
1taxis200.0
2taxis250.0
3taxis300.0
4taxis700.0
5taxis700.0
6taxis800.0
7taxis1000.0
8taxis2400.0
9taxis3000.0
10taxis4000.0
2题:
#!/usr/bin/envpython
printfilter(lambdax:x%3==0andx%5!=0,range(1000))
3题:
#!/usr/bin/envpython
sum1=0
count=6
defheight(x):
ifx==1orx==2:
return10
returnfloat(height(x-1))/2
foriinrange(1,count+1):
sum1+=height(int(i))
printsum1
4题:
#!/usr/bin/envpython
sum1=0
count=6
defheight(x):
ifx==10:
return0
return(height(x+1)+1)*2
printheight(0)
3. 1.
linear regression algorithm in linear regression, we want to build a model to fit the relationship between a dependent variable y and one or more independent variables (predictive variables) X. Is a target variable, it is a scalar linear regression model, can be understood as a very simple neural network:...
2.
logistic regression algorithm in logistic regression, we try to model the linear combination of given input characteristics, and get the output results of binary variables. For example, we can try to use the information of money and time spent by candidates to predict the outcome of an election (win or lose)
linear regression algorithm in linear regression, we want to build a model to fit the relationship between a dependent variable y and one or more independent variables (predictive variables) X. Is a target variable, it is a scalar linear regression model, can be understood as a very simple neural network:...
2.
logistic regression algorithm in logistic regression, we try to model the linear combination of given input characteristics, and get the output results of binary variables. For example, we can try to use the information of money and time spent by candidates to predict the outcome of an election (win or lose)
4. The core of this problem lies in the design algorithm:
according to the description: the programming idea of this problem should be as follows: the sum of any three numbers divided by 2 = the average of any two of the remaining three numbers = the price of the game machine
you can do this by putting six numbers into an array and doing a multi-layer nested loop to traverse all combinations. When the above conditions are met, you can perform an action to return results, and there may be more than one answer.
according to the description: the programming idea of this problem should be as follows: the sum of any three numbers divided by 2 = the average of any two of the remaining three numbers = the price of the game machine
you can do this by putting six numbers into an array and doing a multi-layer nested loop to traverse all combinations. When the above conditions are met, you can perform an action to return results, and there may be more than one answer.
5.
forcock ring(1,20+1):<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>><<<<<<<<<<<<>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< />
forheninrange(1,33+1):
forbiddyinrange(1,99+1):
if(5*cock+3*hen+biddy/3)==100:
if(cock+hen+biddy)==100:
ifbiddy%3==0:
printcock,hen,hen
Python3d
6. of course. Python is a universal language. This is not difficult. Unless the algorithm is dependent on a specific hardware and software environment. Otherwise, all of them can be realized
now most algorithms are focused on big data and artificial intelligence. The basic algorithm has not been studied by many people. Python supports many libraries on big data and artificial intelligence. It's easy
recently, we are studying the algorithm of semantic learning, which is very fast to implement with Python. Usually, the new algorithm is written in Python first, and then converted to other languages after verification.
now most algorithms are focused on big data and artificial intelligence. The basic algorithm has not been studied by many people. Python supports many libraries on big data and artificial intelligence. It's easy
recently, we are studying the algorithm of semantic learning, which is very fast to implement with Python. Usually, the new algorithm is written in Python first, and then converted to other languages after verification.
7. >& quot;
set enc=utf-8
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
"
set longmenu=zh CN.UTF-8
set helplang=en
if has(" syntax") < br /> syntax on
endif
"& quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot;
set enc=utf-8
set fencs=utf-8,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936
"
set longmenu=zh CN.UTF-8
set helplang=en
if has(" syntax") < br /> syntax on
endif
"& quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot; quot;
8. 1. Put the start node into the open list (both F and g values of the start node are regarded as 0)
2. Repeat the steps:
I. find the node with the minimum F value in the open list, and take the found node as the current node
II. Delete the current node from the open list and add it to the closed list
III. for each node adjacent to the current node, perform the following steps in turn:
1. If the adjacent node is not passable or the adjacent node is already in the closed list, no operation will be performed, and continue to check the next node
2. If the adjacent node is not in the open list, add the node to the open list, set the parent node of the adjacent node as the current node, and save the G and F values of the adjacent node< If the adjacent node is in the open list, judge whether the g value reached by the current node is less than the original saved g value. If it is less than, set the parent node of the adjacent node as the current node, and reset the G and F values of the adjacent node.
IV. cycle end condition:
when the end node is added to the open list as the node to be inspected, Indicates that the path is found and the loop should be terminated
or when the open list is empty, it means that there is no new node to add, and there is no destination node in the inspected nodes, it means that the path cannot be found, and the cycle ends at this time
3. Starting from the end node, traverse along the parent node, and save the coordinates of the entire traversed node. The traversed node is the final path;
2. Repeat the steps:
I. find the node with the minimum F value in the open list, and take the found node as the current node
II. Delete the current node from the open list and add it to the closed list
III. for each node adjacent to the current node, perform the following steps in turn:
1. If the adjacent node is not passable or the adjacent node is already in the closed list, no operation will be performed, and continue to check the next node
2. If the adjacent node is not in the open list, add the node to the open list, set the parent node of the adjacent node as the current node, and save the G and F values of the adjacent node< If the adjacent node is in the open list, judge whether the g value reached by the current node is less than the original saved g value. If it is less than, set the parent node of the adjacent node as the current node, and reset the G and F values of the adjacent node.
IV. cycle end condition:
when the end node is added to the open list as the node to be inspected, Indicates that the path is found and the loop should be terminated
or when the open list is empty, it means that there is no new node to add, and there is no destination node in the inspected nodes, it means that the path cannot be found, and the cycle ends at this time
3. Starting from the end node, traverse along the parent node, and save the coordinates of the entire traversed node. The traversed node is the final path;
9. (a) The complexity of the algorithm is O (n), because there is only one while loop and I & lt; n. So the complexity is linear, only related to n
(b) the complexity of the algorithm is O (n & # 178;), In fact, the complexity of the algorithm is NxN / 2 = n & # 178/ 2, because there are nested for loops
(c) the complexity of the algorithm is O (n), because there is only a while loop, although I = IX2, it is a constant level operation
(d) the complexity of the algorithm is O (log I), which is a logarithmic level operation. Every time I is divided by 2, it is log (I) base (2)
(E) the complexity of the algorithm is O (n log n)
(f) the complexity of the algorithm is O (2 ^ I), which is a recursive algorithm, (g) the complexity of the algorithm is O (N 2 ^ n), which is a data exchange algorithm, a recursion + A for loop
(b) the complexity of the algorithm is O (n & # 178;), In fact, the complexity of the algorithm is NxN / 2 = n & # 178/ 2, because there are nested for loops
(c) the complexity of the algorithm is O (n), because there is only a while loop, although I = IX2, it is a constant level operation
(d) the complexity of the algorithm is O (log I), which is a logarithmic level operation. Every time I is divided by 2, it is log (I) base (2)
(E) the complexity of the algorithm is O (n log n)
(f) the complexity of the algorithm is O (2 ^ I), which is a recursive algorithm, (g) the complexity of the algorithm is O (N 2 ^ n), which is a data exchange algorithm, a recursion + A for loop
10. Lbfgs is a kind of algorithm. There are many introctions on the Internet. You can start from BFGS. If you want to write this by yourself, you'd better find some ready-made statistical packages.
Hot content