Arjun Thakur has Published 1109 Articles

Java program to swap two numbers using XOR operator

Arjun Thakur

Arjun Thakur

Updated on 13-Mar-2020 09:47:31

360 Views

Following is a program to swap two numbers using XOR operator.Examplepublic class ab31_SwapTwoNumberUsingXOR {    public static void main(String args[]){       int a, b;       Scanner sc = new Scanner(System.in);       System.out.println("Enter a value :");       a = sc.nextInt();       ... Read More

Write an example to find whether a given string is palindrome using recursion

Arjun Thakur

Arjun Thakur

Updated on 13-Mar-2020 07:31:14

813 Views

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.Following is an example to find palindrome of a given number using recursive function.Examplepublic ... Read More

Java program to implement selection sort

Arjun Thakur

Arjun Thakur

Updated on 13-Mar-2020 05:44:07

3K+ Views

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is ... Read More

How can I eliminate numbers in a string in Python?

Arjun Thakur

Arjun Thakur

Updated on 05-Mar-2020 11:25:01

121 Views

You can create an array to keep track of all non digit characters in a string. Then finally join this array using "".join method. examplemy_str = 'qwerty123asdf32' non_digits = [] for c in my_str:    if not c.isdigit():       non_digits.append(c) result = ''.join(non_digits) print(result)OutputThis will give the outputqwertyasdfexampleYou can ... Read More

How to Find Hash of File using Python?

Arjun Thakur

Arjun Thakur

Updated on 05-Mar-2020 10:46:21

2K+ Views

You can find the hash of a file using the hashlib library. Note that file sizes can be pretty big. Its best to use a buffer to load chunks and process them to calculate the hash of the file. You can take a buffer of any size.Exampleimport sys import hashlib ... Read More

How do I serialize a Python dictionary into a string, and then back to a dictionary?

Arjun Thakur

Arjun Thakur

Updated on 05-Mar-2020 06:48:18

430 Views

The JSON module is a very reliable library to serialize a Python dictionary into a string, and then back to a dictionary. The dumps function converts the dict to a string. exampleimport json my_dict = {    'foo': 42,    'bar': {       'baz': "Hello",       'poo': ... Read More

How can I write an SQL IN query with a Python tuple?

Arjun Thakur

Arjun Thakur

Updated on 05-Mar-2020 06:33:30

2K+ Views

To write an SQL in query, you need to ensure that you provide the placeholders in the query using  so that the query is properly escaped. For example, Examplemy_tuple = ("Hello", "world", "John") placeholder= '?' placeholders= ', '.join(placeholder for _ in my_tuple) query= 'SELECT name FROM students WHERE id IN ... Read More

What's the canonical way to check for type in python?

Arjun Thakur

Arjun Thakur

Updated on 05-Mar-2020 06:23:57

97 Views

If you want to check if an object, x is an instance of exactly a given type(not a subtype), you can use typeto get its type and check using is statement.examplex = "Hello" if type(x) is str:    print("x is an instance of str")OutputThis will give the outputx is an ... Read More

How to group Python tuple elements by their first element?

Arjun Thakur

Arjun Thakur

Updated on 05-Mar-2020 06:03:54

522 Views

Python has a function called defaultdict that groups Python tuple elements by their first element.Examplelst = [    (1, 'Hello', 'World', 112),    (2, 'Hello', 'People', 42),    (2, 'Hi', 'World', 200) ]from collections import defaultdictd = defaultdict(list) for k, *v in lst:    d[k].append(v) print(d)OutputThis will give the outputdefaultdict(, ... Read More

How can I convert a bytes array into JSON format in Python?

Arjun Thakur

Arjun Thakur

Updated on 05-Mar-2020 05:44:42

19K+ Views

You need to decode the bytes object to produce a string. This can be done using the decode function from string class that will accept then encoding you want to decode with. examplemy_str = b"Hello" # b means its a byte string new_str = my_str.decode('utf-8') # Decode using the utf-8 encoding ... Read More

Advertisements