Looping through equations - python-3.x

Is there a way to loop through these calculations and input them into a dataframe as the indicated variable instead of having to calculate them individually. I know repetition is a no-no, but I am not sure how else to do this.
The values of P are constants for different minerals (e.g., P_Cpx, P_Pl) and the D values are constants for different elements and the corresponding mineral (e.g., D_Nb_Cpx).
D_Th_bulk = (P_Cpx * D_Th_Cpx) + (P_Pl * D_Th_Pl) + (P_Opx * D_Th_Opx) + (P_Ol * D_Th_Ol) + (P_Mt * D_Th_Mt) + (P_Ilm * D_Th_Ilm) + (P_Ap * D_Th_Ap) + (P_Chr * D_Th_Chr) + (P_Maj_gn * D_Th_Maj_gn) + (P_Amp * D_Th_Amp)
D_Nb_bulk = (P_Cpx * D_Nb_Cpx) + (P_Pl * D_Nb_Pl) + (P_Opx * D_Nb_Opx) + (P_Ol * D_Nb_Ol) + (P_Mt * D_Nb_Mt) + (P_Ilm * D_Nb_Ilm) + (P_Ap * D_Nb_Ap) + (P_Chr * D_Nb_Chr) + (P_Maj_gn * D_Nb_Maj_gn) + (P_Amp * D_Nb_Amp)
D_La_bulk = (P_Cpx * D_La_Cpx) + (P_Pl * D_La_Pl) + (P_Opx * D_La_Opx) + (P_Ol * D_La_Ol) + (P_Mt * D_La_Mt) + (P_Ilm * D_La_Ilm) + (P_Ap * D_La_Ap) + (P_Chr * D_La_Chr) + (P_Maj_gn * D_La_Maj_gn) + (P_Amp * D_La_Amp)
D_Ce_bulk = (P_Cpx * D_Ce_Cpx) + (P_Pl * D_Ce_Pl) + (P_Opx * D_Ce_Opx) + (P_Ol * D_Ce_Ol) + (P_Mt * D_Ce_Mt) + (P_Ilm * D_Ce_Ilm) + (P_Ap * D_Ce_Ap) + (P_Chr * D_Ce_Chr) + (P_Maj_gn * D_Ce_Maj_gn) + (P_Amp * D_Ce_Amp)
Thank you in advance.

Related

Python - Create a new field in Pandas df with result of a scipy function

