Randomize characters in string? - string

unfortunately I am struggling a bit with what I thought would be an easy task.
I am writing an autocorrect in AHK for words I frequently mistype, and instead of writing every possible way to write every word incorrectly I have written a list with words I often mistype in their correct form. I now want to take each item from that list, leave the first character as is and randomize two consectutive letters in every possible way with the exclusion "is already in list".
So in pseudo code it would be:
For each word in correctWords
{
FirstLetter = split to chararray(0)
newWord = split to chararray(>0)
randomized = Firstletter + newWord.randomizeTwoLetters
if(!correctWords.Contains(randomized); correctWords.AddToList(randomized))
}
The part I struggle with is obviously the randomizeTwoLetters(), how would you go about that?
I hope you can help me, thank you!

Pulled from the AutoHotkey Help File:
AutoCorrect 4700 Common Words
If you still want to do this yourself (in C#?) look into various algorithms like: Generating a random sequence with no repeats for starters.

Related

Using the len() function in python I am somehow getting the wrong length

So I am learning to code and I am an absolute beginner here. Beginner as in I've been coding for maybe like 5 hours total. Not including the 4.5 hour video I watched at 1.5 times speed for my intro to python.
I am trying to make a hangman game and was messing around with it. I have a separate file full of words that the game randomly draws from, and then the user tries to guess it. My problem is that after selecting the "hidden_word" if I use len(hidden_word) and it gives me the wrong length of the word.
They are all single words, and in the text file there are no spaces before or after any of the words. Each word is on a new line, and every word selected has a length of 1 greater than it should be. For instance, the word Jinx, apparently has 5 letters.
The file is literally just this list, but 45 lines.
Awkward
Bagpipes
Banjo
Bungler
...
My code:
from random import randint
# open words file and choose a hidden word
dictionary = open("words.txt", "r")
words = dictionary.readlines()
hidden_word = words[randint(0, 45)]
dictionary.close()
print(hidden_word)
It always gives me a length of 1 longer than it should be.
Thank you for the help.
As #jonrsharpe pointed out in the comments, the newline character \n counts as a character, and that newline character is included in each item of .readlines().
To accurately get the length, you're going to want to strip the whitespace (spaces and newlines, etc.) before trying to find the length:
hidden_word = hidden_word.strip()
print(len(hidden_word))

Function that sorts words by punctuation in excel

I have a task to create reversed alphabetized list in excel. I thought it was easy to do, created a function to write words from behind and sorted list by that. It would work... if my language was English. But my language is Slovak, which uses bunch of characters with punctuation like á, ä, ô, š etc. And syllables containing these letters should be grouped. For example words strany, hrany, planý, plány, vraný, vrany should be sorted in order hrany, strany, vrany, plány, planý, vraný. Instead of, these words are sorted in order plány,
planý,
hrany,
strany,
vrany,
vraný.
I thought that switching language is enough, but seems all collates sort this way. I have tried to switch from ISO 8859-2 to unicode and several other encodings, but it didn't make a change as well.
So my question is, is there any encoding+locale setting in windows 10 that will do it? And if not, is it possible to do it through VBA function?
Thanks for any idea.
I have solved this problem by myself with pretty simple solution:
1, get hex codes of the characters
2, translate them into unique code containing only ascii chars (a = aa, á = ab...)
3, sort this translated row

Excel function to get chars between hyphens

I have a string like:
AB-CD-EF-GH-IK
I wanna get EF between second and third hyphen.
Please help me to figure it out, Thanks
Even shorter is:
=TRIM(MID(SUBSTITUTE(A1,"-",REPT(" ",LEN(A1))),2*LEN(A1),LEN(A1)))
Regards
This will work with varying lengths of strings between the dashes. Doesn't look pretty but works.
=LEFT(REPLACE(REPLACE(A1,1,FIND("-",A1),""),1,FIND("-",REPLACE(A1,1,FIND("-",A1),"")),""),FIND("-",REPLACE(REPLACE(A1,1,FIND("-",A1),""),1,FIND("-",REPLACE(A1,1,FIND("-",A1),"")),""))-1)
Not because this is the right approach, but because shorter than (what was at the time!) the accepted Answer:
=MID(A6,FIND("-",A6,FIND("-",A6)+1)+1,FIND("-",A6,FIND("-",A6,FIND("-",A6)+1)+1)-FIND("-",A6,FIND("-",A6)+1)-1)
A small point in its favour may be that it uses only two common-or-garden functions:
MID to extract the string
FIND to find the index numbers of the relevant characters.

String compare : Comparing 'zürich' and 'zurich' results in -1

I'm trying to do a string compare for 'zürich' and 'zurich'
Something like this:
int compareResult = String.Compare(zürich, zurich);
So what happens is that it returns -1, which causes a problem as I'm using compareResult for an if-else later.
Can someone point me to the right direction on why does this happen. Do I need to clean this first before comparing "zürich" or is it something else?
you use the method just fine, but the strings are actually different.
so, in order to make this comparison in your way, you need:
decide if this you want every comparison that uses ü and other "special" latin characters to look at them as they were the simple characters.
i.e. in every time you see ü, it will treat it as a "u"
if so, you need to do pre-processing of both the strings, and replace all special chars with regular ones.
there is another thread about it here:
How can I remove accents on a string?
hope it helped.

Complex replacement in gVim

I have been a terrible person as of late when it comes to Minecraft. I have over-modded it to the point that I need to completely re-write the IDs of them all.
The only problem is that... It'll take about a couple of hours jut to re-write them ONCE, not to mention if any of them collide with the original game. So, in order to save time, I figured I'd use Vim, but after reading through several of the helpful posts on here, I still only know a minimal amount about the replacement feature/command. Here's what I'm trying to do:
Replace this
I:exampleModnamePath.id=16389
I:exampleModnamePat2.id=19657
Etc.
With this
I:exampleModnamePath.id=20000
I:exampleModnamePath.id=20001
Etc.
This continues for a while, and to those who answer, could you please inform me of how it works, so I don't have to ask these questions all the time?
For your perusal:
:let g:num = 1
:g/\.id=\d\+$/exec 's!\.id=\d\+$!.id='.g:num.'! | let g:num=g:num+1'
This is slightly simplified version of my code for (re)numbering chapters in the ebooks.
Idea in a nutshell: use :g to run something over affected lines; use :exec to generate/run new substitution command AND increment the counter. Tried it once and was surprised to find that the trick worked. Was inspired by my previous toying with :g//s/// combo.
I'm not sure what is the rule you are using to choose which number to use for replacement but if all you need
is just a new number that doesn't collide with previous ones you could try just replacing the first digit
with something in a range not used. Something like replacing 16389 with 76389
To do that you could use this :s/Path.id=.\(.*\)/Path.id=7\1
That would search for the string Path.id= followed by a single character and then a group of more characters.
I will replace it with the string Path.id=7 and the group previously selected.
You could make it more selectiv adding letters before Path.id to match only certain types of paths.

Resources