# Number - String conversions

# Problem #1: converting a non-negative number to a binary-string representation
def int2bin(n):
    if n == 0: return '0'
    answer = ''
    while n > 0:
        if n%2 == 0:
            answer = '0' + answer
        else:
            answer = '1' + answer
        n //= 2
    return answer

# Problem #2: converting a binary string into a number
def binstr2int(s):
    answer = 0
    for c in s:
        if c == '1':
            answer = 2*answer + 1
        else:
            answer = 2*answer
    return answer

# Problem #3: converting from base-10 string to number.  Basically the same idea as #2:
def str2int(s):
    answer = 0
    for c in s:
        answer = 10*answer + (ord(c) - ord('0'))
    return answer

#Problem #4: converting from an integer to Roman-numeral form
def int2Roman(n):
    ones = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX']
    tens = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX']
    return tens[n//10] + ones[n%10]

#Problem #5: convert a string representation of a floating point number to a number
def str2float(s):
    # capture the sign (and remove it)
    sign = 1
    if s[0] == '-':
        sign = -1
        s = s[1:]
    # find the position of the decimal point and remove it
    pos_point = -1
    for i in range(len(s)):
        if s[i] == '.':
            pos_point = i
            s = s[:i] + s[i+1:]
            break
    # if no decimal point found:
    if pos_point == -1:
        return sign * str2int(s)  # yes, we can cheat but only with our own inventions
    # having removed the decimal point, it's an integer - remember to shift its value down
    return sign * str2int(s) / (10 ** (len(s) - pos_point))

#Problem #6: Classify a character as a letter (True) or not (False)
def isLetter(c):
    return (ord('A') <= ord(c) <= ord('Z')) or (ord('a') <= ord(c) <= ord('z'))

#Problem #7: mirror a lower-case string around the middle of the alphabet
def mirror(s):
    answer = ''
    for c in s:
        answer += chr(ord('z') - (ord(c) - ord('a')))
    return answer