Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 106 of 151

Conversion of Set To Stream in Java

Samual Sam
Samual Sam
Updated on 18-Jun-2020 1K+ Views

Being a type of Collection, we can convert a set to Stream using its stream() method. Example import java.util.HashSet; import java.util.Set; import java.util.stream.Stream; public class Tester {     public static void main(String args[]) {         Set<String> set = new HashSet<String>();         set.add("a");         set.add("b");         set.add("c");         set.add("d");         set.add("e");         set.add("f");         Stream<String> stream = set.stream();         stream.forEach(data->System.out.print(data+" "));     } } Output a b c d e f

Read More

Character Stream vs Byte Stream in Java

Samual Sam
Samual Sam
Updated on 18-Jun-2020 11K+ Views

Byte StreamsJava byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are, FileInputStream and FileOutputStream. Following is an example which makes use of these two classes to copy an input file into an output file −Exampleimport java.io.*; public class CopyFile {    public static void main(String args[]) throws IOException {               FileInputStream in = null;       FileOutputStream out = null;       try {          in = new ...

Read More

Checking internet connectivity in Java

Samual Sam
Samual Sam
Updated on 18-Jun-2020 6K+ Views

Internet connectivity can be checked using java.net.URL and java.net.URLConnection class. Following are the required steps.Create a URL object and pass it the URL say GoogleCall URL.openConnection() method to get a URLConnection object.Call URLConnection.connect() method to check the internet connectivity. connect() method opens a communications link to the resource referenced by the passed URL if a connection has not already been established.Exampleimport java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class Tester {    public static void main(String[] args) {       try {          URL url = new URL("http://www.google.com");          URLConnection connection ...

Read More

Automatic resource management in Java

Samual Sam
Samual Sam
Updated on 18-Jun-2020 987 Views

automatic resource management or try-with-resources is a new exception handling mechanism that was introduced in Java 7, which automatically closes the resources used within the try-catch block.ResourceA resource is an object which is required to be closed once our program finishes. For example, a file is read, database connection and so on.UsageTo use the try-with-resources statement, you simply need to declare the required resources within the parenthesis, and the created resource will be closed automatically at the end of the block. Following is the syntax of the try-with-resources statement.Syntaxtry(FileReader fr = new FileReader("file path")) {        // use the resource ...

Read More

Can we change Python for loop range (higher limit) at runtime?

Samual Sam
Samual Sam
Updated on 17-Jun-2020 481 Views

No, You can't modify a range once it is created. Instead what you can do is use a while loop instead. For example, if you have some code like:for i in range(lower_limit, higher_limit, step_size):# some code if i == 10:    higher_limit = higher_limit + 5You can change it to:i = lower_limit while i < higher_limit:    # some code    if i == 10:       higher_limit = higher_limit + 5    i += step_size

Read More

How to comment each condition in a multi-line if statement in Python?

Samual Sam
Samual Sam
Updated on 17-Jun-2020 322 Views

You can do this directly if you are surrounding your multiline if statements conditions in a parenthesis. For example,if (cond1 == 'val1' and    cond2 == 'val2' and # Some comment    cond3 == 'val3' and # Some comment    cond4 == 'val4'):However, this is not possible if you try to do this without a parenthesis. For example, the following code will give an error:if cond1 == 'val1' and \    cond2 == 'val2' and \ # Some comment    cond3 == 'val3' and \ # Some comment    cond4 == 'val4':

Read More

What is python .. ("dot dot") notation syntax?

Samual Sam
Samual Sam
Updated on 17-Jun-2020 538 Views

There is no special .. ("dot dot") notation syntax in python. You can, however, see this in case of floats accessing their properties. For example,f = 1..__truediv__ # or 1..__div__ for python 2 print(f(8))This will give the output:0.125What we have is a float literal without the trailing zero, which we then access the __truediv__ method of. It's not an operator in itself; the first dot is part of the float value, and the second is the dot operator to access the object's properties and methods. This can also be achieved using:>>> f = 1. >>> f 1.0 >>> f.__truediv__

Read More

Magic Square

Samual Sam
Samual Sam
Updated on 17-Jun-2020 6K+ Views

The magic square is a square matrix, whose order is odd and where the sum of the elements for each row or each column or each diagonal is same. The sum of each row or each column or each diagonal can be found using this formula. n(n2+ 1)/2Here are the rules to construct a magic square −We will start from the middle column of the first row, of the matrix, and always go to the top left corner to place next numberIf the row exceeds, or the row is not in the matrix, then, change the column as left column and ...

Read More

Polynomial Time Approximation Scheme

Samual Sam
Samual Sam
Updated on 17-Jun-2020 1K+ Views

Polynomial Time Approximation schemeWe can find some polynomial time solution for NP-Complete problems like 0-1 Knapsack problem or Subset sum problem. These problems are very popular in the real world, so there must be some ways to handle these problems.The Polynomial Time Approximation Scheme (PTAS) is a type to approximate algorithms for optimization problems. For the 0-1 Knapsack problem, there is a Pseudo Polynomial Solution, but when the values are large, the solution is not feasible. Then we need a PTAS solution.Some NP-complete problems like Graph Coloring, K-Center problem etc. they have no known polynomial time solution. PTAS used to approximate ...

Read More

Lexicographically minimum string rotation

Samual Sam
Samual Sam
Updated on 17-Jun-2020 785 Views

Let us consider a string is given, we know that the string is a sequence of characters. The Lexicographical rotation is the rotation of string, to convert characters in lexicographical order.The solution is simple, we simply concatenate the given string with itself, then in another array, all rotation of strings are stored. After that sort the array in ascending order, the lowest value is the final result.Input and OutputInput: The String “BCAAFAABCD” Output: Rotated String: “AABCDBCAAF”AlgorithmminStrRotation(str)Input − The given string.Output − Minimum string rotation required.Begin n := length of str define strArr to store ...

Read More
Showing 1051–1060 of 1,507 articles
« Prev 1 104 105 106 107 108 151 Next »
Advertisements