How to increase variables by 1 in Nim - nim-lang

I would like to know how to increase variables in Nim by 1, alike the (variablename)++ function in other programming languages.
My Code by far:
int i = 1
int j = 1
for i in countup(1, 10):
j = j + 1
echo "number: "
echo i
I want to change the j = j + 1 with the Nim version of: j++

You can use the += operator:
j += 1

You can increment a variable by one in Nim with the inc() function: inc(var_name)

Related

Given a string s, find the length of the longest substring without repeating characters? (I need to find the bug in code I wrote)

Please help as this is getting on my nerves I can't figure out what I'm doing wrong and have tried trace code.
Link to problem: https://leetcode.com/problems/longest-substring-without-repeating-characters/
I created a solution using a sliding window. It works on most test cases, but fails for a few (such as "ad"). I can't figure out where the bug is. I basically keep track in a dictionary of characters I've seen and the last index I saw them at which gets updated periodically in a loop. I use two indices i and j; i gets updated when I find a repeat character. I return the max of current max and length of current substring which is i-j. Here is my code below:
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if len(s) < 2:
return len(s)
m = 1
i = 0
j = 1
d = {}
d[s[0]] = 0
while j < len(s):
if s[j] in d and d[s[j]] >= i:
m = max(m, j -i)
i = j
d[s[j]] = j
j += 1
return max(m, j - i - 1)
Why does this fail for some cases? Example:
"au"
Output
1
Expected
2
Last line should be return max(m, j - i). Because i is the last index we see repeated character. So. We start this index to end of the string.So length is len(s) - i . And since j = len(s) (while loop ends when j = len(s)) so last substring length is j-i. not j-i-1
And also we are updating i wrong.let's say s = "abcadf". In while loop when we see second "a" ,so j = 3, we should update i = 1, not 3. Because in this case our longest substring will start with "b".So we should update i as i = d[s[j]] + 1. So final result:
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if len(s) < 2:
return len(s)
m = 1
i = 0
j = 1
d = {}
d[s[0]] = 0
while j < len(s):
if s[j] in d and d[s[j]] >= i:
m = max(m, j -i)
i = d[s[j]] + 1
d[s[j]] = j
j += 1
return max(m, j - i)

Why is this specific if condition never executes ? [if mid < k <= right:]

Im trying to solve a specific leetcode problem and but a particular if else block never executes in my code and I cant figure why. Here is the code. I'm new to python and I think i'm making a noob mistake but I just figure what it is.
class Solution:
def findDuplicate(self, nums: List[int]) -> int:
left, right = 1, len(nums) - 1
while left < right :
mid = left + (right-left)//2
count = 0
print("l,r -->" + str(left) + ',' + str(right))
print("mid -->" + str(mid))
for k in nums :
if mid < k <= right: # this block never executes.
print(k)
count += 1
print("count -->" + str(count))
if count > right -mid:
left = mid + 1
else :
right = mid
return right
For one thing, this
if mid < k <= right: # this block never executes.
is not doing what you think it is -- instead, you want
if mid < k and k <= right:

How should I fix my infinite while loop that takes in 3 conditions? Also stylistic questions for novice

