Working on a piece of code; The only way I can get it to work at the moment is by using global variables. I'd like to do it by using return, but when I do so it gives me the whole jumble of the function when I call the variable using "n = genTable()" for example.
def genTable():
global n
n = Min #n starts as min
print ("%-6s %-6s %-6s %s" %("n", "Seq", "Bin", "Perf")) #Header
print "--------------------------------" #Header
while n <= Max:
Seq = seqCalc() #Calls calculation for sequential
Bin = binCalc() #Calls calculation for binary
if n > 0:
Perf = round(Seq / Bin) #Calculate Performance
else:
Perf = 0
print ("%-6s %-6s %-6s %s" %(n, Seq, int(Bin), int(Perf))) #Prints results
n = n + Int
def seqCalc():
Seq = n / 2 #Sequential Calculation
return Seq
I have omitted other variables from other functions; Just concerned with n right now. (Ignore Bin)
Is there a way to call return without getting the whole mess of the function repeated by "seqCalc"?
Related
So I'm trying to make 2 lists with random variables compare to each other to find the probability of them being the same. What I've done is made 2 lists with random numbers using a for loop, but in order to find the probability I'm trying to create the lists within another for loop in order to make 10000 pairs of lists to compare but I can't get it to work.
import random
import collections
N= 10000
count = 0
playerPick=[]
randomPick=[]
for j in range (N):
for i in range(4):
playerPick.append(random.randrange(1,21))
print(playerPick)
for i in range(4):
randomPick.append(random.randrange(1,21))
print(randomPick)
if collections.Counter(playerPick) == collections.Counter(randomPick):
count+=1
probability = count/N
print("Probability of winning: ", probability)
The lists end up being super long but I just want them to be 4 long.
This may be a more efficient way to calculate the average match count.
import random
import collections
N = 10000 # main list length
L = 4 # each element is list of 4 elements
def getpct():
# create random list of lists
playerPick=[[random.randrange(1,21) for x in range(L)] for n in range(N)]
randomPick=[[random.randrange(1,21) for x in range(L)] for n in range(N)]
# create match list, 1=match else 0
matches = [1 if p==r else 0 for p,r in zip(playerPick,randomPick)]
return sum(matches)/N # percent matches
allpcts = [getpct() for r in range(1000)] # run test 1000 times
avgpct = sum(allpcts)/1000 # average percent
print(f'Avg Pct: {avgpct}%')
Output
Avg Pct: 6.2000000000000025e-06%
When I try to create a counter and increment it in an if-else statement the thinkscript compiler throws confusing errors which tell me it's not allowed, yet I've seen this done in several examples. They even have a reserved word: rec in order to allow for incrementing counters.
score = score + 1; produces: # Already assigned: Score at...
rec score = score + 1; produces: # identifier already used: score at ...
# not allowed inside an IF/THEN/ELSE statement
#
# TD Ameritrade IP Company, Inc. (c) 2017-2019
#
input price = close;
input length = 9;
input displace = 0;
def score = 0;
def smavrgg = Average(price[-displace], length);
def expMvAvrg = ExpAverage(price[-displace], length);
plot SMA = smavrgg;
SMA.SetDefaultColor(GetColor(1));
plot AvgExp = expMvAvrg;
AvgExp.SetDefaultColor(GetColor(1));
# 1 if uptrend, 0 if downtrend
def lastTrendisUp = (close[0] - close[1]) > 0 ;
def secondLastTrendisUP = (close[1] - close[2]) > 0;
def thirdLastTrendisUP = (close[2] - close[3]) > 0;
def fourthLastTrendisUP = (close[3] - close[4]) > 0;
input lookback = 5;
# defines intBool (array) that indicates whether one or the other crossed.
def bull_cross = SMA crosses above AvgExp;
def bear_cross = AvgExp crosses below SMA;
# returns the highest value in the data array for the lookback.
# so [0, 1, 0, 0] means a cross happened within the last units. and 1 will be returned.
if (bull_cross[0] or bear_cross[0]) then {
if lastTrendisUp {
# Already assigned: Score at...
score = score + 1;
# identifier already used: score at ...
# not allowed inside an IF/THEN/ELSE statement
rec score = score + 1;
} else {
}
} else if (bull_cross[1] or bear_cross[1]) {
if secondLastTrendisUP {
} else {
}
} else if (bull_cross[2] or bear_cross[2]) {
if thirdLastTrendisUP {
} else {
}
} else if (bull_cross[3] or bear_cross[3]) {
if fourthLastTrendisUP {
} else {
}
} else if (bull_cross[4] or bear_cross[4]) {
} else {
}
# If most recent cross happened in the last 4
# and most recent cross occured on a green candle.
def bull_lookback = Highest(bull_cross, lookback);
def bear_lookback = Highest(bear_cross, lookback);
# def think = if bull_lookback or bear_lookback
plot signal = if bull_lookback then 2 else if bear_lookback then 1 else 0;
signal.AssignValueColor(if signal == 2 then Color.DARK_GREEN else if signal == 1 then Color.DARK_RED else Color.DARK_ORANGE);
AssignBackgroundColor(if signal == 2 then Color.DARK_GREEN else if signal == 1 then Color.DARK_RED else Color.DARK_ORANGE);
Once you define a variable in Thinkscript and assign it, it's only valid for one bar, it behaves as a constant so it can't be reassigned. I'm pretty sure you can't even place a Def command into a conditional, just like in most codes. In order to create a 'dynamic' SCORE, you need to assign the dynamic value in the same line you instantiate. You don't need
def score = 0;
since when you define the variable, it will have a zero value anyway.
You also don't the extra variables for the 'trendisup' placeholders, because really
secondLastTrendisUp
is the same as saying
lastTrendisUp[1]
because it was already computed in the last bar.
You can accomplish the counter without the extra variables using a FOLD statement, like this:
def score= fold index=0 to 4
with p=0
do p + ((bearcross[index] or bullcross[index]) and lastTrendisUp[index]);
This will add one to the score each time the conditions are true, and assign the total to the SCORE variable. I think this is what you would like to accomplish, I can't tell, since you never show what you're doing with the score variable later on... If you are looking to just find out if bullcross or bearcross condition and also the lasttrendisup condition evaluates to true in any of the last five bars, then you add 'while p=0' above the with, and it will return a one to SCORE as soon as it encounters the first true instance.
counter to increase a variable by 1, on each bar:
score = score[1] + 1;
the [1] means, go get the value from that variable from 1 bar ago.
The answer is that variables in thinkscript cannot be changed.
I have a beginner question. Loops are extremely hard for me to understand, so it's come to me asking for help.
I am trying to create a function to count the amount of even numbers in a user input list, with a negative at the end to show the end of the list. I know I need to use a while loop, but I am having trouble figuring out how to walk through the indexes of the input list. This is what I have so far, can anyone give me a hand?
def find_even_count(numlist):
count = 0
numlist.split()
while numlist > 0:
if numlist % 2 == 0:
count += 1
return count
numlist = raw_input("Please enter a list of numbers, with a negative at the end: ")
print find_even_count(numlist)
I used the split to separate out the indexes of the list, but I know I am doing something wrong. Can anyone point out what I am doing wrong, or point me to a good step by step explanation of what to do here?
Thank you guys so much, I know you probably have something more on your skill level to do, but appreciate the help!
You were pretty close, just a couple of corrections:
def find_even_count(numlist):
count = 0
lst = numlist.split()
for num in lst:
if int(num) % 2 == 0:
count += 1
return count
numlist = raw_input("Please enter a list of numbers, with a negative at the end: ")
print find_even_count(numlist)
I have used a for loop rather than a while loop, stored the outcome of numlist.split() to a variable (lst) and then just iterated over this.
You have a couple of problems:
You split numlist, but don't assign the resulting list to anything.
You then try to operate on numlist, which is still the string of all numbers.
You never try to convert anything to a number.
Instead, try:
def find_even_count(numlist):
count = 0
for numstr in numlist.split(): # iterate over the list
num = int(numstr) # convert each item to an integer
if num < 0:
break # stop when we hit a negative
elif num % 2 == 0:
count += 1 # increment count for even numbers
return count # return the total
Or, doing the whole thing in one line:
def find_even_count(numlist):
return sum(num % 2 for num in map(int, numlist.split()) if num > 0)
(Note: the one-liner will fail in cases where the user tries to trick you by putting more numbers after the "final" negative number, e.g. with numlist = "1 2 -1 3 4")
If you must use a while loop (which isn't really the best tool for the job), it would look like:
def find_even_count(numlist):
index = count = 0
numlist = list(map(int, numlist.split()))
while numlist[index] > 0:
if numlist[index] % 2 == 0:
count += 1
index += 1
return count
I'm working through the book NLP with Python, and I came across this example from an 'advanced' section. I'd appreciate help understanding how it works. The function computes all possibilities of a number of syllables to reach a 'meter' length n. Short syllables "S" take up one unit of length, while long syllables "L" take up two units of length. So, for a meter length of 4, the return statement looks like this:
['SSSS', 'SSL', 'SLS', 'LSS', 'LL']
The function:
def virahanka1(n):
if n == 0:
return [""]
elif n == 1:
return ["S"]
else:
s = ["S" + prosody for prosody in virahanka1(n-1)]
l = ["L" + prosody for prosody in virahanka1(n-2)]
return s + l
The part I don't understand is how the 'SSL', 'SLS', and 'LSS' matches are made, if s and l are separate lists. Also in the line "for prosody in virahanka1(n-1)," what is prosody? Is it what the function is returning each time? I'm trying to think through it step by step but I'm not getting anywhere. Thanks in advance for your help!
Adrian
Let's just build the function from scratch. That's a good way to understand it thoroughly.
Suppose then that we want a recursive function to enumerate every combination of Ls and Ss to make a given meter length n. Let's just consider some simple cases:
n = 0: Only way to do this is with an empty string.
n = 1: Only way to do this is with a single S.
n = 2: You can do it with a single L, or two Ss.
n = 3: LS, SL, SSS.
Now, think about how you might build the answer for n = 4 given the above data. Well, the answer would either involve adding an S to a meter length of 3, or adding an L to a meter length of 2. So, the answer in this case would be LL, LSS from n = 2 and SLS, SSL, SSSS from n = 3. You can check that this is all possible combinations. We can also see that n = 2 and n = 3 can be obtained from n = 0,1 and n=1,2 similarly, so we don't need to special-case them.
Generally, then, for n ≥ 2, you can derive the strings for length n by looking at strings of length n-1 and length n-2.
Then, the answer is obvious:
if n = 0, return just an empty string
if n = 1, return a single S
otherwise, return the result of adding an S to all strings of meter length n-1, combined with the result of adding an L to all strings of meter length n-2.
By the way, the function as written is a bit inefficient because it recalculates a lot of values. That would make it very slow if you asked for e.g. n = 30. You can make it faster very easily by using the new lru_cache from Python 3.3:
#lru_cache(maxsize=None)
def virahanka1(n):
...
This caches results for each n, making it much faster.
I tried to melt my brain. I added print statements to explain to me what was happening. I think the most confusing part about recursive calls is that it seems to go into the call forward but come out backwards, as you may see with the prints when you run the following code;
def virahanka1(n):
if n == 4:
print 'Lets Begin for ', n
else:
print 'recursive call for ', n, '\n'
if n == 0:
print 'n = 0 so adding "" to below'
return [""]
elif n == 1:
print 'n = 1 so returning S for below'
return ["S"]
else:
print 'next recursivly call ' + str(n) + '-1 for S'
s = ["S" + prosody for prosody in virahanka1(n-1)]
print '"S" + each string in s equals', s
if n == 4:
print '**Above is the result for s**'
print 'n =',n,'\n', 'next recursivly call ' + str(n) + '-2 for L'
l = ["L" + prosody for prosody in virahanka1(n-2)]
print '\t','what was returned + each string in l now equals', l
if n == 4:
print '**Above is the result for l**','\n','**Below is the end result of s + l**'
print 'returning s + l',s+l,'for below', '\n','='*70
return s + l
virahanka1(4)
Still confusing for me, but with this and Jocke's elegant explanation, I think I can understand what is going on.
How about you?
Below is what the code above produces;
Lets Begin for 4
next recursivly call 4-1 for S
recursive call for 3
next recursivly call 3-1 for S
recursive call for 2
next recursivly call 2-1 for S
recursive call for 1
n = 1 so returning S for below
"S" + each string in s equals ['SS']
n = 2
next recursivly call 2-2 for L
recursive call for 0
n = 0 so adding "" to below
what was returned + each string in l now equals ['L']
returning s + l ['SS', 'L'] for below
======================================================================
"S" + each string in s equals ['SSS', 'SL']
n = 3
next recursivly call 3-2 for L
recursive call for 1
n = 1 so returning S for below
what was returned + each string in l now equals ['LS']
returning s + l ['SSS', 'SL', 'LS'] for below
======================================================================
"S" + each string in s equals ['SSSS', 'SSL', 'SLS']
**Above is the result for s**
n = 4
next recursivly call 4-2 for L
recursive call for 2
next recursivly call 2-1 for S
recursive call for 1
n = 1 so returning S for below
"S" + each string in s equals ['SS']
n = 2
next recursivly call 2-2 for L
recursive call for 0
n = 0 so adding "" to below
what was returned + each string in l now equals ['L']
returning s + l ['SS', 'L'] for below
======================================================================
what was returned + each string in l now equals ['LSS', 'LL']
**Above is the result for l**
**Below is the end result of s + l**
returning s + l ['SSSS', 'SSL', 'SLS', 'LSS', 'LL'] for below
======================================================================
This function says that:
virakhanka1(n) is the same as [""] when n is zero, ["S"] when n is 1, and s + l otherwise.
Where s is the same as the result of "S" prepended to each elements in the resulting list of virahanka1(n - 1), and l the same as "L" prepended to the elements of virahanka1(n - 2).
So the computation would be:
When n is 0:
[""]
When n is 1:
["S"]
When n is 2:
s = ["S" + "S"]
l = ["L" + ""]
s + l = ["SS", "L"]
When n is 3:
s = ["S" + "SS", "S" + "L"]
l = ["L" + "S"]
s + l = ["SSS", "SL", "LS"]
When n is 4:
s = ["S" + "SSS", "S" + "SL", "S" + "LS"]
l = ["L" + "SS", "L" + "L"]
s + l = ['SSSS", "SSL", "SLS", "LSS", "LL"]
And there you have it, step by step.
You need to know the results of the other function calls in order to calculate the final value, which can be pretty messy to do manually as you can see. It is important though that you do not try to think recursively in your head. This would cause your mind to melt. I described the function in words, so that you can see that these kind of functions is are descriptions, and not a sequence of commands.
The prosody you see, that is a part of s and l definitions, are variables. They are used in a list-comprehension, which is a way of building lists. I've described earlier how this list is built.
A_quantity = 10
B_quantity = 20
the quantity amount
N = float (input('please enter the quantity of package: '))
X_total = float (N*99.00)
the fee from input
Q_discount = (0.2*X_total)
W_discount = (X_total*0.3)
discounts from input total
Y_total = (X_total-Q_discount)
M_total = (X_total-W_discount)
the fee with the discount
def main ():
if N >= A_quantity:
print ('the total cost is $', \
format (Y_total, ',.2f'))
else:
if N >= B_ quantity:
print ('the total cost is $', \
format (M_total, ',.2f'))
main ()
the results should be 10 packages for $792.00
and 20 packages for $1,380.00
yet the second statement gets the 20% discount also which total to $1549.00, when it should get only 30% discount
I don't know which language it is, but it's an algorithm problem : you should first try for the highest value cause the way it is designed now, if N = 30 , you will always enter the "if" , never the "else" , and if N=5 , you will enter the "else" , but the the if inside it...
let me try although I don't know the language:
def main ():
if N >= B_quantity:
print ('the total cost is $', \
format (M_total, ',.2f'))
else:
if N >= A_quantity:
print ('the total cost is $', \
format (Y_total, ',.2f'))
main ()
take the value of the product divided by 100 and multiplied by the discount
and then get this result and the value of the product subitrair
var SomaPercent = ValorUnit/100 * descont;
var result_fim = ValorUnit-SomaPercent;
you can change the if condition to
if N >= A_quantity && N < B_quantity ...
if N >= B_quantity
..