I get a sytax error at line 4 (I knwo there excess lines rn) - ti-basic

Menu("RADIAN CONVERSION","DEGREE TO RADIAN",1,"RADIAN TO DEGREE",2
Lbl 1
Input "DEGREE=",C
C/180Frac→W
Goto 3
Lbl 3
Goto 4
Lbl 2
Input "RADIAN=""LEAVE OUT PIE",A
A(180)→D
Goto 5
Lbl 4
Disp "RADIAN",P
Lbl 5
Disp "DEGREE=",D
Stop
Error is in line 4 the question mark is just the frac conversion
I know there is easier ways to do this but i just wanna learn

Related

sorting two vector array to get the cycles

I'm trying get a sorting function that takes an array like the following:
0 2
1 5
2 3
3 0
4 1
5 4
and sorts it by cycles, i.e. it should output
0 2
2 3
3 0
1 5
5 4
4 1
Is there something build-in or how get this done in a lean way?
This worked
y=[]; %% results go here
k=1;
while length(x)>0 %% items left? loop
_x=x(k,:); %% local copy
y=[y ; _x]; %% append
x(k,:)=[]; %% remove from array
try
k=find(x(:,1)==_x(2)); %% find next
catch
k=1; %% no next? complete cycle, start over
end
end

Count characters starting at zero?

I need to write a for-each loop that lists each character in
mystery_string with its index. Example below:
mystery_string= "Olivia," output would be:
0 O
1 l
2 i
3 v
4 i
5 a
I cannot use the range function on this problem.
This is my code, but the number starts at 1. What am I doing wrong?
mystery_string = "CS1301"
count = 0
for current_letter in mystery_string:
count = count + 1
print (count , current_letter)
I have been getting this as output:
1 C
2 S
3 1
4 3
5 0
6 1
but it needs to start at zero.
Just add the count (count += 1) after you print in the for loop
Note: Also, please format your code in a code block surrounded with a tick(`) or multiline code with 3 tick (```)
The pythonic way is to use enumerate() in such a case. This way you'll get both the index and the content of your string.
mystery_string = "CS1301"
for count, current_letter in enumerate(mystery_string):
print (count , current_letter)

Boyer-Moore Algorithm: Reaching the End of the Text

I'm working through this algorithm and the pattern and text are not matching up.
The text is: AADBCCAAA
The pattern is: CCAAA
I've created both the bad-symbol table and good-suffix table.
Bad Symbol:
! A C (! represents letters not in the pattern)
5 1 3
Good-suffix:
k d2
1 2
2 6
3 6
4 6
5 6
As for my searching:
AADBCCAAA
CCAAA Since there is no match, shift 3 because C causes the mismatch
This lines up the right-most A in the pattern with the 2nd to last A in the text. This means
d1 = 3-2 = 1 and d2 = 6. The max of the two is 6 so shift 6.
With Boyer-Moore, does this mean since you can't shift 6, it will just compare the pattern with the end of the text and find the match or am I doing something wrong?

READ statement doesn't read last number from file

Suppose you have this program
Subroutine readDIM which reads the dimensions (rows, columns) of a matrix from a txt file. (In order to simplify, let it be an INTEGER). ReadDIM works using tokens and it works fine by assumption.
A text file containing for example:
1 2 3 4
1 2 20 5
3 0 333 3
Returns nrow = 3, ncol = 4
Since readDIM has given the true dimensions of the matrix, I want to allocate space to:
REAL, DIMENSION (:,:), ALLOCATABLE :: vMatrix
To read the matrix from a txt file and to store it into the 2d-array. So I've written the following
SUBROUTINE buildVMatrix
OPEN(UNIT=1, FILE = filename, STATUS ='OLD',IOSTAT=ios);
ALLOCATE(vMatrix(nrow,ncol));
WRITE(*,*) "Register matrix from file:", filename;
WRITE(*,*) "-------------------------------------------------------";
DO i = 1, UBOUND(vMatrix,1)
READ(1,*, IOSTAT = ios) (vMatrix(i,j),j=1,UBOUND(vMatrix,2));
!IF(ios /= 0 ) EXIT
END DO
CLOSE(1)
END SUBROUTINE
When I print vMatrix the output is:
matrix.txt : 1 2 3 4 buildVMatrix output (once printed) 1 2 3 4
1 2 20 5 1 2 20 5
3 0 333 3 3 0 333 0
It doesn't read the last number. I know it's caused by the DO loop inside buildVMatrix, but can't explain myself this and have no idea how to fix it writing a different code.
It's because there's no line ending at the last line in your txt file, try to type a return after the last number.

R - Improving the performance of a simple loop

I'm a beginner with R, so I'm having trouble thinking of things the "R way"...
I have this function:
upOneRow <- function(table, column) {
for (i in 1:(nrow(table) - 1)) {
table[i, column] = table [i + 1, column]
}
return(table)
}
It seems simple enough, and shouldn't take that long to run, but on a dataframe with ~300k rows, the time it takes to run is unreasonable. What is the right way to approach this?
Instead of the loop you could try something like this:
n <- nrow(table)
table[(1:(n-1)), column] <- table[(2:n), column];
to vectorize is the key
Simple answer: Columns in a data.frame are also vectors which can be indexed with [,]
my.table <- data.frame(x = 1:10, y=10:1)
> my.table
x y
1 1 5
2 2 4
3 3 3
4 4 2
5 5 1
my.table$y <-c(my.table[-1,"y"],NA) #move up one spot and pad with NA
> my.table
x y
1 1 4
2 2 3
3 3 2
4 4 1
5 5 NA
Now you function repeats the last data point at the end. If this is really what you want, pad with tail(x,1) instead of NA.
my.table$y <-c(my.table[-1,"y"],tail(my.table$y,1)) #pad with tail(x,1)
> my.table
x y
1 1 4
2 2 3
3 3 2
4 4 1
5 5 1
If I understand you right, you're trying to "move up" one column of a data frame, with the first element going to the bottom. Then, It might be achieved as:
col <- table[, column]
table[, column] <- col[c(nrow(table), 1:(nrow(table)-1))]

Resources