local function Numeral(roman)
local symbols = {}
local sum = 0
for i = 1,#roman do
local val = roman:sub(i,i)
sum = sum + symbols[val]
end
return sum
end
print(Numeral('II'))
Basically, I'm making a roman numeral to number converter. What I want to do is access a key in a dictionary with a variable like
local var = 'I'
local something = dictionary.var
But with what I'm doing, it may be a bit more complicated. Is it possible?
When using a table for the roman numbers then a metatable with the table functions in __index make things a bit easier.
This example show what i mean...
> _VERSION
Lua 5.4
> romans=setmetatable({},{__index=table})
> romans:insert('I')
> romans:insert('II')
> romans:insert('III')
> romans:insert('IV')
> romans:insert('V')
> romans:insert('VI')
> romans:insert('VII')
> romans:insert('VIII')
> romans:insert('VIIII')
> romans:insert('X')
> romans:concat(', ',1,10)
I, II, III, IV, V, VI, VII, VIII, VIIII, X
( Done in a interactive Lua console ( lua -i ) )
For more about doing converting from and to roman numbers you can read also...
https://gist.github.com/efrederickson/4080372
But be aware its not free of bugs.
You can do it better ;-)
So I am trying to learn R on my own and am just working through the online tutorial. I am trying to code a recursive function that prints the first n terms of the Fibonacci sequence and can't get the code to run without the error:
Error in if (nterms <= 0) { : missing value where TRUE/FALSE needed
My code does ask me for input before entering the if else statement either which I think is odd as well. Below is my code any help is appreciated.
#Define the fibonacci sequence
recurse_fibonacci <- function(n) {
# Define the initial two values of the sequence
if (n <= 1){
return(n)
} else {
# define the rest of the terms of the sequence using recursion
return(recurse_fibonacci(n-1) + recurse_fibonacci(n-2))
}
}
#Take input from the user
nterms = as.integer(readline(prompt="How many terms? "))
# check to see if the number of terms entered is valid
if(nterms <= 0) {
print("please enter a positive integer")
} else {
# This part actually calculates and displays the first n terms of the sequence
print("Fibonacci Sequence: ")
for(i in 0:(nterms - 1)){
print(recurse_fibonacci(i))
}
}
This is a problem of readline in non-interactive mode. readline does not wait for a keypress and immediately executes the next instruction. The solution below is the solution posted in this other SO post.
I post below a complete answer, with the Fibonnaci numbers function a bit modified.
recurse_fibonacci <- function(n) {
# Define the initial two values of the sequence
if (n <= 1){
n
} else{
# define the rest of the terms of the sequence using recursion
Recall(n - 1) + Recall(n - 2)
}
}
#Take input from the user
cat("How many terms?\n")
repeat{
nterms <- scan("stdin", what = character(), n = 1)
if(nchar(nterms) > 0) break
}
nterms <- as.integer(nterms)
# check to see if the number of terms entered is valid
if(nterms <= 0) {
print("please enter a positive integer")
} else {
# This part actually calculates and displays the first n terms of the sequence
print("Fibonacci Sequence: ")
for(i in 0:(nterms - 1)){
print(recurse_fibonacci(i))
}
}
This code is the contents of file fib.R. Running in a Ubuntu 20.04 terminal gives
rui#rui:~$ Rscript fib.R
How many terms?
8
Read 1 item
[1] "Fibonacci Sequence: "
[1] 0
[1] 1
[1] 1
[1] 2
[1] 3
[1] 5
[1] 8
[1] 13
rui#rui:~$
To make it work with Rscript replace
nterms = as.integer(readline(prompt="How many terms? "))
with
cat ("How many terms?")
nterms = as.integer (readLines ("stdin", n = 1))
Then you can run it as Rscript fib.R, assuming that the code is in the file fib.R in the current working directory.
Otherwise, execute it with source ("fib.R") within an R shell.
Rscript does not operate in interactive mode and does not expect any input from the terminal. Check what interactive () returns in both the cases. Rscript will return FALSE as it is non-interactive, but the same function when run within an R shell (with source ()) it will be true.
?readline mentions that it cannot be used in non-interactive mode. Whereas readLines explicitely connect to stdin.
The code works fine but you shouldn't enter it into the terminal as is. My suggestion: put the code into a script file (ending .R) and source it (get help about it with ?source but it's actually pretty straightforward).
In R-Studio you can simply hit the source button.
I'm trying to do many conditional events in R but im getting the warning:
Warning messages:
1: In if (closeV > openV) { :
the condition has length > 1 and only the first element will be used
2: In if ((highV - closeV) < Minimum) { :
the condition has length > 1 and only the first element will be used
3: In if ((openV - lowV) > Threshold) { :
the condition has length > 1 and only the first element will be used
4: In if (((openV - lowV) < Threshold)) { :
the condition has length > 1 and only the first element will be used
5: In if ((closeV - openV) < Threshold) { :
the condition has length > 1 and only the first element will be used
6: In if ((closeV - lowV) < (Threshold * 2)) { :
the condition has length > 1 and only the first element will be used
this is a huge nest of ifs, it is not optimized right now but i cant get it to work because of that warning.
There are around of 40 ifs in that function, any idea of what i need to do to get around this warning?
The code looks something like this
if(closeV>openV)#1 First we check if we have a positive value
{
if((highV-closeV)<Minimum)
{
if((openV-lowV) >Threshold)
{
if((closeV-openV)<Threshold)
{
#3.1 This is a Hammer with positive movement
if((closeV-lowV)<(Threshold*2))
{
#3.1.1 not much movement
return(X*2)
}
else if((closeV-lowV)>(Treshold*2))
{
#3.1.2 a lot of movement
return(X*3)
}
}
else if((closeV-openV)>Threshold)
{
#3.2 Hammer but with a lot of movement
if((closeV-lowV)<(Threshold*2))
{
#3.2.1 not much movement
return(X)
}
else if((closeV-lowV)>(Treshold*2))
{
#3.2.2 a lot of movement
return(X*5)
}
}
}
else if(((openV-lowV)<Threshold)
and it keeps on going through a lot of possibilites
The issue is not the nested if-statements, but rather the data structure you feed into them: The warning tells you that the comparison operator is only applied to the first element of the data structure you feed into the if-statements.
While
a = seq(1, 10, 1)
b = seq(0, 18, 2)
if (a>b){
print(a)
} else{
print(b)
}
throws the same warning messages you get,
a = seq(1, 10, 1)
b = seq(0, 18, 2)
for (i in 1:10) {
if (a[i]>b[i]){
print(a[i])
} else{
print(b[i])
}
}
in contrast evaluates smoothly.
Also, please notice that although both pieces of code are evaluated, they give very different results.
I started learning how to program in Julia, and I'm making a pretty simple code, but it's not working as I wish, and I'm lost because I can't find where's the error.
Basically, I have a vector like this one: (1,0,0,1,1) and I made two functions that will change the entries of the vector.
The first function needs to change every entry of the vector for 1.
The second function needs to change every entry as follows: if the entry is 1, then change it for 0, and vice versa.
I have the next code:
function vectorMethodOne(vector1)
for i = 1:length(vector1)
if vector1[i] == 0
vector1[i] = 1
end
end
return vector1
end
function vectorMethodTwo(vector1)
for i = 1:length(vector1)
if vector1[i] == 0
vector1[i] = 1
elseif vector1[i] == 1
vector1[i] = 0
end
end
return vector1
end
The problem happens when I run the code like this:
vectorEx = rand(0:1, 5)
println("Original Vector:")
println(string(vectorEx))
println("Vector using method 1:")
vectorM1 = vectorMethodOne(vectorEx)
println(string(vectorM1))
println("Vector using method 2:")
vectorM2 = vectorMethodTwo(vectorEx)
println(string(vectorM2))
The output looks like this:
> Original Vector:
> [1,0,0,1,1]
> Vector using method 1:
> [1,1,1,1,1]
> Vector using method 2:
> [0,0,0,0,0]
But I want that the output looks like this:
> Original Vector:
> [1,0,0,1,1]
> Vector using method 1:
> [1,1,1,1,1]
> Vector using method 2:
> [0,1,1,0,0]
If I only run the vectorMethodTwo, it works like I want, like this:
vectorEx = rand(0:1, 5)
println("Original Vector:")
println(string(vectorEx))
println("Vector using method 2:")
vectorM2 = vectorMethodTwo(vectorEx)
println(string(vectorM2))
And the output looks like this:
> Original Vector:
> [1,0,0,1,1]
> Vector using method 2:
> [0,1,1,0,0]
But I want that every function run over the original vector (1,0,0,1,1) but the vectorMethodTwo is running over the modified vector (1,1,1,1,1) and I can't understand where's the error in my code.
Let's look at your output:
> Original Vector:
> [1,0,0,1,1]
> Vector using method 1:
> [1,1,1,1,1]
> Vector using method 2:
> [0,0,0,0,0]
Odd. Method 2 looks like method 1 flipped. Let's check:
println(vectorMethodTwo([1,1,1,1,1]))
> [0,0,0,0,0]
Very suspicious! Why could this be happening? Please think about this before moving to the next section.
Your "functions" are mutating the vector. When you do this in vectorMethodOne,
vector1[i] = 1
, you are changing the contents of vector1 that was passed in. That vector1 refers to the same memory as vectorEx.
Do not write code that mutates your inputs (unless you name the function accordingly). Either create a copy of your vector before mutating it, or try a list comprehension:
function vectorMethodOne(vector1)
return [x == 0 ? 1 : x for x in vector1]
end
function vectorMethodTwo(vector1)
return [x == 0 ? 1 : x == 1 ? 0 : x for x in vector1]
end
These do not modify the contents of the input vector1 in any way.
i used following tutorial for learning R programming language
R programming language
v <- c("Hello","loop")
cnt <- 2
repeat {
print(v)
cnt <- cnt+1
if(cnt > 5) {
break
}
}
but when i run this code, it gave me the following error
> if(cnt > 5) {
+ break
+ }
Error: object 'cnt' not found
>
this example was taken from tutorial itself, what is wrong in given code?