Pine Script 5 - Unexpected Boolean outcome - pine-script-v5

I have a simple user input (string) where the 1st char is a number. Through substring and tonumber I convert this into a float. I plot the float on a chart to verify the user input changes the plotted number. I then make numbers part of a group (bool). The group(s) containing the number selected by the user should translate to true while other groups remain false. I plot a shape on the chart to verify the true/false outcome but every group seems to translate to true.
//#version=5
strategy("My strategy", overlay=true)
i1 = "1 - Option 1"
i2 = "2 - Option 2"
i3 = "3 - Option 3"
i4 = "4 - Option 4"
i5 = "5 - Option 5"
i6 = "6 - Option 6"
i7 = "7 - Option 7"
input = input.string(i3, title = "Make a choice", options = [i1, i2, i3, i4, i5, i6, i7])
// Get the number from the user input
inputNr = str.tonumber(str.substring(input, 0, 1))
// Test the input number and plot it on the chart
plot(inputNr * 1000, "Number") // The number plots correct (so far so good)
// Make the numbers part of a group.
// The group(s) containing the numbers should become true while other groups should become false, all based on the user input
group1 = inputNr == 5 or 6 or 7
group2 = inputNr == 1 or 2 or 3 or 4
group3 = inputNr == 3 or 4 or 5
group4 = inputNr == 6 or 7
// Test if a group is true by plotting a shape
// Here I get stuck... Every group, regardless of the user input gives a true
// My expected outcome was that if I sellect i4 from the list that only group2 & group3 would return true while others become false
plotshape(group1, location = location.top, style = shape.arrowdown)
I verified the data types (float & bool). The number plots correctly, the group while boolean does plot but the outcome is not what I expected. I expected when choosing input 4 that only the groups containing nr 4 (group 2 & group 3) would become true while others remain false.

You should try :
group1 = (inputNr == 5) or (inputNr == 6) or (inputNr == 7)
group2 = (inputNr == 1) or (inputNr == 2) or (inputNr == 3) or (inputNr == 4)
group3 = (inputNr == 3) or (inputNr == 4) or (inputNr == 5)
group4 = (inputNr == 6) or (inputNr == 7)

Related

Program not running with error unsupported operand type(s) for +: 'function' and 'int'

Im creating a program for NIM for my Python Intro class, and am having a problem with trying to get the program to finish.
I am at a loss...
def main():
import random
#User random to generate integer between 10 and 100 for random pile size of marbles.
ballCount = random.randint(10, 100)
print("The size of the pile is: ",ballCount)
#Generate a random integer between 0 and 1, this will tell if the human or computer takes first turn.
def playerTurn(ballCount):
playerTurn = random.randint(0, 1)
if playerTurn == 0:
print("Its the computers turn...")
else:
print("Its your turn...")
#Generate a random integer between 0 and 1, to determine if the computer is smart or dumb.
computerMode = random.randint(0, 1)
def computerDumbMode(ballCount):
computerMode = random.randint(0, 1)
if computerMode == 0:
print("This computer is very dumb, no need to stress")
def computerSmartMode(ballCount):
computerMode = random.randint(0, 1)
if computerMode == 1:
print("This computer is very intelligent, beware")
#In dumb mode the computer generates random values (between 1 and n/2),
#you will use a random integer n for ballCount, when it is the computers turn.
while ballCount != 1: #This will compile untill you are left with only one marble.
if playerTurn == 0: #This will initiate computers turn.
if computerMode == 1: #This will initiate Smart Mode.
temp = random.randint(1, ballCount/2) #Pick a random number between 1, and n/2.
else: # Pick your own number of Marbles for your ballCount (n).
if ballCount - 3 > 0 and ballCount - 3 < ballCount/2:
temp = ballCount - 3
elif ballCount - 7 > 0 and ballCount - 7 < ballCount/2:
temp = ballCount - 7
elif ballCount - 15 > 0 and ballCount - 15 < ballCount/2:
temp = ballCount - 15
elif ballCount - 31 > 0 and ballCount - 31 < ballCount / 2:
temp = ballCount - 31
else:
temp = ballCount - 63
print("The computer has chosen: %d marbles." % temp) #Print the number of marbles the computer has chosen.
ballCount -= temp #Then subtract the number of marbles chosen by the computer.
else:
ballCountToPick = int(input("It is now your turn, Please pick the number of marbles in the range 1 - %d: " % int(ballCount/2))) #Reads the number of marbles to be picked by user.
while ballCountToPick < 1 or ballCountToPick > ballCount/2: #If computer reads this invalidly, it will try repeatedly.
ballCountToPick = int(input("The number you chose, is incorrect. Try again, and pick marbles in the range 1 - %d: " % int(ballCount/2)))
ballCount -= ballCountToPick #Subtract the marbles that were chosen by user.
playerTurn = (playerTurn + 1) % 2 #Changes the turn of player.
print("Now the pile is of size %d." % ballCount)
if playerTurn == 0: #Show the outcome.
print("You came, you saw, and you won... CONGRATULATIONS!!!")
else:
print("Once again... You lost and the computer wins!!!")
main()
Trying to get the program/game to run and print end result if the computer or person wins!
Your "playerTurn" variable is a function in the following line:
playerTurn = (playerTurn + 1) % 2 #Changes the turn of player.
Have a separate "playerTurn" function and "player_turn" variable will resolve your problem.

