Printed by Owen L. Astrachan SandwichBar.java Jan 13, 06 9:09 Page 1/1 import java.util.*; public class SandwichBar { int whichOrder(String[] available, String[] orders){ int w=−1; int t=0; 5 for(int m=0;m<orders.length;m++){ t=0; String s = orders[m]; String[] want=s.split(" "); 10 for(int k=0;k<want.length; k++){ w=−1; 15 for(int i=0;i<available.length;i++){ if(available[i].equals(want[k])){ w++; 20 } } //one sandwich part is not available if(w==−1){t=−1;} } 25 //if all the ingredients were there if(t==0){return m;} } //if you got to the end and nothing is returned, you will go hungry return −1; 30 } } 35 amb7_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan SandwichBar.java Jan 13, 06 9:09 Page 1/1 //SandwichBar.java //My first Compsci 100 APT //01/12/2006 //Time taken: 2.5 hours 5 10 import java.util.*; public class SandwichBar { public int whichOrder(String[] available, String[] orders){ int result = −1; //Initialize result variable boolean foundParts = false; //Boolean for checking if all contents of sandwich are available for( int k = 0; k < orders.length; k++){ //Iterate through orders Scanner orderScan = new Scanner(orders[k]); //Break up order into parts 15 while(orderScan.hasNext()){ 20 //Check to make sure there is a next element in scanner foundParts = false; //Reset boolean to false for each new part String orderPart = orderScan.next(); //Put each part in string for (String avail: available){ if (orderPart.equals(avail)){ foundParts = true; //if the part is found change boolean } } if(!foundParts){ break; //If a single part is unavailable, there 25 30 is no point to continue } } if(foundParts){ result = k; break; //If all parts are found for an order, stop and 35 40 output its index } } return result; } 45 } 50 apn3_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan SandwichBar.java Jan 13, 06 9:09 5 10 15 20 // // // // // // // // // // // // // // // // // // Page 1/1 David Zhang 2006−01−11 SandwichBar APT problem. Problem Statement It’s time to get something to eat and I’ve come across a sandwich bar. Like most people, I prefer certain types of sandwiches. In fact, I keep a list of the types of sandwiches I like. The sandwich bar has certain ingredients available. I will list the types of sandwiches I like in order of preference and buy the first sandwich the bar can make for me. In order for the bar to make a sandwich for me, it must include all of the ingredients I desire. Given a String[] available, a list of ingredients the sandwich bar can use, and a String[] orders, the types of sandwiches I like, in order of preference (most preferred first), return the 0−based index of the sandwich I will buy. Each element of orders represents one type of sandwich I like as a space−separated list of ingredients in the sandwich. If the bar can make no sandwiches I like, return −1. public class SandwichBar { /** * Figures out what sandwich to buy based on what’s available and what y our * preferences are. Handles repeated array elements in a less−than−optim al * way. * * @param available * Array of available ingredients. * @param orders * Ordered array of what sandwiches are preferred. * @return Array index of first available sandwich; −1 if no preferred * sandwiches are available. */ public int whichOrder(String[] available, String[] orders) { String myOrders[][] = new String[orders.length][]; for(int i = 0; i < orders.length; i++) myOrders[i] = orders[i].split(" "); for(int i = 0; i < myOrders.length; i++) { String order[] = myOrders[i]; boolean hasIngredients = true; for(String ingredient : order) { if(!arrayContains(available, ingredient)) hasIngredients = false; } if(hasIngredients) return i; } 25 30 35 40 45 50 return −1; } /** * Linear search for an item inside an array. * @param haystack Array to search in. * @param needle What to find in the array. * @return True if needle is found in haystack, false if not. */ private static boolean arrayContains(String[] haystack, String needle) { for(String s : haystack) if(needle.equals(s)) return true; return false; } 55 60 65 } drz_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan SandwichBar.java Jan 13, 06 9:09 5 Page 1/1 /* * Harish Srinivasan * Compsci 100 * January 12, 2005 */ import java.util.*; public class SandwichBar { 10 int whichOrder(String[] available, String[] orders) { ArrayList availIngreds = new ArrayList(); for (String availIngred : available) { availIngreds.add(availIngred); } ArrayList ingredList = new ArrayList(); boolean makable; 15 for (int i=0; i < orders.length; i++) { makable = true; Scanner s = new Scanner(orders[i]); while (s.hasNext()) { ingredList.add(s.next()); } for (Object ingred : ingredList) { if (!availIngreds.contains(ingred)) { ingredList.clear(); makable=false; break; } } if (makable) { return i; } } return −1; 20 25 30 35 } } hms4_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan SandwichBar.java Jan 13, 06 9:09 5 Page 1/1 //Justin Mullen //Compsci 100 //First attempt at sadwich bar−− // problem with k and n which causes a return of −1 // when there are ingridients used more than once in a sandwich order. //−−Need to figure out java commands to replace counters //Score: 15/30 10 import java.util.*; 15 20 25 30 35 public class SandwichBar { public int whichOrder(String[] available, String[] orders) { for(int i = 0; i < orders.length; i++) { int n = 0; String s = orders[i]; Scanner scanner = new Scanner(s); //break up orders into substrings int k = 1; while(scanner.hasNext()) { String t = scanner.next(); //check to see if ingridients match //(compare substrings to ’available’) for(int j = 0; j < available.length; j++) { if(t.equals(available[j])) { n++; } } //"k" is a counter for the no. of substrings k++; } System.out.println(i); System.out.println(n); System.out.println(k); System.out.println("−−−−"); 40 if(n >= k) //return index of appropriate order return i; 45 } //if no sandwich matches... return −1; 50 } } jnm12_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan SandwichBar.java Jan 13, 06 9:09 Page 1/1 import java.util.*; 5 10 public class SandwichBar { public int whichOrder(String[] available, String[] orders) { ArrayList avail = new ArrayList(); for(int i = 0; i < available.length; i++) { avail.add(available[i]); } for(int counter = 0; counter < orders.length; counter ++) { String[] preference = orders[counter].split(" "); boolean test = true; 15 for(int j = 0; j < preference.length; j++) { if(!avail.contains(preference[j])) test = false; } 20 if(test == true) return counter; 25 } return −1; } 30 } jsb17_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan Jan 13, 06 9:09 5 10 SandwichBar.java Page 1/1 /* * Name: Karl Wang * Course: Compsci 100.1 * * Purpose: This program takes in input of two string arrays, * one holding all available ingredients and another holding * the customer’s preferred sandwiches. It returns the index * of the first sandwich, in order of preference, that can * be made from available ingredients. */ import java.util.*; 15 20 25 30 35 public class SandwichBar { public int whichOrder(String[] available, String[] orders) { ArrayList allAvailable = new ArrayList(Arrays.asList(available)) ; // convert available array to arrayList for (int k = 0 ; k < orders.length ; k++) // loop through entire array of orders { Scanner sc = new Scanner (orders[k]); // create a scanne r to dissect each string from orders array boolean availability = true; // set up a boolean to keep track of whether all ingredients in this preferred sandwich are available while (sc.hasNext()) // loop through all individual ingr edients in orders[k] string { if (allAvailable.indexOf(sc.next()) == −1) // if anytime an ingredient is not available, change availability to false { availability = false; } } if (availability) // only if availability is true (all i ngredients are available) do I return the index of this order { return k; } } return −1; // if after all orders are deemed unavailable, return −1 } } kw37_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan SandwichBar.java Jan 13, 06 9:09 Page 1/1 //Lingren Zhang import java.util.*; 5 public class SandwichBar { public int whichOrder(String[] available, String[] orders){ //convert Array available into an ArrayList available1 ArrayList available1 = new ArrayList(Arrays.asList(available)); for (int k = 0; k < orders.length; k++) { //order contains all the ingredients to make the k−th sa 10 ndwich String[] order = orders[k].split(" "); //check to see if all the ingredients are available boolean flag = true; for (String ingredient: order) { if (available1.indexOf(ingredient) < 0) { flag = false; } } 15 20 25 30 //if all the ingredients are available, return k, since this is the first sandwich they can make if (flag) { return k; } } return −1; } } lz7_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan Jan 13, 06 9:09 SandwichBar.java Page 1/1 import java.util.*; 5 10 15 20 25 public class SandwichBar { public int whichOrder(String[] available, String[] orders) { ArrayList<String> availableList = new ArrayList<String>(); for (String availableItem : available) { availableList.add(availableItem); } for (int i=0; i<orders.length; i++) { String[] orderArray = orders[i].split(" "); boolean test = true; for (String item : orderArray) { if (availableList.indexOf(item) == −1) test = false; } if (test == true) return i; } return −1; } } mfm11_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan SandwichBar.java Jan 13, 06 9:09 Page 1/1 import java.util.Scanner; import java.util.Set; import java.util.TreeSet; 5 10 public class SandwichBar { public int whichOrder(String[] available, String[] orders) { int myIndex = −1; // put all ingredients into set Set ingredients = new TreeSet(); for( String current : available) { ingredients.add(current); } //check if bar has ingredients for(int x =0; x< orders.length; x++) { Scanner scanner = new Scanner(orders[x]); boolean hasIng = true; 15 20 while(scanner.hasNext() && hasIng) { String ing = scanner.next(); if(!ingredients.contains(ing)) { hasIng = false; 25 } 30 } if(hasIng) { myIndex= x; break; } 35 } return myIndex; 40 } } mwt4_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan Jan 13, 06 9:09 5 10 15 20 SandwichBar.java Page 1/1 public class SandwichBar { public int whichOrder(String[] available, String[] orders){ for (int orderme = 0; orderme < orders.length; orderme++) { int theworks = 0; String sandwich = orders[orderme]; String[] ingreds = sandwich.split(" "); for (int ingred = 0; ingred < ingreds.length; ingred++) { int check = 0; for (int avcheck = 0; avcheck < available.length ; avcheck++) { if (available[avcheck].equals(ingreds[in gred])) check += 1; } if (check>0) theworks += 1; } if (theworks==ingreds.length) return orderme; } return −1; } } nab9_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan SandwichBar.java Jan 13, 06 9:09 Page 1/1 /* Pete Osterland, COMPSCI 100 *APT: SandwichBar Search *Last Modified: 1/11/06 */ 5 import java.util.*; public class SandwichBar { int whichOrder(String [] available, String[] orders) { 10 ArrayList barItems = new ArrayList(); boolean sandwich = false; int index = −1; for (int i=0; i<available.length; i++) barItems.add(available[i]); for (int j=0; j<orders.length; j++) { if(sandwich==false){ Scanner s = new Scanner(orders[j]); sandwich = true; index = j; while (s.hasNext()) { if(!barItems.contains(s.next())) { sandwich = false; index = −1; break; } 15 20 25 } } } 30 return index; } } pmo5_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan Jan 13, 06 9:09 5 10 15 20 SandwichBar.java Page 1/1 //RuiJun Chen public class SandwichBar { public int whichOrder(String[] available, String[] orders) { for(int a=0; a<orders.length; a++) { String[] blah=orders[a].split(" "); boolean sandwich=true; boolean ingredient=false; for(int b=0; b<blah.length; b++) { ingredient=false; int whee=0; while(!ingredient && whee<available.length){ if(available[whee].equals(blah[b])) ingredient=true; whee++; } if(!ingredient) sandwich=false; } if(sandwich) return a; } return −1; } } rc50_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan Jan 13, 06 9:09 SandwichBar.java Page 1/1 /** * @author Ralitsa Markova */ 5 10 15 20 25 30 35 40 import java.util.Scanner; public class SandwichBar { public int whichOrder(String[] available, String[] orders) { boolean thisSandwich; boolean thisProduct; for (int i = 0; i < orders.length; i++) { Scanner scan = new Scanner(orders[i]); thisSandwich = true; while (scan.hasNext()) { thisProduct = false; for (int k = 0; k < available.length; k++) { if (available[k].equals(scan.next())) { thisProduct = true; break; } } if (!thisProduct) { thisSandwich = false; break; } } if (thisSandwich) { return i; } } return −1; } } rgm8_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan SandwichBar.java Jan 13, 06 9:09 Page 1/1 import java.util.*; 5 10 public class SandwichBar { int whichOrder(String[] available, String[] orders){ for(int i=0; i < orders.length; i++){ String s = orders[i]; String[] list = s.split(" "); // // // for(String word : list){ System.out.println(word); } ArrayList ingredients = new ArrayList(); for(String word : available){ if(!(ingredients.contains(word))){ ingredients.add(word); } } 15 20 String[] check = new String[list.length]; for(int k=0; k < list.length; k++){ if(ingredients.contains(list[k])){ check[k] = "true"; } else{ check[k] = "false"; } } 25 30 // // // // for(String result : check){ System.out.println(result); } System.out.println("−−−−−"); 35 ArrayList results = new ArrayList(); for(String word : check){ results.add(word); } 40 if(!(results.contains("false"))){ return i; } } return −1; 45 } } rjm19_1 SandwichBar.java 1/1 Printed by Owen L. Astrachan SandwichBar.java Jan 13, 06 9:09 5 Page 1/1 public class SandwichBar { public int whichOrder(String[] available, String[] orders){ for(int i=0;i<orders.length;i++){ int j=0; String s1 = orders[i]; String[] list = s1.split(" "); while(isAvailable(list[j], available)&&j<list.length){ j++; if(j==(list.length)) return i; } 10 15 } return −1; } public boolean isAvailable(String word, String[] available){ for(int k=0;k<available.length;k++){ if(word.equals(available[k])) return true; } return false; } 20 25 30 } rmz_1 SandwichBar.java 1/1
© Copyright 2024 Paperzz