I'm currently trying to create an app with Flutter that helps a user memorize French verbs. Here's a snippet of my code:
class Quiz {
List<String> questionList;
List<String> answerList;
Quiz(List questionList, List answerList){
this.questionList = questionList;
this.answerList = answerList;
}
}
As you can see, I've created a Quiz class that takes in two parameters: a list of test questions, and a list of their corresponding answers.
I then define a function that actually returns a 'Quiz' object. Here it is:
Quiz quizGenerator (questions, answers) {
final Set<int> indexS = Set();
for (int i = 0; i <= 9; i++){
indexS.add(Random().nextInt(49));
}
List<String> testQ;
List<String> testA;
List<int> indexes = indexS.toList();
for (int i = 0; i <= 9; i++){
var element = indexes[i];
testQ[i] = questions[element];
testA[i] = answers[element];
}
var newTest = Quiz(testQ, testA);
return newTest;
}
I wanted this function to return a 'Quiz' object that consisted of ten verbs to test that were randomly picked from a larger list of fifty verbs called "questions", as well as their corresponding answers from the larger list "answers". Since I was planning to pick the test questions by generating random index numbers, and I didn't want verbs to repeat in a test, I created a set of integers called 'indexS', and populated it with ten random numbers in the range of 0-49. This set essentially contained the index numbers of the ten questions that would be asked from the larger "questions" list.
I then converted the set to a list of integers called "indexes". I also created two other lists called "testQ" and "testA" that would actually contain the ten test questions and their corresponding answers respectively. These two lists would be the 'Quiz' object's properties. From there, I used a for loop to fill 'testQ' and 'testA' with the ten test questions and their answers by using the random index numbers stored in the list 'indexes'. The end result of all this was that a 'Quiz' object was returned by 'quizGenerator'.
However, I'm getting the following error when I try to call 'quizGenerator':
The following NoSuchMethodError was thrown while handling a gesture:
The method '[]=' was called on null.
Receiver: null
Tried calling: []=(0, "to open")
I apologize if the answer to this problem is obvious, but I've barely just started programming, and I've scoured the internet for days without much luck. Let me know if something is unclear or if you'd need anything else to better answer my question. Thank you!
Edit:
Here are the declarations for "questions" and "answers":
List<String> answers = ['rentrer', 'vivre', 'rendre', 'tenir', 'oublier', 'travailler', 'manger', 'entrer', 'devenir', 'commencer', 'payer', 'tirer', 'ouvrir', 'changer', 'excuser', 'dormir', 'occuper', 'marcher', 'envoyer', 'apprendre', 'boire', 'garder', 'montrer', 'asseoir', 'porter', 'prier', 'servir', 'écrire', 'retrouver', 'gagner', 'acheter', 'rappeler', 'lire', 'monter', 'quitter', 'emmener', 'toucher', 'continuer', 'raconter', 'repondre', 'sauver', 'rencontrer', 'fermer', 'valoir', 'compter', 'bouger', 'apporter', 'décider', 'vendre', 'expliquer',];
List<String> questions = ['to bring in','to live','to return','to hold','to forget','to work','to eat','to go in','to become','to start','to pay','to pull','to open','to change','to forgive','to sleep','to occupy','to walk','to send','to learn','to drink','to look after','to show','to sit down','to carry','to pray','to serve','to write','to find', 'to win', 'to buy', 'to remind', 'to read', 'to go up', 'to leave', 'to take', 'to touch', 'to continue', 'to tell', 'to answer', 'to save', 'to meet', 'to close', 'to hold', 'to count', 'to move', 'to bring', 'to decide', 'to sell', 'to explain',];
testQ & testA are not initialized. Initialize them with an empty list.
List<String> testQ = [], testA = [];
Use add method to add data to the list.
testQ.add(questions[element]);
testA.add(answers[element]);
If some of random numbers are same then set would only store unique ones. Don't use the Set and directly use the List or use i <= _variable.length instead of i <= 9 for the condition where variable could be some list or set.
Related
I have a list object type and I want to sort it by date in ascending order.
First of all I get the resevations that are between these dates and saving it to a new List.
Now I need someway to sort it.
I tried Collections.sort(reservationsByDate) & Collections.sort(reservationsByDate, Collections.reverseSort() , but it didn't produce anything.
I'm kinda new to java so if theres something that im missing please help me implement this.
heres my code:
public List<Reservation> getAllReservationsSortedByDate(LocalDate from, LocalDate to) {
int fromDate = from.getDayOfMonth();
int toDate = to.getDayOfMonth();
ArrayList<Reservation> reservationsByDate = new ArrayList<>();
for (Reservation reservation : reservations) {
if (reservation.getFromDate().getDayOfMonth() >= fromDate && reservation.getToDate().getDayOfMonth() <= toDate) {
reservationsByDate.add(reservation);
}
}
//reservationsByDate needs to be sorted by dates in ascending order...
return reservationsByDate;
}
Thank you for your help.
You are iterating over "reservations". The definition of this field is not shown. If it is empty the result would always be an empty list.
I am working on a vocabulary quiz app (French to English / English to French).
I need to check if what user types is what is expected.
Example : "Une machine à laver" --> Expected answer is "A washing machine".
The user can make many different sorts of mistakes such as spelling : "A watching machine", a word order mistake : "A machine washing" or a total mistake "A garden tool".
I have managed to deal with the checking when the expected word is just one word : "un jardin : a garden".
But with compound words, the issue of words order comes up.
I split the Strings into two lists :
_expectedAnswer contains the different elements of the answer : [washing,machine]
_typedAnswer contains the different elements of what the user typed : [machine, washing] or [watching,machine] or [washing,machine], or [a,washing,machine] (Here, the user added an article before the noun, which shouldn't be seen as a mistake).
At the end, the algorithm must tell the user what type of mistakes he has done :
Word order mistake.
Word order is correct, but one or all elements contain spelling problems.
word order mistake + spelling problems
completely wrong.
For the spelling check I use the levenstein algorithm. And it is satisfactory.
So first I thought I should check if _typedAnswer contains all the elements of _expectedAnswer.
-> Should check if the sequence of elements is the same : the order is OK, and no spelling pb.
-> Elements are all present but sequence is not respected : Problem with word order, no spelling mistakes.
-> Elements are not present
--> Then we check if "similar elements" are present (which would indicate user made a spelling mistake) .... and check the order of elements too...
Any advice to help me work out this algorithm ?
I have read a lot about all the functions linked to dart lists, but I have to admit, that I kinda got lost on which one would be pertinent to use...
I took the the last hour to solve your problem. I made it easy for you to test it and understand what is the logic behind it.
In order to use it in your code you have to put the control-flow statements into a separate function and pass the list of user input elements as well as the list of expected result elements to it.
For BOTH lists you need each word as a String, which is trimmed from all whitespaces!
I believe you are able to do that part, as described in your question already. I would deeply appreciate if you can upvote for my effort and accept the answer, if it helped you out.
EDIT: Requires an official dart package on pub.dev
Add a line like this to your package's pubspec.yaml:
dependencies:
collection: ^1.15.0
Here is the logic, please copy & test in inside DartPad:
import 'package:collection/collection.dart';
void main() {
List expectedAnswer = ["one", "two", "three"];
List listWrongSpelling = ["oe", "two", "three"];
List listWrongSpellings = ["oe", "twoo", "three"];
List listWrongOrder = ["two", "one", "three"];
List listEntirelyWrong = ["dskfrm", "twhoo", "111", "tedsf"];
List listIdentical = ["one", "two", "three"];
// FYI: Checks if there is any kind of mistake (is used below dynamically)
Function eq = const ListEquality().equals;
final result1 = eq(expectedAnswer, listWrongSpelling); // false
final result2 = eq(expectedAnswer, listWrongSpellings); // false
final result3 = eq(expectedAnswer, listWrongOrder); // false
final result4 = eq(expectedAnswer, listEntirelyWrong); // false
final result5 = eq(expectedAnswer, listIdentical); // true, the perfect answer
// print(result1);
// print(result2);
// print(result3);
// print(result4);
// print(result5);
// CHECK IF ANSWER IS PERFECT:
bool isPerfect(List toBeChecked, List expectedResult) {
Function eq = const ListEquality().equals;
return eq(toBeChecked, expectedResult) ? true : false;
}
// ONLY EXECUTE OTHERS IF THERE IS AN MISTAKE:
// Checks for word order mistake
// Condition: Must contain each element with identical value, hence only order can be wrong
bool checkOrder(List toBeChecked, List expectedElements) {
List<bool> listOfResults = [];
for (var element in toBeChecked)
{
bool result = expectedElements.contains(element);
listOfResults.add(result);
}
// If one element is not in expectedElements return false
return listOfResults.contains(false) ? false : true;
}
// Checks for any spelling errors
bool checkSpelling(List toBeChecked, List expectedElements) {
// Once this function gets executed there are only two errors possible:
// 1st: Unexpected elements (e.g. an article) (and possibly spelling errors) >> return false
// 2nd: No unexpected elements BUT spelling errors >> return true
return toBeChecked.length == expectedElements.length ? true : false;
}
// FINAL RESULT
String finalResult = "";
// Please try out the other lists from above for all possible cases!
bool isPerfectAnswer = isPerfect(listIdentical, expectedAnswer);
bool isWordOrderIncorrect = checkOrder(listIdentical, expectedAnswer);
bool isSpellingIncorrect = checkSpelling(listIdentical, expectedAnswer);
if(isPerfectAnswer) {
// The user entered the correct solution perfectly
finalResult = "Everything is correct!";
} else if(isWordOrderIncorrect) {
// CHECKS IF ONLY WORD ORDER IS WRONG
// false means there are unexpected elements in the user input
// true there are no unexpected elements, but the order is not correct, since the first check failed!
// Is true the case, then both lists contain the same elements.
finalResult = "Your word order is incorrect!";
} else if(isSpellingIncorrect) {
// Either unexpected elements (lenght of lists must differ) OR misspelled (same length, error in spelling)
finalResult = "Your spelling is incorrect!";
} else {
// If it gets here, the input has:
// Unexpected elements (e.g. an article), possibly spelling errors AND possibly order mistakes
// You could check if the order is correct, but what´s the point to write complex logic for that,
// knowing there are also unexpected elements, like additional prepositions or wrong words, in addition
// to spelling mistakes.
// Just show your user a message like this:
finalResult = "Unfortunatelly your answer is incorrect. Try again!";
}
// PRINT RESULT:
print(finalResult);
}
In advance, please forgive me if I do not give adequate background information for my question. Long time reader, first time asker.
I am making a program where one has a database of cars accessed through a tab delimited .txt file (we did something like this recently in my programming class, so I wanted to expand upon it).
Instead of using the terminal window, my format is displaying the Car objects (containing make, model, year, price, etc.) in ArrayList. I'm using JFrame, a JList, and a ListModel since I'm using an array of Car objects.
In my program, I wanted to create a delete method where the user could delete items from the database. Initially they would select the item from the JList and then would click on the delete button. This invokes the delete() method, which is the tab shown below...
void delete()
{
int i = list.getSelectedIndex();
String string = (String)listModel.getElementAt(i);
for(Car c : cars)
{
String year = String.valueOf(c.getYear());
String conditionReport = String.valueOf(c.getConditionReport());
String price = String.valueOf(c.getPrice());
if(c.getMake().indexOf(string) != -1 && c.getModel().indexOf(string) != -1 && year.indexOf(string) != -1 && conditionReport.indexOf(string) != -1 && price.indexOf(string) != -1 && c.getWarranty().indexOf(string) != -1 && c.getComments().indexOf(string) != -1)
{
int choice = JOptionPane.showConfirmDialog(null, "Are you sure you would like to remove the " + cars.get(i).getYear() + " " + cars.get(i).getMake() + " " + cars.get(i).getModel() + "?", "Choose One", JOptionPane.YES_NO_OPTION);
if(choice == JOptionPane.NO_OPTION || choice == JOptionPane.CLOSED_OPTION)
{
return;
} else
{
cars.remove(c);
listModel.removeElementAt(i);
}
}
}
writeFile();
}
I have pinpointed my issue to be inside the if statement. (I printed things before and after to try to find where the program is lying. 'list' is my JList and 'listmodel' is my default list model. Car is an object I created that contains the elements (as seen by the get methods). The elements shown in the listModel are merely Strings that show getMake(), getModel(), and so forth... (Each 'get' item is separated by about 10 spaces.)
What am I doing wrong in the if statement? I figured that the getMake() and getModel() (and so forth) would be substrings of the index selected.
Thank you so much for your assistance! Any input regarding ways I could make further questions more specific and clear would be greatly appreciated!
It seems like you are doing this to find the selected Car in some kind of data structure. You would be better off doing something like programming a custom list model that had access to cars itself. Then you could retrieve the selection more immediately. If cars is an ArrayList that list merely parallels I also don't see why you can't do something to the effect of cars.remove(list.getSelectedIndex());. Or since JList can display any object, override Car's toString to display what the list currently displays and have the list display Cars. Then you can cars.remove((Car)list.getSelectedValue());.
But aside from that, based on your description it sounds like you mean to do the evaluation the other way. It's the list item that should contain all of the Car attributes, rather than all of the Car attributes containing the list item. So something like
if( string.contains(c.getMake()) && string.contains(year) // and so on
(Or with indexOf but since contains merely returns indexOf > -1, using contains makes your code somewhat shorter.)
I'm using action script and I have an array with more than 400.000 strings and now i'm using a loop and apply a regex to each item of the array to check if it's valid or not. In case it's valid, i put such item in a result array.
this process take too long, so it's a nuisance because all the process must executed many times.
I've been thinking about if there is any other way (faster) i could use for applying the regex to all items without using a loop.
Anyone could give me an idea?
EDIT
Here I attach the code used:
var list:Array;
var list_total:Array = new Array;
var pattern:String = '^['+some_letters+']{'+n+'}$';
var cleanRegExp:RegExp = new RegExp(pattern, 'gi');
for (var i:int=0; i<_words.length; i++) {
list = _words[i].match(cleanRegExp);
if (list != null)
for (var j:int=0; j < list.length; j++)
list_total.push(list[j]);
}
Thanks.
This is not a complete answer, but may help you optimize your code.
Try to do operations in your loop that are as efficient as possible. Time them using the global getTimer() function so you can compare which methods are the most efficient. When measuring/comparing, you may want to trigger your code many times, so that these differences are noticeable.
// before test
var startTime:Number = getTimer();
// do the expensive operation
var endTime:Number = getTimer();
trace("operation took ", endTime - startTime, " milliseconds.");
For example, one improvement is inside a for loop, is to not query the array for it's length each time:
for (var i:int = 0; i < myArray.length; i++)
Instead, store the length in a local variable outside of the array and use that:
var length:int = myArray.length;
for (var i:int = 0; i < length; i++)
The difference is subtle, but accessing the length from the local variable will be faster than getting it from the Array.
Another thing you can test is the regular expression itself. Try to come up with alternate expressions, or use alternate functions. I don't recall the specifics, but in one project we determined (in our case) that using the RegEx.test() method was the fastest way to do a comparison like this. It's likely that this may be as quick as String.match() -- but you won't know unless you measure these things.
Grant Skinner has some awesome resources available on his site. They are worth reading. This slide show/presentation on performance is worth looking at. Use the arrow keys to change slides.
Edit
Without hearing Grant's presentation, the initial slides may not seem that interesting. However, it does get very interesting (with concrete code examples) around slide #43: http://gskinner.com/talks/quick/#43
I do not think there is any good way to avoid using a loop.
The loop could be optimized further though.
Like someone already suggested read the array length to a var so the loop doesn't have to check length each iteration.
Instead of the nested loop use concat to join the list array to the lists_total. I'm not sure if this is actually faster. I guess it depends on how many matches the regexp gets.
Here is the modified code.
var list:Array;
var list_total:Array = new Array;
var pattern:String = '^['+some_letters+']{'+n+'}$';
var cleanRegExp:RegExp = new RegExp(pattern, 'gi');
var wordsLength:int = _words.length;
for (var i:int=0; i<wordsLength; i++) {
list = _words[i].match(cleanRegExp);
if (list != null)
lists_total = lists_total.concat(list);
}
I was coding with 2 CStringList objects. Each has its own data, for eg one has name and other the phoneno, and both are in sync, i.e, if there is a phoneno there is a name and viceversa.
Now, i have 2 combobox in which i show the names and the respective phonenos. The name combobox is sorted, hence the sync between the two goes for a toss. hence for sorting i did the following:
int aComboElementNo = myNameComboBox.GetCount();
if( aComboElementNo >= 1 )
{
for( int aIndex = 0; aIndex < aComboElementNo; aIndex++ )
{
CString aTempStr;
// Getting the string in the combobox
myNameComboBox.GetLBText( aIndex, aTempStr );
// Getting the position where the name is present in the list
POSITION aPos = aNameList.Find( aTempStr );
// setting the appropriate phoneno in the 2nd combobox
myPhoneComboBox.AddString( aPhoneList.GetAt( aPos ) );
}
}
When i executed this i got the names in the myPhoneComboBox rather than the phonenos.
Now i have 2 qns:
how come i get the name present in namelist when i am accessing the phonelist? isn't it a breach, as i am able to access some other variables data using some other variable.
how to sort the 2nd list.
I Hope U are using CStringArray and not CStringList.
You need to use FindIndex rather than Find since Find will return OBJECT Pos rather than the Index count....
and to get the element with array use simply [] the operator.
If You still want to use CStringList then through Iterator Find the Index Count of the first match of string in one List and Use FindIndex of that IndexCount to get the postition object for the secondlist to use GetAt to the second list.
Why do you have 2 separate lists? Why not one CTypedPtrArray of structures holding both the name and the phone nb?
That is a crazzy, forgive me, stupid way to find names. It assumes the names are unique. God help me, I've had to deal with these things, name fields should never be viewed as unique, its bloody dangerious. Just ask my dad Baash05 Sr.
I'd assume there's an ID or some data set as the app adds to the combo box. Please use that in your map. My guess is the programmer set the data to either the ID of the name, or a pointer to the object that contained the name. (person object/business object/student object...).
If the code that adds the names didn't add a way to tell the difference between George Forman and any of his kids, then make an argument to the boss, that it's implementation should be changed, because by god it should be!
int aComboElementNo = myNameComboBox.GetCount();
for( int aIndex = 0; aIndex < aComboElementNo; aIndex++ )
{
int nameLocal = myNameComboBox.GetItemData( aIndex);
myPhoneComboBox.InsertString(aIndex, aPhoneList[namelocal] );
}