Skip to main content

 Permutation function 



  1. def permute(s):
  2.     n = len(s) 
  3.     if n <= 1: 
  4.         return s
  5.     elif n == 2:
  6.         return [''.join([s[0],s[1]]),''.join([s[1],s[0]])]
  7.     else:# n > 2
  8.         snew = []
  9.         for i in range(n):
  10.             a = [k for k in s]#copy s to a
  11.             #print(s)
  12.             ele = a.pop(i)
  13.             for p in permute(a):
  14.                 q = ele + p
  15.                 if not(q in snew):
  16.                     snew.append(q) 
  17.         return snew

  18. def permutations(string):
  19.     s = [ele for ele in string]
  20.     return permute(s)
This code is not efficientcy

Using set() in python rather list()

def permutations(string):
  if len(string) == 1: return set(string)
  first = string[0]
  rest = permutations(string[1:])
  result = set()
  for i in range(0, len(string)):
    for p in rest:
      result.add(p[0:i] + first + p[i:])
  return result
Or intertool library:
import itertools

def permutations(string):
    return list("".join(p) for p in set(itertools.permutations(string)))

Comments

Popular posts from this blog

Tự nhiên

Tự nhiên là những gì thuộc về tính tự có vốn là của nó, không có sự giải thích nào cho tự nhiên cả. Đó gọi là tự nhiên như cái tên mang ý nghĩa của nó vậy. Khoa học tự nhiên là khoa học liên quan tới nghiên cứu và tìm hiểu các vấn đề thuộc về đặc tính vốn có của sự vật theo một hình thức tự nó mà không phải là do con người tạo ra. Theo đó toán học, lý học, hóa học được xem là các ngành khoa học tự nhiên. Thật ra toán học nên được xem là ngành khoa học xã hội hơn là tự nhiên, vì về bản chất, toán học là thứ ngôn ngữ do con người sáng tạo ra. Vì thế nó mang những hình thái phụ thuộc vào nhận thức và cảm quan của người sáng tạo ra nó. Tuy nhiên toán học mang đặc tính tự nhiên ở chỗ, nó là phương tiện được xây dựng để mô tả đường lối của tự nhiên.
Experiment to CC2640R2 and Sensor Booster Pack In this example, I follow a tutorial from TI to read sensor OTP3001. http://dev.ti.com/tirex/content/simplelink_academy_cc2640r2sdk_1_13_02_07/modules/ble_02_thermostat/ble_02_thermostat.html Because the sensor TMP0007 has out of date now, so I little modify to use with OTP3001. Step1. Copy file i2ctmp from simpllink sdk  cc2640R2 version Reference The Booster pack datasheet  http://www.ti.com/lit/ug/slau666b/slau666b.pdf TMP0007 and OTP3001 share the I2C bus including SCL, SDA.  Address's  TMP is 0x04, Data Register: 0x00 Address's OTP is 0x47, DataRegister: 0x00  So in the code we modify: #define OTP3001_REG         0x0000  /* Die Temp Result Register for TMP006 */ #define OTP3001_ADDR        0x47; According to OTP3001 datasheet, in default, a configuration register (address 0x01) in sleep mode, so if we read data ou...