Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

blockquote {
  font-family: Menlo, Monaco, "Courier New", monospace;
  color: #333333;
  display: block;
  padding: 8.5px;
  margin: 0 0 9px;
  font-size: 12px;
  line-height: 18px;
  background-color: #f5f5f5;
  border: 1px solid #ccc;
  border: 1px solid rgba(0, 0, 0, 0.15);
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
  white-space: pre;
  white-space: pre-wrap;
  word-break: break-all;
  word-wrap: break-word;
}

Problem Statement

We can describe detailed direction by repeating the directional names: north, south, east and west. For example, northwest is the direction halfway between north and west, and northnorthwest is between north and northwest. 

In this problem, we describe more detailed direction between north and west as follows.

"north" means $0$ degrees.

"west" means $90$ degrees.

If the direction $dir$ means $a$ degrees and the sum of the occurrences of "north" and "west" in $dir$ is $n$ ($\geq$ 1),
"north"$dir$ (the concatenation of "north" and $dir$) means $a - \frac{90}{2^n}$ degrees and "west"$dir$ means $a + \frac{90}{2^n}$ degrees.

Your task is to calculate the angle in degrees described by the given direction.

Input

The input contains several datasets. The number of datasets does not exceed $100$. 

Each dataset is described by a single line that contains a string denoting a direction.
You may assume the given string can be obtained by concatenating some "north" and "west",
the sum of the occurrences of "north" and "west" in the given string is between $1$ and $20$, inclusive,
and the angle denoted by the given direction is between $0$ and $90$, inclusive.
The final dataset is followed by a single line containing only a single "#".

Output

For each dataset, print an integer if the angle described by the given direction can be represented as an integer, otherwise print it as an irreducible fraction. Follow the format of the sample output.

Sample Input

north
west
northwest
northnorthwest
westwestwestnorth
#

Output for the Sample Input

0
90
45
45/2
315/4



Sample input:
north
west
northwest
northnorthwest
westwestwestnorth
#


Sample output:
0
90
45
45/2
315/4

Submission status: Wrong Answer

