|
hooah posted:This is the kind of thing that makes me really glad I don't have to devise algorithms for my job. I do not miss this kind of crap from school. algorithms aren’t too bad until you get into the “aha but if we take the median of medians then we can guarantee n^(sqrt(2)) worst-case running time instead of n^2!” poo poo, which absolutely is a pain in the rear end. Not a fan of leetcode interviews at all. But a surprising amount of the algorithmic work nowadays is on managing worst-case complexity, things like timsort. And some of that stuff there isn’t an algorithmic basis at all for the heuristics, it’s just “we ran a bunch of tests and this worked best”.
|
# ? Sep 10, 2022 00:44 |
|
|
# ? Jun 13, 2024 04:19 |
|
ok, we're learning lambda expressions. I have to modify some code using this method to give a pass/fail for the contents of an array. The rules are "Only the fruits that meet the following criteria will pass: 1. at least 15 grams in weight. 2. 30 days of age or younger unless it has a hardshell 3. color cannot be black" I am given three fruit types: kiwis, banannas, and coconuts. My initial lambda expression looks like this: FruitInspector goodInspector = (x.getweight >= 15, x.getage >= 30, !(x.getcolor)equals("brown"))) -> {return true;}; but that doesn't excuse hardshells like coconuts and is throwing all sorts of errors. Please help me understand lambda expressions.
|
# ? Sep 30, 2022 20:15 |
|
When I'm having trouble with lambdas I first try to write things as a function and then turn them into a lambda. In this case you just have a really convoluted boolean expression, Java code:
If you're sure that functions passes all the criteria you want, then you can convert it into a lambda easily. Java code:
|
# ? Sep 30, 2022 20:38 |
|
Janitor Prime posted:When I'm having trouble with lambdas I first try to write things as a function and then turn them into a lambda. In this case you just have a really convoluted boolean expression, That last bit throws an error: The method stream(T[]) in the type Arrays is not applicable for the arguments (List<Fruit>) Here is some of the provided code: code:
|
# ? Sep 30, 2022 20:43 |
|
It seems like there is a specific type for the FruitInspector that has a pass method. Lambdas let you create a quick implementations of these simple interfaces and I'm guessing in this case this should workJava code:
|
# ? Sep 30, 2022 21:08 |
|
Is it just me or does this problem not really make a very convincing case for lambda functions? I mean, they want to teach you how to use them, sure, but I hope there’s a future problem to teach you /when/ to use them.
|
# ? Sep 30, 2022 22:46 |
|
samcarsten posted:ok, we're learning lambda expressions. I have to modify some code using this method to give a pass/fail for the contents of an array. The rules are You shouldn't have code in the left side of the expression, those are the variables you're passing into the function, which in this case would just be x, then put your logic on the right side inside a code block, like: Java code:
Java code:
Java code:
RandomBlue fucked around with this message at 23:07 on Sep 30, 2022 |
# ? Sep 30, 2022 23:01 |
|
Hippie Hedgehog posted:Is it just me or does this problem not really make a very convincing case for lambda functions? It could be the bullshit hard question in the homework where most aren't expected to succeed but the teacher wants to see how they try (and likely fail) to solve it.
|
# ? Sep 30, 2022 23:17 |
|
Just telling them the answer isn't going to help them understand lambdas. e: These two tutorials don't look bad: https://javabeginnerstutorial.com/core-java-tutorial/java-lambda-beginners-guide/ https://www.educative.io/blog/java-lambda-expression-tutorial RandomBlue fucked around with this message at 23:26 on Sep 30, 2022 |
# ? Sep 30, 2022 23:21 |
|
RandomBlue posted:Just telling them the answer isn't going to help them understand lambdas. Oh absolutely - when I say bullshit hard I don't mean that the teacher shouldn't set them, it's that the student is right to find it bullshit hard, and learn from what they couldn't manage to achieve. Sometimes you learn more from failing than you do from succeeding.
|
# ? Sep 30, 2022 23:26 |
|
Anyone have strong opinions on getting the Oracle Java 17 cert? Is it worth it if I already have a strong resume (aiming for senior/principal level)? If anyone has taken it, how much study should I plan to do?
|
# ? Oct 3, 2022 01:31 |
|
Pedestrian Xing posted:Anyone have strong opinions on getting the Oracle Java 17 cert? Is it worth it if I already have a strong resume (aiming for senior/principal level)? If anyone has taken it, how much study should I plan to do? I'd vote that an Oracle Java cert is a net neutral at best for an early career, with perhaps a net negative IMO for senior+ types. But I'm idiosyncratic and really only respect the OSCP.
|
# ? Oct 5, 2022 02:10 |
|
I'm curious if H2 has some benefits that I'm missing, or if I've somehow been using it wrong? I make some little toy apps in Spring Boot just because I'm very familiar with it, but I've learned the hard way over the years that H2 is not to be trusted in the long run with data, because it seems to be very vulnerable to corruption if the process ever has a dirty shutdown in a way that I haven't seen other databases fail. I've seen this basically as long as I've attempted to use H2, and I get the impression that H2 is only ever used for testing applications locally with transient data. I've compensated by replacing H2 with SQLite, but sqlite usage seems to be outside of the (non-android) Java culture, and I'm curious to hear what people are reaching for in an embedded database. Is SQLite king of the space like I think it is, or should I check out any other options?
|
# ? Oct 10, 2022 17:12 |
|
The trick with embedded databases is in the long run they all have that problem. I've had plenty of SQLite databases eat themselves over the years too. I use H2 a lot and it has lots of really nice features a lot of the other embedded ones don't, like multithreaded access modes, BLOB compression options, Postgres mode, optional embedded web-gui, ability to support multiple clients and transport protocols, etc. That being said it might be using a sledgehammer to swat a fly depending on what you are doing. SQLite can be fine if you just need table storage without sweating the bells and whistles or if you just want a more cross-language supported way to store data. For instance I do have one application that has both a C/C++ app and a Java app so we standardized on SQLite instead of trying to support an interface or service layer abstraction. Not optimal but given the scope of that app it made the most fiscal sense. No matter which database you embed though you will eventually want to have a set of startup routines to attempt auto recovery and run any maintenance functions the DB in question provides to clean up transaction files/etc. The only one I wouldn't really recommend is probably Derby, its been flakier than either H2 or SQLite for me and the recovery options always seemed worse. Just a personal opinion though.
|
# ? Oct 10, 2022 18:15 |
|
H2 also has a handful of weird incompatibilities with T-SQL that make it a pain in the rear end to use as a test DB. Thankfully there's testcontainers now.
|
# ? Oct 11, 2022 02:08 |
|
I like H2 but the 2.x upgrade that uses a new DB format with no upgrading of existing databases is pretty lovely. You have to export your data with the old version, create a new DB with the new version and import the data. Migrating to that automatically in our app is going to be a PITA for us.
|
# ? Oct 11, 2022 02:31 |
|
ok, new assignment. I have to write code based on the following template which takes a file and lists misspelled words and word frequency using maps and sets. I have no idea how to begin. to start with, it has code already to load the dictionary file. The file I am given to analyze is a plain text file of the bill of rights. I know what the output should look like. How do you do something like this?code:
|
# ? Oct 11, 2022 15:23 |
|
samcarsten posted:The file I am given to analyze is a plain text file of the bill of rights. I know what the output should look like. How do you do something like this? Well, the only part you have to write is the process(File file) method: samcarsten posted:
And the only thing you really need to figure out are the misspelled words, in two different sorted orders, based on that output. samcarsten posted:
So, your task boils down to: 1. Take a File object, read it, and decide for each word if that word is spelled correctly. 2. If not spelled correctly, add it to a list of misspelled words. 3. If spelled correctly, update the count of number of times you've seen that word. 4. Sort the list of misspelled words alphabetically. 5. Sort the list of times of you've seen each word in descending numerical order. ulmont fucked around with this message at 16:11 on Oct 11, 2022 |
# ? Oct 11, 2022 16:06 |
|
Read through the existing code, there's already code there showing you how to load a file one word at a time. Copy that and for each word being loaded figure out if it's in the dictionaryWords collection and if it is add it to the wordToFrequency map, checking if it exists first. If it does exist, increment the frequency, if not, add it with a frequency of 1. If it's not in the dictionary you add it to the misspelledWords set. Then afterwards you loop through your wordToFrequencyMap and add each entry to the frequencyToWordMap. Why they chose a Set for mispelledWords and not dictionaryWords is beyond me though, dictionaryWords should obviously be a HashSet since you're searching it frequently and searching lists is slow in comparison.
|
# ? Oct 11, 2022 16:09 |
|
How do I increment a value in treemap? I googled it, but most people are talking about hashmaps.
|
# ? Oct 12, 2022 12:14 |
|
They should be interchangeable as both are subclasses of AbstractMap.
|
# ? Oct 12, 2022 12:35 |
|
samcarsten posted:How do I increment a value in treemap? I googled it, but most people are talking about hashmaps. It doesn't matter the specific implementation, what we're working with is a Map<String, Integer> You find the key-value pair using the key (String) and then change the value (Integer) to value++
|
# ? Oct 12, 2022 12:52 |
|
ok, that's done. How should I go about turning word to frequency to frequency to word? Also I'm getting an error input file not found and I don't know how to fix it.code:
samcarsten fucked around with this message at 13:23 on Oct 12, 2022 |
# ? Oct 12, 2022 13:16 |
|
How are you invoking the program / specifying the input file? It looks like main is opening it and looking for the file specified as a single argument, however in process(File file) you've hard-coded the Bill of Rights. Is BillofRights.txt in your classpath? You should be able to drag and drop it into your IDE to help with getting it linked correctly. Additionally where is DICTIONARY_FILE aka dictionary.txt?
|
# ? Oct 12, 2022 14:26 |
|
Tesseraction posted:How are you invoking the program / specifying the input file? It looks like main is opening it and looking for the file specified as a single argument, however in process(File file) you've hard-coded the Bill of Rights. both files are in the src folder of my eclipse workspace of this project. I have no idea why its not finding them.
|
# ? Oct 12, 2022 15:05 |
|
samcarsten posted:both files are in the src folder of my eclipse workspace of this project. I have no idea why its not finding them. Should you not be attempting to read the File object provided to you?
|
# ? Oct 12, 2022 15:08 |
|
necrotic posted:Should you not be attempting to read the File object provided to you? those are the files i am supposed to be reading. maybe i'm not understanding you.
|
# ? Oct 12, 2022 15:29 |
|
Can you post the text of the Exception? It should have the name of the .java file you've got plus its line numbers - if you could also supply those line numbers we can figure out where the error is happening. edit, picking out a screenshot I posted in this thread a decade ago you should have something like this:
|
# ? Oct 12, 2022 15:34 |
|
it's triggering the "check command line" exception built into main.
|
# ? Oct 12, 2022 15:38 |
|
You need to specify the filename you want to run it over as a command-line argument. I haven't used Eclipse in about a decade but there should be somewhere that you set up a launch configuration, including what command-line arguments it uses when running your program.
|
# ? Oct 12, 2022 15:41 |
|
Open the Run settings (Run -> Run... in Eclipse as I remember it) and go to Java Application -> Main -> Arguments -> Program arguments and put in ./BillofRights.txt
|
# ? Oct 12, 2022 15:42 |
|
Tesseraction posted:Open the Run settings (Run -> Run... in Eclipse as I remember it) and go to Java Application -> Main -> Arguments -> Program arguments and put in hmm, it says no such file exists when i do that.
|
# ? Oct 12, 2022 15:49 |
|
Should've known - if you go to the physical file structure where is BillofRights.txt relative to Main.java (or whatever you've called it) I vaguely recall that it'll be like src/Main.java and then something like files/BillofRights.txt or maybe even just ./BillofRights, in which case the argument should be ../BillofRights.txt Java shouldn't be arsey about / vs \ as long as you use them consistently.
|
# ? Oct 12, 2022 15:52 |
|
ok, moved stuff and it found the file. Now i'm getting a new error: Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "value" is null at csc251lab9.WordCountWithSpellCheck.process(WordCountWithSpellCheck.java:47) at csc251lab9.WordCountWithSpellCheck.main(WordCountWithSpellCheck.java:28) code:
|
# ? Oct 12, 2022 15:56 |
|
Threw that into Notepad++ to get the line number it's this line:code:
Try locally assigning input.next() to a variable. code:
|
# ? Oct 12, 2022 16:04 |
|
ok, that fixed that. Now, how do i turn word to frequency into frequency to word? It won;'t let me use a for loop, as it's not an iterable.
samcarsten fucked around with this message at 16:11 on Oct 12, 2022 |
# ? Oct 12, 2022 16:08 |
|
I believe your teacher granted a hint with this:code:
|
# ? Oct 12, 2022 16:24 |
|
i'm not sure how to use collections. the documentation is a little confusing.
|
# ? Oct 12, 2022 16:34 |
|
Welcome to software development!
|
# ? Oct 12, 2022 16:35 |
|
|
# ? Jun 13, 2024 04:19 |
|
Ah good, I still have Eclipse installed. You keep trying to work your way around collections and I'll play around with them too and refresh my memory. With any luck you won't need my help.
|
# ? Oct 12, 2022 16:39 |