Some more interesting stuff in Python (part-3)

Jyothi
2 min readMay 12, 2024

ord()

ord() function is to return the ascii value of the char.

array concatenation: a single stmt: nums +nums

Shuffle array:

In the C language, performing in-place shuffling of an array may not be an easy task. However, in Python, arrays are treated as lists, meaning that elements can be easily removed and inserted in the middle. This makes shuffling a much simpler process:

  • nums.pop(n + i) removes an element from the second half of the array.
  • nums.insert(2*i + 1, nums.pop(n + i)) it inserts this removed element at the appropriate position

This is possible with bit manipulations and with the limitation that array element value should be lesser than or equal to 15 bits that is 32767. Integer data type is 32 bits, one bit is reserved for signed bit. It can hold 31 bits value. If the array element value is 15 bits, another 15 bits can be used to hold another value temporarily.

Eg

So temporarily some of the elements can hold 2 values: new value and old value.

With this idea we can build a logic to do inplace shuffle:

Steps:

· Copy y1,y2,…yn to the expected positions (2*ii+1) at most significant bits of first 15 bits

· Copy x2,x3,…xn to the new locations. We will be copying from right to left to not to overwrite the existing values. Also as some of the elements have two values (old and new) , extract only the x values

· Reset the old values at indices 1, 3, 5 …2*n-3

--

--