Solution code:
from fractions import Fraction
import numbers
while True:
    s = input()
    if s == '#':
        break
    if s == 'west':
        print(90)
    elif s == 'north':
        print(0)
    elif s[:5] == 'north':
        n = pow(2, (len(s) - len('west')) // len('north'))
        f = Fraction(90, n)
        print(f.numerator, f.denominator, sep='/')
    else: 
        n = pow(2, (len(s) - len('north')) // len('west'))
        f = 90 - Fraction(90, n)
        print(f.numerator, f.denominator, sep='/')

Submission status: Wrong Answer

Solution code:
while 1:
    s = raw_input().replace("north","0").replace("west","1")
    if s == "#": break

    u = 90*int(s[-1])
    d = 1
    s = s[:-1]
    while s:
        if s[-1] == "1": u = 2*u+90
        d *= 2
        s = s[:-1]

    while u%2 == 0 and d > 1:
        u /= 2
        d /= 2
    print u if d == 1 else "%d/%d"%(u,d)

Submission status: Wrong Answer

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

while True:
    direction = input()
    if direction == '#':
        break
    north_count = 0
    direct = direction[:]
    while 'north' in direct:
        north_count += 1
        direct = direct[direct.index('north')+len('north'):]

    west_count = 0
    direct = direction[:]
    while 'west' in direct:
        west_count += 1
        direct = direct[direct.index('west')+len('west'):]
    if north_count == 0 or west_count == 0:
        if north_count == 0:
            ans = 90
        elif west_count == 0:
            ans = 0
    else:
        if north_count > west_count:
            ans = 90
            num = north_count
        else:
            num = west_count
            ans = 90 * (2**west_count-1)
        while ans%2 == 0:
            ans = ans // 2
            num -= 1
        if num != 0:
            ans = str(ans) + '/' + str(num*2)
    print(ans)

Submission status: Wrong Answer

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

def reduction(num, denom):
    p = num
    q = denom
    while p%q != 0:
        r = p%q
        p = q
        q = r
    return [num/q, denom/q]

d = raw_input()
while d != "#":
    l = 0
    north = 0
    west = 0
    while l < len(d):
        if d[l] == 'n':
            north += 1
            l += 5
        elif d[l] == 'w':
            west += 1
            l += 4
    if d == "north":
        print 0
    elif d == "west":
        print 90
    elif north >= west:
        num = 90
        denom = 2**north
        ans = reduction(num, denom)
        if ans[1] == 1:
            print ans[0]
        else:
            print str(ans[0])+'/'+str(ans[1])
    else:
        num = 90*(2**west)-90
        denom = 2**west
        ans = reduction(num, denom)
        if ans[1] == 1:
            print ans[0]
        else:
            print str(ans[0])+'/'+str(ans[1])

    d = raw_input()

Submission status: Wrong Answer

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

def reduction(num, denom):
    p = num
    q = denom
    while p%q != 0:
        r = p%q
        p = q
        q = r
    return [num/q, denom/q]

d = raw_input()
while d != "#":
    l = 0
    cnt = 0
    a = [0, 1]
    while l < len(d):
        if d[l] == 'n':
            if cnt == 0 or a[0] == 0:
                a[0] = 0
            else:
                while a[1] != 2**cnt:
                    a[0] *= 2
                    a[1] *= 2
                a = reduction(a[0]-90, a[1])
            l += 5
        elif d[l] == 'w':
            if cnt == 0 or a[0] == 90:
                a[0] = 90
            else:
                while a[1] != 2**cnt:
                    a[0] *= 2
                    a[1] *= 2
                a = reduction(a[0]+90, a[1])
            l += 4
        cnt += 1
    a = reduction(a[0], a[1])
    if a[1] == 1:
        print a[0]
    else:
        print str(a[0])+'/'+str(a[1])

    d = raw_input()

Submission status: Wrong Answer

Solution code:

def main():
    while True:
        a = raw_input()
        if a == '#':
            break
        len_north = a.count('north')
        len_west = a.count('west')
        div = pow(2, abs(len_west - len_north))
        if len_west == 0:
            print 0
        elif len_north == 0:
            print 90
        else:
            print (str(45) if len_north > len_west else str(90 * div - 45)) + ("/" + str(div) if div != 1 else "")

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from fractions import *
while True:
	s=input()
	if s=='#':break
	angle=Fraction(45)
	cnt=n=w=0
	while cnt<len(s):
		if s[cnt]=='n':
			n+=1
			angle-=Fraction(90/2**n)
			cnt+=5
		else:
			w+=1
			angle+=Fraction(90/2**w)
			cnt+=4
	print(angle)

Submission status: Wrong Answer

Solution code:
from fractions import Fraction

def calc(s):
    i = n = ans = 0
    while i < len(s):
        if s[i] == 'n':
            i += 5
            if n == 0:
                ans = Fraction(0,1)
            elif ans > 0:
                ans -= Fraction(90, 2**n)
        else:
            i += 4
            if n == 0:
                ans = Fraction(90,1)
            elif ans < 90:
                ans += Fraction(90, 2**n)
        n += 1
    return ans

while True:
    S = input()
    if S == '#': break
    print(calc(S))

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        s = S()
        if s == '#':
            break
        a = 0
        i = 0
        while len(s) > 0:
            i += 1
            a *= 2
            if s[:5] == 'north':
                s = s[5:]
            else:
                s = s[4:]
                a += 90
        b = 2**(i-1)
        g = fractions.gcd(a,b)
        if b//g > 1:
            rr.append('{}/{}'.format(a//g,b//g))
        else:
            rr.append(a // g)

    return '\n'.join(map(str, rr))


print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        s = S()
        if s == '#':
            break
        a = 0
        i = 0
        b = 1
        while len(s) > 0:
            if s[:5] == 'north':
                s = s[5:]
                if a > 0:
                    a -= 90 / b
            else:
                s = s[4:]
                if a < 90:
                    a += 90 / b
            b *= 2
        a *= b
        g = fractions.gcd(a,b)
        a = int(a//g)
        b = int(b//g)
        if b > 1:
            rr.append('{}/{}'.format(a,b))
        else:
            rr.append(a)

    return '\n'.join(map(str, rr))


print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        s = S()
        if s == '#':
            break
        a = 0
        b = 1
        while len(s) > 0:
            b *= 2
            if s[:5] == 'north':
                s = s[5:]
            else:
                s = s[4:]
                a += 90 / b
                if len(s) == 0:
                    a += 90 / b
        a *= b
        g = fractions.gcd(a,b)
        a = int(a//g)
        b = int(b//g)
        if b > 1:
            rr.append('{}/{}'.format(a,b))
        else:
            rr.append(a)

    return '\n'.join(map(str, rr))


print(main())

Submission status: Accepted

Solution code:
from fractions import Fraction
while True:
  s=input()
  if s=="#": break
  s="".join([c for c in s if c=='n' or c=='w'])
  x=0 if s[-1]=='n' else 90
  for i in range(len(s)-1):
    if s[-2-i]=='n':
      x-=Fraction(45,1<<i)
    else:
      x+=Fraction(45,1<<i)
  print(x)

Submission status: Accepted

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

import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        while True:
            s = raw_input()
            if s == '#':
                break
            angle = 0
            count = 0
            while s:
                if count == 0:
                    if s.endswith('north'):
                        angle = 0
                        s = s[:-5]
                    elif s.endswith('west'):
                        angle = 90
                        s = s[:-4]
                else:
                    if s.endswith('north'):
                        angle -= 90.0 / 2 ** count
                        s = s[:-5]
                    elif s.endswith('west'):
                        angle += 90.0 / 2 ** count
                        s = s[:-4]                
                count += 1
            denom = 1
            while angle != int(angle):
                angle *= 2
                denom *= 2
            if denom == 1:
                print int(angle)
            else:
                print '/'.join(map(str, [int(angle), denom]))

                
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Submission status: Accepted

Solution code:
while 1:
    s = raw_input().replace("north","0").replace("west","1")
    if s == "#": break

    u = 90*int(s[-1])
    d = 1
    s = s[:-1]
    while s:
        if   s[-1] == "0": u = 2*u-90
        elif s[-1] == "1": u = 2*u+90
        d *= 2
        s = s[:-1]

    while u%2 == 0 and d > 1:
        u /= 2
        d /= 2
    print u if d == 1 else "%d/%d"%(u,d)

Submission status: Accepted

Solution code:
while 1:
    s = raw_input().replace("north","0").replace("west","1")
    if s == "#": break

    u = 90*int(s[-1])
    d = 1
    s = s[:-1]
    while s:
        u *= 2; d *= 2
        u += 90 if s[-1] == "1" else -90
        s = s[:-1]

    while u%2 == 0 and d > 1:
        u /= 2; d /= 2
    print u if d == 1 else "%d/%d"%(u,d)

Submission status: Accepted

Solution code:
while 1:
    s = raw_input().replace("north","0").replace("west","1")[::-1]
    if s == "#": break
    u = 90*int(s[0])
    s = s[1:]
    d = 2**len(s)
    for i in s:
        u = 2*u+(90 if i == "1" else -90)
    while u%2 == 0 and d > 1:
        u /= 2; d /= 2
    print u if d == 1 else "%d/%d"%(u,d)

Submission status: Accepted

Solution code:
while 1:
    s = raw_input().replace("north","0").replace("west","1")[::-1]
    if s == "#": break
    u = 90*int(s[0])
    d = len(s)-1
    for i in s[1:]: u = 2*u+(90 if i == "1" else -90)
    while (u+1)%2*d: u /= 2; d -= 1
    print u if d == 0 else "%d/%d"%(u,2**d)

Submission status: Accepted

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

while True:
    direction = ''.join(reversed(input()))
    if direction == '#':
        break
    count = 0
    ans = 0
    while direction != '':
        if direction[0] == 'h':
            if count == 0:
                ans =  0
            else:
                ans = ans * 2 - 90
            direction = direction[len('north'):]
        else:
            if count == 0:
                ans = 90
            else:
                ans = ans * 2 + 90
            direction = direction[len('west'):]
        count += 1

    while ans % 2 == 0 and count > 1:
        ans //= 2
        count -= 1
    if count <= 1:
        print(ans)
    else:
        print(str(ans)+'/'+str(2**(count-1)))

Submission status: Accepted

Solution code:
while 1:
	s = raw_input()
	if s == "#":
		break
	length = len(s)
	l = list()
	num = -1
	i = 0
	while i < length :
		if s[i:i+4] == "west":
			l.append("w")
			i += 4
		else:
			l.append("n")
			i += 5
		num += 1
	l = l[::-1]
	direction = 90 * (2**num) if l[0] == "w" else 0
	for i in range(1,num+1):
		if l[i] == "w":
			direction += 90 * 2**(num-i)
		else :
			direction -= 90 * 2**(num-i)
	while num > 0 and direction % 2 == 0:
		direction /= 2
		num -= 1
	print direction if num == 0 else str(direction)+"/"+str(2**num)

Submission status: Accepted

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

def reduction(num, denom):
    p = num
    q = denom
    while p%q != 0:
        r = p%q
        p = q
        q = r
    return [num/q, denom/q]

d = raw_input()
while d != "#":
    l = 0
    direc = []
    while l < len(d):
        if d[l] == 'n':
            direc.append('n')
            l += 5
        elif d[l] == 'w':
            direc.append('w')
            l += 4
    direc = direc[::-1]

    a = []
    for i in range(len(direc)):
        if direc[i] == 'n':
            if i == 0:
                a = [0, 1]
            else:
                a[0] = a[0]*2-90
                a[1] *= 2
        elif direc[i] == 'w':
            if i == 0:
                a = [90, 1]
            else:
                a[0] = a[0]*2+90
                a[1] *= 2
    a = reduction(a[0], a[1])
    if a[1] == 1:
        print a[0]
    else:
        print str(a[0])+'/'+str(a[1])

    d = raw_input()

Submission status: Accepted

Solution code:
from fractions import Fraction

while True:
    s = input()
    if s == '#':
        break
    if s[-4:] == 'west':
        ans = Fraction(90)
        s = s[:-4]
    else:
        ans = Fraction(0)
        s = s[:-5]
    i = 1
    while s:
        if s[-4:] == 'west':
            ans += Fraction(90, 1 << i)
            s = s[:-4]
        else:
            ans -= Fraction(90, 1 << i)
            s = s[:-5]
        i += 1
    print(ans)

Submission status: Accepted

Solution code:
from fractions import Fraction

while True:
    s = input()
    if s == '#':
        break
    N = len(s)
    if s[-1] == 't':
        ans = Fraction(90)
        p = 4
    else:
        ans = Fraction(0)
        p = 5
    i = 1
    while p < N:
        if s[-1-p] == 't':
            ans += Fraction(90, 1 << i)
            p += 4
        else:
            ans -= Fraction(90, 1 << i)
            p += 5
        i += 1
    print(ans)

Submission status: Accepted

Solution code:
from fractions import *
while True:
	s=input()[::-1]
	if s=='#':break
	if s[0]=='h':
		angle=Fraction(0)
		cnt=5
	else:
		angle=Fraction(90)
		cnt=4
	i=1
	while cnt<len(s):
		if s[cnt]=='h':
			angle-=Fraction(90/2**i)
			cnt+=5
		else:
			angle+=Fraction(90/2**i)
			cnt+=4
		i+=1
	print(angle)

Submission status: Accepted

Solution code:
import re
while 1:
    s=input()
    if s=='#':break
    s=re.split('[w|n]',s)[1:];c=1
    d=90 if s[-1]=='est' else 0
    for x in s[:-1][::-1]:
        d*=2
        d+=90if x=='est'else-90
        c*=2
    while d&1==0 and c&1==0:d//=2;c//=2
    if c==1:print(d)
    else:print(d,c,sep='/')

Submission status: Accepted

Solution code:
from fractions import Fraction


def rec(s):
  if s[0] == 'n':
    if len(s) > 5:
      t = rec(s[5:])
      return [(t[0] - 45) * 2, t[1] + 1]
    else:
      return [0, 0]
  else:
    if len(s) > 4:
      t = rec(s[4:])
      return [(t[0] + 45) * 2, t[1] + 1]
    else:
      return [90, 0]

while True:
  s = input()
  if s == "#":
    break
  
  [n, d] = rec(s)
  d = 2 ** d
  ans = Fraction(n, d)
  print(ans)

Submission status: Accepted

Solution code:
from fractions import Fraction

def calc(s):
    n = ans = 0
    while len(s) >= 4:
        if s[-4] == 'w':
            s = s[:-4]
            if n == 0:
                ans = Fraction(90,1)
            else:
                ans += Fraction(90, 2**n)
        else:
            s = s[:-5]
            if n == 0:
                ans = Fraction(0,1)
            else:
                ans -= Fraction(90, 2**n)
        n += 1
    return ans

while True:
    S = input()
    if S == '#': break
    print(calc(S))

Submission status: Accepted

Solution code:
from math import gcd

while True:
    s = str(input())
    if s == "#":
        break

    li = list()
    while s:
        if s[:5] == "north":
            li.append(-1)
            s = s[5:]
        else:
            li.append(1)
            s = s[4:]
    
    ans = li[-1] * 45 + 45
    li = li[:-1]
    mot = 1
    for d in reversed(li):
        ans = ans * 2 + d * 90
        mot *= 2

    g = gcd(ans, mot)
    ans, mot = ans // g, mot // g
    if mot == 1:
        print(ans)
    else:
        print("{0}/{1}".format(ans, mot))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    def f(a):
        if len(a) == 1:
            if a[0] == 'n':
                return 0
            return 90
        t = f(a[1:])
        if a[0] == 'n':
            return t - 90 / (2**(len(a)-1))
        return t + 90 / (2**(len(a)-1))

    while True:
        s = S()
        if s == '#':
            break
        a = []
        while len(s) > 0:
            if s[:5] == 'north':
                a.append('n')
                s = s[5:]
            else:
                a.append('w')
                s = s[4:]
        t = f(a)
        b = 2**(len(a))
        a = int(t*b)
        g = fractions.gcd(a,b)
        a //= g
        b //= g
        if b > 1:
            rr.append('{}/{}'.format(a,b))
        else:
            rr.append(a)

    return '\n'.join(map(str, rr))


print(main())