I have a pandas df that contains fields A, B, and C. I have a scipy minimization function that uses A, B, and C, and returns x. I want to add the value of x as new field for each row in the same df. I have tested the scipy function separately on single values of A, B, and C entered directly as values instead of as df['name'] and it works, returning the value of x. The code does not work when I try to operate on the entire df. Any ideas? Each of A, B, and C are Float64 in the df. Here is the error message:
Here is the code:
from scipy.optimize import minimize_scalar
import math
# set parameters
current_price_per_share = df['share_price']
shares_outstanding_in_millions = df['shares_(diluted)'] / 1000000
owners_income = df['owners_income'] / 1000000
def growth_needed(x, current_price_per_share, shares_outstanding_in_millions, owners_income):
# set growth rates and cost of capital
fcf_growth_1_to_5 = x / 100
fcf_growth_6_to_10 = fcf_growth_1_to_5 / 2 # half the rate of year 1-5
terminal_growth_rate = .03 # constant
cost_of_capital = .1 #constant
# calculate ten years of Free Cash Flow and the Terminal Value
FCF_01 = owners_income * (1 + fcf_growth_1_to_5)
FCF_02 = FCF_01 * (1 + fcf_growth_1_to_5)
FCF_03 = FCF_02 * (1 + fcf_growth_1_to_5)
FCF_04 = FCF_03 * (1 + fcf_growth_1_to_5)
FCF_05 = FCF_04 * (1 + fcf_growth_1_to_5)
FCF_06 = FCF_05 * (1 + fcf_growth_6_to_10)
FCF_07 = FCF_06 * (1 + fcf_growth_6_to_10)
FCF_08 = FCF_07 * (1 + fcf_growth_6_to_10)
FCF_09 = FCF_08 * (1 + fcf_growth_6_to_10)
FCF_10 = FCF_09 * (1 + fcf_growth_6_to_10)
term_value = (FCF_10 * (1 + terminal_growth_rate)) / (cost_of_capital - terminal_growth_rate)
# calcuate the Present Value for each period
PV_01 = FCF_01 * (1 /(( 1 + cost_of_capital) ** 1))
PV_02 = FCF_02 * (1 /(( 1 + cost_of_capital) ** 2))
PV_03 = FCF_03 * (1 /(( 1 + cost_of_capital) ** 3))
PV_04 = FCF_04 * (1 /(( 1 + cost_of_capital) ** 4))
PV_05 = FCF_05 * (1 /(( 1 + cost_of_capital) ** 5))
PV_06 = FCF_06 * (1 /(( 1 + cost_of_capital) ** 6))
PV_07 = FCF_07 * (1 /(( 1 + cost_of_capital) ** 7))
PV_08 = FCF_08 * (1 /(( 1 + cost_of_capital) ** 8))
PV_09 = FCF_09 * (1 /(( 1 + cost_of_capital) ** 9))
PV_10 = FCF_10 * (1 /(( 1 + cost_of_capital) ** 10))
PV_TV = term_value * (1 /(( 1 + cost_of_capital) ** 11))
#sum the Present Values and calculate the value per share
intrinsic_value = PV_01 + PV_02 + PV_03 + PV_04 + PV_05 + PV_06 + PV_07 + PV_08 + PV_09 + PV_10 + PV_TV
intrinsic_value_per_share = intrinsic_value / shares_outstanding_in_millions
# calculate the growth rate in year 1-5 needed to match the current share price
# the square and square root are to force to zero before adding back the growth rate
return ((math.sqrt((intrinsic_value_per_share - current_price_per_share) ** 2)) + x )
res = minimize_scalar(growth_needed, method='bounded', bounds=(-50, 100), args=(current_price_per_share, shares_outstanding_in_millions, owners_income,))
# notice the trailing comma in the Args function to make it a tuple
df['implied_growth'] = res.x / 100

Haskell minigame: check solvability of a maze (updated)