Can't assign a big number to a variable out of the while loop in scala

I want to write a program that can find the N-th number,which only contains factor 2 , 3 or 5.
def method3(n:Int):Int = {
var q2 = mutable.Queue[Int](2)
var q3 = mutable.Queue[Int](3)
var q5 = mutable.Queue[Int](5)
var count = 1
var x:Int = 0
while(count != n){
val minVal = Seq(q2,q3,q5).map(_.head).min
if(minVal == q2.head){
x = q2.dequeue()
q2.enqueue(2*x)
q3.enqueue(3*x)
q5.enqueue(5*x)
}else if(minVal == q3.head){
x = q3.dequeue()
q3.enqueue(3*x)
q5.enqueue(5*x)
}else{
x = q5.dequeue()
q5.enqueue(5*x)
}
count+=1
}
return x
}
println(method3(1000))
println(method3(10000))
println(method3(100000))
The results
51200000
0
0
When the input number gets larger , I get 0 from the function.
But if I change the function to
def method3(n:Int):Int = {
...
q5.enqueue(5*x)
}
if(x > 1000000000) println(('-',x)) //note here!!!
count+=1
}
return x
}
The results
51200000
(-,1006632960)
(-,1007769600)
(-,1012500000)
(-,1019215872)
(-,1020366720)
(-,1024000000)
(-,1025156250)
(-,1033121304)
(-,1036800000)
(-,1048576000)
(-,1049760000)
(-,1054687500)
(-,1061683200)
(-,1062882000)
(-,1073741824)
0
.....
So I don't know why the result equals to 0 when the input number grows larger.
An Int is only 32 bits (4 bytes). You're hitting the limits of what an Int can hold.
Take that last number you encounter: 1073741824. Multiply that by 2 and the result is negative (-2147483648). Multiply it by 4 and the result is zero.
BTW, if you're working with numbers "which only contains factor 2, 3 or 5", in other words the numbers 2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, ... etc., then the 1,000th number in that sequence shouldn't be that big. By my calculations the result should only be 1365.

Why do some values not function for my code?

I am trying to create an app that will have the user input a number and the app will determine if it is prime or not. Here is my code:
var arr: [Int] = [2, 3, 4, 5, 6, 7, 8, 9]
let text = Int(textField.text!)!
for x in arr {
if text == 1 {
iLabel.text = "NOT PRIME!"
}
else if x == 2 || x == 3 || x == 5 || x == 7 {
iLabel.text = "IT'S PRIME!"
}
else if text % x == 0 {
iLabel.text = "NOT PRIME!"
break
}
else if text % x != 0 {
iLabel.text = "IT'S PRIME!"
break
}
}
For the most part, this works. However, when I set variable "text" to equal something like 82, the result is "IT'S PRIME!" despite it being evenly divisible by 2...can anyone explain the flaw in my code?
if text == 1 else if x == 2 else if text % x == 0
^ ^ ^
it will trigger x=2 and set "IT'S PRIME` before it gets to the modulo/remainder check, which it will then skip. It will trigger the same thing at loop 3, 5, 7.
82 is not divisible by 8 or 9 so it won't happen to trigger the remainder check at the end of the loop and accidentally output the correct value, leaving "IT'S PRIME" in the output box, without actually checking whether it's prime.

