What does this program do:
Continue the programming problem from last time:
Sample input:
2, 5, 3, 5, 4, 4, 3
3, 4, 5, 3, 2, 2, 1
2, 1, 2, 4, 5
1
2
3
4
5
Sample output:
1
0
0
3
3
Test input:
1, 1, 2, 4, 3, 4, 1
2, 3, 1, 5, 4, 2, 4
1, 2, 4, 2, 1
2
5
1
2
4
Test output:
3
1
0
0
0
ANSWERS:
What does this program do:
55.
The three arrays look like this after the program has finished (notice the patterns! Almost every question like this has a pattern and it helps speed things up):
Programming (sorry for any formatting issues):
My python solution:
def remove(hand): count = [0,0,0,0,0] for i in hand: count[i-1]+=1 for i in range(5): if count[i]>=4: while i+1 in hand: hand.remove(i+1) return hand opp = input().split(", ") pla = input().split(", ") dec = input().split(", ") oppo = [] play = [] deck = [] for i in range(7): oppo.append(int(opp[i])) play.append(int(pla[i])) for i in range(5): deck.append(int(dec[i])) for i in range(5): num = int(input()) if num in oppo: while num in oppo: oppo.remove(num) play.append(num) else: play.append(deck[0]) del deck[0] play = remove(play) count = 0 for i in play: if i==num: count += 1 print("output:",count)
My java solution:
import java.util.*; class Main { public static ArrayList<Integer> remove(ArrayList<Integer> hand){ int[] count = new int[5]; for(int i : hand){ count[i-1]++; } for(int i=0; i<5; i++){ if(count[i]>=4){ for(int j=0; j<hand.size(); j++){ if(hand.get(j)==i+1){ hand.remove(j); j--; } } } } return hand; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] opp = in.nextLine().split(", "); String[] pla = in.nextLine().split(", "); String[] dec = in.nextLine().split(", "); ArrayList<Integer> oppo = new ArrayList<Integer>(); ArrayList<Integer> play = new ArrayList<Integer>(); ArrayList<Integer> deck = new ArrayList<Integer>(); for(int i=0; i<7; i++){ oppo.add(Integer.parseInt(opp[i])); play.add(Integer.parseInt(pla[i])); } for(int i=0; i<5; i++){ deck.add(Integer.parseInt(dec[i])); } for(int i=0; i<5; i++){ int num = in.nextInt(); if(oppo.contains(num)){ for(int j=0; j<oppo.size(); j++){ if(oppo.get(j)==num){ oppo.remove(j); play.add(num); j--; } } } else { play.add(deck.get(0)); deck.remove(0); } play = remove(play); int count = 0; for(int j : play){ if(j==num){ count++; } } System.out.println(count); } } }
46
https://www.codiva.io/p/4691b42c-747c-43b0-b867-24ddf7fd0edc (still work in progress)