Issues with MID$ function - basic

I'm trying to print out every character of a given string on a new line.
1 INPUT ""; A$
2 E%=0
3 IF E% < LEN(A$) GOTO 5
4 END
5 PRINT MID$(A$,E%,E%+1)
6 E% = E% + 1
7 GOTO 3
I keep on getting
ILLEGAL QUANTITY ERROR IN 5
and I don't know why.

You have three problems with your code, two of them related.
First, E% should start at 1 not 0.
Second, because E% starts at 1, you chould check for <= instead of <.
Finally, your MID$() function parameters should be as below:
1 INPUT ""; A$
2 E%=1
3 IF E% <= LEN(A$) GOTO 5
4 END
5 PRINT MID$(A$,E%,1)
6 E% = E% + 1
7 GOTO 3
Next you should look into FOR/NEXT loops.

Related

recursive function does not work as expected

Could someone explain the code? I just can not understand why this code gives output like this:
1
3
6
10
15
21
I expected the code to give something like this:
1
3
5
7
9
11
What am I missing here?
def tri_recursion(k):
if(k > 0):
result = k + tri_recursion(k-1)
print(result)
else:
result = 0
return result
tri_recursion(6)
For your recursive function, the termination condition is k=0.
It's clear that if k=0, tri_recursion(0) = 0.
If k=1, tri_recursion(1) = 1 + tri_recursion(0), which from above, is 1 + 0 or 1.
If k=2, tri_recursion(2) = 2 + tri_recursion(1), which from above, is 2 + 1 or 3.
If k=3, tri_recursion(3) = 3 + tri_recursion(2), which from above, is 3 + 3 or 6.
If k=4, tri_recursion(4) = 5 + tri_recursion(3), which from above, is 4 + 6 or 10.
If k=5, tri_recursion(5) = 4 + tri_recursion(4), which from above, is 5 + 10 or 15.
If k=6, tri_recursion(6) = 6 + tri_recursion(5), which from above, is 6 + 15 or 21.
See the pattern?
Your code is calculating the sum of numbers up to n where n is 6 in the above case. The print statement prints the intermediate results. Hence the output 1 3 6 10 15 21.
1 - The sum of numbers from 0 to 1
3 - The sum of numbers from 0 to 2
6 - The sum of numbers from 0 to 3
10 - The sum of numbers from 0 to 4
15 - The sum of numbers from 0 to 5
21 - The sum of numbers from 0 to 6

how do i use a while loop to print numbers on the same line of output?

n = 10
while n > 0:
print(n)
n=n-1
so far i have this which gives me
10
9
8 7 6 5 4321
but I want 10 9 8 7 6 5 4 3 2 1
You should use the 'end' parameter of print. By default, end="\n" in print, a newline character
n = 10
while n > 0:
print(n, end=" ")
n=n-1
There are several ways you could do that.
Firstly, and probably the simplest, you can use the end argument of print().
For instance, you could do something like this:
n = 10
while n > 0:
print(n, end=" ") # note the 'end' argument
n=n-1
Which gives : 10 9 8 7 6 5 4 3 2 1 (with a trailing space and without carriage return).
Another way I can think of, but involves a bit more complexity in my opinion, is using join().
Example:
print(" ".join(str(i) for i in range(10, 0, -1)))
Which gives : 10 9 8 7 6 5 4 3 2 1 (with a carriage return and without trailing space).

Python recursive index changing

I am trying to arrange matrix in way that it will dynamically change the indexes.
I have tried to do it by means of for loop, however it only does once for each index.
def arrangeMatrix(progMatrix):
for l in range(len(progMatrix)):
for item in range(len(progMatrix[l])):
if indexExists(progMatrix,l + 1,item) and progMatrix[l + 1][item] == " ":
progMatrix[l + 1][item] = progMatrix[l][item]
progMatrix[l][item] = " "
The original list is:
1 0 7 6 8
0 5 5 5
2 1 6
4 1 3 7
1 1 1 7 5
And my code should fill all gapped indexes from up to bottom, however my result is:
1 0 6 8
0 5 5
2 1 7
4 1 3 7 6
1 1 1 7 5 5
The actual result should be:
1 0
0 5 8
2 1 7 5
4 1 3 7 6 6
1 1 1 7 5 5
Any help or hint is appreciated.Thanks in advance
It is probably easier if you first iterate the columns, since the change that happens in one column is independent on what happens in other columns. Then, per column, you could iterate the cells from the bottom to the top and keep track of the y-coordinate where the next non-space should "drop down" to.
No recursion is needed.
Here is how that could be coded:
def arrangeMatrix(progMatrix):
for x in range(len(progMatrix[0])):
targetY = len(progMatrix)-1
for y in range(len(progMatrix)-1,-1,-1):
row = progMatrix[y]
if row[x] != " ": # Something to drop down
if y < targetY: # Is it really to drop any lower?
progMatrix[targetY][x] = row[x] # copy it down
row[x] = " " # ...and clear the cell where it dropped from
targetY -= 1 # since we filled the target cell, the next drop would be higher

Can someone help me with the method behind this shell script?

Can someone help me with the method behind this linux command? I know what the outputs are, just need to know how you figure them out.
#!/bin/sh
i=0
for a in 9 8 7 6 5 4
do
j=1
for b in 1 2 3 4
do
let "j+=2"
done
let "i+=1"
done
echo "i=$i j=$j"
Fist, indenting helps make it clearer:
#!/bin/sh
i=0
for a in 9 8 7 6 5 4
do
j=1
for b in 1 2 3 4
do
let "j+=2"
done
let "i+=1"
done
echo "i=$i j=$j"
The a and b variables are never used, other than to make the outer loop execute 6 times (one loop for each number in the list 9 8 7 6 5 4) and to make the inner loop execute 4 times (one loop for each number in the list 1 2 3 4).
The outer loop adds 1 to the value of i (initialized to zero) each of the 6 times through the loop, ending up with a value of 6.
The inner loop adds 2 to the value of j each of the 4 times through the loop. Even though the inner loop itself is executed 6 times, since j is re-initialized to 1 before each execution of the inner loop, the final value of j is only 9 (1+2+2+2+2).

Function coverage () in R

I want to understand what the function coverage does to an IRange. for example the codes below:
ir <- IRanges (1:3, width = 3)
ir
IRanges object with 3 ranges and 0 metadata columns:
start end width
[1] 1 3 3
[2] 2 4 3
[3] 3 5 3
coverage (ir)
integer-Rle of length 5 with 5 runs
Lengths: 1 1 1 1 1
Values : 1 2 3 2 1
why the values repeats itself like 123 then 21
I figured it out.
The right answer is that we count the ranges covering each number starting from 1 till the last number in the last range.
for example
ir <- IRanges (4:6, width = 3)
first, we draw a plot for that IRange staring from 1 which is not included in any range and ending with 8 which is the boundry of the last range
second, we count the ranges of the Ir that covers each of these number from 0 to 8
count = c (0,0,0,1,2,3,2,1)
Rle (count)
numeric-Rle of length 8 with 6 runs
Lengths: 3 1 1 1 1 1
Values : 0 1 2 3 2 1

Resources