I am just starting to learn programming. I had already coded some functions and stuff in other languages, and whilst trying to complete this Python challenge Kevin and Stuart want to play the 'The Minion Game'.
Game Rules
Both players are given the same string. Both players have to make substrings using the letters of the string.
- Stuart has to make words starting with consonants.
- Kevin has to make words starting with vowels.
- The game ends when both players have made all possible substrings.
Scoring A player gets +1 point for each occurrence of the substring in the string.
For Example:
- String = BANANA
- Kevin's vowel beginning word = ANA here, ANA occurs twice in BANANA. Hence, Kevin will get 2 Points.
For better understanding, see this image.
I have not yet implemented the feature that multiple occurrences of substrings yield more points.
I switched over to Java.
I initially wanted to start learning Python, but the company I just started an apprenticeship in almost exclusively uses Java, so I figured I might as well learn that language.
I tried completing it by mostly using trial and error and some google, I did not watch a lot of guides so far, so figuring out what I need to code in what way took me quite a few hours. Now my code basically does what it is supposed to do, but I feel like it is written unnecessarily complicated and could be much shorter.
Since I have not really understood scope and accessibility of methods yet, I was not able to reuse code as I would have liked to and needed to rewrite a lot.
public static void main(String[] args)
{
System.out.println("Please enter a word");
Scanner consoleInput = new Scanner(System.in);
String userInput = consoleInput.nextLine();
System.out.println(GetVowels(userInput).size() + " words starting with wovels: " + GetVowels(userInput));
System.out.println(GetConsonants(userInput).size() + " words starting with consonants: " + GetConsonants(userInput));
}
static ArrayList<String> GetVowels(String userInput){
char[] vowels = "aeiou".toCharArray();
ArrayList<String> possibleWords = new ArrayList<String>();
for(int i = 0; i < vowels.length; i++){
int offset = 0;
while(userInput.indexOf(vowels[i], offset) >= 0){
int wordStartingIndex = userInput.indexOf(vowels[i], offset);
for (int u = wordStartingIndex; u <= userInput.length(); u++)
{
String maybeAWord = userInput.substring(wordStartingIndex, u);
if(maybeAWord.length() > 0 && possibleWords.indexOf(maybeAWord) < 0)
{
possibleWords.add(maybeAWord);
}
}
offset = userInput.indexOf(vowels[i], offset) +1;
}
}
return possibleWords;
}
static ArrayList<String> GetConsonants(String userInput){
char[] vowels = "aeiou".toCharArray();
ArrayList<String> possibleWords = new ArrayList<String>();
ArrayList<Integer> vowelIndexes = new ArrayList<Integer>();
ArrayList<Integer> consonantIndexes = new ArrayList<Integer>();
for(int i = 0; i < vowels.length; i++) {
int offset = 0;
while (userInput.indexOf(vowels[i], offset) >= 0)
{
vowelIndexes.add(userInput.indexOf(vowels[i], offset));
offset = userInput.indexOf(vowels[i], offset) + 1;
}
}
for(int i = 0; i < userInput.length(); i++)
{
if(vowelIndexes.indexOf(i) < 0)
{
int wordStartingIndex = i;
int offset = wordStartingIndex;
while(offset <= userInput.length())
{
String maybeAWord = userInput.substring(wordStartingIndex, offset);
if(maybeAWord.length() > 0 && possibleWords.indexOf(maybeAWord) < 0)
{
possibleWords.add(maybeAWord);
}
offset = offset + 1;
}
}
}
return possibleWords;
}
I absolutely do not expect anyone to rewrite part of my code in a more compact or efficient way, but I would be very grateful for some tips or hints on what I could write differently, how I might be able to reuse some of the code that I had to write multiple times, or just things that I should avoid in the future.