Created
          July 5, 2014 06:25 
        
      - 
      
- 
        Save matxpg/fdc8ef288251b34c1935 to your computer and use it in GitHub Desktop. 
    decimal to base conversion
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #rem: int, int -> int | |
| #print the remainder of (x/n) | |
| def rem(x,n): | |
| #prevent division by zero | |
| assert(n != 0) | |
| return x % n | |
| def dec2b (xIn,b): | |
| """ Convert n from base 10 (decimal) to base b, printing steps and result. """ | |
| assert(b > 1) | |
| result = [] | |
| while(xIn > 0): | |
| print xIn, '/', b, ' R', rem(xIn,b) | |
| xIn,r = divmod(xIn,b) | |
| result.append(r) | |
| printResult = ''.join(map(str,result))[::-1] | |
| return printResult | |
| #TODO: Implement hexadecimal lettering!!! | |
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
output:
2 :
2 / 2 R 0
1 / 2 R 1
10
4 :
4 / 2 R 0
2 / 2 R 0
1 / 2 R 1
100
8 :
8 / 2 R 0
4 / 2 R 0
2 / 2 R 0
1 / 2 R 1
1000