I have two codes which should perform the same thing but in the first, I am not getting the result but in the second one I am getting output
if (Method == "EMM" ):
if ((Loan_Obligation/12)+EMI) !=0:
DSCR_Post = EBITDA_EMM/((Loan_Obligation/12)+EMI)
else:
0
elif (Method != "EMM" ):
if ((Loan_Obligation/12)+EMI) !=0:
DSCR_Post = EBITDA/((Loan_Obligation/12)+EMI)
else:
0
and other one is:
if (Method == "EMM"):
DSCR_Post = EBITDA_EMM/((Loan_Obligation/12)+EMI) if ((Loan_Obligation/12)+EMI) !=0 else 0
else:
DSCR_Post = EBITDA/((Loan_Obligation/12)+EMI) if ((Loan_Obligation/12)+EMI) !=0 else 0
print('DSCR_Post:',DSCR_Post)
Can someone help me what is the difference between the two codes
In your first code snippet, you are not assigning the 0 to DSCR_Post as you do in the second. Modify as follows:
if Method == "EMM" :
if (Loan_Obligation / 12) + EMI !=0:
DSCR_Post = EBITDA_EMM / ((Loan_Obligation / 12) + EMI)
else:
DSCR_Post = 0 # the 0 has to be assigned!
else: # you do not need a condition here! It can either be equal or not, no third state possible.
if (Loan_Obligation / 12) + EMI !=0:
DSCR_Post = EBITDA / ((Loan_Obligation / 12) + EMI)
else:
DSCR_Post = 0
print('DSCR_Post:',DSCR_Post)
Which can be simplified to the following:
ebid = EBITDA_EMM if Method == "EMM" else EBITDA
DSCR_Post = 0 # 0 will be overwritten if ...
if (Loan_Obligation / 12) + EMI != 0:
DSCR_Post = ebid / ((Loan_Obligation / 12) + EMI)
print('DSCR_Post:',DSCR_Post)
Related
I have written this code, but the problem is for large number for P and SLOT, the code will be stocked in a loop and unfortunately, I do not know how to fix this problem. I would be more than thankful if someone can help me to fix this problem.
I have attached the code below for your further review.
This code is for developing a patient scheduling system based on the patient no-show.
import pandas as pd
import numpy as np
import random
import datetime
SLOT = 12 # Number of slots per day
PHY = 2 # Number of Physicians
P = 36 # Number of Patients
number_OB = np.zeros((PHY, 1)) # Number of overbooked for each physician
Limit_OB = np.zeros((PHY, 1))
for i in range(Limit_OB.shape[0]):
Limit_OB[i] = 2 # Capacity of each physician is to visit maximum 2 patients in each slot
preference = [random.randint(1, 3) for i in range(P)] # patients' preference for seeing physician 1 and 2: Preference 3 means the patient has no preference for physician 1 or 2
noshowtype = [random.randint(0, 1) for i in range(P)] # 0 means the patient no-show is low and 1 means the patient no-show is high
availability = np.random.randint(2, size=(P,SLOT)) # This matrix shows the availability of each patients for each slot
# Initialization
print(preference)
print(len(preference))
print(noshowtype)
print(len(noshowtype))
print(availability)
availability.shape
totpat = np.zeros((PHY, SLOT))
tot_pat_sch = np.zeros((PHY,1))
assign = np.zeros(shape=(P, SLOT, PHY))
totpatslot = np.zeros((SLOT, 1))
noapp = np.zeros((P, 1))
put = np.zeros((P, SLOT))
unschedule = np.zeros((P,SLOT))
slotcap = [2,1,1,1,2,1,1,1,2,1,1,1]
totpatslot = [0,0,0,0,0,0,0,0,0,0,0,0]
tot_pat_sch = [0,0]
for p in range(P):
count = 0
if preference[p] == 3:
for phy in range(PHY):
for s in range(SLOT):
tot_pat_sch[phy] = tot_pat_sch[phy] + totpat[PHY-1,s]
if tot_pat_sch[0] == tot_pat_sch[1]:
preference[p] = random.randint(1, 2)
trial = 0
if p == 4 :
print("patient assigned ",p)
while trial <=1:
if noshowtype[p] == 0 & count ==0:
for s in range(SLOT):
if totpat[preference[p]-1, s] == 0:
if availability[p,s] == 1:
assign[p,s,preference[p]-1] = 1
put[p,s] = 1
count = 1
trial = 2
totpat[preference[p]-1,s] = totpat[preference[p]-1,s] + 1
totpatslot[s] = totpatslot[s] + 1
break;
print("patient scheduled ",p)
if count == 0:
for s in range(SLOT):
if totpat[preference[p]-1, s] < slotcap[s]:
if availability[p,s] == 1:
assign[p,s,preference[p]-1] = 1
number_OB[PHY-1] = number_OB[PHY-1] + 1
put[p,s] = 1
count = 1
trial = 2
totpat[preference[p]-1,s] = totpat[preference[p]-1,s] + 1
totpatslot[s] = totpatslot[s] + 1
else:
unschedule[p,s] = unschedule[p,s] + 1
noapp[p] = 1
break;
if noshowtype[p] == 1 & count == 0:
for s in range(SLOT):
if totpat[preference[p]-1, s] == 0:
if availability[p,s] == 1:
assign[p,s,preference[p]-1] = 1
put[p,s] = 1
count = 1
trial = 2
totpat[preference[p]-1,s] = totpat[preference[p]-1,s] + 1
totpatslot[s] = totpatslot[s] + 1
break;
print("patient scheduled ",p)
if count == 0:
for s in range(SLOT):
if totpat[preference[p]-1, s] < slotcap[s]:
if availability[p,s] == 1:
assign[p,s,preference[p]-1] = 1
number_OB[PHY-1] = number_OB[PHY-1] + 1
put[p,s] = 1
count = 1
trial = 2
totpat[preference[p]-1,s] = totpat[preference[p]-1,s] + 1
totpatslot[s] = totpatslot[s] + 1
else:
unschedule[p,s] = unschedule[p,s] + 1
noapp[p] = 1
break;
I'll put it as an answer so that you can understand what I was commenting about.
for p in range(P):
...
trial = 0
while trial <=1:
if ():
for ():
if ():
if ():
...
trial = 2
break;
if ():
for ():
if ():
if ():
...
trial = 2
else:
...
break;
#similarly the other ifs
this is the structure of the code right now, which does not seem simple and is probably redundant at many points. the issue that i was asking you to check was what happens when none of the first indentation if conditions are met? the variable trial will not get updated and you will be stuck in an infinite loop inside while. the only way to break out of this while is too make the variable trial>1 or use a break.
anyways i do not see the point of the while loop here.
data = input("Input:")
def parse_to_int(dat): # parse input data to int list
individuals = dat.split(" ")
num = []
for individual in individuals:
num.append((int)(individual))
return num
def get_Max_and_MaxIndex_MinIndex(num_list): # get maximul value, its index and minimum value
sign = 1
val = 0
max_val = 0
min_val = 0
max_ind = 0
count = 0
for num in num_list:
count += 1
val = val + sign * num
sign = -1 * sign
if (max_val < val):
max_val = val
max_ind = count
if (min_val > val):
min_val = val
return max_val, max_ind, min_val
def sub_sum(ind): # calculate subsum until ind
global num_input
global subsum
subsum = 0
for i in range(ind + 1):
subsum += num_input[i]
return subsum
def sub_sum_with_sign(ind): # calculate subsum with sign until ind
global num_input
global subsum_withsign
subsum_withsign = 0
sign = 1
for i in range(ind + 1):
subsum_withsign += sign * num_input[i]
sign = -1 * sign
return subsum_withsign
def fill_data(ind): # fill data for print in empty data
global print_data
global cur_x
global cur_y
if ind % 2 == 0:
str = '/'
dx = 1
dy = 1
else:
str = '\\'
dx = 1
dy = -1
for _ in range(num_input[ind]):
print_data[cur_y][cur_x] = str
cur_x += dx
cur_y += dy
if ind % 2 == 0:
cur_y -= 1
else:
cur_y += 1
return print_data
# main program
num_input = parse_to_int(data)
col_num = 0 # column count
for elem in num_input:
col_num += elem
max_min_num = get_Max_and_MaxIndex_MinIndex(num_input)
row_num = max_min_num[0] - max_min_num[2] # row count
start_num = 0
for i in range(max_min_num[1]):
start_num += num_input[i] # maximum height element number
subsum = 0
subsum_withsign = 0
cur_x = 0
cur_y = -1 * max_min_num[2]
# start output
print("Output:\n")
print(" " * start_num + "o" + " " * (col_num - start_num))
print(" " * (start_num - 1) + "/|\\" + " " * (col_num - start_num - 1))
print(" " * (start_num - 1) + "< >" + " " * (col_num - start_num - 1))
# prepare data for print
print_data = []
for i in range(row_num):
print_data.append([])
for j in range(col_num):
print_data[i].append(" ")
for ind in range(len(num_input)):
fill_data(ind)
# print data
for indr in range(row_num - 1, -1, -1):
print("".join(print_data[indr]))
The above code will give this Output
In this code, a graph is generated using python without using any libraries. The Highest tip of the graph is marked.
I want to mark the lowest tip of the graph as shown in the image below. I completely have no idea about this one. which values should be changed in order to mark the lowest tip in the generated graph?
I am a newbie to stackoverflow, please comment if you need more clarity with this..
How to mark the lowest tip here Please Help me with this
With an = (an-2 + 1)×an-1 with a0 = 0 and a1 = 1 formula find dummy_numbers(max)
My code:
def dummy_numbers(nums):
binsize = (((nums - 2) +1) * (nums -1))
return map(lambda x: int(nums + binsize * x), range(nums))
for num in dummy_numbers(10):
print(num)
my code prints different result than I expected
Use an actual generator with yield to make this easier. The tricky part here is keeping track of an-1 and an-2 as you iterate. This can be achieve like so:
second_last, last = None, None
for current in range(10):
second_last, last = last, current
assert (second_last, last) == (8, 9)
You also need to hardcode in the constant value that get returned for 0 and 1:
def dummy_numbers(an):
if an == 0:
yield 0
elif an == 1:
yield 0
yield 1
else:
an_2, an_1 = None, None
for an_0 in dummy_numbers(an - 1):
an_2, an_1 = an_1, an_0
yield an_0
yield (an_2 + 1) * an_1
for num in dummy_numbers(10):
print(num)
Outputs:
0
1
1
2
4
12
60
780
47580
37159980
1768109008380
You could also make this non-recursive like so:
def dummy_numbers(an):
an_2, an_1 = None, None
for i in range(an):
if i == 0:
an_0 = 0
elif i == 1:
an_0 = 1
else:
an_0 = (an_2 + 1) * an_1
yield an_0
an_2, an_1 = an_1, an_0
I am asked to binary search a list of names and if these names start with a particular letter, for example A, then I am to print that name.
I can complete this task by doing much more simple code such as
for i in list:
if i[0] == "A":
print(i)
but instead I am asked to use a binary search and I'm struggling to understand the process behind it. We are given base code which can output the position a given string. My problem is not knowing what to edit so that I can achieve the desired outcome
name_list = ["Adolphus of Helborne", "Aldric Foxe", "Amanita Maleficant", "Aphra the Vicious", "Arachne the Gruesome", "Astarte Hellebore", "Brutus the Gruesome", "Cain of Avernus"]
def bin_search(list, item):
low_b = 0
up_b = len(list) - 1
found = False
while low_b <= up_b and found == False:
midPos = ((low_b + up_b) // 2)
if list[midPos] < item:
low_b = midPos + 1
elif list[midPos] > item:
up_b = midPos - 1
else:
found = True
if found:
print("The name is at positon " + str(midPos))
return midPos
else:
print("The name was not in the list.")
Desired outcome
bin_search(name_list,"A")
Prints all the names starting with A (Adolphus of HelBorne, Aldric Foxe .... etc)
EDIT:
I was just doing some guess and check and found out how to do it. This is the solution code
def bin_search(list, item):
low_b = 0
up_b = len(list) - 1
true_list = []
count = 100
while low_b <= up_b and count > 0:
midPos = ((low_b + up_b) // 2)
if list[midPos][0] == item:
true_list.append(list[midPos])
list.remove(list[midPos])
count -= 1
elif list[midPos] < item:
low_b = midPos + 1
count -= 1
else:
up_b = midPos - 1
count -= 1
print(true_list)
Not too sure if this is what you want as it seems inefficient... as you mention it seems a lot more intuitive to just iterate over the entire list but using binary search i found here i have:
def binary_search(seq, t):
min = 0
max = len(seq) - 1
while True:
if max < min:
return -1
m = (min + max) // 2
if seq[m][0] < t:
min = m + 1
elif seq[m][0] > t:
max = m - 1
else:
return m
index=0
while True:
index=binary_search(name_list,"A")
if index!=-1:
print(name_list[index])
else:
break
del name_list[index]
Output i get:
Aphra the Vicious
Arachne the Gruesome
Amanita Maleficant
Astarte Hellebore
Aldric Foxe
Adolphus of Helborne
You just need to found one item starting with the letter, then you need to identify the range. This approach should be fast and memory efficient.
def binary_search(list,item):
low_b = 0
up_b = len(list) - 1
found = False
midPos = ((low_b + up_b) // 2)
if list[low_b][0]==item:
midPos=low_b
found=True
elif list[up_b][0]==item:
midPos = up_b
found=True
while True:
if found:
break;
if list[low_b][0]>item:
break
if list[up_b][0]<item:
break
if up_b<low_b:
break;
midPos = ((low_b + up_b) // 2)
if list[midPos][0] < item:
low_b = midPos + 1
elif list[midPos] > item:
up_b = midPos - 1
else:
found = True
break
if found:
while True:
if midPos>0:
if list[midPos][0]==item:
midPos=midPos-1
continue
break;
while True:
if midPos<len(list):
if list[midPos][0]==item:
print list[midPos]
midPos=midPos+1
continue
break
else:
print("The name was not in the list.")
the output is
>>> binary_search(name_list,"A")
Adolphus of Helborne
Aldric Foxe
Amanita Maleficant
Aphra the Vicious
Arachne the Gruesome
Astarte Hellebore
I want to make a binary calculator and I have a problem with the subtraction part. Here is my code (I have tried to adapt one for sum that I've found on this website).
maxlen = max(len(s1), len(s2))
s1 = s1.zfill(maxlen)
s2 = s2.zfill(maxlen)
result = ''
carry = 0
i = maxlen - 1
while(i >= 0):
s = int(s1[i]) - int(s2[i])
if s <= 0:
if carry == 0 and s != 0:
carry = 1
result = result + "1"
else:
result = result + "0"
else:
if carry == 1:
result = result + "0"
carry = 0
else:
result = result + "1"
i = i - 1
if carry>0:
result = result + "1"
return result[::-1]
The program works fine with some binaries subtraction but it fails with others.
Can someone please help me because I can't find the mistake? Thanks a lot.
Short answer: Your code is wrong for the case when s1[i] == s2[i] and carry == 1.
Longer answer: You should restructure your code to have three separate cases for s==-1, s==0, and s==1, and then branch on the value of carry within each case:
if s == -1: # 0-1
if carry == 0:
...
else:
...
elif s == 0: # 1-1 or 0-0
if carry == 0:
...
else:
...
else: # 1-0
if carry == 0:
...
else:
...
This way you have a separate block for each possibility, so there is no chance of overlooking a case like you did on your first attempt.
I hope the answer below it helps.
def binarySubstration(str1,str2):
if len(str1) == 0:
return
if len(str2) == 0:
return
str1,str2 = normaliseString(str1,str2)
startIdx = 0
endIdx = len(str1) - 1
carry = [0] * len(str1)
result = ''
while endIdx >= startIdx:
x = int(str1[endIdx])
y = int(str2[endIdx])
sub = (carry[endIdx] + x) - y
if sub == -1:
result += '1'
carry[endIdx-1] = -1
elif sub == 1:
result += '1'
elif sub == 0:
result += '0'
else:
raise Exception('Error')
endIdx -= 1
return result[::-1]
normalising the strings
def normaliseString(str1,str2):
diff = abs((len(str1) - len(str2)))
if diff != 0:
if len(str1) < len(str2):
str1 = ('0' * diff) + str1
else:
str2 = ('0' * diff) + str2
return [str1,str2]