Enable contrast version

Tutor profile: John A.

Inactive
John A.
Software Developer who Likes to Teach
Tutor Satisfaction Guarantee

Questions

Subject: Python Programming

TutorMe
Question:

Define a Python function pairs() which... Given an unsorted list as input, outputs a list of all the items in that list which has a pair in the input list. Each pair should result in the item being printed exactly once. An item may appear in multiple pairs Example: pairs( [2, 3, 1, 2, 3, 3, 3, 2, 4] ) outputs [2, 3, 3] Test like this: inputList = [2, 3, 1, 2, 3, 3, 3, 2, 4] print("inputList = ", inputList) outputList = pairs( inputList ) print("outputList = ", outputList)

Inactive
John A.
Answer:

def pairs( input_list ): # Use a dictionary...a/k/a associative array or map... # ...can also use a set... dictionary = {} output_list = [] for pos, item in enumerate(input_list): if item in dictionary: # We've found a pair...append it to our list of pairs... output_list.append(item) # ...then delete it from the dictionary so we can # begin again with our pair detection... del dictionary[item] else: # This is an item not in the dictionary right now: # place it in the dictionary so we can detect # a pair... dictionary[item] = 1 return output_list

Subject: Javascript Programming

TutorMe
Question:

Create a recursive function in JavaScript combineArraysRecursively( array_of_arrays ) that forms all possible combinations of an arbitrary number of arbitrary-length arrays via string concatenation. Input should be an array of arrays. Output should be a single array Because it should handle an arbitrary number of arrays, it cannot simply be nested for loops, although you may think of it as the recursive equivalent of nested for loops. Run a demo like this (after filling in combineArraysRecursively()...) let input = [ ["A","B","C"], ["+", "-", "*", "/"], ["1","2"] ]; console.log("input = ", input ); let output = combineArraysRecursively( input ); console.log("combineArrayRecursively(input) = ", output ); //expected_output: [ // "A+1","A+2","A-1", "A-2", "A*1", "A*2", "A/1", "A/2", // "B+1","B+2","B-1", "B-2", "B*1", "B*2", "B/1", "B/2", // "C+1","C+2","C-1", "C-2", "C*1", "C*2", "C/1", "C/2"] //}

Inactive
John A.
Answer:

