How can I select a word under a cursor position in Kate via shortcut? - cursor

During programming I have to often select a whole word and make delete it. I am doing this by double click in mouse, but i prefer do it by keyboard - it is possible?

As already mentioned, removing the word to the left/right is done with ctrl+backspace and ctrl+delete.
There is also ctrl+h, which selects the word under the cursor. May also be handy if you want to delete it. But originally, ctrl+h was added as search for word under cursor (i.e., pressing ctrl+h again will jump to the next instance).

I do not have a Kate IDE right now to test, but these two should work, they are kind of universal.
Ctrl+Backspace – Delete previous word
Ctrl+Delete – Delete next word
You could also just select a word left or right from the cursor with the following short keys
Shift+Ctrl+Left or Right Arrow Keys – Select words respectively left or right from the cursor
Using the short keys in a middle of the word you will delete respectively the left or the right part of the word.
Hope this helps :)

Related

How to undo multiple steps in a Tic-Tac-Toe game in C

I want to program a version of the Tic-Tac-Toe game using C, in which we have 'n × n' board decided by the user, and the loser is decided by the first who get first 'n' X's or O's in a row or column etc..
One of the requirements is to let the players be able to undo multiple steps, that means to get back to the board status as it was a couple of steps ago by entering a negative odd number.
For example, if player 1 entered '-3' as the row index, the game needs to revert back how it was 3 steps before (in case there has already been 3 steps done within the game), show the board and give the turn to player 2.
Any idea how I would be able to make such a function or at least a tip how would I start programming it?
Thanks!
idea how I would be able to make such a function or at least a tip how would I start programming it?
An alternative to undoing a step it to instead keep a history (linked list) of all actively. Replay the list sans the last one to "undo". Less efficient yet less complicated and less error prone to undo complicated steps - which may be present in more advanced games. Sometimes, "backing up" is wrought with subtle complexities where the prior state is not truly fully restored. Akin to #Tom Karzes #David C. Rankin
As a bonus, you have a game record.
Consider your game has events like:
Default setup-up
Select size n.
Select X or O to go first
Make a move
Rotate view
Save game
Restore game
Undo last command <-- now one can undo more than just the last "move"
etc.
Normally you would have a stack where you can put each new move and when you want to undo a move, you just pop it from the stack and do the reverse move on your game model.

I'm trying to understand the K&R's exercise 1-21 [duplicate]

This question already has answers here:
K&R answer book exercise 1.21
(2 answers)
Closed 4 years ago.
This is the question I am trying to understand:
Write a program 'entab' that replaces strings of blanks by the minimum
number of tabs and blanks to achieve the same spacing. When either a
tab or a single blank would suffice to reach a tab stop, which should
be given preference?
Decoding the question:
a. It's a program that injects 'tabs' in the input.
b. If a string consists of continuous blank spaces, then these blank
spaces has to be replaced with minimum number of tabs and spaces.
How should the program behave in the following below inputs:
hey*****
******hi
hey**************hi!
hi####hey!
hi***how****are*you?
hi#**hey!
What should be the criteria to decide on the minimum number of tabs, and combination of min tabs and spaces?
'#' for tab, '*' for blank space, TABSTOP = 8.
What does the statement mean: "when either a tab or a single blank would suffice to reach a tab stop."
Note: I have gone through answers of this duplicate question, but I
don't find them helpful.
The linked possible-duplicate question focuses on doing the arithmetic correctly. I'm guessing your problem is more primitive than that: you don't really know what a tab stop is.
When K&R wrote their exercises, they could expect the reader to have experience with typewriters, which is where tab stops come from. The tab key caused the carriage to slide over to the position of the next stop, which was a real physical thing that literally stopped the carriage from moving further until another key was pressed. (Later typewriters, becoming more computer-like, had programmed stop locations instead of physical stops.)
The purpose of tabs was tabulation (making tables). The tab stops were set at the horizontal locations separating the columns of the table, and then the table was entered by pressing the tab key after each value. For example, if I want to type this table:
Character ASCII
Tab 9
Linefeed 10
Carriage Return 13
Space 32
Without using tabs, I have to type space many times after the word "Tab", not as many times after "Linefeed", and only a few times after "Carriage Return". But if I set a tab stop at the position where the second column starts, it becomes easier. I press the tab key once after the word "Tab", and the carriage advances to the correct place for the 9, press the tab key once after the word "Linefeed", and it advances to the correct place for the 10, etc. If I needed a third column, I would set another tab stop a little bit to the right of the second column.
You can experience this in a text editor - not a fancy IDE that assigns all kinds of unrelated functions to the tab key, but a plain one like vi. Or even a terminal emulator, running a program that does nothing (cat > /dev/null). Type several lines of words of various lengths, with a tab after each one, and observe how they line up, and what happens when one of your words is long enough to occupy 2 table columns. Keep playing with it until you have an intuitive understanding of what the tab character does.
Modern text editors and terminal emulators usually have tab stops set every 8 characters. That's what "tabstop = 8" means. The stops are at columns 8,16,24,32,..., or if you believe the leftmost column is 1 instead of 0, columns 9,17,25,33,... Tab stops are actually programmable on vt100-ish terminals, but that's a rarely used feature.
Back in your text editor, a the start of a new line, type 1 2 Tab 3 4. You get 12 and 34 separated by some whitespace made of a tab character. Then start another new line and type the same thing with a space before the tab: 1 2 space Tab 3 4. The second line looks exactly like the first, but this time the whitespace between 12 and 34 is made of a space and a tab. Make a third line that looks the same without using any tabs, by typing space until it lines up.
Those 3 lines are examples of possible input to the exercise 1-21 program. The first one, with a single tab and no spaces, uses the minimum number of characters so that's what you want to output for all 3 inputs.
If you need help figuring out a general formula for how many tabs and spaces to output, see the other question. Here's a rough description that leaves some details for you to think about: As you read the input, keep track of what column you're in. When you get to a space or tab, read to the end of the sequence of spaces and tabs, remembering what column you were in when the sequence started. At the end of the sequence, you know what column the cursor is in, and what column you want to move it to, and you must calculate the optimal sequence of spaces and tabs to make that movement.

