SHARE
TWEET

lab3

a guest Oct 21st, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. file = open('input.txt')
  2.  
  3. def from2to10(n):
  4.     n10 = 0
  5.     p = 0
  6.     while n > 0:
  7.         n10 += (n % 10) * (2 ** p)
  8.         p += 1
  9.         n //= 10
  10.     return n10
  11.  
  12. def numLen(n):
  13.     i = 0
  14.     while(n > 0):
  15.         n //= 10
  16.         i += 1
  17.     return i
  18.  
  19. def from10tofact(n):
  20.     r = 1
  21.     fnr = 0
  22.     while n > 0:
  23.         y = n % r
  24.         y *= 10 ** numLen(n) if fnr > 0 else 1
  25.         fnr += y
  26.         fnr *= 10
  27.         n //= r
  28.         r += 1
  29.     fnr //= 10
  30.     fn = 0
  31.     while fnr > 0:
  32.         d = fnr % 10
  33.         fnr //= 10
  34.         fn += d
  35.         fn *= 10
  36.     fn //= 10
  37.     return fn
  38.  
  39. def convert(s1, s2, n):
  40.     if s1 == '2' and s2 == '10':
  41.         return from2to10(n)
  42.     elif s1 == '10' and s2 == 'fact':
  43.         return from10tofact(n)
  44.  
  45. charDigit = {
  46.     '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9
  47. }
  48. def strToInt(s):
  49.     num = 0
  50.     for c in s:
  51.         num += charDigit[c]
  52.         num *= 10
  53.     num //= 10
  54.     return num
  55.  
  56. def main():
  57.     for line in file.readlines():
  58.         line = line.replace('\n', '')
  59.         line = line.split(' ')
  60.         s1 = line[0]
  61.         s2 = line[1]
  62.         nums = line[2:]
  63.         for n in nums:
  64.             n = strToInt(n)
  65.             print('Converting ', n, ' from ', s1, ' to ', s2, ' -> ', convert(s1, s2, n), '\n')
  66.  
  67. if __name__ == '__main__':
  68.     main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top