I have a set of integers that act as ids for other variables in my code. I wish to create a list of them, numbered from 1 to N:
i_counter = 0
i_counter = i_counter + 1
i_A = i_counter
i_counter = i_counter + 1
i_B = i_counter
...
Is there a way to write these in one line? I'd like to be able to rearrange the code line-by-line to change the order of the counters.
In Julia, each statement is also an expression, so you can just chain together statements to get what you want.
i_counter = 0
i_A = i_counter = i_counter + 1
i_B = i_counter = i_counter + 1
or more elegantly, as in Bogumil's comment
i_counter = 0
i_A = i_counter += 1
i_B = i_counter += 1
In other words, there is no need for anything analogous to Python's new "walrus" operator := in Julia, because every statement such as i_counter = i_counter + 1 is already an expression that returns a value.
Related
I have the following algorithm that adds t consecutive numbers starting at s.
Addup (s,t)
Make result = s
For i from s to t-1
Result = Result + s + i
Return Result
So Addup (3,3) = 3+4+5 = 12 & Addup (4,5) = 4+5+6+7+8
How can this be made recursive?
Q
In Python, for example:
def Addup(s,t):
if t == 1:
return s
else:
return s + Addup(s+1, t-1)
In the following example:
Fs = cell(2)
i = 1
for i = 1:2
local i # same as local i = i
Fs[i] = () -> i
end
println(Fs[1]()) # 1
println(Fs[2]()) # 2
println(i) # 1
Is it true that local i is the same as local i = i?
Why cannot I do the same for while loop as such?
Fs = cell(2)
i = 1
while i <= 2 #LoadError: UndefVarError: i not define
local i = i
Fs[i] = ()->i
i += 1
end
But have to use let:
Fs = cell(2)
i = 1
while i <= 2
let i = i
Fs[i] = ()->i
end
i += 1
end
println(Fs[1]()) # 1
println(Fs[2]()) # 2
When you introduce new variable, and try to get it's value before assignment, Julia throws an UndefVarError error, in the first example:
Fs = cell(2)
i = 1
for i = 1:2
local i # same as local i = i
Fs[i] = () -> i
end
println(Fs[1]()) # 1
println(Fs[2]()) # 2
println(i) # 1
For block introduces a new local i, adn for command itself assign it so the code successfully dose it's duty. But in the second example:
Fs = cell(2)
i = 1
while i <= 2 #LoadError: UndefVarError: i not define
local i = i
Fs[i] = ()->i
i += 1
end
A new i was introduced in while block, but before assignment you try to get it's value, and this produces an error.
In 3d one let command declare a new local block and a new i variable for it's block.
julia> function test3()
Fs = cell(2)
i = 1
while i <= 2
let i = 100
println(i)
end
i += 1
end
println(i)
end
test3 (generic function with 1 method)
julia> test3()
100
100
3# while block use the function's scope
Hi I'm new to python and programming in general. I am trying write a program that uses a while loop to add integers from 1 to the number entered. the program also has to give an error statement if the user enters a 0 or negative number. So far the integers add up and the error statement works but the program is not looping, it only asks the user to input a number one time. Please help. This is my source code so far. Thanks
x = int(input("Enter a positive number not including zero:" ))
total = 0
n = 1
while n <= x:
total = total + n
n = n + 1
# prints the total of integers up to number entered
print("Sum of integers from 1 to number entered= ",total)
if x <= 0 or x == -x:
print ("invalid entry")
Try this code...
op='y'
while op=='y':
x = int(input("Enter a positive number not including zero:" ))
total = 0
n = 1
if x > 0:
while n <= x:
total = total + n
n = n + 1
# prints the total of integers up to number entered
print("Sum of integers from 1 to number entered= ",total)
else:
print ("invalid entry")
op = raw_input("Are you want to continue this operation (y/n):" )
Put your whole code this way
done = False
while not done:
//your entire code here except the last 2 lines
if x > 0:
done = True
I have a vector of widths,
ws = c(1,1,2,1,3,1)
From this vector I'd like to have another vector of this form:
indexes = c(1,2,3,5,6,7,9,11,12)
In order to create such vector I did the following for loop in R:
ws = c(1,1,2,1,3,1)
indexes = rep(0, sum(ws))
counter = 1
counter2 = 1
last = 0
for(i in 1:length(ws))
{
if (ws[i] == 1)
{
indexes[counter] = counter2
counter = counter + 1
} else {
for(j in 1:ws[i])
{
indexes[counter] = counter2
counter = counter + 1
counter2 = counter2+2
}
counter2 = counter2 - 2
}
counter2 = counter2+1
}
The logic is as follows, each element in ws specifies the respective number of elements in index. For example if ws is 1, the respective number of elements in indexes is 1, but if ws is > 1, let us say 3, the respective number of elements in index is 3, and the elements are skipped 1-by-1, corresponding to 3,5,7.
However, I'd like to avoid for loops since they tend to be very slow in R. Do you have any suggestions on how to achieve such results only with vector operations? or some more crantastic solution?
Thanks!
Here's a vectorized one-liner for you:
ws <- c(1,1,2,1,3,1)
cumsum((unlist(sapply(ws, seq_len)) > 1) + 1)
# [1] 1 2 3 5 6 7 9 11 12
You can pick it apart piece by piece, working from the inside out, to see how it works.
The basic concept behind this code is that whenever it runs, the quantity from an element decreases and the quantity from the same element, but from a different array, increases. For whatever reason, the second while loop only runs once and stops. For example, if total1 = 11 and total2 = 0, the first time the code is executed, total1 = 10 and total2 = 1. However after that, total1 = 9 and total2 = 1 and so on. Can anyone tell me what is wrong with my code? Any and all help would be appreciated.
<%
count = 0
do while NOT rs3.EOF
if rs3("ITEM_NO") = itemnum then
qtyArray(count) = qtyArray(count) - qtyreq
end if
if qtyArray(count) >= 0 and rs3("ITEM_NO") = itemnum then
total1 = total1 - qtyreq
end if
count = count + 1
rs3.MoveNext
loop
rs3.MoveFirst
pickcount = 0
do while NOT rs3.EOF
if qtyPick(pickcount) >= 0 and rs3("ITEM_NO") = itemnum then
qtyPick(pickcount) = qtyPick(pickcount) + qtyreq
total2 = total2 + qtyreq
end if
rs3.MoveNext
pickcount = pickcount + 1
loop
%>
total2 = total2 + qtyreq
Please make sure "qtyreq" variable is not 0 and really adds +1 to your total2.
There is a line on your second loop pickcount = pickcount + 1. Just move this line above the line rs3.MoveNext of your second loop