1. Finish the coding problem we started in class
Sample input:
5, D, 2, D, 6, H, 9, D, 9, S, 6, H
4, C, 1, C, 6, C, 7, H, 5, S, 4, D
3, D, 4, H, 5, C, 6, S, 2, D, 1, D
9, S, 8, H, 7, C, 7, D, 9, H, 3, H
1, C, 1, D, 9, H, 8, S, 9, S, 7, C
Sample output:
9, D
6, C
1, D
NONE
7, C
Test input:
4, S, 2, S, 5, S, 6, S, 8, S, 9, S
7, H, 3, S, 3, H, 3, D, 3, C, 2, H
9, D, 3, C, 5, H, 1, S, 7, D, 9, S
6, C, 1, S, 2, H, 7, S, 8, D, 9, H
1, D, 2, S, 3, D, 4, S, 2, H, 2, C
Test output:
5, S 2, H
7, D NONE
3, D
Copy+paste your finished code as a reply to this post.
2. What is the value of f(6,3)?
2. f(6,3)=22
1. (This code is probably wrong and apparently it also has a few errors that I'm not sure how to fix. Also, there's a lot of "System.out.println"s that I added for testing purposes)
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner scanGoBrr = new Scanner(System.in); System.out.println("Enter input:"); String input = scanGoBrr.nextLine(); scanGoBrr.close(); System.out.println("Your input is: "+ input); String[] splitted = input.split(", "); String[] cards = new String[splitted.length+2]; for(int i = 0; i < splitted.length; i+=2) { cards[i] = splitted[i].concat(splitted[i+1]); System.out.println(cards[i]); } String[] playerCard = cards[0].split(""); System.out.println("The suit of the player's card is " + playerCard[1]); System.out.println("The card's number is " + playerCard[0]); String[] validCards = new String[]{"aa", "aa", "aa"}; int x = 0; for(int i = 1; i <= cards.length-1; i++) { if(cards[i].contains(playerCard[1]) == true) { validCards[x] = cards[i]; x++; System.out.println(validCards[x]); } } boolean foundCard = false; while(foundCard == false) { if(validCards[i].contains(Integer(i).toString()) == true) { foundCard = true; System.out.println(validCards[i]); } else { while(foundCard == false) { if(validCards[i].contains(Integer(i).toString()) == true)) { foundCard = true; System.out.println(validCards[i]); } } } } if(foundCard == false) { System.out.println("NONE"); } } }
2. f(6,3) = 22
- William
import java.util.Scanner; import java.io.*; public class Main { public static void main(String[] args) throws IOException { Scanner in = new Scanner(new FileReader("input.txt")); in.useDelimiter(", |\\s|,"); int cards, reference, best_value; String suits, best_suit; int[] card_numbers = new int[6]; String[] card_suits = new String[6]; for (int repeating_times = 0; repeating_times < 5; repeating_times++){ reference = 0; best_suit = "NONE"; best_value = 10; cards = in.nextInt(); suits = in.next(); card_numbers[reference] = cards; card_suits[reference] = suits; reference++; cards = in.nextInt(); suits = in.next(); card_numbers[reference] = cards; card_suits[reference] = suits; reference++; cards = in.nextInt(); suits = in.next(); card_numbers[reference] = cards; card_suits[reference] = suits; reference++; cards = in.nextInt(); suits = in.next(); card_numbers[reference] = cards; card_suits[reference] = suits; reference++; cards = in.nextInt(); suits = in.next(); card_numbers[reference] = cards; card_suits[reference] = suits; reference++; cards = in.nextInt(); suits = in.next(); card_numbers[reference] = cards; card_suits[reference] = suits; // This is the first case, does nothing to best_value if it does not exist. for (int i = 1; i < 6; i++){ if (card_suits[0].equals(card_suits[i])){ best_suit = card_suits[0]; if (card_numbers[i] > card_numbers[0] && best_value > card_numbers[i]){ best_value = card_numbers[i]; } } } //If the first case is not satisfied, then this calculates the smallest of the same suit. if (best_value == 10) { for (int i = 1; i < 6; i++){ if (card_suits[0].equals(card_suits[i])){ if (card_numbers[i] < card_numbers[0] && card_numbers[i] < best_value){ best_value = card_numbers[i]; } } } } //If no cases are satisfied, then this does either NONE or the best choice of card. if (best_suit.equals("NONE")){ System.out.println("NONE"); } else { System.out.println(best_value + ", " + best_suit); } } } }
2. 22
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner (System.in); System.out.println("Scanner is now ready"); System.out.println("Input whatever you need to."); ArrayList<String> inputcenter = new ArrayList<String>(); for(int i = 0; i<5; i++){ String Input = in.nextLine(); Input = Input.replaceAll(", ",""); inputcenter.add(Input); } int x = 0; while(x<5){ } String lead = inputcenter.get(x).substring(1,1); int y = 2; int z = 3; String sendhelp = ""; String topCard = "9"; String bottemCard = "9"; ArrayList<String> dealerCards = new ArrayList<String>(); ArrayList<String> qualifiedCards = new ArrayList<String>(); for(int i = 1; i <= 5; i++) { sendhelp = inputcenter.get(x).substring(y,z); y = y + 2; z = z + 2; dealerCards.add(sendhelp); } for(int i = 0; i < 5; i++) { if((dealerCards.get(i).charAt(1)) == lead.charAt(1)){ qualifiedCards.add(dealerCards.get(i)); } } for(int i = 0; i < qualifiedCards.size(); i++){ if(qualifiedCards.get(i).charAt(0) > lead.charAt(0)) { if(Character.getNumericValue((qualifiedCards.get(i).charAt(0))) < Character.getNumericValue((topCard.charAt(0)))){ topCard = qualifiedCards.get(i); } } else if(Character.getNumericValue((qualifiedCards.get(i).charAt(0))) < Character.getNumericValue((bottemCard.charAt(0)))){ bottemCard = qualifiedCards.get(i); } else { continue; } } boolean empty = qualifiedCards.isEmpty(); if(empty == true) { System.out.println("None"); } else if(topCard != "99"){ System.out.println(topCard); } else{ System.out.println(bottemCard); } in.close(); } } 2) f(6,3) = 22
2) f(6, 3) = 22
It sounds hard
So far, I only figured out the second one which is f(6, 3) = 22. Still working on the first one
1) There is probably a shorter way but this works-
def game(cards): opp_num = cards[0] opp_suit = cards[1] larger = [] smaller = [] for i in range(2, 11, 2): dealer_num = cards[i] dealer_suits = cards[i + 1] dealer_card = [dealer_num, dealer_suits] if dealer_suits != opp_suit: continue else: if dealer_num > opp_num: if larger == []: larger = dealer_card else: if dealer_num < larger[0]: larger = dealer_card else: if larger == []: smaller = dealer_card else: if dealer_num < smaller[0]: smaller = dealer_card if larger != []: return larger elif smaller != []: return smaller else: return 0 move_list = '' for num in range(0, 5): card_list = input().split(', ') move = game(card_list) if move != 0: move_list += str(move[0] + ', ' + move[1] + '\n') else: move_list += 'NONE\n' print(move_list)
2) f(6, 3) = 22