Comparator and Arraylist - sorting words - arraylist

I am working on a scrabble assignment. We have to assign values to each word and then put them in order of point value. We will read the words from a text file. So I'm thinking to create an ArrayList to store each word. My question is, how do I read each character and assign a value to it? I believe I will use a for loop. I'm just confused when I start working with char... I think that's what this will be.
Once I figure out how to give a score to each word, do I then use the comparator interface to sort them?
EDIT: I realized maybe I didn't make this clear. Each letter is given a value from the Scrabble game. So I have to read each letter of each word and assign a value that will add up to a total score for the word. Like the word QUIT would be 10 + 1 + 1 + 1 = 13.

What you could do is to add all words as keys in a hash table, initiating them all with a value of 0. Then you loop through the keys and use a for loop to check the value of each character of each word, adding the word’s sum value as the value for that word’s key in the hash table.
The for loop for each word would look something like this:
int sumValue = 0;
for(int i =0; i < word.length(); i++)}
sumValue += value(word.charAt(i))
}
The return value() is just my way to represent that you return the value of the specific character you are at. Of course this depends on which character it is. Maybe it would be wise to keep another hash table with each character associated with a scrabble value that you access in the loop (like A:1, X:8 etc).

Related

Freemarker: Removing Items from One Sequence from Another Sequence

This may be something really simple, but I couldn't figure it out and been trying to find an example online to no avail. I'm basically trying to remove items found in one sequence from another sequence.
Example #1
Items added to the cart is in one sequence; items removed from cart is in another sequence:
<#assign Added_Items_to_Cart = "AAAA,BBBB,CCCC,DDDD,EEEE,FFFF">
<#assign Deleted_Items_from_Cart = "BBBB,DDDD">
The result I'm looking for is: AAAA,CCCC,EEEE,FFFF
Example #2
What if the all items added to and deleted from cart are in the same sequence?
<#assign Cart_Activity = "AAAA,BBBB,BBBB,CCCC,DDDD,EEEE,DDDD,FFFF,Add,Add,Delete,Add,Add,Add,Delete,Add">
The result I'm looking for is the same: AAAA,CCCC,EEEE,FFFF
First things first: You ask about sequence but the data you are dealing with are strings.
I know you are using the string to work as a sequence (and it works), but sequences are sequences and strings are strings, and they have diferente ways of dealing with. I just felt this was important to clarify if someone who is starting to learn how to program get to this answer.
Some assumptions since you're providing strings with data separated by comma:
You want a string with data separated by comma as a result.
You know how to properly create strings with data separated by comma.
You dont have commas in your items names.
Observations:
I'll give you the logic but not the code donne, as this can be a great chance for you to learn/practice freemarker (stackoverflow spirit, you know...)
You question is not about something specific of freemaker (it just happens to be the language you want to work with). Think about adding the logic tag to you question. :-)
Now to the answer on how to do what you want on a "string that is working as a sequence":
Example #1
Change your string to a real sequence :-)
1 - Use a built-in to split your string on commas. Do it for both Added_Items_to_Cart and Deleted_Items_from_Cart. Now you have two real sequences to work with.
2 - Create a new string tha twill be your result .
3 - Iterate over the sequence of added itens.
4 - For each item of the added list, you will check if the deleted list also contains this item.
4.1 - If the deleted list contains the item you do nothing.
4.2 - If the deleted list do not contains the item, you add that item to your string result
At the end of this nested iteration (thats another hint) you should get the result you're looking for.
Example #2
There are many ways of doing it and i'll just share the one that pops out of my mind right now.
I think it's noteworthy that in this approach you will always have an even sized list, as you always insert 2 infos each time: item and action.
So always the first half will be the 'item list' and the second half will be the 'action list'.
1 - Change that string to a sequence (yes, like on the other example).
2 - Get half of its size (in your example size = 16 so half of it is 8)
3 - Iterate over a range from 0 to half-1 (in your example 0 to 7)
4 - At each iteration you'll have a number. Lets call it num (yes I'm very creative):
4.1 - If at the position num + half you have the word "Add" you add the item of position num in your result string
4.2 - If at the position num + half you have the word "Delete" you remove the item of position num from your result string
And for the grand finale, some really usefull links that will help you in your freemarker life forever!!!
All built-ins from freemarker:
https://freemarker.apache.org/docs/ref_builtins.html
All directives from freemarker:
https://freemarker.apache.org/docs/ref_directive_alphaidx.html
Freemarekr cheatsheet :
https://freemarker.apache.org/docs/dgui_template_exp.html#exp_cheatsheet

Livecode: How do I program a button to create unique variables?

