SHARE
TWEET

Untitled

a guest Oct 18th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Функция, переводящая строку в число
  2. def parseInt(line):
  3.   dig = 0
  4.   for c in line: dig = dig*10 + ord(c)-48
  5.   return dig
  6.  
  7. # Начальный список, чисел Фибоначчи
  8. FIBS = [1, 2]
  9.  
  10. # Функция, переводящая из 10 сс. в двоичную
  11. def decToBin(decnum):
  12.   # переводим строку в числу
  13.   decnum = parseInt(decnum)
  14.   binum = ''
  15.   while decnum > 0:
  16.     binum = str(decnum % 2) + binum
  17.     decnum //= 2
  18.   binum = list(binum)
  19.   i = len(binum)-4
  20.   # добавляем точечки между разрядами.....
  21.   while i > 0:
  22.     binum.insert(i, '.')
  23.     i-=4
  24.   decnum = ''
  25.   for i in binum: decnum += i;
  26.   return decnum
  27.  
  28. # Переводит из Фибоначчиевой в десятичную
  29. def zeckToDec(zecknum):
  30.   result = 0
  31.   zecknum = zecknum[::-1]
  32.   for z in range(len(zecknum)):
  33.     if z >= len(FIBS):
  34.       FIBS.append(FIBS[z - 1] + FIBS[z - 2])
  35.     result += FIBS[z] if zecknum[z] != '0' else 0
  36.   return str(result)
  37.  
  38. infile = open('input30.txt')
  39. outfile = open('output30.txt', 'w')
  40. for line in infile:
  41.   arr = line.split(' ')
  42.   arr[len(arr)-1] = arr[len(arr)-1][0:-1]
  43.   if arr[0] == '10':
  44.     outfile.write('10 -> 2: ')
  45.     for i in range(len(arr)-3): outfile.write(arr[i + 2] + ' -> ' + decToBin(arr[i + 2]) + ' | ');
  46.     outfile.write(arr[len(arr)-1] + ' -> ' + decToBin(arr[len(arr)-1]) + '\n')
  47.   else:
  48.     outfile.write('Fib -> 10: ')
  49.     for i in range(len(arr)-3): outfile.write(arr[i + 2] + ' -> ' + zeckToDec(arr[i + 2]) + ' | ');
  50.     outfile.write(arr[-1] + ' -> ' + zeckToDec(arr[-1]) + '\n')
  51. outfile.close()
  52. infile.close()
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top