Problem:
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

Solution (Python):
from fractions import Fraction
while True:
  s=input()
  if s=="
  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)

Solution (Python):



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()

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

    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)

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

    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)

Solution (Python):
while 1:
    s = raw_input().replace("north","0").replace("west","1")[::-1]
    if s == "
    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)

Solution (Python):
while 1:
    s = raw_input().replace("north","0").replace("west","1")[::-1]
    if s == "
    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)

Solution (Python):



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)))

Solution (Python):
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)

Solution (Python):



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()

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
from fractions import *
while True:
	s=input()[::-1]
	if s=='
	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)

Solution (Python):
import re
while 1:
    s=input()
    if s=='
    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='/')

Solution (Python):
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)

Solution (Python):
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 == '
    print(calc(S))

Solution (Python):
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))

Solution (Python):
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())