Skip to content

Commit 9e759e2

Browse files
committed
Added jan contests
1 parent 7cea34e commit 9e759e2

File tree

11 files changed

+190
-0
lines changed

11 files changed

+190
-0
lines changed
File renamed without changes.

_posts/2019-01-21-jan-contests.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
layout: post
3+
title: Practice Contests for January ACSL
4+
date: 2019-1-21
5+
author: Sanjit Bhat
6+
summary: Problems and solutions from 2016-17 and 2017-18 January ACSL
7+
categories: comp2
8+
---
9+
10+
# 2016-17 Contest
11+
12+
* [Short answers]({{ site.url }}/cpt-blog/contests/16-17/c2/c_2_sr.pdf)
13+
14+
* [Short answer solutions]({{ site.url }}/cpt-blog/contests/16-17/c2/c_2_sr_sol.pdf)
15+
16+
* [Coding problem]({{ site.url }}/cpt-blog/contests/16-17/c2/Ascending_sr_2.pdf) (including test data)
17+
18+
* [Sanjit's solution]({{ site.url }}/cpt-blog/contests/16-17/c2/ascending_sanjit_bhat.java) to coding problem
19+
20+
# 2017-18 Contest
21+
22+
* [Short answers]({{ site.url }}/cpt-blog/contests/17-18/c2/c_2_sr.pdf)
23+
24+
* [Short answer solutions]({{ site.url }}/cpt-blog/contests/17-18/c2/c_2_sr_sol.pdf)
25+
26+
* [Coding problem]({{ site.url }}/cpt-blog/contests/17-18/c2/c_2_enclosure_sr.pdf) (including test data)
27+
28+
* [Sanjit's solution]({{ site.url }}/cpt-blog/contests/17-18/c2/enclosure_sanjit_bhat.java) to coding problem
122 KB
Binary file not shown.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Sanjit Bhat
3+
ACSL Ascending Strings
4+
Contest #2 2016-17
5+
Acton Boxborough Regional High School
6+
Senior Division
7+
*/
8+
9+
import java.io.*;
10+
import java.util.*;
11+
12+
public class ascending_sanjit_bhat {
13+
static String your_name = "Sanjit Bhat";
14+
static String prob_name = "ascending";
15+
16+
public static void main(String[] args) throws IOException {
17+
Scanner sc = new Scanner(new File(prob_name + ".in"));
18+
PrintWriter out = new PrintWriter(new FileWriter(prob_name + ".out"), true);
19+
out.println(your_name);
20+
21+
for (int test_case = 0; test_case < 5; test_case++) {
22+
try {
23+
String raw_string = sc.nextLine();
24+
25+
// first number will always be leftmost digit
26+
int curr_highest = Integer.parseInt(raw_string.substring(0, 1));
27+
raw_string = raw_string.substring(1);
28+
out.print(curr_highest + " ");
29+
30+
int curr_num = 0;
31+
boolean is_left = false; // whether we pull new digits from left or right side
32+
while (curr_num <= curr_highest && raw_string.length() > 0) {
33+
int string_length = raw_string.length();
34+
if (is_left) {
35+
// pull digit from left side
36+
curr_num = curr_num * 10 + Integer.parseInt(raw_string.substring(0, 1));
37+
raw_string = raw_string.substring(1);
38+
} else {
39+
// pull digit from right side
40+
curr_num = curr_num * 10 + Integer.parseInt(raw_string.substring(string_length-1, string_length));
41+
raw_string = raw_string.substring(0, string_length-1);
42+
}
43+
if (curr_num > curr_highest) {
44+
// reset vars and print out if total number pulled is larger than previous
45+
out.print(curr_num + " ");
46+
curr_highest = curr_num;
47+
curr_num = 0;
48+
is_left = !is_left;
49+
}
50+
}
51+
out.println();
52+
} catch (Exception e) {
53+
out.println("Something's wrong with this test case");
54+
e.printStackTrace();
55+
}
56+
}
57+
sc.close();
58+
out.close();
59+
}
60+
}

contests/16-17/c2/c_2_sr.pdf

128 KB
Binary file not shown.

contests/16-17/c2/c_2_sr_sol.pdf

119 KB
Binary file not shown.
82 KB
Binary file not shown.

contests/17-18/c2/c_2_sr.pdf

540 KB
Binary file not shown.

contests/17-18/c2/c_2_sr_sol.pdf

534 KB
Binary file not shown.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Sanjit Bhat
3+
ACSL Enclosure
4+
Contest #2 2017-18
5+
Acton Boxborough Regional High School
6+
Senior Division
7+
*/
8+
9+
import java.io.*;
10+
import java.util.*;
11+
12+
public class enclosure_sanjit_bhat {
13+
static String your_name = "Sanjit Bhat";
14+
static String prob_name = "enclosure";
15+
16+
public static void main(String[] args) throws IOException {
17+
Scanner sc = new Scanner(new File(prob_name + ".in"));
18+
PrintWriter out = new PrintWriter(new FileWriter(prob_name + ".out"), true);
19+
out.println(your_name);
20+
21+
// Note: watch out when testing this problem. ACSL incorrectly uses different '-' chars in different test cases
22+
for (int test_case = 0; test_case < 10; test_case++) {
23+
try {
24+
// read in input
25+
String raw_input = sc.nextLine();
26+
raw_input = "+" + raw_input + "+"; // for one indexing and end curly brace case
27+
28+
// identify missing enclosure
29+
char[] operators = {'+', '−', '–', '*', '/'}; // see the note in line 21
30+
char[] enclosures = {'{', '[', '(', ')', ']', '}'};
31+
boolean[] enclosures_exist = new boolean[6];
32+
for (int i = 0; i < enclosures.length; i++) {
33+
if (raw_input.indexOf(enclosures[i]) != -1) enclosures_exist[i] = true;
34+
}
35+
int missing = 0;
36+
for (int i = 0; i < 6; i++) {
37+
if (enclosures_exist[i] && !enclosures_exist[6-i-1]) {
38+
missing = 6-i-1;
39+
}
40+
}
41+
char conj_missing = enclosures[6-missing-1];
42+
43+
// reverse string if missing starting enclosure to simplify for loop stack simulation
44+
raw_input = (missing < 3) ? (new StringBuilder(raw_input)).reverse().toString() : raw_input;
45+
46+
ArrayList<Integer> output_locs = new ArrayList<>();
47+
Stack<Character> st_enclosure = new Stack<>(); // this is the KEY data structure for enclosure matching
48+
boolean only_nums = true;
49+
50+
for (int i = 0; i < raw_input.length(); i++) {
51+
char curr_char = raw_input.charAt(i);
52+
53+
boolean is_enclosure = false;
54+
char conj_enclosure = 'x';
55+
for (int j = 0; j < enclosures.length; j++) {
56+
if (curr_char == enclosures[j]) {
57+
is_enclosure = true;
58+
conj_enclosure = enclosures[6-j-1];
59+
}
60+
}
61+
62+
boolean is_operator = false;
63+
for (char c : operators) {
64+
if (curr_char == c) is_operator = true;
65+
}
66+
67+
// if start enclosure (conjugate not in stack), push first
68+
if (is_enclosure && st_enclosure.search(conj_enclosure) == -1) st_enclosure.push(curr_char);
69+
70+
// checks for 1) more than single int enclosed, 2) enclosures matched, 3) non-number
71+
if (!only_nums && !st_enclosure.isEmpty() && st_enclosure.peek() == conj_missing && (is_operator || is_enclosure)) output_locs.add(i);
72+
73+
// if end enclosure (conjugate in stack), 2 cases
74+
if (is_enclosure && st_enclosure.search(conj_enclosure) != -1) {
75+
if (st_enclosure.peek() == conj_enclosure) {
76+
st_enclosure.pop();
77+
} else st_enclosure.push(curr_char);
78+
}
79+
80+
// 1) conjugate of missing enclosure there and 2) operator there -> we have more than single int enclosed
81+
if (st_enclosure.search(conj_missing) != -1 && is_operator) only_nums = false;
82+
}
83+
84+
// output answers
85+
for (int i = 0; i < output_locs.size(); i++) {
86+
if (missing < 3) {
87+
// deal with reversed string indexing
88+
out.print(raw_input.length() - output_locs.get(output_locs.size()-i-1));
89+
} else out.print(output_locs.get(i));
90+
if (i != output_locs.size()-1) out.print(", ");
91+
}
92+
out.println();
93+
94+
} catch (Exception e) {
95+
out.println("Something's wrong with this test case");
96+
e.printStackTrace();
97+
}
98+
}
99+
sc.close();
100+
out.close();
101+
}
102+
}

0 commit comments

Comments
 (0)