function combineArraysRecursively( array_of_arrays ){ // First, handle some degenerate cases... if( ! array_of_arrays ){ // Or maybe we should toss an exception...? return []; } if( ! Array.isArray( array_of_arrays ) ){ // Or maybe we should toss an exception...? return []; } if( array_of_arrays.length == 0 ){ return []; } for( let i = 0 ; i < array_of_arrays.length; i++ ){ if( ! Array.isArray(array_of_arrays[i]) || array_of_arrays[i].length == 0 ){ // If any of the arrays in array_of_arrays are not arrays or are zero-length array, return an empty array... return []; } } // Done with degenerate cases... let outputs = []; function permute(arrayOfArrays, whichArray=0, output=""){ arrayOfArrays[whichArray].forEach((array_element)=>{ if( whichArray == array_of_arrays.length - 1 ){ // Base case... outputs.push( output + array_element ); } else{ // Recursive case... permute(arrayOfArrays, whichArray+1, output + array_element ); } });/* forEach() */ } permute(array_of_arrays); return outputs; }/* function combineArraysRecursively() */

Subject: Java Programming

TutorMe
Question:

Cash Register: Create a Java class CashRegister with a static method public static ArrayList<BillOrCoin> calculateChange( int purchasePriceCents, int cashGivenCents ) throws Exception ...that figures out what kind of change to give to a customer. Use a static inner class... public static class BillOrCoin { public BillOrCoin(String name, int value){ this.name = name; this.value = value; } public String name; public int value; public String toString(){ return String.format("BillOrCoin: name='%s', value=%d", this.name, this.value ); } } The cash register has unlimited amounts of the following bills/coins within it with their values in cents: * 'PENNY': 1, * 'NICKEL': 5, * 'DIME': 10, * 'QUARTER': 25, * 'ONE': 100, * 'FIVE': 500, * 'TEN': 1000, * 'TWENTY': 2000, * 'FIFTY': 5000, * 'ONE HUNDRED': 10000 You will be supplied with the purchase price and the amount of cash that the customer gives to you...all in cents... You should return an ArrayList representing how many of each bill or coin is included in the change. If the customer does not give you enough cash, throw an Exception. Example: CashRegister.calculateChange( 10.35, 11.00 ) returns listOut[0] = BillOrCoin: name='QUARTER', value=2 listOut[1] = BillOrCoin: name='DIME', value=1 listOut[2] = BillOrCoin: name='NICKEL', value=1 meaning 2 quarters, 1 dime, and 1 nickel of change are returned for a purchase price of $10.35 if the customer gives you $11.00 in cash. Run some demo inputs and print their outputs in CashRegister's main() method.

Inactive
John A.
Answer:

import java.util.ArrayList; class CashRegister { public static class BillOrCoin { public BillOrCoin(String name, int value){ this.name = name; this.value = value; } public String name; public int value; public String toString(){ return String.format("BillOrCoin: name='%s', value=%d", this.name, this.value ); } } public static BillOrCoin[] billsAndCoins = { new BillOrCoin("PENNY", 1), new BillOrCoin("NICKEL", 5), new BillOrCoin("DIME", 10), new BillOrCoin("QUARTER", 25), new BillOrCoin("ONE", 100), new BillOrCoin("FIVE", 500), new BillOrCoin("TWENTY", 2000), new BillOrCoin("FIFTY", 5000), new BillOrCoin("ONE HUNDRED", 10000), }; // Used for demo cases in main()... public static class InputPair { public InputPair( int purchasePriceCents, int cashGivenCents ){ this.purchasePriceCents = purchasePriceCents; this.cashGivenCents = cashGivenCents; } public int purchasePriceCents; public int cashGivenCents; } public static ArrayList<BillOrCoin> calculateChange( int purchasePriceCents, int cashGivenCents ) throws Exception { ArrayList<BillOrCoin> listOut = new ArrayList<BillOrCoin>(); //System.out.format("purchasePriceCents = %d, cashGivenCents = %d%n", purchasePriceCents, cashGivenCents ); if( purchasePriceCents == cashGivenCents ){ return listOut; // Return empty list -- no change. } else if( cashGivenCents < purchasePriceCents ){ throw new Exception( "Not enough cash to cover purchase!" ); } // Difference begins as total difference, then is reduced down to zero // as we utilize different coins and bills... int differenceCents = cashGivenCents - purchasePriceCents; // Important: Begin with largest coins or bills and work our way down to // smallest coins or bills... for( int i=CashRegister.billsAndCoins.length - 1; i >= 0; i-- ){ // This is *integer* *division*, so only if dividend is greater than one // will the bill or coin "go into" the remaining differenceCents... int dividend = differenceCents / billsAndCoins[i].value; if( dividend >= 1 ){ int howMany = dividend; listOut.add( new BillOrCoin( billsAndCoins[i].name, howMany ) ); // Add this change item to the output list... differenceCents -= howMany * billsAndCoins[i].value; // Subtract this change item from the difference... } if( differenceCents <= 0 ){ // No more change needed... break; } } return listOut; } // Demo of CashRegister... public static void main(String[] args) { InputPair[] inputPairs = { new InputPair( 1035, 1100 ), new InputPair( 1700, 1600 ), // should throw an error -- not enough change new InputPair( 1700, 1700 ), new InputPair( 4500, 5000 ), new InputPair( 4526, 5000 ), new InputPair( 4571, 5000 ), new InputPair( 4875, 5000 ), new InputPair( 4876, 50025 ), }; ArrayList<BillOrCoin> listOut; for( int i=0; i<inputPairs.length; i++){ System.out.format("=====================%n"); System.out.format("i=%d: Calling CashRegister.calculateChange( purchasePriceCents = %d, cashGivenCents = %d )...%n", i, inputPairs[i].purchasePriceCents, inputPairs[i].cashGivenCents ); try { listOut = CashRegister.calculateChange( inputPairs[i].purchasePriceCents, inputPairs[i].cashGivenCents ); System.out.format("i=%d: listOut: %d item%s%n", i, listOut.size(), (listOut.size()==1?"":"s")); for(int j=0; j<listOut.size(); j++){ System.out.format("\t" + "listOut[%d] = %s%n", j, listOut.get(j).toString() ); } } catch(Exception e){ System.out.format("i=%d: Caught exception: \"%s\"%n", i, e.toString() ); } } }/* main() */ }/* class CashRegister */

Contact tutor

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

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.