In [ ]:
%autosave 0
Integer before colon shows number of shift, convert text. If shift is 1, A -> B, Z -> A, b -> c, 2 -> 3.
0:Plain Text
1:Heloo Bob
2:My number is 09970693885
27:Heloo Bob
Plain Text
Ifmpp Cpc
Oa pwodgt ku 21192815007
Ifmpp Cpc
As for text conversion, it's necessary to care of circling such as z to a. There are several methods to handle this.
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='')
In this problem, input text contains shift + ':' + text. There are several methods to treat it.
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:])
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
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)
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)