Enable contrast version

Tutor profile: Lustre J.

Online
Lustre J.
Seasoned Software Developer with tutoring experience
Tutor Satisfaction Guarantee

Questions

Subject: Python Programming

TutorMe
Question:

Write a function called heap_parent that takes an array representing a min_heap, and a value as parameters. If the min_heap contains the value, then the function should return a list/array containing the value and all the ancestors of the value up to and including the root node. If the heap does not contain the value, then the function should return an empty list.

Inactive
Lustre J.
Answer:

#This program search and print the path of a no in a min heap def heap_parent(heap=[], no=0): path = [] if (has_path(heap, 0, path, no)): return path else: return [] def has_path(heap=[], index=0, path=[], no=0): if (index >= len(heap)): return False path.append(heap[index]) if (heap[index] == no): return True if (index < len(heap) and (has_path(heap, (2 * index) + 1, path, no) or has_path(heap, (2 * index) + 2, path, no))): return True del path[len(path) - 1] return False # 3 # / \ # 5 9 # / \ / \ # 6 8 20 10 # / \ / #12 18 9 heap = [3, 5, 9, 6, 8, 20, 10, 12, 18, 9] path = heap_parent(heap, 12) print(path)

Subject: Java Programming

TutorMe
Question:

Merge two sorted linked list into one sorted linked list

Inactive
Lustre J.
Answer:

import java.util.LinkedList; import java.util.List; import java.util.Queue; /** * This program merge two sorted LinkedList using Queue interface * * @author lustre.japang * */ public class Main { /** * Main method to run the program * * @param args */ public static void main (String[]args) { // List1 with the predefined and sorted values List < Integer > list1 = new LinkedList <> (); list1.add (1); list1.add (3); list1.add (5); // List2 with the predefined and sorted values List < Integer > list2 = new LinkedList <> (); list2.add (2); list2.add (4); list2.add (5); list2.add (6); // The merged and sorted queue is being return from the merge method Queue < Integer > result = merge (list1, list2); System.out.println ("First list"); System.out.println (list1); System.out.println ("Second list"); System.out.println (list2); System.out.println ("Merged list"); System.out.println (result); } /** * * @param list1 * @param list2 * @return a queue which contains the sorted values of list1 and list2 */ private static Queue < Integer > merge (List < Integer > list1, List < Integer > list2) { Queue < Integer > queue = new LinkedList <> (); // index for iterating the list1 int index1 = 0; // index for iterating the list2 int index2 = 0; // loop as until one of the given lists is empty, check for the smaller // item from the list and add to the queue while (index1 < list1.size () && index2 < list2.size ()) { if (list1.get (index1) < list2.get (index2)) { queue.add (list1.get (index1)); index1++; } else { queue.add (list2.get (index2)); index2++; } } // in case list1 is not empty, add all the remaining items which are // already sorted to the queue while (index1 < list1.size ()) { queue.add (list1.get (index1)); index1++; } // in case list2 is not empty, add all the remaining items which are // already sorted to the queue while (index2 < list2.size ()) { queue.add (list2.get (index2)); index2++; } // finally return the queue return queue; } }

Subject: C++ Programming

TutorMe
Question:

Write a C++ program to simulate Book management system. The program should provide these options: 1. Add a book 2. Display all the books 3. Search a book based on ISBN 4. Update a book based on ISBN

Inactive
Lustre J.
Answer:

/* This program simulate a Book management system where the user can: 1. Add a Book which has a title, author name, ISBN and whether it is an eBook or PaperBook 2. Display all the books 3. Search the book based on the ISBN 4. Modify the book based on an ISBN */ #include <iostream> #include <vector> using namespace std; static int index = 0; class Book { private: string title; string author; string isbn; string ebook; public: Book(){} Book(string title_, string author_, string isbn_, string ebook_){ title = title_; author = author_; isbn = isbn_; ebook = ebook_; } void setTitle(string title_){ title = title_; } string getTitle(){ return title; } void setAuthor(string author_){ author = author_; } string getAuthor(){ return author; } void setIsbn(string isbn_){ isbn = isbn_; } string getIsbn(){ return isbn; } void print() { cout << "Book title:" << title <<", Author: " << author << ", ISBN:" <<isbn <<", is ebook:" << ebook; } }; /* function to display the menu */ string displayMenu() { string choice; cout << "Book Management System\n"; cout << "1. Add a book\n"; cout << "2. Display the books\n"; cout << "3. Search by ISBN\n"; cout << "4. Update book by ISBN\n"; cout << "5. Exit\n"; getline(cin, choice); return choice; } /* function to add the book */ void addBook(vector<Book> &books) { string title, author, isbn, ebook; cout << "Enter the title : "; getline(cin, title); cout << "Enter the author name : "; getline(cin, author); cout << "Enter the ISBN : "; getline(cin, isbn); cout << "Is it an ebook(Y/N) : "; getline(cin, ebook); string ebook_ = (ebook.compare("Y") || ebook.compare("y")) ? "true" : "false"; Book b(title, author, isbn, ebook_); books.push_back(b); } /* function to display the book */ void displayBooks(vector<Book> &books) { for (auto i = books.begin(); i != books.end(); ++i){ i->print(); cout << "\n"; } } /* function to search the book based on isbn */ void searchBook(vector<Book> &books) { string isbn; cout << "Enter the ISBN : "; getline(cin, isbn); for (auto i = books.begin(); i != books.end(); ++i){ if(i->getIsbn().compare(isbn) == 0) { i->print(); break; } } } /* function to update the book based on isbn */ void updateBook(vector<Book> &books) { string isbn; cout << "Enter the ISBN : "; getline(cin, isbn); bool found = false; for (auto i = books.begin(); i != books.end(); ++i){ { if(i->getIsbn().compare(isbn) == 0) { found = true; books.erase(i); //break; } } cout << found; if(found) { string title, author, ebook; cout << "Enter the title : "; getline(cin, title); cout << "Enter the author name : "; getline(cin, author); cout << "Is it an ebook : "; getline(cin, ebook); string ebook_ = (ebook.compare("Y") || ebook.compare("y")) ? "true" : "false"; Book b(title, author, isbn, ebook_); books.push_back(b); } displayBooks(books); } } int main() { int choice; try{ vector<Book> books; do { //Display the menu for all the options available choice = std::stoi(displayMenu()); //once user provide the input, based on its value, pass to the switch case switch(choice) { case 1://add a book addBook(books); cout << books.size(); break; case 2: //display all the books displayBooks(books); break; case 3://search the book based on ISBN searchBook(books); break; case 4://update the book based on ISBN updateBook(books); break; default: break; } } while(choice!=5); return 0; } catch(...) { cout << "Invalid input"; return -1; } }

Contact tutor

Send a message explaining your
needs and Lustre will reply soon.
Contact Lustre

Request lesson

Ready now? Request a lesson.
Start Lesson

FAQs

What is a lesson?
A lesson is virtual lesson space on our platform where you and a tutor can communicate. You'll have the option to communicate using video/audio as well as text chat. You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.
How do I begin a lesson?
If the tutor is currently online, you can click the "Start Lesson" button above. If they are offline, you can always send them a message to schedule a lesson.
Who are TutorMe tutors?
Many of our tutors are current college students or recent graduates of top-tier universities like MIT, Harvard and USC. TutorMe has thousands of top-quality tutors available to work with you.