roy-and-cipher-disk

Roy's friends has been spying on his text messages, so Roy thought of an algorithm to encrypt text messages.  

Encryption Algorithm is as follows:
We say message to be encrypted as Plain Text and encrypted form of message as Cipher.
Plain Text consists of lower case alphabets only.
Consider the Cipher Disk as shown in figure.  

Initially, we start with 0 (zero). For each character in Plain Text, we move either clockwise or anti-clockwise on the disk depending on which way is closest from where we are currently standing.
If both clockwise and anti-clockwise distances are equal, we give priority to clockwise movement.
Clockwise movements are represented using positive numbers while Anti-clockwise movements are represented as negative numbers.

Roy needs your help in implementing this algorithm. Given a Plain Text message, your task is to encrypt it using above algorithm and print the Cipher Text.

Input:
First line contains integer T - number of test cases.
Each of next T lines contains a string representing Plain Text message.  

Output:
For each test case, print the encrypted form of given string in new line.
Each line should consist of space separated integers in the range [-12,13].
See the sample test case for more clarification.  

Constraints:
1 ≤ T ≤ 100
1 ≤ Length of Plain Text string ≤ 100  

Sample Test Case Explanation:
Explanation for 3rd sample test case "correct"SAMPLE INPUT
3
aeiou
hackerearth
correct

SAMPLE OUTPUT
0 4 4 6 6
7 -7 2 8 -6 13 13 -4 -9 2 -12
2 12 3 0 13 -2 -9

Explanation

We begin from 0 (zero)
1. 'a'->'c' - two steps clockwise, we reach 'c'
2. 'c'->'o' - twelve steps clockwise, we reach 'o'
3. 'o'->'r' - three steps clockwise, we reach 'r'
4. 'r'->'r' - we are already at 'r', so zero steps
5. 'r'->'e' - thirteen steps clockwise, we reach 'e'
6. 'e'->'c' - here moving anti-clockwise is optimal, so two steps anticlockwise, and for anticlockwise we add negative sign.
7. 'c'->'t' - again anti-clockwise, nine steps.

Source: hackerearth | Difficulty: EASY
Tags: Data structures

Implementation

Ad-hoc


Solutions:
'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
#print 'Hello World!'
def encrypt(string):
	current=97
	result=""
	for c in string:
		diff=ord(c)-current
		if diff>13:
			diff=diff-26
		elif diff<=-13:
			diff=26+diff
		current=ord(c)
		result+=str(diff)+" "
	return result[:-1]
t=int(input())
if t>=1 and t<=100:
	for i in range(t):
		text=input()
		print(encrypt(text))


#!/usr/bin/python
l=[-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13]
pattern=[]
for i in range(26):
#	if(i>13):
#		pattern.append(26-i)
#	else:
	pattern.append(i)
#print pattern
N=eval(input())
for n in range(N):
	a=input()
	prev="a"
	for i in a:
	if(i==prev):
		print(0, end=' ')
	else:
		x1=pattern[ord(i)-97]
		x2=pattern[ord(i)-97]-26
		y1=pattern[ord(prev)-97]
		y2=pattern[ord(prev)-97]-26
		w=-(y1-x1)
		x=-(y1-x2)
		y=-(y2-x1)
		z=-(y2-x2)
		amin=999999999
		if(w in l):
		if(abs(w)<amin):
			amin=w
		if(x in l):
		if(abs(x)<amin):
			amin=x

		if(y in l):
		if(abs(y)<amin):
			amin=y

		if(z in l):
		if(abs(z)<amin):
			amin=z
#	print w,x,y,z,amin,"\n",
		print(amin, end=' ')
		prev=i
	print()


arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g','h','i', 'j', 'k', 'l', 'm', 'n','o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ]
t = eval(input())
for i in range(t):
	init = 0
	s = input()
	for x in s:
		if(arr.index(x)==init):
			print(0, end=' ')
		elif(arr.index(x)<init):
			anticlock = init-arr.index(x)
			clock = 26-anticlock
			if(anticlock<clock):
				print(0-anticlock, end=' ')
			else:
				print(clock, end=' ')
		else:
			clock = arr.index(x)-init
			anticlock = 26-clock
			if(anticlock<clock):
				print(0-anticlock, end=' ')
			else:
				print(clock, end=' ')
		init = arr.index(x)
	print("")


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
def roy_and_cipher_disk(word):
	base = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1]
	current = 0
	output = ''
	for character in word:
		index = ((ord(character) - ord('a')) - current)

		current = (ord(character) - ord('a'))
		output = output + str(base[index % 26]) + ' '
	print(output.strip())


