Found 34494 Articles for Programming

How to check if array contains three consecutive dates in java?

Nikitha N
Updated on 19-Feb-2020 12:22:03

820 Views

To check to find whether a given array contains three consecutive dates:Convert the given array into a list of type LocalDate.Using the methods of the LocalDate class compare ith, i+1th and i+1th, i+2th elements of the list if equal the list contain 3 consecutive elements.ExampleLive Demoimport java.time.LocalDate; import java.time.Month; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; public class ConsicutiveDate {    public static void main(String args[]) {       String[] dates = {"5/12/2017", "6/12/2017", "7/12/2017"};       List localDateList = new ArrayList();       for (int i = 0; i

How to get a list of all sub-directories in the current directory using Python?

Rajendra Dharmkar
Updated on 16-Dec-2019 06:18:58

9K+ Views

To get a list of all subdirectories in a directory, recursively, you can use the os.walk function. It returns a three tuple with first entry being all the subdirectories.You can use it as follows:import os subdirs = [x[0] for x in os.walk('.')] print(subdirs)You can also list the directories(immediate only) using the os.listdir function and filtering the list using the os.path.isdir function.For exampleimport os d = '.' subdirs = [os.path.join(d, o) for o in os.listdir(d) if os.path.isdir(os.path.join(d,o))]

What is the lambda expression to convert array/List of String to array/List of Integers in java?

Srinivas Gorla
Updated on 16-Jun-2020 10:12:36

616 Views

You can convert an array list of strings to an array list of integers as:ExampleLive Demoimport java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class LamdaExpressions {    public static void main(String args[]) {       ArrayList list = new ArrayList();       list.add("123");       list.add("223");       list.add("323");       list.add("334");       List listInteger =          list.stream().map(s -> Integer.parseInt(s)).collect(Collectors.toList());       System.out.println(listInteger);    } }Output[123, 223, 323, 334]

How can I put a Java arrays inside an array?

Abhinanda Shri
Updated on 16-Jun-2020 10:11:04

2K+ Views

ExampleLive Demoimport java.util.Arrays; public class ArrayWithinAnArray{    public static void main(String args[]) {       int[] myArray1 = {23, 56, 78, 91};       int[] myArray2 = {123, 156, 178, 191};       int[] myArray3 = {223, 256, 278, 291};       int[] myArray4 = {323, 356, 378, 391};       int [][] arrayOfArrays = {myArray1, myArray2, myArray3, myArray4};       System.out.println(Arrays.deepToString(arrayOfArrays));    } }Output[[23, 56, 78, 91], [123, 156, 178, 191], [223, 256, 278, 291], [323, 356, 378, 391]]

Why are classes sometimes declared final in Java?

Sreemaha
Updated on 30-Jul-2019 22:30:20

539 Views

If a class is declared final, you cannot inherit it. If you try it gives you a compile-time error as − Example final class Super { private int data = 30; } public class Sub extends Sub { public static void main(String args[]){ } } Output Exception in thread "main" java.lang.Error: Unresolved compilation problem: at Sub.main(Sub.java:7)

How to print full path of current file's directory in Python?

Alekhya Nagulavancha
Updated on 24-Feb-2023 12:09:56

16K+ Views

The path of a current file is defined with the help of directory hierarchy; and it contains the backtracked path from the current file to the root directory this file is present in. For instance, consider a file “my_file” belongs to a directory “my_directory”, the path for this file is defined as given below ./my_directory/my_file The directory, sub-directory and the file are all separated using the “/” separator in the path. Therefore, to get current file's full path, you can use the os.path.abspath() function. If you want only the directory path, you can call os.path.dirname() method. Using os.path.abspath() Method ... Read More

What is __init__.py in Python?

Rajendra Dharmkar
Updated on 16-Dec-2019 06:14:02

2K+ Views

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable. An example of the __init__.py file:from math import sin from my_package import my_funcThe above code ensures that the imported functions are available in the package's modules when it is imported as it is defined ... Read More

How to calculate a directory size using Python?

Alekhya Nagulavancha
Updated on 24-Feb-2023 12:04:36

6K+ Views

A directory is simply defined as a collection of subdirectories and single files; or either one of them. These subdirectories are separated using a “/” operator in a directory hierarchy. A directory hierarchy is constructed by organizing all the files and subdirectories within a main directory, also known as “root” directory. When the size of a directory is to be calculated, we will consider it as a root directory and calculate the individual sizes of all the files and subdirectories (if any) present in it. Hence, to get the size of a directory we must walk through the hierarchy to ... Read More

How to find a file using Python?

Rajendra Dharmkar
Updated on 22-Aug-2023 11:38:06

1K+ Views

Python stands out as one of the most adaptable and popular languages in the huge world of programming. Its ease of use and dependability make it a preferred option for developers working in a variety of fields. A frequent chore for programmers is looking for files on their machines. Finding files in Python is a crucial ability to have, whether you're organizing data, analyzing logs, or developing a file management solution. In this article, we'll examine a number of Python file-finding strategies and approaches. We'll discuss both the conventional and more sophisticated methods, giving detailed explanations to make sure you ... Read More

How do I find the location of my Python site-packages directory?

Rajendra Dharmkar
Updated on 22-Aug-2023 11:26:10

2K+ Views

When it comes to Python development, one essential aspect is knowing how to locate the Python "site-packages" directory. This directory holds a plethora of third-party libraries and packages that enhance Python's capabilities and streamline the development process. Whether you are an experienced developer or just starting, understanding the precise location of the "site-packages" directory empowers you to effectively manage installed packages and enables manual interaction with them. In this comprehensive article, we will explore a variety of methods to pinpoint the location of the "site-packages" directory on different operating systems. From utilizing the versatile "sys" library to leveraging the comprehensive ... Read More

Advertisements