Programming Articles - Page 2364 of 3363

Amazing hacks of Python

sudhir sharma
Updated on 14-Jul-2025 15:42:30

249 Views

Python is one of the programming languages known for its simple syntax, readability. Whether working on development, data analysis, or automation, Python provides the tools to get the task done with less effort. But beyond the basics, Python has some hacks that make the code not only shorter but also more efficient. These are not bugs or workarounds, but the smart ways of using the Python built-in features and functions to solve tasks in a faster manner. In this article, we are going to learn about the amazing hacks of Python. Performing a Swap Without a ... Read More

FieldNamingPolicy enum using Gson in Java?

raja
Updated on 07-Jul-2020 12:04:50

1K+ Views

Gson library provides the naming conventions as part of enum FieldNamingPolicy. We can set the field naming policy using the setFieldNamingPolicy() method of the GsonBuilder class.FieldNamingPolicy enum ConstantsIDENTITY − Using this naming policy, the field name is unchanged.LOWER_CASE_WITH_DASHES − Using this naming policy, modify the Java Field name from its camel-cased form to a lower case field name where each word is separated by a dash (-).LOWER_CASE_WITH_UNDERSCORES − Using this naming policy, modify the Java Field name from its camel-cased form to a lower case field name where each word is separated by an underscore (_).UPPER_CAMEL_CASE − Using this naming policy, ... Read More

How to get the JsonGenerator settings using Jackson in Java?

raja
Updated on 07-Jul-2020 12:05:39

415 Views

The JsonGenerator class can be responsible for writing JSON data as a stream instead of constructing an Object Model in memory. The list of settings that can be turned on/off is present in an enum JsonGenerator.Feature, it contains static method values() that returns an array containing the constants of this enum type.Syntaxpublic static enum JsonGenerator.Feature extends EnumExampleimport java.io.*; import com.fasterxml.jackson.core.*; public class JsonGeneratorSettingsTest {    public static void main(String[] args) throws IOException {       StringWriter writer = new StringWriter();       JsonFactory jsonFactory = new JsonFactory();       JsonGenerator jsonGenerator = jsonFactory.createGenerator(writer);       for(JsonGenerator.Feature feature : JsonGenerator.Feature.values()) {     ... Read More

How to get the values of a key using the JsonPointer interface in Java?

raja
Updated on 07-Jul-2020 11:58:26

824 Views

The JSONPointer is a standard that defines a string syntax that can be used to access a particular key value in the JSON document. An instance of JSONPointer can be created by calling the static factory method createPointer() on the Json class. In the JSONPointer,  every string syntax is prefixed with “/”. We can get the value of a key by calling the getValue() method on the JsonPointer object.JSON fileExampleimport javax.json.*; import java.io.*; public class JsonPointerTest {    public static void main(String[] args) throws Exception {       JsonReader jsonReader = Json.createReader(new FileReader("simple.json"));       JsonStructure jsonStructure = jsonReader.read();       JsonPointer jsonPointer1 ... Read More

Importance of the JsonPatch interface in Java?

raja
Updated on 07-Jul-2020 11:55:50

653 Views

The JsonPatch interface is a format for storing a sequence of operations that can be applied to the target JSON structure. There are few operations like add, remove, replace, copy, move and test can be stored in JsonPath and operated on JSON structure. The JsonPatchBuilder interface can be used for constructing a JSON patch using the Json.createPatchBuilder().JSON fileExampleimport java.io.*; import javax.json.Json; import javax.json.JsonPatch; import javax.json.JsonPatchBuilder; import javax.json.JsonReader; import javax.json.JsonStructure; public class JsonPatchTest {    public static void main(String[] args) throws Exception {       JsonPatchBuilder jsonPatchBuilder = Json.createPatchBuilder();       JsonPatch jsonPatch = jsonPatchBuilder.add("/postalCode", "500072").remove("/age").build();       JsonReader reader = Json.createReader(new FileReader("simple.json"));     ... Read More

Minimum and Maximum number of pairs in m teams of n people in C++

Arnab Chakraborty
Updated on 23-Oct-2019 09:15:43

338 Views

Problem statementN participants of the competition were split into M teams in some manner so that each team has at least one participant. After the competition each pair of participants from the same team became friends.Your task is to write a program that will find the minimum and the maximum number of pairs of friends that could have formed by the end of the competition.Algorithm1. We can obtain max pairs using below formula: maxPairs = ((n – m) * (n – m + 1)) / 2 2. We can obtain min pairs using below formula: minPairs = m * (((n ... Read More

Minimizing array sum by applying XOR operation on all elements of the array in C++

Arnab Chakraborty
Updated on 23-Oct-2019 09:11:59

259 Views

DescriptionGiven an array of size, N. Find an element X such that the sum of array elements should be minimum when XOR operation is performed with X and each element of an array.If input array is: arr [] = {8, 5, 7, 6, 9} then minimum sum will be 30 Binary representation of array elments are: 8 : 1000 5 : 0101 7 : 0111 6 : 0101 9 : 1001 If X = 5 then after performing XOR sum will be 30: 8 ^ 5 = 13 5 ^ 5 = 0 7 ^ 5 = 2 6 ^ ... Read More

Find all the patterns of “1(0+)1” in a given string using Python Regex

Pradeep Elance
Updated on 23-Oct-2019 08:22:30

275 Views

In this tutorial, we are going to write a program which finds all the occurrences of the 1(0+1) in a string using the regexes. We have a re module in Python which helps us to work with the regular expressions.Let's see one sample case.Input: string = "Sample 1(0+)1 string with 1(0+)1 unnecessary patterns 1(0+)1" Output: Total number of pattern maches are 3 ['1(0+)1', '1(0+)1', '1(0+)1']Follow the below steps to write the code for the program.Algorithm1. Import the re module. 2. Initialise a string. 3. Create a regex object using regular expression which matches the pattern using the re.compile(). Remember to ... Read More

Check if both halves of the string have the same set of characters in Python

Pradeep Elance
Updated on 23-Oct-2019 08:20:20

200 Views

We have to check whether two halves of a string have the same set of characters or not in Python. The frequency of the characters in the two halves must be identical. If the length of the string is odd, ignore the middle and check for the remaining characters. Follow the below steps to write code for the program.Algorithm1. Initialize a string. 2. Initialize an empty dictionary variable alphabets. 3. Initialize a variable mid with length / 2. 4. Write a loop until mid element.    4.1. Initialize the corresponding dictionary item by alphabets[char] with one if it's not initialized. ... Read More

Basic calculator program using Python program

Niharikaa Aitam
Updated on 20-Jun-2025 19:27:44

2K+ Views

In this tutorial, we are going to build a basic calculator in Python. As we all know that a calculator will give six options to the user from which they select one option and we will perform the respective operation. Following are the arithmetic operations that we can perform using a basic calculator - Addition Subtraction Multiplication Division Floor Division Modulo Steps in Developing the Basic Calculator Following are the steps involved in creating a basic calculator in Python - Defining Arithmetic Functions First, we are defining all the arithmetic operations that can be performed by using a ... Read More

Advertisements