Check a multiple in Swift?

I am trying to find the odd numbers and a multiple of 7 between a 1 to 100 and append them into an array. I have got this far:
var results: [Int] = []
for n in 1...100 {
if n / 2 != 0 && 7 / 100 == 0 {
results.append(n)
}
}
Your conditions are incorrect. You want to use "modular arithmetic"
Odd numbers are not divisible by 2. To check this use:
if n % 2 != 0
The % is the mod function and it returns the remainder of the division (e.g. 5 / 2 is 2.5 but integers don't have decimals, so the integer result is 2 with a remainder of 1 and 5 / 2 => 2 and 5 % 2 => 1)
To check if it's divisible by 7, use the same principle:
if n % 7 == 0
The remainder is 0 if the dividend is divisible by the divisor. The complete if condition is:
if n % 2 != 0 && n % 7 == 0
You can also use n % 2 == 1 because the remainder is always 1. The result of any mod function, a % b, is always between 0 and b - 1.
Or, using the new function isMultiple(of:, that final condition would be:
if !n.isMultiple(of: 2) && n.isMultiple(of: 7)
Swift 5:
Since Swift 5 has been released, you could use isMultiple(of:) method.
In your case, you should check if it is not multiple of ... :
if !n.isMultiple(of: 2)
Swift 5 is coming with isMultiple(of:) method for integers , so you can try
let res = Array(1...100).filter { !$0.isMultiple(of:2) && $0.isMultiple(of:7) }
Here is an efficient and concise way of getting the odd multiples of 7 less than or equal to 100 :
let results: [Int] = Array(stride(from: 7, through: 100, by: 14))
You can also use the built-in filter to do an operation on only qualified members of an array. Here is how that'd go in your case for example
var result = Array(1...100).filter { (number) -> Bool in
return (number % 2 != 0 && number % 7 == 0)
}
print(result) // will print [7, 21, 35, 49, 63, 77, 91]
You can read more about filter in the doc but here is the basics: it goes through each element and collects elements that return true on the condition. So it filters the array and returns what you want

iphone: how to check uitableview row modulus

it's a hard to explain issue so i hope i can state it
i have a tableview and i want that..
row number 1 have a background and row number 2 have another background .
and row number 3 have a third background..
the fourth row should have the first background and the fifth should have the second ..
and so forth ..
i used this code
if(row+1%1==0){
bg = [UIImage imageNamed:#"row1.png"];
selectionBg = [UIImage imageNamed:#"row1.png"];
}
else if(row+1%2==0){
bg = [UIImage imageNamed:#"row2.png"];
selectionBg = [UIImage imageNamed:#"row2.png"];
}else if(row+1%3==0){
bg = [UIImage imageNamed:#"row3.png"];
selectionBg = [UIImage imageNamed:#"row3.png"];
}
i'm trying to use modulus but i get lost .. so is there a way for that?
thanks in advance
You should use mod 3 because you have three options:
if (row % 3 == 0) {
// Option A
} else if (row % 3 == 1) {
// Option B
} else {
// Option C
}
By the way, do you understand what a modulo operation does? This might be interesting to read (from Wikipedia):
In computing, the modulo operation finds the remainder of division of
one number by another.
Given two positive numbers, a (the dividend)
and n (the divisor), a modulo n (abbreviated as a mod n) can be
thought of as the remainder, on division of a by n. For instance, the
expression "5 mod 4" would evaluate to 1 because 5 divided by 4 leaves
a remainder of 1, while "9 mod 3" would evaluate to 0 because the
division of 9 by 3 leaves a remainder of 0; there is nothing to
subtract from 9 after multiplying 3 times 3.
This is what happens in the code:
row row % 3 option
0 0 A
1 1 B
2 2 C
3 0 A
4 1 B
5 2 C
6 0 A
… … …
Instead of using the modulos use a static counter
Like the following
static int rowBGSelector = 0;
switch (rowBGSelector) {
case 0:
NSLog(#"%d, first", i); //Chose BG 1
break;
case 1:
NSLog(#"%d, second", i); //Chose BG 2
break;
case 2:
NSLog(#"%d, third", i); //Chose BG 3
break;
default:
rowBGSelector = -1; //Reset the static
break;
}
rowBGSelector++; //Increment