Loop thru the dataset and get yearly average

The excel file has 20 tabs. Each tab has over 2000 rows of daily forward curves. The layout of the data on each tab: the first column is the date 2007-01-01, 2007-01-02, 2007-01-03.. and the first row is 2008-01-01, 2008-02-01,2008-03-1 all the way to 2025-12-01
Is there a way to loop through all the row and return the average price of Y+1, Y+2, Y+3 for each day in the first column?
Not sure if there is a better way, but this is how I would do it. Make a new sheet with whatever formatting you want (ie. by day of the year or whatever). Once you have that, in column B next to the January 1, type =average( Then use the shortcut CTRL+PgDown to switch tabs from left to right. Click the first day entry on the first year, then press the , key, then use CTRL+PgDown (I use the right CTRL key with my left hand) to quickly cycle through clicking the first entry again on each tab. After the first comma, you won't have to press it again, so it should be pretty easy.
When you've added the last entry, hit enter and the average for the first day will be there. If you have already done the formatting for each day in column A, just click the cell with the first average. You'll see a small square in the bottom right corner of the cell. Double click it and voila.

Implementing A* algorithm

So I am implementing A* algorithm in C. Here's the procedure.
I am using Priority Queue [using array] for all the open nodes. Since I'll have duplicate distances, that is more than one node with same distance/Priority, hence while inserting a node in PQ, if the parent of the inserted node has the same priority, I still swap them both, so that my newest entered member remains on the top( or as high as possible),so that I keep following a particular direction. Also, on removing, when I swap the topmost element with the last one, then again, if the swapped last element has the same as one of its children, then it gets swapped to the bottom.(I am not sure if this will affect in any way).
Now the problem is say I have a 100*100 matrix, and I have obstacles from (0,20) to (15,20) of the 2D array , in which I am moving. Now for a starting position (2,2) and ending position (16,20) I get a straight path, i.e. firstly go all the way to right, then go down till 15 then move one right and I am done.
But, if I have starting as (2,2) and last as (12,78) i.e. the points are separated by the obstacles and the path has to go around it, I still go via (16,20) and my path after (16,20) is still straight, but my path upto (16,20) is zig zag, i.e. I go some distance straight down, then some right, then down then right and so on, ultimately reaching (16,20) and going straight after that.
Why this zig zag path for the first half of the distance, what can I do to make sure that my path is straight, as it is, when my destination is (16,20) and not (12,78).
Thanks.
void findPath(array[ROW][COLUMN],sourceX,sourceY,destX,destY) {
PQ pq[SIZE];
int x,y;
insert(pq,sourceX,sourceY);
while(!empty(pq)) {
remove(pq);
if(removedIsDestination)
break; //Path Found
insertAdjacent(pq,x,y,destX,destY);
}
}
void insert(PQ pq[SIZE],element){
++sizeOfPQ;
PQ[sizeOfPQ]==element
int i=sizeOfPQ;
while(i>0){
if(pq[i].priority <= pq[(i-1)/2].priority){
swapWithParent
i=(i-1)/2;
}
else
break;
}
}
You should change your scoring part. Right now you calculate absolute distance. Instead calculate min move distance. If you count each move as one then if you were at (x,y) and going to (dX,dY) that would be
distance moved + (max(x,dX) - min(x,dx) + max(y,dY) - min(y,dY))
A lower value is considered a higher score.
This heuristic is a guess at how many moves it would take if there was nothing in the way.
The nice thing about the heuristic is you can change it to get the results you want, for example if you prefer to move in a straight line as you suggest, then you can make this change:
= distance moved + (max(x,dX) - min(x,dx) + max(y,dY) - min(y,dY))
+ (1 if this is a turn from the last move)
This will cause you to "find" solutions which tend to go in the same direction.
If you want to FORCE as few turns as possible:
= distance moved + (max(x,dX) - min(x,dx) + max(y,dY) - min(y,dY))
+ (1 times the number of turns made)
This is what is nice about A* -- the heuristic will inform the search -- you will still always find a solution, but if there is more than one you can influence where you look first -- this makes it good for simulating AI behavior.
Doubt : How is the first one and second calculating way different from
each other?
The first one puts a lower priority on a move that is a turn. The second one puts a lower priority on a path with more turns. In some cases (eg, the first turn) the value will be the same, but over all the 2nd one will pick paths that have as few turns as possible, where the first one might not.
Also, 1 if this is a turn from the last move , for this,
say i have source at top left and destination at bottom right, now my
path normally would be, left,left,left...down,down,down.... Now, 1 if
this is a turn from the last move, according to this, when I change
from left to down, will I add 1?
Yes
Wont it make the total value more and the priority for down will decrease.
Yes, exactly. You want to not look at choices that have a turn in them first. This will make them lower priority and your algorithm will investigate other options with a higher priority -- exactly what you want.
Or 1 if this is a turn from the last move is when I move to a cell, that is not abutting the cell previously worked upon? Thnks –
No, I don't understand this question -- I don't think it makes sense in this context -- all moves have to abut the previous cell, diagonal moves are not allowed.
Though, I'd really appreciate if you could tell me one instance where the first and second methods will give different answers. If you could. Thanks alot. :) 
Not so easy without seeing the details of your algorithm but the following might work:
The red are blocks. The green is what I would expect the first one to do, it locally tries to find the least turn. The blue is the least turn solution. Note, how far the red areas are from each other and the details of how your algorithm influence if this will work. As I have it above -- having an extra turn only costs 1 in the heuristic. SO, if you want to be sure this will work change the heuristic like this:
= distance moved + (max(x,dX) - min(x,dx) + max(y,dY) - min(y,dY))
+ (25 times the number of turns made)
Where 25 is bigger than the distance to get past the 2nd turn in the green path. (Thus after the 2nd turn the blue path will be searched.)

WPF RichTextBox TextChanged event - how to find deleted or inserted text?

While creating a customized editor with RichTextBox, I've face the problem of finding deleted/inserted text with the provided information with TextChanged event.
The instance of TextChangedEventArgs has some useful data, but I guess it does not cover all the needs. Suppose a scenario which multiple paragraphs are inserted, and at the same time, the selected text (which itself spanned multiple paragraphs) has been deleted.
With the instance of TextChangedEventArgs, you have a collection of text changes, and each change only provides you with the number of removed or added symbols and the position of it.
The only solution I have in mind is, to keep a copy of document, and apply the given list of changes on it. But as the instances of TextChange only give us the number of inserted/removed symbols (and not the symbols), so we need to put some special symbol (for example, '?') to denote unknown symbols while we transform our original copy of document.
After applying all changes to the original copy of document, we can then compare it with the richtextbox's updated document and find the mappings between unknown symbols and the real ones. And finally, get what we want !!!
Anybody has tried this before? I need your suggestions on the whole strategy, and what you think about this approach.
Regards
It primarily depends on your use of the text changes. When the sequence includes both inserts and deletes it is theoretically impossible to know the details of each insert, since some of the symbols inserted may have subsequently been deleted. Therefore you have to choose what results you really want:
For some purposes you must to know the exact sequence of changes even if some of the inserted symbols must be left as "?".
For other purposes you must know exactly how the new text differs from the old but not the exact sequence in which the changes were made.
I will techniques to achieve each of these results. I have used both techniques in the past, so I know they are effective.
To get the exact sequence
This is more appropriate if you are implementing a history or undo log or searching for specific actions.
For these uses, the process you describe is probably best, with one possible change: Instead of "finding the mappings between the unknown symbols and the real ones", simply run the scan forward to find the text of each "Delete" then run it backward to find the text of each "Insert".
In other words:
Start with the initial text and process the changes in order. For each insert, insert '?' symbols. For each delete, remove the specified number of symbols and record them as the text deleted.
Start with the final text and process the changes in reverse order. For each delete, insert '?' symbols. For each insert, remove the specified number of symbols and record them as the text inserted.
When this is complete, all of your "Insert" and "Delete" change entries will have the associated text to the best of our knowledge, and any text that was inserted and immediately deleted will be '?' symbols.
To get the difference
This is more appropriate for revision marking or version comparison.
For these uses, simply use the text change information to compute a set of integer ranges in which changes might be found, then use a standard diff algorithm to find the actual changes. This tends to be very efficient in processing incremental changes but still gives you the best updates.
This is particularly nice when you paste in a replacement paragraph that is almost identical to the original: Using the text change information will indicate the whole paragraph is new, but using diff (ie. this technique) will mark only those symbol runs that are actually different.
The code for computing the change range is simple: Represent the change as four integers (oldstart, oldend, newstart, newend). Run through each change:
If changestart is before newstart, reduce newstart to changestart and reduce oldstart an equal amount
If changeend is after newend, increase newend to changeend and increase oldend an equal amount
Once this is done, extract range [oldstart, oldend] from the old document and the range [newstart, newend] from the new document, then use the standard diff algorithm to compare them.

Resources