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

Serial Port Profile with CC2650 and CC2640R2 (part 2) Modified to transfer ADC values up to PC Step 1:  Include ACD.h library #include <ti/drivers/ADC.h>  Step 2: Insert period event in order to update ADC read values from the potentiometer #define SSSS_ICALL_EVT                         ICALL_MSG_EVENT_ID // Event_Id_31 #define SSSS_QUEUE_EVT                         UTIL_QUEUE_EVENT_ID // Event_Id_30 #define SSSS_PERIODIC_EVT                      Event_Id_00 //SOLUTION // Bitwise OR of all events to pend on #define SSSS_ALL_EVENTS                        (SSSS_ICALL_EVT        | \                                ...

Arduino Code for test Heart Rate 7 Click

Heart Rate 7 Click is the newest module from MikroE which uses VEM8080 photodetector has a wide range spectrum from 300nm - 1000nm. To control and acquisition data, AFE4404 from TI inc. is adopted. This chip permits control 3 LED channels, and sample heart rate default 100 SPS.  A 22-bit ADC permit collecting very small changed voltage from a PD sensor. In this example we config Arduino Mega  2650 as below: Pin 4 for RST PIN 5 CLK PIN 6 INT PIN 20 SDA PIN 21 SCL Config registers follow the default of AFE4404 datasheet Page 27 with some minor changes. 1. Config Internal Clock through  AFE_CONTROL2 register addr.: 0x23 value: 0x104218  // setup all dynamic power to save energy 2. Control LED2 current through AFE_LEDCNTRL register addr: 0x22 value: 0x000100 3. Read data using PROG_TG_EN signal through AFE_TIA_GAIN register addr: 0x21 value: 0x000125 Time to start and end of PROG_TG setup through two registers: AFE_PROG_TG_STC register (...
Serial Port Profile with CC2650 and CC2640R2 This test based on code from TI support under link: For CC2650: https://github.com/ti-simplelink/ble_examples/blob/ble_examples-2.2/docs/spp_ble.md For CC2640R2: https://github.com/ti-simplelink/ble_examples/blob/simplelink_cc2640r2_sdk-2.20/examples/rtos/CC2640R2_LAUNCHXL/bleapps/simple_serial_socket_client/readme.md In this experiment, I use CC2640R2 as Server (Peripheral) and CC2650 as Client (Central) as the image below: Step1: import project spp_ble_client_cc2650lp (app and stack) for CC2650 Step2: modify Code of Service in file serial_port_service:               C0E0 ==> C0C0               C0E1 ==> C0C1               C0E2 ==> C0C2 If you don't change, Two Blue Tooth Board cant know to service each other. Step 3: import project simple_serial_socket_client_CC2640r2 for CC2640R2 here we using RealTerm...