-
Notifications
You must be signed in to change notification settings - Fork 0
Assignment 5 - code review #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
karineek
wants to merge
24
commits into
assignment5_base
Choose a base branch
from
master
base: assignment5_base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
1ed2e91
assignment 5, second commit
ce33dca
remove env. files
karineek a40d4e2
remove env. files
karineek 101c856
remove env. files
karineek 7d5e18a
remove env. files
karineek 32c04d4
remove env. files
karineek 637ef21
remove env. files
karineek 3875d6f
assignment 5, fix after code review
8b9c545
Merge origin/master
a35bdb9
assignment 5, fix build
95b497a
assignment 5, fix build - second try
dd83412
assignment 5, fix build - second try
9966ad7
brute force fix of commits
karineek e029e22
brute force fix of commits
karineek 9f039b5
brute force fix of commits
karineek c1d102c
brute force fix of commits
karineek 435aae2
assignment 5, fix error from code review
5094dfe
assignment 5, revert back to 1ed2e9114b64038435e525c76da636f13751803d
e927bb8
rename dict to alphabet
fd92daa
fix for recent code review
3921b2e
first commit, 6
defbadf
Basic working project for summit
edade50
code review's fix
a077a09
code review's fix
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
CodeU_Assignement_5/src/codeu_assignement_5/KemUnknownLanguage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| package codeu_assignement_5; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * | ||
| * @author Karine | ||
| */ | ||
| public class KemUnknownLanguage { | ||
| /* | ||
| * Input: a alphabetionary (a list of words in lexicographic order) of all words in an unknown/invented language, | ||
| * Output: alphabet (an ordered list of characters) of that language, letters we cannot decide are appended in the end. | ||
| */ | ||
| public List<Character> getAlphabeit(String[] alienWords) { | ||
| if (alienWords == null) { | ||
| return null; | ||
| } | ||
|
|
||
| List<Character> alphabet = null; | ||
|
|
||
| /* Get the current order by prefix size, starting from 0 */ | ||
| alphabet = inferAlphabetFromCommonPrefix(alienWords, 0); /* Basic Alphabeit */ | ||
| /* A,R,C from the example! */ | ||
|
|
||
| /* Add from prefix while there are letters after first letter of prefix */ | ||
| addToDictByPrefixSize(alienWords, alphabet); | ||
|
|
||
| /* Add all the chars we cannot decide in the end - According to the comments in Slack */ | ||
| for (String w : alienWords) { | ||
| for (Character c: w.toCharArray()) { | ||
| if (!alphabet.contains(c)) { | ||
| alphabet.add(c); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return alphabet; | ||
| } | ||
|
|
||
| /* | ||
| * Inputs: All words with the same prefix (all the first prefixSize chars are the same!) | ||
| * Output: Per common prefix, gets the order of the chars | ||
| * | ||
| * Process: Given an array of words that all match up to a common prefix length, infer an | ||
| * alphabetical ordering of the characters based on the order of the first differing character. | ||
| * No check is made that the common prefix really is the same across the list. | ||
| */ | ||
| private List<Character> inferAlphabetFromCommonPrefix(String[] words, int prefixSize) { | ||
| List<Character> ret = new ArrayList(); | ||
| for (String w : words) { | ||
| if ((w.length() > prefixSize) && (!ret.contains(w.charAt(prefixSize)))) { | ||
| ret.add(w.charAt(prefixSize)); | ||
| } | ||
| } | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| /* | ||
| * Input: the original set of words, current word we are working on, its prefix size | ||
| * Output: the set of the words with this prefix | ||
| */ | ||
| private String[] getWordsWithSamePrefix(String[] alienWords, int curr, int prefixSize) { | ||
| ArrayList<String> tempDict = new ArrayList(); | ||
| tempDict.add(alienWords[curr]); | ||
|
|
||
| String prefix = alienWords[curr].substring(0, prefixSize); | ||
|
|
||
| /* Forward search */ | ||
| for (int i=curr+1; i<alienWords.length; i++) { | ||
| if (!alienWords[i].startsWith(prefix)) { | ||
| break; | ||
| } | ||
| tempDict.add(alienWords[i]); | ||
| } | ||
|
|
||
| /* Backward search */ | ||
| for (int i=curr-1; i >= 0; i--) { | ||
| if (!alienWords[i].startsWith(prefix)) { | ||
| break; | ||
| } | ||
| tempDict.add(0,alienWords[i]); | ||
| } | ||
|
|
||
| return tempDict.toArray(new String[tempDict.size()]); | ||
| } | ||
|
|
||
| /* | ||
| * Get two alphabetionary with common latters and merge them into the first alphabet, | ||
| * if none, cannot know, add all these cases later on | ||
| * E.g., we have the current alphabet, A,T,C | ||
| * and withset of rules saying A,R,T to be the new alphabet: | ||
| * A,R,T,C | ||
| */ | ||
| private void mergeDict(List<Character> alphabet1, List<Character> alphabet2) { | ||
| int from2 = 0; /* Start from location 0 in alphabet1 */ | ||
|
|
||
| /* Pass on alphabet2 and merge it into alphabet1 */ | ||
| for (int i=0; i < alphabet2.size(); i++) { | ||
| /* Adds to the alphabet only to satisfy a rule, if not adds all in the end | ||
| (no rule, or only says "it comes after that symbol", | ||
| with no restriction of "but it also comes before the other symbol") */ | ||
| if (alphabet1.contains(alphabet2.get(i))) { | ||
| if ((i+1) < alphabet2.size() | ||
| && (alphabet1.contains(alphabet2.get(i+1))) | ||
| && (alphabet1.indexOf(alphabet2.get(i)) > alphabet1.indexOf(alphabet2.get(i+1)))) { | ||
| /* Need to fix the order */ | ||
| alphabet1.remove(alphabet2.get(i)); | ||
| alphabet1.add(alphabet1.indexOf(alphabet2.get(i+1)), alphabet2.get(i)); | ||
| } else { | ||
| /* Find common point, merge here! */ | ||
| /* Case where we have: */ | ||
| alphabet1.addAll(alphabet1.indexOf(alphabet2.get(i)), alphabet2.subList(from2, i)); | ||
| } | ||
| from2 = i+1; /* The next location in alphabet2 to start adding to in alphabet1 */ | ||
| } | ||
| } | ||
|
|
||
| /* For all the symbols in alphabet2 that only needs to come after, adds it to the end of alphabet1 */ | ||
| if (from2 <= (alphabet2.size()-1)) { | ||
| alphabet1.addAll(alphabet2.subList(from2, alphabet2.size())); /* Add the tail */ | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| For each length of prefix (1,2,3,4,....(i)), collects all the prefixes | ||
| and if found a new rule adds it to the dictionary | ||
| */ | ||
| private void addToDictByPrefixSize(String[] alienWords, List<Character> alphabet) { | ||
| /* For each size of prefix does the following: */ | ||
| for (int i=0; i < alienWords.length; i++) { /* For each word, do: */ | ||
| for (int j=1; j < alienWords[i].length(); j++) { /* For each prefix size */ | ||
| int prefixSize = j; | ||
| String[] sublistWords = getWordsWithSamePrefix(alienWords, i, prefixSize); | ||
| if (sublistWords.length > 1) { | ||
| List<Character> tempDict = inferAlphabetFromCommonPrefix(sublistWords, prefixSize); | ||
| if (!tempDict.isEmpty()) { | ||
| mergeDict(alphabet,tempDict); | ||
| } | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intention of this method is actually pretty clear to me, although I'd still prefer to have it explained in words, rather than via "Input: ... Output: ...".
"Output: the set of the words with this prefix" is mildly misleading - "set" implies a lack of ordering, while in this context order is very important. "Ordered list of words with this prefix" is less ambiguous.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It helps me to remember the technical part of it. But I added the whole description as what is the algorithm or process. Thanks!