 Enumerate in python

Enumerate add an iterable counter and return enumerate object. Enumerate objects can be directly used in for loop or convert into the list of tuples using the list() method.

`some_string = "python"some_dict = {}print("Convert it into list")print(list(enumerate(some_string)))for i, some_dict[i] in enumerate(some_string):    passprint("Dict")print(some_dict)`

The output

`Convert it into list[(0, 'p'), (1, 'y'), (2, 't'), (3, 'h'), (4, 'o'), (5, 'n')]Dict{0: 'p', 1: 'y', 2: 't', 3: 'h', 4: 'o', 5: 'n'}`

--

--

# LeetCode: Palindrome Number

Given an integer `x`, return `true` if `x` is palindrome integer.

An integer is a palindrome when it reads the same backward as forward. For example, `121` is palindrome while `123` is not.

Example 1:

`Input: x = 121Output: true`

Example 2:

`Input: x = -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.`

Program

`class Solution:    def isPalindrome(self, x: int) -> bool:        if x > 0:            a = int(str(x)[::-1])        if x <= 0:            a = int(str(x*-1)[::-1])        if a == x:            return True        else:            return False`

--

--

# Leetcode: Reverse Integer

Given a signed 32-bit integer `x`, return `x` with its digits reversed. If reversing `x` causes the value to go outside the signed 32-bit integer range `[-231, 231 - 1]`, then return `0`.

Assume the environment does not allow you to store 64-bit integers (signed or unsigned).

Example 1:

`Input: x = 123Output: 321`

Example 2:

`Input: x = -123Output: -321`

Program:

`class Solution(object):    def reverse(self, x):        flag = 0        list1 =[]        if x < 0:            flag = 1                list1[:0]= str(abs(x))        result = int("".join(list1[::-1]))        if flag == 1:            result = result * -1        if result > 2**31 - 1 or result <= -2**31:            return 0        else:             return(result)`

--

--

# Unique pointer in C++

Facts of unique pointer

1. The unique pointer doesn’t share its memory.
2. It can’t be copied to another unique by using an assignment operator.
3. The ownership can be moved from one to another.
4. “unique ptr” wants to limit the ownership of the object.

Program

`#include <iostream>#include <memory>using namespace std;class Demo {public:  Demo() {      printf("Constructor called\n");  }    void fun() {      printf("Function calling\n");  }    ~Demo() {      printf("Destructor called\n");  }    };int main(){    /* Create unique pointer */    unique_ptr<Demo> ptr (new Demo());    ptr->fun();    printf("***********\n");    /* Move the ownership of the pointer */    unique_ptr<Demo> ptr1 = move(ptr);    ptr1->fun();        }`

Output

`Constructor calledFunction calling***********Function callingDestructor called`

--

--

# Void pointer

In c++, we can’t assign the address of a variable of one data type to a pointer of another data type.

In such a case, use a void pointer or generic pointer.

Facts:

1. malloc() and calloc() returns void * type and allow this function to be used to allocate memory of any data type.
2. void pointers are used to implement a generic function in c.
3. void pointer can’t be dereferenced.
`int main() {    int a = 10;    void *ptr = &a;    printf("%d", *ptr);}`

Output:

`compile error: void* is not pointer-to-object type`

4. void pointer doesn’t allow pointer arithmetic.

Program:

`#include <iostream>using namespace std;int main(){    void *ptr1;     struct student {        int a;        int b;    };    struct student *stud_ptr;       stud_ptr->a = 10;    stud_ptr->b = 20;ptr1 = (struct student *)malloc(sizeof(student));    ptr1 = stud_ptr;    printf("%d\n", (*(student *)ptr1).a);return 0;}`

Output

`10`

--

--

# Leetcode: Two Sum

Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

`Input: nums = [2,7,11,15], target = 9Output: [0,1]Output: Because nums + nums == 9, we return [0, 1].`

Example 2:

`Input: nums = [3,2,4], target = 6Output: [1,2]`

Program:

`list = [2,7,11,15]trg = 9output = []for index in range(0,len(list)):    for index1 in range(index+1):        if (list[index]+list[index1] == trg):            if (index != index1):                output.append(index)                output.append(index1)print(output)`

--

--