Found 33676 Articles for Programming

Rename multiple files using Java

AmitDiwan
Updated on 04-Jul-2020 10:46:58

1K+ Views

Following is the code to rename multiple files using Java −Exampleimport java.io.File; import java.io.IOException; public class Demo{    public static void main(String[] argv) throws IOException{       String path_to_folder = "path\to\folder\where\multiple\files\are\present";       File my_folder = new File(path_to_folder);       File[] array_file = my_folder.listFiles();       for (int i = 0; i < array_file.length; i++){          if (array_file[i].isFile()){             File my_file = new File(path_to_folder + "\" + array_file[i].getName());             String long_file_name = array_file[i].getName();             String[] my_token = long_file_name.split("\s"); ... Read More

How to clear screen using Java?

AmitDiwan
Updated on 04-Jul-2020 10:44:46

3K+ Views

Following is the code to clear screen using Java −Example Live Demopublic class Demo{    public static void main(String[] args){       System.out.print("\033[H\033[2J");       System.out.flush();    } }OutputThe screen would be clearedA class named Demo contains the main function. Here, the ANSI escape code is written, that clears the screen. The flush function resets the cursor to the top of the window screen.

Java Program for check if a given number is Fibonacci number?

Shriansh Kumar
Updated on 01-Aug-2024 11:57:13

3K+ Views

For a given input number, write a Java program to check if it is a Fibonacci number. Any number that belongs to Fibonacci series is called as Fibonacci number. The Fibonacci series is a sequence of numbers formed by the sum of its two previous integers. The first two terms of this series are 0 and 1 and further terms go like 1, 2, 3, 5, 8 and so on. This series was named after a famous Italian mathematician, Leonardo Fibonacci. Example Scenario 1 Input: num1 = 8; Output: 8 is a Fibonacci number 8 comes into ... Read More

Number of Recent Calls in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:27:37

443 Views

Suppose we want to write a class called RecentCounter to count recent requests. This class has only one method: ping(t), where t is representing some time in milliseconds. This will return the number of pings that have been made from 3000 milliseconds ago until now. Any ping with time in [t - 3000, t] will count, including the current ping. And it is guaranteed that every call to ping uses a strictly larger value of t than before.So, if the input is like Call ping four times ping(1), ping(100), ping(3001), ping(3002), then the output will be 1, 2, 3, 3 ... Read More

X of a Kind in a Deck of Cards in C++

Arnab Chakraborty
Updated on 04-Jul-2020 10:25:58

303 Views

Suppose we have a deck of cards, each card has an integer written on it. We have to check whether we can choose X >= 2 such that it is possible to split the entire deck into 1 or more groups of cards, where the following condition satisfies: Each group has exactly X number of cards. All of the cards in each group have the same number.So, if the input is like deck = [1, 2, 3, 4, 4, 3, 2, 1], then the output will be True, as possible partitions are [1, 1], [2, 2], [3, 3], [4, 4].To ... Read More

Smallest Range I in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:24:36

423 Views

Suppose we have an array A of integers, now for each integer A[i] we can choose any x with range [-K to K] then add x to A[i]. Now after this process, we have some array B. We have to find the smallest possible difference between the maximum value of B and the minimum value of B.So, if the input is like A = [0,10], K = 2, then the output will be 6, as B = [2,8]To solve this, we will follow these steps −MAX := (maximum of A) - KMIN := (minimum of A) + Kdifference := MAX - MINif difference

Groups of Special-Equivalent Strings in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:23:13

304 Views

Suppose we have an array of strings called A. One move onto S consists of swapping any two even indexed characters of S, or any two odd indexed characters of S.Now, two strings S and T are special-equivalent if after any number of moves onto S, S is same as T. So, if S = "zzxy" and T = "xyzz" are special-equivalent because we may make the moves like "zzxy" to "xzzy" to "xyzz" that swap S[0] and S[2], then S[1] and S[3].Now, a group of special-equivalent strings from A is a non-empty subset of A such that −In every ... Read More

Projection Area of 3D Shapes

Arnab Chakraborty
Updated on 04-Jul-2020 10:21:08

515 Views

Suppose there is a N x N grid, we place some 1 x 1 x 1 cubes that are axis-aligned with the x, y, and z. Here each value v = grid[i][j] is showing a tower of v cubes placed on top of grid cell (i, j). We view the projection of these cubes onto the xy, yz, and zx planes. Here, we are viewing the projection when looking at the cubes from the top, the front, and the side view. We have to find the total area of all three projections.So, if the input is like [[1, 2], [3, ... Read More

Walking Robot Simulation in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:19:23

1K+ Views

Suppose there is a robot on an infinite grid that starts at point (0, 0). It is facing towards north direction. Now the robot can receive one of three possible types of commands −-2 to turn left 90 degrees-1 to turn right 90 degreesany value from 1 to 9 to move forward x unitsThere are some grid squares that are obstacles.We also have another array called obstacle, this indicates the i-th obstacle is at grid point (obstacles[i][0], obstacles[i][1]), If the robot wants to move onto them, the robot stays on the previous grid square instead.We have to find the square ... Read More

Binary Gap in Python

Arnab Chakraborty
Updated on 04-Jul-2020 10:17:08

2K+ Views

Suppose we have a positive integer N, we have to find the longest distance between two consecutive 1's in the binary representation of N. If there are no two-consecutive 1's, then return 0.So, if the input is like 22, then the output will be 2, as 22 in binary is 10110. There are three ones In the binary representation of 22, and two consecutive pairs of 1's. The first consecutive pair of 1's have distance 2, then the second consecutive pair of 1's have distance 1. Answer will be the largest of these two distances, which is 2.To solve this, ... Read More

Advertisements