check :: String -> Int -> Int -> IO()
check map len n = do
let current = searchMap map '#'
if (flood map current Up len n)
then printf "\nThis map is solvable. \n"
else printf "\nThis map is unsolvable. \n"
flood :: String -> Int -> Direction -> Int -> Int -> Bool
flood map current direct len n = do
let upward = current - n
let downward = current + n
let leftward = current - 2
let rightward = current + 2
if (current < 0 || current > len || (map !! current) == '*' || (map !! current) == 'x')
then False
else if (map !! current) == 't'
then True
else do
case direct of
Up -> do
let newMap = replaceSign map current "x"
(flood map upward Up len n) || (flood map leftward Leftx len n) || (flood map rightward Rightx len n)
Down -> do
let newMap = replaceSign map current "x"
(flood map downward Down len n) || (flood map leftward Leftx len n) || (flood map rightward Rightx len n)
Leftx -> do
let row = findRow current n 0
if current < (row * n)
then False
else do
let newMap = replaceSign map current "x"
(flood map upward Up len n) || (flood map downward Down len n) || (flood map leftward Leftx len n)
Rightx -> do
let row = findRow current n 0
if current >= ((row + 1) * n)
then False
else do
let newMap = replaceSign map current "x"
(flood map upward Up len n) || (flood map downward Down len n) || (flood map rightward Rightx len n)
I am working on a project which needs to make a minigame. The player will need to input a map.
Something like this:
* * * * * - - - - - - - - - - - - - - - - - - - * * * * *
* * * * * b - - - - - - - - - - - - - - - - - b * * * * *
* * * * * - * * * * * * * * * * * * * * * * * - * * * * *
* * * * * - * * - - - - * * * * * - - - - * * - * * * * *
* * * * * - * * - y y - * * * * * - y y - * * - * * * * *
* * * * * - * * - - - - * * * * * - - - - * * - * * * * *
* * * * * - * * * * * * - - b - - * * * * * * - * * * * *
* * * * * - * * * * * * - * * * - * * * * * * - * * * * *
# - - - - - * * * * * * - * * * - * * * * * * p - - - - t
* * * * * - * * * * * * - * * * - * * * * * * - * * * * *
* * * * * - - - - - - - - * * * - - - - - - - - * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
I struggling in a function to determine whether the map is solvable or not.
In the code, current means the current location the program is trying. len is the length of the String holding the map. n is the length of one line of the map. In the map, # means the ball. - means roads that the ball can move on. b means bonus block (not related to this problem). * means obstacle. t means target p/o/y means blocks with special use (ball can stop there). My current approach is to use a function try to try recursively. The function will turn roads that have already tried into x and the function will return False if it hit x. When function hits obstacles (*) or boundaries of map it will try another two directions. Eg. if you are currently moving Up. Then it will try Leftx and Rightx. If it hits special blocks (p/o/y), it will try the other three directions. Eg. if you are currently moving Rightx, then it will try Up, Down, and Rightx. However, the program returns "Exception: stack overflow". Is it something wrong with my algorithm? How can I fix this?
The problem is that once your search has proceeded all the way right and up, so the map looks like this, with the current position the leftmost - in the first row:
* * * * * - - - - - - - - - - - - - - - - - - - * * * * *
* * * * * x - - - - - - - - - - - - - - - - - b * * * * *
* * * * * x * * * * * * * * * * * * * * * * * - * * * * *
* * * * * x * * - - - - * * * * * - - - - * * - * * * * *
* * * * * x * * - y y - * * * * * - y y - * * - * * * * *
* * * * * x * * - - - - * * * * * - - - - * * - * * * * *
* * * * * x * * * * * * - - b - - * * * * * * - * * * * *
* * * * * x * * * * * * - * * * - * * * * * * - * * * * *
x x x x x x * * * * * * - * * * - * * * * * * p - - - - t
* * * * * - * * * * * * - * * * - * * * * * * - * * * * *
* * * * * - - - - - - - - * * * - - - - - - - - * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
you enter the following loop with no further x marks made to the map:
try map 10 Up ... =
-- at top row, so we evaluate:
(try map current Leftx len n) || ...
try map 10 Leftx ... =
-- star to the left, so we evaluate:
(try map current Up len n) || ...
try map 10 Up ... =
-- at top row, so we evaluate:
(try map current Leftx len n) || ...
-- and we're stuck in an infinite loop
The stack eventually overflows because Haskell is keeping track of all those right-hand sides of the || operator, though it never gets a chance to try any of them.
I think you'll need to either modify your algorithm to mark walls you've already tried, or maybe switch from working with a single path to flood filling all the non-stars -- if you hit the target while flood fillling, the maze is solvable.

Stuck on a program in Python