if __name__ == '__main__':
	cases = int(input())
	start = 1
	while start <= cases:
		word = input().strip()
		roy_and_cipher_disk(word)
		start = start + 1


import string
import math
s=list(string.ascii_lowercase)
op=[]
for i in range(int(eval(input()))):
	plain=input()
	prev='a'
	for a in plain:
		curr=a
		if curr>=prev:
			diff=int((s.index(curr)-s.index(prev)))
			if diff > 13:
				diff = (s.index(prev) + 26-s.index(curr))*-1
			op.append(diff)
		else:
			diff=25+s.index(curr)-s.index(prev)+1
			if diff>13:
				diff=s.index(curr)-s.index(prev)
			op.append(diff)
		prev=curr

	for el in op:
		print(el, end=' ')
	print("")
	op=[]
	prev='a'


for _ in range(eval(input())):
	text = input()
	pos = 0
	output = ''
	for c in text:
		move = (ord(c) - ord('a') - pos) % 26 
		output += str(move if move <= 13 else move - 26) + ' '
		pos = pos + move
	print(output[:-1])


n=int(input())
for i in range(n):
	s=input().strip()
	l=len(s)
	cur=0
	
	for j in range(l):
		c=ord(s[j])-97
		q=cur
		ccount=0
		acount=0
		while(1):
			if q==c: 
				break
			q=(q+1)%26
			ccount+=1
		q=cur
		while(1):
			if q==c:
				break
			q=(q-1+26)%26
			acount+=1
		if ccount<=acount:
			print(ccount, end=' ')
		else:
			print(-1*acount, end=' ')
		cur=c
	print()


import string
keys=string.ascii_lowercase #for getting lower alpha and using them as keys
dic={}
a=0

for i in keys:#---->setting dictionary
	dic[i]=a
	a+=1

t=eval(input())
while t:
	t-=1
	out=[]
	line=input()
	for w in range(len(line)):
		if w==0:
			if dic[line[w]]<=13:
				out.append(dic[line[w]])
			else:
				out.append(dic[line[w]]-26)
		else:
			if 0<=dic[line[w]]-dic[line[w-1]]<=13:
				out.append(dic[line[w]]-dic[line[w-1]])
			
			elif 13<dic[line[w]]-dic[line[w-1]]:
				out.append(dic[line[w]]-26-dic[line[w-1]])
			elif -13<dic[line[w]]-dic[line[w-1]]<0:
				out.append(dic[line[w]]-dic[line[w-1]])
			elif dic[line[w]]-dic[line[w-1]]<-13:
				out.append((dic[line[w]]-(dic[line[w-1]]-26)))
			elif dic[line[w]]-dic[line[w-1]]==-13:
				out.append(-dic[line[w]]+dic[line[w-1]])
			
	for i in out:
		print(i, end=' ')
	print()        
	


from string import ascii_lowercase
disk = list(ascii_lowercase)      #All alphabets arranged in order , numbered from 0 to 25
pointer =0
cases = input()
cases = int(cases)
if cases<=100 and cases>=1:
	ans=""
	while (cases > 0):
		pointer=0
		ans=""
		text = input()
		if len(text) >=1 and len(text)<=100:
			for i in text:
				z = abs(disk.index(i)-pointer)   #Make this absolute
				if z<13 and (disk.index(i)-pointer)>=0:
					ans+=str(z)
				elif z<13 and (disk.index(i)-pointer)<0:
					ans+=str(-1*z)
				elif z==13 or z==-13:
					ans+=str(13)
				elif z>13 and (disk.index(i)-pointer)<0:
					ans+=str((13-(z%13)))
				else:
					ans+=str(-1*(13-(z%13)))
				pointer=disk.index(i)
				ans+=" "
		print(str(ans))
		cases-=1


cipher = []
msgArr = []


noOfInput = eval(input())
for i in range(0,noOfInput):
	msg = input()
	msg = 'a'+msg
	msgArr.append(msg)
	

def findcipher(msg,ind):
	answer = []
	for i in range (0,len(msg)-1):
		
		diff = ord(msg[i])-ord(msg[i+1])
		if not diff:
			answer.append(0)	
		else:
			if(diff>0):
				newdiff = (26-( ord(msg[i]) - ord('a') ) ) + (ord(msg[i+1]))-ord('a')	
				if diff < newdiff :
					answer.append(-diff)
				else:
					answer.append(newdiff)
			if(diff<0):
				newdiff=(ord(msg[i])-ord('a'))	+ (26-( ord(msg[i+1]) - ord('a') ) )
				if abs(diff) <= newdiff:
					answer.append(-diff)
				else:
					answer.append(-newdiff)  	
	cipher.append(answer)  
 
