Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. import sys
  2. import math
  3.  
  4. mess=input()
  5. cm=''
  6. for i in mess:
  7.     arr = list("{0:b}".format(ord(i)).zfill(7))
  8.    
  9.     print("Debug messages...", arr, file=sys.stderr)
  10.    
  11.     j=0
  12.     count=1
  13.     cm=''
  14.     new=''
  15.     while j < len(arr)-1:
  16.        
  17.         if j == 0: # get first series item
  18.             currentSeries = arr[0]
  19.  
  20.         if arr[j] == arr[j+1]: # if current item is equal to the next count and move to along to next item in loop
  21.             count+=1
  22.            
  23.         if arr[j] != arr[j+1] or j == len(arr)-2: # update the new string with the code, change the series for next binary 0 or 1
  24.            
  25.             if currentSeries == '1':
  26.                 new+='0 ' # code for 1
  27.                 currentSeries='0' #end of series so switch binary digit
  28.             else:
  29.                 new+='00 ' # code for 0
  30.                 currentSeries='1' #end of series so switch binary digit
  31.                
  32.             new+='0'*count # add the amount of 0's according to the count + plus 1 for being the last/current item
  33.             count=1 # reset the counter
  34.            
  35.             if j != len(arr)-2: # if not the end of array add a space, we do this as we don't want space at end of string
  36.                 new+=' '
  37.            
  38.         j+=1
  39.     #End of while        
  40.    
  41. print(new)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement