3-point problems, Woburn Collegiate Institute Programming Enrichment Group Online Judge

PEG Judge - A plus B

Given two integers a and b, output the sum of a and b. A and B, as well as A+B will be less than 10000 in absolute value.

1
2
3
def a_plus_b():
    xs = (int(x) for x in (input(), input()))
    print(sum(xs))

PEG Judge - BlueBook - Grades

Given T (1 ≤ T ≤ 100) integers in the range (-1000, 1000), representing percentages, calculate which letter grade each of them correspond to.

The mark table is as follows:

A 80 - 100
B 70 - 79
C 60 - 69
D 50 - 59
F 0 - 49
X Anything else
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def get_grade(num):
    grades = [[80, "A"], [70, "B"], 
              [60, "C"], [50, "D"],
              [0, "F"]]
    for g in grades:
        gnum, glet = g
        if num >= gnum:
            return glet


def blue_book_grades():
    cases = int(input())
    for case in range(cases):
        num = int(input())
        if num < 0 or num > 100:
            print("X")
        else:
            print(get_grade(num))

PEG Judge - Primes

Given an integer N ≤ 100, output the first N primes, one per line.

Brute force solution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def is_prime(n):
    if n < 2:
        return False
    if n == 2:
        return True
    for i in range(2, n):
        if not n % i:
            return False
    return True


def primes():
    lim = int(input())
    n = 2
    while lim > 0:
        if is_prime(n):
            print(n)
            lim -= 1
        n += 1

PEG Judge - BlueBook - Leap Year

Given T (1 ≤ T ≤ 10000) non-negative year values, where each year Y is 0 ≤ Y ≤ 1000000, determine whether or not each one is a leap year.

Remember: A year is a leap year if it is divisible by 4 but not by 100 or if it is divisible by 400. 0 is considered a leap year.

1
2
3
4
5
6
7
8
def is_leap_year(y):
    return not y % 400 or (y % 100 and not y % 4)


def blue_book_leap_year():
    cases = int(input())
    for case in range(cases):
        print('1' if is_leap_year(int(input())) else '0')

PEG Judge - BlueBook - Min

Find the minimum in a list of real numbers.

Input: the first line contains an integer N (1 ≤ N ≤ 10 000). Each of the following N lines contains one real number x (-1000 < x < 1000).

Output: the minimum of the N real numbers given, rounded to two decimal places.

Solved without using built-in min().

1
2
3
4
5
6
7
8
9
10
def min_():
    cases = int(input())
    min = float(input())
    cases -= 1
    while cases > 0:
        next = float(input())
        cases -= 1
        if next < min:
            min = next
    print(f"{round(min, 2):.02f}")

PEG Judge - BlueBook - Max

Given T real numbers, find and output the largest one to four decimal places.

Input: the first line contains a single integer T (1 ≤ T ≤ 1000000). Each of the following T lines contains one real number x (-106 ≤ x ≤ 106).

Output: a single line: the largest of the T real numbers.

Solved without using built-in max().

1
2
3
4
5
6
7
8
9
10
def max_():
    cases = int(input())
    max = float(input())
    cases -= 1
    while cases > 0:
        next = float(input())
        cases -= 1
        if next > max:
            max = next
    print(f"{round(max, 4):0.04f}")

PEG Judge - BlueBook - Direction

Given an integer N (0 ≤ N < 360), print out the closest compass bearing to that integer if N is the bearing in degrees. If there is a tie, print the “smaller” of the two possible outputs.

The bearings arranged in increasing order of “size” are: N, E, S, W. This means choose N over E if the input is 45, and N over W when the input is 315.

Input: the input consists of an integer T (1 ≤ T ≤ 1000), denoting the number of test cases and then each line following consists of one integer N.

Output: output the closest compass bearing according to the input.

1
2
3
4
5
6
7
8
9
10
11
12
def direction():
    segments = ((45, "N"), (135, "E"), (225, "S"), (314, "W"))
    cases = int(input())
    while cases > 0:
        bearing = int(input())
        for seg in segments:
            if bearing <= seg[0]:
                print(seg[1])
                break
        if bearing > seg[0]:
            print(segments[0][1])
        cases -= 1