for i in range(0,noOfInput):
	findcipher(msgArr[i],i)	
for one in cipher:
	
	for element in one:
		print(element, end=' ')
	print('')


def roy_and_cipher_disk():
	base = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1]
	pre = []
	for i in range(26):
		tmp = base[(26-i):] + base[:(26-i)]
		pre.append(tmp)
	for t in range(eval(input())):
		s = input()
		slen = len(s)
		res = []
		curr = 'a'
		for i in range(slen):
			res.append(str(pre[ord(curr)-ord('a')][ord(s[i])-ord('a')]))
			curr = s[i]
		print(' '.join(res))
roy_and_cipher_disk()


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
import sys

N = int(eval(input()))
for i in range(N):
	s = input()
	num = 97
	for c in s:
		tmp = ord(c)
		if tmp > num:
			t1 = tmp - num
			t2 = 26 - t1
			if t1 <= t2:
				sys.stdout.write("%d " % t1)
			elif t1 > t2:
				sys.stdout.write("%d " % -t2)
		elif tmp < num:
			t1 = abs(tmp - num)
			t2 = 26 - t1
			if t1 < t2:
				sys.stdout.write("%d " % -t1)
			elif t1 >= t2:
				sys.stdout.write("%d " % t2)
		else:
			sys.stdout.write("0 ")
		num = tmp
	sys.stdout.write("\n")


#!/usr/bin/env python
#-*- coding: utf-8 -*-

from collections import defaultdict

l = list(range(0, 26)) * 5000

T = int (input ())
for t in range (T):
	s = input ()
	pos = 2522
	for i in s:
		c = ord (i) - ord ('a')
		x, y = 0, 0
		cnt = 0
		for i in range (pos, pos + 50):
			if l [i] == c:
				x = cnt
				break
			cnt += 1
		cnt = 0
		for i in range (pos, pos - 50, -1):
			if l [i] == c:
				y = cnt
				break
			cnt -= 1
		# print pos, c, x, y
		if abs (x) <= abs (y):
			print(x, end=' ')
			pos += x
		else:
			print(y, end=' ')
			pos += y
	print()




for _ in range (int(input())):
	st=input()
	l=len(st)
	j=97
	for i in range (l):
		k=ord(st[i])
		if abs(k-j)>abs(26-(k-j)):
			print((k-j-26), end=' ')
		elif k-j==-13 or k-j==13:
			print(("13"), end=' ')
		elif abs(k-j)>abs(26+(k-j)):
			print((26+(k-j)), end=' ')
		else:
			print((k-j), end=' ')
		j=k
	print();




t=int(input())
while t>0:
	text=input()
	current=0
	for letter in text:
		character=ord(letter)-97
		diff=character-current
		if diff>=0 and diff<=13:
			print(diff, end=' ')
			current=character
			continue
		elif diff>13:
			diff=diff-26
			print(diff, end=' ')
			current=character
			continue
		elif diff<-12:
			diff=diff+26
			print(diff, end=' ')
			current=character
			continue
		elif diff<0 and diff>=-12:
			print(diff, end=' ')
			current=character
			continue
	t=t-1
	print('')


t=int(input())
while t:
	t-=1
	msg=input()
	cur=0

	for i in range(len(msg)):
		p=ord(msg[i])-97
		from_beg=0
		from_end=0
		temp=cur
		while 1:
			if temp==p:
				break
			from_beg+=1
			temp=(temp+1)%26
		
		temp=cur
		while 1:
			if temp==p:
				break
			from_end+=1
			temp=(temp+25)%26
			
		if from_beg<=from_end:
			print(from_beg, end=' ')
		else:
			print(-1*from_end, end=' ')
		cur=p
	print()
		


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
alfdict = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
for i in range(eval(input())):
	source = 0
	target = 0
	opseq = []
	for val in input():
		target = alfdict[val]
		diff = target - source
		if abs(diff) < 13:
			opseq.append(diff)
		elif abs(diff) == 13:
			opseq.append(13)
		elif abs(diff) > 13:
			sign = None
			if diff < 0:
				sign = 1
			else:
				sign = -1
			opseq.append(sign*(26%abs(diff)))
		source = target
	oput = str(opseq)[1:-1]
	print(oput.replace(',', ''))


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
numOfTestCase=int(input())
for i in range(numOfTestCase):
	strInput=input()
	strInput='a'+strInput
	for j in range(len(strInput)-1):	
		first=ord(strInput[j])
		sec=ord(strInput[j+1])
		
		if sec>=first:
			gap=sec-first
			if gap<=13:
				print(gap, end=' ')
			else:
				print(-(13-(gap-13)), end=' ')
		else:
			gap= first-sec
			if gap<13:
				print(-gap, end=' ')
			else:
				print(13-(gap-13), end=' ')
	print('')	