I am doing an assignment for school and I just can't get past the final hurdle. I've only been doing coding for about 2 weeks and this is beyond me on how to fix. I am hoping someone here can help me in any way possible.
We need to make an Eiffel Tower with Python that has an adjustable size, and we cannot use any statements we haven't learned. The only ones I know at the moment are the math operators, if, print, newlines, and basic formatting.
Here are the basics of my assignment and how I've gone about it: https://imgur.com/a/e0Aas1z
here is my code in python:
size = int(input('Enter Eiffel Tower Size:\n'))
print(' ' * ((size * 2) + (size - 1)) + '$')
print(((' ' * (size * 3 - 2)) + '|Z|' + '\n') * int(size * 1.5), end='')
print(' ' * ((size * 3) - (size + 2)) + '/' + 'Z' * (size * 2 + 1) + '\\')
print(' ' * ((size * 3) - (size + 2)) + 'H' + ' ' * (size * 2 + 1) + 'H')
print(' ' * ((size * 3) - (size + 2)) + 'H' + ' ' * (size * 2 + 1) + 'H')
print(' ' * (size - 2) + '/' + '%' * (size * 4 +1) + '\\')
print((' ' * (size - 3) + '##' + ' ' * (size * 4 + 1) + '##' + '\n') * int(size // 1.5), end='')
print((' ' * (size - 4) + '##' + ' ' * (size * 4 + 1) + ' ##' + '\n') * int(size // 1.5))
Any number 4 or less causes my last line to become broken, and the program to print incorrectly. I'm at my wits end trying to figure this out and I don't know what else to try.
You just need to get the tower off the edge of the console. Add more space on the left for each print statement:
size = 2
print(' ' * (size + (size * 2) + (size - 1)) + '$')
print(((' ' * (size + size * 3 - 2)) + '|Z|' + '\n') * int(size * 1.5), end='')
print(' ' * (size + (size * 3) - (size + 2)) + '/' + 'Z' * (size * 2 + 1) + '\\')
print(' ' * (size + (size * 3) - (size + 2)) + 'H' + ' ' * (size * 2 + 1) + 'H')
print(' ' * (size + (size * 3) - (size + 2)) + 'H' + ' ' * (size * 2 + 1) + 'H')
print(' ' * (size + size - 2) + '/' + '%' * (size * 4 +1) + '\\')
print((' ' * (size + size - 3) + '##' + ' ' * (size * 4 + 1) + '##' + '\n') * int(size // 1.5), end='')
print((' ' * (size + size - 4) + '##' + ' ' * (size * 4 + 1) + ' ##' + '\n') * int(size // 1.5))
Output
$
|Z|
|Z|
|Z|
/ZZZZZ\
H H
H H
/%%%%%%%%%\
## ##
## ##

Creating a large letter 'A' in python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I started printing a pyramid to start...
Here's what I made so far:`
num = int(input("Enter the Number: "))
for i in range(1, num+1):
for j in range(0, i):
print(" ", end="")
for j in range(1, (num*2 - (2*i - 1))+1):
if i == 1 or j == 1 or j ==(num*2 -(2*i-1)):
print("*", end="")
else:
print(" ", end="")
print()
This is what the output should look like...
*
* *
* *
* *
*********
* *
* *
* *
Here a simple solution.
def large_a(height):
rows = ["*"] + ["*" + " " * (2 * i + 1) + "*" for i in range(height - 1)]
middle = len(rows) // 2
rows[middle] = rows[middle].replace(" ", "*")
return "\n".join(f"{r:^{height * 2}}" for r in rows)
print(large_a(8))
*
* *
* *
* *
*********
* *
* *
* *
print(large_a(15))
*
* *
* *
* *
* *
* *
* *
***************
* *
* *
* *
* *
* *
* *
* *

how to print FlippedTriangle pattern using python

Here is my code but it is not working as expected
def printFlippedTriangle(width):
for i in range(0, width):
for J in range(0, width-i):
print(" ", end=" ") # single line
for j in range(0,i):
print(" "+"* ", end=" ") # single line
j=j-1
print("*")
Am getting this:
*
* * * * *
* * * * * * *
* * * * * * *
* * * * *
am suppose to get:
*
* *
* * *
* * * *
* * * * *
Any idea and or suggestion will be appreciated
This will get the job done, and in a single loop too!
def triangle(w):
for i in range(0, w):
print(' ' * ((w - i - 1) * 2), end='') # spaces for each row
print('* ' * (i + 1), end='') # * for each row
print() # new line
>>> triangle(5)
*
* *
* * *
* * * *
* * * * *
Each row needs width - rowNumber - 1 spaces and rowNumber + 1 asterisks when starting from 0

Resources