writing code to test the Hailstone Sequence, also called Collatz conjecture. Code will print out the number of iterations of the Hailstone sequence.
def main():
start_num = eval (input ("Enter starting number of the range: "))
end_num = eval (input ("Enter ending number of the range: "))
The main problem is that my code returns an infinite loop. I want to check all of these conditions in one statement
while (start_num > 0 and end_num > 0 and end_num > start_num):
cycle_length = 0
max_length = 0
max_number = 0
my code seems inefficient, there is probably a better way to approach the problem
for i in range(start_num, (end_num + 1)):
cycle_length = 0
while (i != 1):
if (i % 2 == 0):
i = i // 2
cycle_length += 1
if (i % 2 == 1):
i = ((3 * i) + 1)
cycle_length += 1
print (cycle_length)
I just started coding, and I always know that there is a more efficient way to approach these problems. Any suggestions on methodology, problem solving, or stylistic advice would be greatly appreciated.
Here is an answer in java. I assume that we will not start with 1.
public static void main(String[] args) {
int counter =0;
Scanner sc = new Scanner(System.in);
System.out.println("Give us a number to start with:");
int start = sc.nextInt();
System.out.println("Give us a number to end with:");
int end = sc.nextInt();
if (end > start) {
for (int i = 0; i <= end - start; i++) {
counter = 0;
int num = start + i;
int temp = num;
while(temp != 1) {
if ( temp % 2 == 0 ) {
temp = temp / 2;
} else {
temp = 3* temp +1;
}
counter++;
}
System.out.println(num + " takes " + counter + "iterations.");
}
} else {
System.out.println("Your numbers do not make sense.");
}
}
Here's an answer in python in case you're staying up late trying to solve this problem. :P Have a good night.
start_num = 1
end_num = 10
for i in range(start_num, (end_num + 1)):
cycle_length=0
num = i
while (num != 1):
if (num % 2 == 0):
num = num // 2
cycle_length+=1
else:
num = ((3 * num) + 1)
cycle_length+=1
print(cycle_length)

How are nested if-else loops resolved in BASIC

I've got this legacy code I'm analyzing:
If (X) then
if Cnt < 4 then Cnt = Cnt + 1 ; 4 samples
Else
if Cnt > 0 then Cnt = Cnt-1 ; keep history
EndIf
Which has Cnt go up and down depending on X
And I'm wondering if that else statement acts like their indention implies they think it does.
The code might be interpreted more like:
If (X) then
if Cnt < 4 then
Cnt = Cnt + 1 ; 4 samples
Else
if Cnt > 0 then
Cnt = Cnt-1 ; keep history
EndIf
In which Cnt get to 4 and then toggles on/off if X is true.
This is basic as compiled using BCI51. That's a basic compiler for an 8051 from back in 1990 by Systronix.
How do nested if-else pairs get resolved in basic?
I remember how QBasic did so, and I'm going to assume that this complier is doing the same. This is really tugging on my memory, so I might be wrong.
If a IF THEN is followed by code on the same line, then it is fully contained. Therefore
if Cnt < 4 then Cnt = Cnt + 1
else
...
would be illegal and you must place the Cnt = Cnt + 1 on it's own line to create a multi-line IF statement. Therefore, the ELSE is paired the topmost IF
Since, in the original code, the Cnt = Cnt + 1 and Cnt = Cnt - 1 are on the same lines as the IF THEN, I would interpret the code as follows:
If (X) then
If Cnt < 4 Then
Cnt = Cnt + 1 ; 4 samples
EndIf
Else
If Cnt > 0 Then
Cnt = Cnt-1 ; keep history
EndIf
EndIf
So, yes, I believe the code operates as the indentation implies.
Are you able to modify the code and test if you see any changes?

Groovy for loop with multiple counters?

Following works in groovy -
for(def i=0;i<10;i++)
print i
But this which is valid in Java, C++ does not work in groovy -
for(def i=0,j=0;i<10;i++,j++)
print i + ' ' + j
Why? How to make this work?
It will not working as Groovy does not accept multiple expressions in a for loop.
Try this:
[0..10,0..10].transpose().each{ i, j ->
println i + ' ' + j
}
to achieve the same result.
Update to make it more generalized. This update is equivalent to increment with i++, j+=3.
(0..<10).collect{[it, it+3]}.each{ i, j ->
println i + ' ' + j
}
Have you tried this:
for( def ( int i, int j ) = [ 0, 0 ]; i < 10; i++, j++ )
If that doesn't work, it might be failing because of the last part.
C++ has a explicit comma operator, which is how it allows constructs like this.
Java does not have a comma operator, but presumably allows constructs such as this as a hack to the for loop.
If Groovy wont allow this, it's most probably because it doesn't allow this hack.

Resources