Print similar objects from TreeMap collection JAVA - collections

How can I print similar objects from TreeMap if at least 1 value of objects are similar? For example I have map of books with authors, name of book and genre. If genres are similar, I want to print all books with this genre, author and name of his book from collection.
singleton.books.put(1, new Books("The Great Gatsby.", "Alex", Genre.DRAMA));
singleton.books.put(2, new Books("Don Quixote", "Samuel", Genre.ACTION));
singleton.books.put(3, new Books("Beloved", "Antony",Genre.DRAMA));
singleton.books.put(4, new Books("To Kill a Mockingbird", "Sofia",Genre.COMEDY));
singleton.books.put(5, new Books("One Hundred Years of Solitude", "Laura",Genre.COMEDY));
singleton.books.put(6, new Books("One Hundred Years of Solitude", "Roberts", Genre.FANTASY));

Related

How to automatically generate a publication list on a Quarto website?

Is there an (easy) way to automatically generate a publication list (from a bib-file) on Quarto website using R?
I plan to build a new academic website using quarto. To reduce the weekly/monthly maintenance load, I intend to structure/organize/build this page to fulfill multiple purposes at once. The site should not only be a classic academic website but also provide the basics for my CV. Hence, I want to generate the CV directly from this website. That seems to be pretty easy using Quarto and combining the .qmd files for a PDF.
However, I also want to use a bib-file of my publications to automatically generate a publication list. I want to list them categorized by type (#article or #book) and in descending chronological order (2022, 2021, 2020).
In addition, I want to add two other features to this list. First, I want to add numbers per category to each publication, so that the first publication in the category #book receives [1], the second [2] and so on. Second, I want to highlight (bold text) my name (e.g., John Doe) when there are multiple authors for a publication.
Is there an easy way to use R in Quarto for getting such an output?
I have attached some example bib-file entries:
#article{Doe2022b,
author = {Doe, John},
date-added = {2022-11-20 09:36:34 +0100},
date-modified = {2022-11-20 09:37:52 +0100},
doi = {10.1109/5.771073},
journal = {Daily Prophet},
number = {3},
pages = {1-25},
title = {I still know it better},
volume = {25},
year = {2020},
bdsk-url-1 = {https://doi.org/10.1109/5.771073}}
#article{Doe2020,
author = {Doe, Jane and Doe, John},
date-added = {2022-11-20 09:34:29 +0100},
date-modified = {2022-11-20 09:36:30 +0100},
doi = {10.1109/5.771073},
journal = {Daily Prophet},
number = {1},
pages = {18-35},
title = {We know it better},
volume = {23},
year = {2020}}
#book{Doe2021,
address = {Hogwarts},
author = {Doe, John},
date-added = {2022-11-20 09:34:11 +0100},
date-modified = {2022-11-20 09:34:19 +0100},
publisher = {Flourish and Blotts},
title = {My first Book},
year = {2021}}
#book{Doe2022,
address = {Hogwarts},
author = {Doe, John},
date-added = {2022-11-20 09:32:15 +0100},
date-modified = {2022-11-20 09:33:56 +0100},
publisher = {Flourish and Blotts},
title = {My second Book},
year = {2022}}
I'm not aware of any existing tool that does what you need. If you want to build it yourself, then use Quarto's Lua support to do so.
For example, to generate a list off all books, you'd write a Lua file like this:
-- This is `books-section.lua`
local function is_book (ref)
return ref.type == 'book'
end
function Pandoc (doc)
local references = pandoc.utils.references(doc)
local books = references:filter(is_book)
doc.meta.references = books
doc.meta.bibliography = nil -- ensure that only books will be used
doc.blocks = pandoc.Blocks{
pandoc.Header(1, 'Books'),
pandoc.Div({}, 'refs')
}
return pandoc.utils.citeproc(doc)
end
and then use it to generate a "Book" section with
quarto pandoc -L books-section.lua my-bibliography.bib \
--to=markdown-citations -o books.qmd
The Lua integration is quite powerful, so you might be able to do everything you need and to integrate the whole process into the normal website generation. See the Quarto docs on Lua development for more details.

Extracting dict keys from values

I am still learning about python and I face some trouble extracting data from a dict. I need to create a loop which check each values and extract the keys. So for this code I need to find the nice students. I am stuck at line 3 #blank.
How do i go about this?
Thanks in advance
class = {"James":"naughty", "Lisa":"nice", "Bryan":"nice"}
for student in class:
if #blank:
print("Hello, "+student+" students!")
else:
print("odd")
Uses dictionary methods "keys(), values(), items()":
def get_students_by_criteria(student_class, criteria):
students = []
for candidate, value in student_class.items():
if value == criteria:
students.append(candidate)
return students
my_class = {"James":"naughty", "Lisa":"nice", "Bryan":"nice"}
print(get_students_by_criteria(my_class, "nice"))
Warning to the word "class" it is a keyword reserved for python programming oriented object

Matching Multiple Dog Breed DropDown Lists with Results Logic

