2 # vim: set fileencoding=utf8
3 # Created: April 29, 2008
4 # Created by: Antonio Chavez
12 LOW_BOUND
= ord('A') + MIN_VAL
14 int_cnvrt(value
, base_frm
, base_to
):
15 ''' int_cvrt: convert the number to the left of the decimal place
16 value: string containing value to convert
17 from: what base to convert from. Can be positive and negative.
18 Negative numbers will represent balanced base. Positive
19 will represent unbalanced base.
20 to: what base to convert to.
22 Example: 3 = unbalanced base 3 (0, 1, 2)
23 -3 = balanced base 3 (-1, 0, 1)
24 Balanced bases can only be odd integers, for the obivious reason
25 that even numbers are not good candidates for balanced numbering
26 Example: -4 = (-1, 0, 1, 2) or (-2, -1, 0, 1). Either system is
27 not balanced. Bases must also have a magnitude greater than 1.
28 For balanced bases greater than 3, a negative number is represented
29 with an 'i' next to it.
30 Example: 4i21 => 4(-2)1
31 Currently conversion balanced bases is not supported. If balanced
32 base is entered, the result will be returned in base 10.
33 A negative sign before the number will negate the result.
36 # check for magnitude greater than 1
37 if abs(base_frm
) < 2 or abs(base_to
) < 2:
38 print "bases must have magnitude greater than 1"
41 # check for a balanced negative base and derive magnitude
42 if base_frm
< 0 and base_frm
*-1%2 != 1:
43 print "base_from is even: negative bases must be odd integers"
46 magnitude_f
= (base_frm
*-1 - 1)/2
48 magnitude_f
= base_frm
50 if base_to
< 0 and base_to
*-1%2 != 1:
51 print "base_to is even: negative bases must be odd integers"
54 magnitude_t
= (base_to
*-1 - 1)/2
58 sum = 0 # base 10 equivalent summation
59 neg
= 1 # used when 'i' is encountered, it negates the previous digit
60 sign
= 1 # sign of summation
61 count
= 1 # amount to multiply next digit by
62 prev
= 1 # the value of the next digit
63 cur
= 0 # current digit
65 if magnitude_f
>= MIN_VAL
66 max_val
= magnitude_f
- MIN_VAL
68 for i
in range(len(value
) - 1, -1, -1):
70 if abs(base_frm
) == 3:
72 if value
[i
] in Trits
.trit_integer
:
73 sum = sum + Trits
.trit_integer
[value
[i
]]*count
74 count
= count
*abs(base_frm
)
76 print "%s invalid input", value
[i
]
79 elif value
[i
].isdigit():
84 print "%s: invalid input", value
[i
]
87 sum = sum + prev
*neg
*count
89 # reset variables to appropiate values
92 count
= count
*abs(base_frm
)
94 elif value
[i
] == '-' and i
== len(value
) - 1:
95 # negate the whole number
100 elif magnitude_f
>= MIN_VAL
and value
[i
].isalpha():
102 cur
= ord(value
[i
].upper()) - LOW_BOUND
104 if cur
> magnitude_f
:
105 print "%s: invalid input", value
[i
]
108 sum = sum + prev
*neg
*count
110 # reset variables to appropriate values
113 count
= count
*abs(base_frm
)
116 print "%s: invalid input", value
[i
]
119 # sum up remaining digit
120 sum = sum + prev
*neg
*count
123 # return base 10 if desired base is balanced
127 # compute unbalanced conversion
133 remainder
= quotient
%magnitude_t
134 quotient
= quotient
/magnitude_t
135 result
= "" + remainder
+ result
140 useful things to know for implementation
141 char to int: ord('a') = 97 ASCII
142 int to char: chr(97) ASCII
143 char to int: int('4')
145 a[0].isalpha() => true
146 a[2].isdigit() => true
150 s.split(".") => returns list of strings broken up by "."
151 "1010.3930" => ["1010", "3930"]
153 s = ".".join(lis_digits) => combine elements in list by "."