Discord.js bot counting specific word per user - discord

I'm trying to make a bot that counts a specific word per user
My bot is in version 12 discord.js. I really don't know how to do this.

So, you are trying to count how many times the user used "x" word in his message. Here are the steps:
Get all the words the user typed into an array:
const args = message.content.slice("!").trim().split(/ +/);
Now, you have an array with all words the user has typed in his message. The "!" in the code is the prefix. You will have to ignore the prefix and not add that into the array.
Loop through the array to find the word
c = 0
args.forEach(word => {
if (word.includes("hi")) c++;
});
Here, c = 0 which increases whenever the word == "hi".

Related

Why won't my python code execute the loop to pick random key words?

I am creating code for a class project and part of it is that I need to find out if a set of randomly selected key words has a collective character length between two desired values. The issue I'm having is that Python stops executing as soon as it reaches the loop section.
here is the whole code, I am having trouble with the part after the second user input.
import random #allows us to generate random numbers elsewhere in the code
set1=[] # creates empty sets
print("type genPassword() to initiate the program")
def genPassword():
print("Answer the following questions in order. What is your first pet’s name? What is your favorite word? What was your first car? What city/town were you born in? What was the name of your first partner? dont forget to press enter after each word, no spaces.")
for c in range(0,5): #allows the user to add elements to the list, each element has to be on a seperate line
ele=input()
set1.append(ele)
print(set1)#displays the current set, currently used for debugging purposes, making sure the code works
minlen=int(input("what is the minimum length you would like the password to be?: "))
maxlen=int(input("what is the max length you would like your password to be? (must be more than the shortest word you input in previous section): "))
passlen=0
while minlen >= passlen >= maxlen:
set2=[] #empties set 2
amnt = random.randint (1,5) #selects a random number for the anount of keywords to use
for f in range(0,amnt):
keys=random.sample(set1,1) #selects a random key word
set2.append(keys) #adds word to set 2
print(set2)#shows the words it chose
set_amnt=len(set2) #how many words in the set
iteration=0
string_len=0
for i in range(0,set_amnt):
word_len=len(set2[iteration][0]) #takes the length of each word and adds it to the lenght of the whole string
iteration=iteration+1
string_len=string_len+word_len
print(string_len) #shows how long the string is (how many letters it is total)
passlen=string_len
I tried switching the loop types and adjusting where variables are in the code, but neither of those things worked and I cant think of what else to try to make it work. I even took the section out of a looping statement and it works then but it for some reason is having trouble with the loop. I expect it to select a random amount of words and tell me how long the whole string is and what words it picked out of a set of five words and then if the string is too long or too short it repicks and does it again and again until the string is within the accepted range of values for character length, printing the length and what words it chose each iteration.
Edit: just tidying up this answer, since its been changed a few times based on my discussion in comments with the OP
This is your problem:
minlen=int(input("what is the minimum length you would like the password to>
maxlen=int(input("what is the max length you would like your password to be>
passlen=0
print ("Entering while loop ", minlen, passlen, maxlen)
while minlen >= passlen >= maxlen:
The passlen is set to zero, in the line above the while - so the condition is never met, as shown in my example output:
['Happy', 'Blue', 'Citroen', 'Liverpool', 'Rachel']
what is the minimum length you would like the password to be?: 5
what is the max length you would like your password to be? (must be more than the shortest word you input in previous section): 19
Entering while loop 5 0 19
I think you are logically trying to say:
if maxlen is bigger than minlen and minlen is bigger than passlen, do the loop.
But what you are actually saying is
if passlen is bigger than minlen and if minlen is bigger than maxlen then do the loop`
so the while line condition becomes (with my sample data):
while 5 >= 0 >= 19:
# some code that will never get executed unless they enter 0 for minimum password length
Firstly, you should re-write the logic of the while condition test, so it does what you intend.
while maxlen >= minlen >= passlen:
Should do the trick.
If that is not what you are intending, then unless you completely refactor the while (and maybe even the for) loop, and what you initially wrote was intended - then the goal you are aiming for is to have the code executed at least once, because you are setting passlen to a meaningful value only the end of the loop, so you only get a meaningful value of passlen after the loop executes once.
When examining logic, I ask myself
Are you sure the logic of the condition statement is correct?
Have a think, what am I trying to achieve with the conditional block/loop,
does my condition test do what I intended to achieve that goal? and
is it better served a different way.
Finally, I would add an if before the while, to test user input. If the min len is bigger than the max len, tell the user they made a boo-boo and exit.
This executes fine, and also goes around the for loop (I think 5? times on the first pass):
minlen=int(input("what is the minimum length you would like the password to>
maxlen=int(input("what is the max length you would like your password to be>
passlen=0
if minlen >= maxlen:
print ("minimum length is greater than or equal to maximum length! Abort")
exit()
while maxlen >= minlen >= passlen:
# the rest of your code
Some stuff I did not want to delete but is no longer the answer
edit: My original answer (before I tidied it up) went off on a tangent with a bunch of waffle about implementing a do code...while test (a do foo until bar) loop for python, since there is no in-built one. I subsequently spotted the (in hindsight obvious) logic error and so don't necessarily need this tangent, but the text might be useful, so rather than delete it, I'm just plopping it here at the end:
How do we get one run of the loop before exiting? Instead of a while do while test do code, we need a do until do code while test which python does not have!
There is a fudge-y workaround to get do-until functionality into python, as detailed here, which goes along the lines of:
while true:
do code
if condition X:
break
This is checking the condition manually at the end of the loop, so that it always gets at least one pass. If you don't like the break, and I am not keen either, you could have
boolTest = true
while boolTest
# do code
if condition_to_test:
boolTest = false
where the boolTest being set to false will cause the while loop to terminate, without having to use a break (I don't know about you, but when I see break - especially if there are several - I instantly am reminded of BBC BASIC and my heroic use of goto as a child!

C# WinForm: Select specific words and change colour

I have a program that allows the user to enter text, and it will highlight any repeat words. It already adds repeated words to one list and all the words to another. I want the program to print out the words, and if a repeat word is used, to highlight it.
I have tried using outputBox.Find(repeatList[i]) with a loop, but this only finds the first word used in the text. I also tried marking the current number for the last letter typed, selecting that point, finding the coordinates after the word.Length was typed, and then changing the colour of that, which didn't work.
for (int h = 0; h < repeatList.Count; h++)
{
for (int c = 0; c < repeatList.Count; c++)
{
outputBox.Find(repeatList[h]);
outputBox.SelectionColor = Color.Red;
}
}
At this point in the code, the outputBox already contains the users input, I just want to know how to compare words and select them for colouring. I've only just started Winforms and have only been coding for a few weeks, so I am sorry - I have looked at other answers but was not able to implement them. Thank you in advance for any responses.
EDIT: I would just like to add that my prefered method for colouring the text would be as it prints each word out, this was my original intention as I'm much more used to console applications where I can just change the colour and print more. If that method is easier to do than checking afterwards, I'm find to do that.
I would use the start index and keep a copy of it.
int startFrom = 0
...
startFrom = outputBox.Find(repeatList[h], startFrom)
You could then use the 'startFrom' index with the text word length to select the text.
Here is an example:
var findText = "test";
int index = 0;
do
{
index = richTextBox1.Find(findText, index, RichTextBoxFinds.WholeWord);
if (index > -1)
{
richTextBox1.Select(index, findText.Length);
richTextBox1.SelectionColor = Color.Red;
index++;
}
} while (index > -1);

How can i check if a word is in dictionary taking the user input that inserted into the array

I've been trying to figure out, the best way to tackle this problem.
The problem Im stuck on is where I need to take the user input(word) and put it in the array and try to confirm/validate if all the letters they've use are in the char array(generates 10 random letters)(letterpool) then check again if the word they've use are valid from the dictionary.
I have a dictionary called "dict.txt" which contains 80k words in lowercase
dictionary. i need somehow take the input(uppercase) and be able to locate the word of the dictionary in lowercase
I hope you guys can help me, Programming language Java
Thanks in advance!
Eric
I'd create a HashMap<Character, Integer> and put the random letters in the map.
map.put(letter, 0);
Then I'd go through the letters from your word and do this
Integer value = map.get(letterFromYourWord);
if(value == null){
//raise exception because the letter from your word is not in your random array
} else {
// increment the value from the map
map.put(letterFromYourWord, value++);
}
then go through the map and check if the values are not 0. If there is one with value 0 then your word is not used in your random array.
with this implementation you can extend easily more functionalities, like counting the letters which are used in your word...
For the validWord function I would suggest making use of the binary search for sorted arrays. Something along those lines:
static boolean validWord(String word, final char[] letters)
{
char[] lettersCopy = letters.clone();
Arrays.sort(lettersCopy); // sort so we can use binary search
for(char c : word.toCharArray())
{
if(Arrays.binarySearch(lettersCopy, c) < 0) //char c from word not in letterPool?
{
return false;
}
}
return true;
}
Then in your TRIALS loop you would just call it like this:
if (validWord(input,letterPool))
{
System.out.println("Yes, the letters match");
}else {
System.out.println("No");
}
I am not sure which Dictionary class you are using there so I cannot help you with that.
Btw:
Arctigors answer using hashmaps is more cpu performant yet more heavy on the memory side. (O(n) instead of O(n*lb(m)))

Character x,y in password - LoadRunner

I'm doing a load test for a sign in page where the user needs to input 2 characters of their password.
I've created an array of characters to say 'password1'.
Using correlation parameters I'm able to get the character number required. What I'm now trying to do is get the character number and match the array i.e. -
Character 1 is required, it will scan the array and bring back char[0].
Character 2 is required, it will scan the array and bring back char[1] etc.
I was thinking of doing a for loop to go through the array and determine where in the array a character is stored. I can't think how to initiate this for loop:
char1 = (char1-1);
char2 = (char2-1);
for(i=0;i<10;i++){
lr_output_message("%s",p[i]);
if (p[i] == p[char1]){
char1 = p[i];
}
}
The for loop works but it equals 115 (s in ASCII), I need a way of converting the value to a character, but I keep getting memory violations.
Sorry if I've over-complicated this issue, but my head has been lost trying to think about how to solve a seemingly easy problem. No doubt some of you will look at it a different way and tell me I've over-complicated it a significant amount!
Closed - worked it out.
Using these buffers instead of for loop.
buf[0] = char1a;
buf[1] = ‘\0’;
buf2[0] = char2a;
buf2[1] = ‘\0’;

matlab divide sentences into words

Im new in matlab and ım trying to take the input from matlab gui which will be entered by a user and divide that sentence into words but I need to have them as letters because Im using a robot to write them. this letters will be send to these robots. Im using two robots and for example when I write 'lou reed' in text when ı press the button matlab function will hold this 2 words in to different char arrays so that ı can have the letters c(i) like this and send them to process. so far ı wrote these but ım stuck.
c = char(get(handles.edit1,'String'));
int count1;
int count2;
char word1;
char space=" ";
for i=1:length(c)
int t = isequal(c(i),space);
if(t==0)
count1=count1+1;
word1=;%ım trying to add the char here to find the new word
else
end
end
ı dont know what to do ı searched but ı couldnt find something usefull maybe ı wasnt looking right.
Anything would be helpful, thankss
What characters are allowed? First you should remove all the characters that are not allowed (substitute them with a space character?). After that just this:
str = ' Once upon a time ';
words_in_str = textscan(str,'%s');
words_in_str{1}
If you have a newer version of MATLAB (greater than 2012a I think), you can use strsplit
characterString = 'lou reed';
C = strsplit(characterString);
C will be a cell array with each element being a separate word.
You can simply find the space characters in your string with
mystring = 'Hello Cruel World';
spaces = find(mystring==' ');
The variable spaces is now a vector pointing to where each of your word breaks are. If you want to break this up into words, you could use
mystring = 'Hello Cruel World';
wordboundaries = [0,find(mystring==' ')];
wordlen = diff([wordboundaries,length(mystring)+1])-1;
numwords = length(wordboundaries);
for w = 1:numwords
idx = wordboundaries(w) + (1:wordlen(w));
word{w} = mystring(idx);
end
display(word);
Now word is a cell array containing the individual words.

Resources