I'm totally stuck on this. I hope some expert here can help me out.
I have a page that lists survey results. The user has to guess the top 3 breeds of a dog. Then, the results are shown. Important: the user is guessing the TOP 3 breeds of the dog and they can be in any order.
For example, the user is shown a photo of a dog and underneath the photo is a list of three dropdowns:
Dropdown_1 Dropdown_2 Dropdown_3
Each of these dropdowns contains the same list of breeds, such as Beagle, German Shepard, Pug, etc. The user then selects one (and only one) breed for each of the dropdowns.
So, in the example above, the user would select:
German Shepard Beagle Pug
Now, when the answer/response page is displayed, they will see if their guesses match the correct answers.
Obviously, it would be easy to write something like:
If (BreedChoice1 = BreedChoice1Answer And BreedChoice2 = BreedChoice2Answer And BreedChoice3 = BreedChoice3Answer) Or
(BreedChoice1 = BreedChoice1Answer And BreedChoice2 = BreedChoice3Answer And BreedChoice3 = BreedChoice2Answer) Or
(BreedChoice1 = BreedChoice2Answer And BreedChoice2 = BreedChoice1Answer And BreedChoice3 = BreedChoice3Answer) Or
(BreedChoice1 = BreedChoice2Answer And BreedChoice2 = BreedChoice3Answer And BreedChoice3 = BreedChoice1Answer) Or
(BreedChoice1 = BreedChoice3Answer And BreedChoice2 = BreedChoice1Answer And BreedChoice3 = BreedChoice2Answer) Or
(BreedChoice1 = BreedChoice3Answer And BreedChoice2 = BreedChoice2Answer And BreedChoice3 = BreedChoice1Answer) Then
Response.Write("You Guessed ALL breeds correctly!")
End If
But how would I display message they says: "You guessed two breeds correctly". And one that says "You guessed one breed correctly"?
Remember that choice 1, 2, and 3 can match the answers 1, 2 and 3 in any order.
Any advice would be appreciated! Thank you in advance.
-- Chris
To solve this it would be simplest to use an Intersect method and to section up your code a little differently.
Firstly I would suggest that we put the "answers" into a list rather than keep them as separate variables (since you suggest they can be in any order). Then you should put your submitted answers also into a collection.
You can then do a very simple Intersect to get a collection that contains the elements common to both:
List<string> breeds = new List<string>() { "Beagle", "German Shepard", "Pug" };
List<string> choices = new List<string>() { "Beagle", "Pug", "Greyhound" };
int correctAnswers = breeds.Intersect(choices).Count();
The int "correctAnswers" then tells you how many they got right. (Obviously if you are using something more complicated than a string, such as a custom "Breed" class, for the breeds you could use some Linq to check the breed name property).
You could then use a neat bit of string interpolation (the $ sign in front of the string declaration) to get your result message:
$"Congratulations, you guessed {correctAnswers} breeds correctly!";
Hope his helps!

[Scala/Scalding]: map ID to name

I am fairly new to Scalding and I am trying to write a scalding program that takes as input 2 datasets:
1) book_id_title: ('id,'title): contains the mapping between book ID and book title, Both are strings.
2) book_sim: ('id1, 'id2, 'sim): contains the similarity between pairs of books, identified by their IDs.
The goal of the scalding program is to replace each (id1, id2) in book_ratings with their respective titles by looking up the book_id_title table. However, I am not able to retrieve the title. I would appreciate it if someone could help with the getTitle() function below.
My scalding code is as follows:
// read in the mapping between book id and title from a csv file
val book_id_title =
Csv(book_file, fields=book_format)
.read
.project('id,'title)
// read in the similarity data from a csv file and map the ids to the titles
// by calling getTitle function
val result =
book_sim
.map(('id1, 'id2)->('title1, 'title2)) {
pair:(String,String)=> (getTitle(pair._1), getTitle(pair._2))
}
.write(out)
// function that searches for the id and retrieves the title
def getTitle(search_id: String) = {
val btitle =
book_id_title
.filter('id){id:String => id == search_id} // extract row matching the id
.project('title) // get the title
}
thanks
Hadoop is a batch processing system and there is no way to lookup data by index. Instead, you need to join book_id_title and book_sim by id, probably two times: for left and right ids. Something like:
book_sim.joinWithSmaller('id1->id, book_id_title).joinWithSmaller('id2->id, book_id_title)
I am not very familiar with the field-based API so consider the above as a pseudocode. You also need to add appropriate projections. Hopefully, it still gives you an idea.

Find and arrange words with same letters from list

I am making a word game and I am wondering how to find and arrange words in a list that have the same letters. In my game, you are presented with 5 letters, and you then have to rearrange the letters tp make a word. So the word could be "acorn", but those 5 letters could also make up "narco" or "racon" which are words in my .txt dictionary (shown on the left).
Example, third line on the right:
Any tips or help would be greatly appreciated!
This is a typical anagram problem, You can store the words as Hashmap key-ed on the sorted version of the word, so a list like
clay, lacy, note, tern, tone, nest, sent
would be stored as hashmap of
acly => {clay , lacy}
enot => {tone, note}
enst => {sent, nest}
enrt => {tern}
Easy to build this, sudo algorithm is
Hashmap<String, HashSet<String>> h;
foreach word in list
String sorted = sort(word); \\so if word is sent, sorted is enst
HashSet<String> currentSet = h.get(sorted);
currentSet.add(word);

Resources