I apologize if this has been asked before (I couldn't find anything).
I'm an extreme noob in Livecode, and I want to know if there is a way of programming a button to create many new, unique variables and assign a value to them. I apologize if this is a dumb question.
Usually you use an array for that. An array is basically a list of things, where each thing is associated with an "index". An index can be any word, so you can use an array like a dictionary, where you'd e.g. have French words as the index, and English words as the value, like:
put "cow" into myDictionary["vache"]
But you can also just use numbers as the keys and make them a numbered list:
put "cow" into allMyAnimals[1]
put "duck" into allMyAnimals[2]
In end effect, you create one variable and put several things in it. For example if you had a loop that calculated something (in this example a number +100) and you wanted to have variables containing all those numbers, but named with 100 less, you'd do something like:
repeat with x = 1 to 250
put x +100 into twoHundredFiftyNumbersFrom101[x]
end repeat
And to read the first one:
answer "the first number is" && twoHundredFiftyNumbersFrom101[1]
Or all of them:
repeat with x = 1 to 250
answer twoHundredFiftyNumbersFrom101[x]
end repeat
Or whatever. You could also use 'do' to build the lines of code as a string, but then you have to make sure your variable names are generated in a fashion that makes them valid identifiers (e.g. have no spaces in them, no special characters). An array key can be any valid string, and the compiler can optimize them, and you can treat them as a whole and pass them between handlers.
Or you can do this "in the clear" with a "do" construction:
on mouseUp
repeat with y = 1 to 10
get random(100)
do "put it into onTheFlyVariable" & y
end repeat
end mouseUp
Step through this handler and watch the variables assemble themselves.

Comparing NSString to NSTextView Range prior to Appending

Coding in Objective-C, I'm appending text to a NSTextView object named subCap in my code like so:
[[[_subCAP textStorage] mutableString]appendString:[NSString stringWithFormat:#"%#", subcapLine]];
subcapLine will have two timecode values such as: "01:00:00:00 01:00:01:00" separated by a single space, then a newline (\n) character, then a string like "ONC314_001_001" followed by two newline chars (\n\n).
The end result will create a list similar to:
01:00:00:00 01:00:01:00
ONC314_001_001
01:00:01:00 01:00:02:00
ONC314_001_002
01:00:02:00 01:00:03:00
ONC314_001_003
etc, etc, etc.
It's a sub caption file for placing text (the ONC314 lines) at appropriate times in a video file, as indicated by the timecodes.
However, I've determined that there is an odd set of circumstances where a timecode pair could be the same as the previous timecode pair, and if that happens, I want to skip appending that line.
So, my question is, given that the timecodes are always 11 chars apiece, separated by a space, can anybody think of a way I can easily grab the prior TC pair and compare it to my current pair in the subcapLine I'm preparing to append? The problem is the text of the sub caption could be random lengths. In my example they're the same, but that isn't always the case.
If I need to check prior to compiling my subcapLine, I can do that too, but I just thought it might be more slick to use a range of some sort to grab the prior pair of TCs from the last-written line in the NSTextView object and compare (again, using a range?) against the TCs in the line I'm about to append?
Thoughts and suggestions much appreciated.
Chris Conlee
When you add a timecode store the length of the text field string just before you add the timecode so you will have the offset to the timecode you are about to add.
Then before adding a new timecode you could simply use the previous offset you stored to extract the substring and do a string comparison and see if the timecodes are identical.
This should allow you to always have an offset to the previous timecode regardless of the length of the subtitles.

How to create a labeled incremental reference in MS Word?

I want to do cross-references manually in microsoft word 2010 (similarly to latex \label - \ref or \cite - \bibliography). I found that the Field function does almost excactly what I want (the syntax is a bit weird). If I type the following to the document (wave brackets are field marks produced by ctrl+f9):
{set dischargeRate {seq Figure}}Figure {ref dischargeRate}: Discharge
rate vs. hole diameter. Figure is from Reference [{ref authorA}].
The results are shown in Figure {ref dischargeRate} and published
previously in [{ref authorB}] and [{ref authorA}].
References:
{set authorA {seq cites}}[{ref authorA}] author, title, journal, year
{set authorB {seq cites}}[{ref authorB}] author, title, journal, year
the above produces:
Figure 1: Discharge
rate vs. hole diameter. Figure is from Reference [1].
The results are shown in Figure 1 and published
previously in [2] and [1].
References:
[1] author, title, journal, year
[2] author, title, journal, year
Is there a way to define increment and reference with one command instead of those three commands: set, seq and ref? Or how do I create a macro that does this for me. I am looking something like {setOrRef sequencename labelname} that shows a number i+1 that can be later referenced by {setOrRef sequencename labelname}.
Also there should be a check that labels are not redefined. For example: If the label does not exist, the sequence (Figure or cites) is incremented by one and that number is assigned to the label. If the label exists the existing number for the label is used.
I consider this as a programming question as it so close to macros and automating Ms Word.
If you want to use field codes and nothing else, I don't think there is a practical way to avoid using SEQ, SET and REF.
There are a number of difficulties detecting the existence/non-existence of bookmarks. I think you can sidestep them using the following set of nested fields, but I cannot say I have tried this "for real". Personally, I would try to avoid this kind of complexity in field coding.
All you need to do is insert the bookmark name you want once in the QUOTE field, i.e. by substituting the "bookmark" name you want instead of "bm". Here, I use "s" as the name that provides sequential reference numbers. It isn't actually a bookmark name, by the way, but a SEQ name.
As usual, all the {} have to be the special field code braces that you can insert using ctrl-F9 on Windows Word. You will still need to be careful about your naming of references, e.g. don't use "AuthorA" and "AuthorA1". You will need to avoid using any other SEQ names such as AuthorA2, and avoid using SEQ names s and s1 elsewhere.
{QUOTE {SET b bm}{SEQ "{b}{SEQ {b}}" \r{SEQ "s{={SEQ {b}}-3 \#"1'';''"}}" \h}{SEQ "{b}1" \c}}
Since it's not obvious how this works, I'll step through. Suppose you name your reference "AuthorA". Then you would insert
{QUOTE {SET b AuthorA}{SEQ "{b}{SEQ {b}}" \r{SEQ "s{={SEQ {b}}-3 \#"1'';''"}} \h}{SEQ "{b}1" \c}}
Bookmark "b" is set to "AuthorA"
{SEQ "{b}{SEQ {b}}"}
expands to
{SEQ "AuthorA{SEQ AuthorA}"}
In the first such set of fields for AuthorA , this expands to
{SEQ "AuthorA1" }
In subsequent sets, it expands to
{SEQ "AuthorA2" }
and so on.
The final field (the one actually inserted by the QUOTE)
{SEQ "{b}1" \c}
expands to
{SEQ "AuthorA1" \c}
i.e. it is always the value returned by the first set of fields in the document for AuthorA.
The value for AuthorA1 is set by
\r{SEQ "s{={SEQ {b}}-3 \#"1'';''"}}
{SEQ {b}}
expands to
{SEQ AuthorA}
which will actually be 2 in the very first set of fields for AuthorA (this is one of Word's SEQ field evaluation quirks). So
{={SEQ AuthorA}-3}
will be -1 and the numeric switch
\#"1'';''"
will return ''. So AuthorA1 is set to
\r{SEQ "s"}
i.e. the next value of the sequence s.
Subsequently,
{={SEQ AuthorA}-3}
is greater than 0, the numeric switch returns '1' and so AuthorA2 will be set to
\r{SEQ "s1"}
But we don't care what AuthorA2 is set to. The purpose of this bit of numeric formatting is to ensure that { SEQ s } only increments the first time time we insert one of these sets of fields for a particular "reference name".
For inserting such stuff, you could create an autotext/building block that would insert the block. All you would need to do is change "bm". A piece of VBA to prompt for a bookmark name and insert the same block would not be hard, but I leave you to look around for that.
As always, even using the general technique suggested, there may be a way to simplify the fields. Personally, I think I would go for a design that let me insert some kind of placeholder and required me to run some "pre-publishing VBA" to set them up correctly.

Store an NSString as a fixed length integer?

having a bit of trouble finding a solution to this.
I want to take a large ordered text file of words and create - in the same order - a text file of fixed length numeric values.
For example:
Input File Output File
AAA -> 00000001
AAH -> 00002718
AAZ -> 71827651
Initially it seemed a hash function would do the trick. However they are one way. Also perhaps they are a bit "heavyweight" for this. After all, I don't need any cryptography. Plus, it's a reference file. It will never change.
Any compression is a bonus not essential. That said, I don't want the file to get any bigger than it already is. Which is why I don't just want to write out the words as text but with fixed lengths.
So, bottom line; input is a NSString of variable length, output is an integer of fixed length. And, I must be able to take the integer and figure out the string.
Any help much appreciated!
Thanks!
xj
Well, this would be a bit of a brute force method, but here's my guess.
Start by making a custom function to convert one letter of text to an integer less than 100. (I'm not sure if such a function already exists, if so then great!) You might need to just go to stuff like "if ([input isEqual: #"a"]){ return 1;}
Then, run that function on each letter of text, and get the final integer by combining the previous results.
For example:
int myVal1 = [intConverter firstLetter];
int myVal2 = [intConverter secondLetter];
int myVal3 = [intConverter thirdLetter];
int finalValue =100^3 + 100^2*myVal1 + 100*myVal2 + myVal3;
Then, finalValue would be of the form 1(myVal1)(myVal2)(myVal3), which is what I think you're looking for.
To get back the original string, simply use the mod (%) and division functions to get the individual values back, then run the intConverter function backwards. (This would probably mean writing a new function that basically runs those if statements in reverse, but oh well.)
I hope this helps.