In [ ]:
%autosave 0

UCSY Competitive Programming Training Test-1 (21, Feb, 2018)

Problem-A

Integer before colon shows number of shift, convert text. If shift is 1, A -> B, Z -> A, b -> c, 2 -> 3.

Sample Input

0:Plain Text
1:Heloo Bob
2:My number is 09970693885
27:Heloo Bob

Sample Output

Plain Text
Ifmpp Cpc
Oa pwodgt ku 21192815007
Ifmpp Cpc

How to solve Problem-A

As for text conversion, it's necessary to care of circling such as z to a. There are several methods to handle this.

  • Create the string which contains ABC...XYZABC....Z
  • Calculate the (offset(index) of original string + shift) mod sizeof(alphabet or digit)

Method-1 is simpler, Method-2 requires less memory and smarter. Method-1 will be better in programming contest, and Method-2 will be better in production code.


In [ ]:
# Sample code of method 1
#!/usr/bin/python3
# -*- coding: utf-8 -*-

#import sys
import string

infile = open('a.in', 'r')
low_ascii = string.ascii_lowercase * 2
high_ascii = string.ascii_uppercase * 2
digit = string.digits * 2

for s in infile:
    if s == '#\n':
        break
    shift_s, text = s.split(':')
    # print(shift_s, text)
    shift_ascii = int(shift_s) % len(string.ascii_lowercase)
    shift_digit = int(shift_s) % len(string.digits)
    
    for ch in text:
        if ch in low_ascii:
            print(low_ascii[low_ascii.index(ch)+shift_ascii], end='')
        elif ch in high_ascii:
            print(high_ascii[high_ascii.index(ch)+shift_ascii], end='')
        elif ch in digit:
            print(digit[digit.index(ch)+shift_digit], end='')
        else:
            print(ch, end='')

In [ ]:
# Sample code of method 2
#!/usr/bin/python3
# -*- coding: utf-8 -*-

#import sys
import string

infile = open('a.in', 'r')
low_ascii = string.ascii_lowercase
high_ascii = string.ascii_uppercase
digit = string.digits

for s in infile:
    if s == '#\n':
        break
    shift_s, text = s.split(':')
    shift = int(shift_s)
    
    for ch in text:
        if ch in low_ascii:
            print(low_ascii[(low_ascii.index(ch)+shift) % len(low_ascii)], end='')
        elif ch in high_ascii:
            print(high_ascii[(high_ascii.index(ch)+shift) % len(high_ascii)], end='')
        elif ch in digit:
            print(digit[(digit.index(ch)+shift) % len(digit)], end='')
        else:
            print(ch, end='')

Text handling

In this problem, input text contains shift + ':' + text. There are several methods to treat it.

  • Use split() method of string (recommended)
  • Use index() method of string, and separate string using slice (str[start:end])
  • Search the location of ':' by checking each character in the string, then use slice

In [ ]:
# Method-1

s = '111:Text text 123 text'
shift, text = s.split(':')
print(shift)
print(text)

In [ ]:
# Method-2

s = '111:Text text 123 text'
p_col = s.index(':')
print(s[:p_col])
print(s[p_col+1:])

In [ ]:
# Method-3

s = '111:Text text 123 text'
p_col = 0
for ch in s:
    if ch == ':':
        break
    p_col += 1
print(s[:p_col])
print(s[p_col+1:])

Text handling, new line character

It is necessary to take care of new line('\n'). readline() method returns text which contains '\n' (iterable file object also returns this format of data). If new line character is to be removed, strip() method removes space tab and new line before and after the text.


In [ ]:
s = '   3 space\t(tab)\n'
print(s.strip())
s ='\t(tab)\n 2nd line\n'
print(s.strip())
s ='\n\nNew Line\n\n\n'
print(s.strip())

In [ ]:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

#import sys
import string

infile = open('a.in', 'r')
low_ascii = string.ascii_lowercase * 2
high_ascii = string.ascii_uppercase * 2
digit = string.digits * 2

for s in infile:
    if s == '#\n':
        break
    shift_s, text = s.strip().split(':')  # Remove \n before split()
    # print(shift_s, text)
    shift_ascii = int(shift_s) % len(string.ascii_lowercase)
    shift_digit = int(shift_s) % len(string.digits)
    
    for ch in text:
        if ch in low_ascii:
            print(low_ascii[low_ascii.index(ch)+shift_ascii], end='')
        elif ch in high_ascii:
            print(high_ascii[high_ascii.index(ch)+shift_ascii], end='')
        elif ch in digit:
            print(digit[digit.index(ch)+shift_digit], end='')
        else:
            print(ch, end='')
            
    print()   # New line for each output

Problem B. Sum of Primes

Input: 1st Line: Number of test Case, 2nd and later Even Number, Output: Number of pairs of Prime number which sum is equal to given even number.

Sample Input

3
18
30
22

Sample Output

2
3
3

In [5]:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

#import sys

infile = open('b.in', 'r')
n = int(infile.readline())
even_num = list()
for i in range(n):
    num = int(infile.readline())
    even_num.append(num)

max_prime = max(even_num)

# Sieve of Eratosthenes
prime_map = [True for i in range(max_prime+1)]
prime_map[0] = prime_map[1] = False  # Not Prime

prime_list = list()
for i in range(2, max_prime+1):
    if prime_map[i] == True:
        prime_list.append(i)
        for mop in range(i*2, max_prime+1, i):
            prime_map[mop] = False

# print(prime_map) # Debug
# print(prime_list) # Debug

# print(even_num)
for i in even_num:
    half = i // 2
    num_combi = 0
    for p in prime_list:
        if p > half:
            break
        if (i-p) in prime_list:
            # print(p, i-p)   # Debug
            num_combi += 1

    print(num_combi)


2
3
3

Problem C. Twin Prime Numbers

Print the number of twin prime number in specific range. Twin Prime number: Both P and P+2 are prime number, for example 3 and 5, 11 and 13.


In [ ]:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

def twin_prime(min_range, max_range):

    prime_map = [True for i in range(max_range+1)]
    prime_map[0] = prime_map[1] = False  # Not Prime
    
    for i in range(2, max_range+1):  # include max_range
        if prime_map[i] == True:
            for mop in range(i*2, max_range+1, i):
                prime_map[mop] = False
    
    num_twin = 0
    for i in range(min_range, max_range-1):  # include max_range
        if prime_map[i] and prime_map[i+2]:
            print(i, i+2, file=sys.stderr) # Debug
            num_twin += 1

    print(num_twin)

infile = open('c.in', 'r')
n = int(infile.readline())
for i in range(n):
    min_range, max_range = map(int,infile.readline().split())
    twin_prime(min_range, max_range)

Tips

  • Use proper variable names (mop: multiple of prime,cleaning equipment to wipe)
  • Insert debug statement when writing program, then remove. Adding debug statement while debugging is not efficient !!!
  • Debug in small portion. Do not try to write large program then debug.
  • Usually stderr can be used to debug output