class Solution(object):
    def toHex(self, num):
        """
        :type num: int
        :rtype: str
        """
        d = { 10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f' }
        l = list(bin(num)[2:])
        l = l[::-1]
        l.extend('0' * (4*(len(l)/4+1) - len(l)))

        group = 0
        hex_result = ""
        temp = 0

        for i in xrange(len(l)):
            temp += (int(l[i]) * (2**group))

            if group == 3:  # start from 0 and every 4 is a group
                if temp < 10:
                    hex_result += str(temp)
                else:
                    hex_result += str(d[temp])
                temp = 0
                group = 0
            else:
                group += 1

        return hex_result[::-1]


def main():
    n = -1
    solution = Solution()
    print solution.toHex(n)


if __name__ == '__main__':
    main()
@@ -1,8 +1,123 @@
+# Solution Reference:%0A# http://stackoverflow.com/questions/16427073/signed-integer-to-twos-complement-hexadecimal%0A%0A
 class So
@@ -232,16 +232,93 @@
 %0A       
+ #### works with positive num but fails with negative num%0A        #%0A        #
  d = %7B 1
@@ -374,24 +374,26 @@
 f' %7D%0A       
+ #
  l = list(bi
@@ -411,16 +411,18 @@
 %0A       
+ #
  l = l%5B:
@@ -433,16 +433,18 @@
 %0A       
+ #
  l.exten
@@ -478,24 +478,33 @@
 en(l)))%0A
+        #
 %0A       
  group =
@@ -491,24 +491,26 @@
    #%0A       
+ #
  group = 0%0A 
@@ -511,24 +511,26 @@
  = 0%0A       
+ #
  hex_result 
@@ -537,24 +537,26 @@
 = %22%22%0A       
+ #
  temp = 0%0A%0A 
@@ -553,24 +553,33 @@
 emp = 0%0A
+        #
 %0A       
  for i i
@@ -570,16 +570,18 @@
 %0A       
+ #
  for i i
@@ -602,28 +602,30 @@
 )):%0A        
+#
     
+ 
 temp += (int
@@ -645,16 +645,25 @@
 group))%0A
+        #
 %0A       
@@ -663,20 +663,22 @@
         
+#
     
+ 
 if group
@@ -722,32 +722,34 @@
  a group%0A       
+ #
          if temp
@@ -755,32 +755,33 @@
 p %3C 10:%0A        
+#
             hex_
@@ -768,32 +768,33 @@
    #            
+ 
 hex_result += st
@@ -800,32 +800,34 @@
 tr(temp)%0A       
+ #
          else:%0A 
@@ -825,32 +825,33 @@
   else:%0A        
+#
             hex_
@@ -838,32 +838,33 @@
    #            
+ 
 hex_result += st
@@ -873,32 +873,34 @@
 d%5Btemp%5D)%0A       
+ #
          temp = 
@@ -905,32 +905,33 @@
 = 0%0A        
+#
         
 group = 0%0A  
@@ -914,24 +914,25 @@
    #        
+ 
 group = 0%0A  
@@ -933,28 +933,30 @@
 = 0%0A        
+#
     
+ 
 else:%0A      
@@ -957,24 +957,25 @@
         
+#
         
 group +=
@@ -962,24 +962,25 @@
    #        
+ 
 group += 1%0A%0A
@@ -978,24 +978,33 @@
 up += 1%0A
+        #
 %0A       
  return 
@@ -995,16 +995,18 @@
 %0A       
+ #
  return 
@@ -1023,16 +1023,109 @@
 %5B::-1%5D%0A%0A
+        bits = 32%0A        return %22%7B0:%7B1%7Dx%7D%22.format(num & ((1%3C%3Cbits) - 1), bits//4).lstrip()%0A%0A
 %0Adef mai

fix 405