t = int(input())
dic = {}
for i,j in zip('abcdefghijklmnopqrstuvwxyz',list(range(26))):
	dic[i]=j

while t > 0:
	t-=1
	s = input()
	curr = 'a'
	l = []
	for i in s:
		d1 = dic[i] - dic[curr]
		if d1 < 0:
			d1 = 26 + d1
		d2 = d1 - 26
		d = d1 if abs(d1) <= abs(d2) else d2
	#	print curr,d1,d2
		l.append(d)
		curr = i
	for j in l[:-1]:
		print(str(j), end=' ')
	print(str(l[-1]))


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
import string
d = dict(list(zip(string.ascii_lowercase, list(range(0, 26)))))
repeat = int(input())
for i in range(repeat):
	output = []
	init = 'a'
	strin = input()
	for letter in strin:
		diff =(d[letter] - d[init])
		if abs( diff )== 13:
			output.append(abs(diff))
		elif abs(diff) <= 13:
			output.append(diff)
		elif diff > 0:
			output.append(diff-26)
		else:
			output.append(diff+26)
		init = letter
	print(' '.join(str(x) for x in output))


t=eval(input())
for i in range (0,t):
	s=input()
	c=0
	for i in range(0,len(s)):
		d=ord(s[i])
		d=d-97
		e=d-c
		f=26-abs(e)
		c=d
		if abs(e)<f:
			print(e, end=' ')
		else:
			if e>13:
				print(-f, end=' ')
			else:
				print(f, end=' ')
	print()


t = int(input())
for kappa in range(t):
	l = []
	start = 0
	string  = input()
	for letter in string:
		end = ord(letter)-97
		diff = end - start
		if 0<=start<=12:
			if 0<=diff<=13 or (-12)<=diff<0:
				l.append(diff)
			else:
				jump = start + (26-end)
				l.append(-jump)
		
		else:
			if (-12)<=diff<0 or 0<=diff<=12:
				l.append(diff)
			else:
				jump = (26 - start) + end
				l.append(jump)
		start = end
	
	l = list(map(str,l))
	print(' '.join(l))
	


'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
t = int(input())
for kappa in range(t):
	l = []
	start = 0
	string  = input()
	for letter in string:
		end = ord(letter)-97
		diff = end - start
		if 0<=start<=12:
			if 0<=diff<=13 or (-12)<=diff<0:
				l.append(diff)
			else:
				jump = start + (26-end)
				l.append(-jump)
		
		else:
			if (-12)<=diff<0 or 0<=diff<=12:
				l.append(diff)
			else:
				jump = (26 - start) + end
				l.append(jump)
		start = end
	
	l = list(map(str,l))
	print(' '.join(l))
				
	
	


n = eval(input())


for i in range(0, n):
	value = list(input())
	value.insert(0,'a')
	for i in range(1, len(value)):
		diff = ord(value[i]) - ord(value[i - 1])
		if abs(diff) > 13:
			if diff > 0:
				print(diff - 26, end=' ')
			else:
				print(26 + diff, end=' ')
		elif diff==-13:
			print(13, end=' ')
		else:
			print(diff, end=' ')
	print() 




'''
# Read input from stdin and provide input before running code

name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
import string
d = dict(list(zip(string.ascii_lowercase, list(range(0, 26)))))
repeat = int(input())
for i in range(repeat):
	output = []
	init = 'a'
	strin = input()
	for letter in strin:
		diff =(d[letter] - d[init])
		if abs( diff )== 13:
			output.append(abs(diff))
		elif abs(diff) <= 13:
			output.append(diff)
		elif diff > 0:
			output.append(diff-26)
		else:
			output.append(diff+26)
		init = letter
	print(' '.join(str(x) for x in output))


t=eval(input())
for i in range (0,t):
	s=input()
	c=0
	for i in range(0,len(s)):
		d=ord(s[i])
		d=d-97
		e=d-c
		f=26-abs(e)
		c=d
		if abs(e)<f:
			print(e, end=' ')
		else:
			if e>13:
				print(-f, end=' ')
			else:
				print(f, end=' ')
	print()