Skip to content

Instantly share code, notes, and snippets.

View wsadrak's full-sized avatar
🏠
Working from home

Wojtek wsadrak

🏠
Working from home
  • Kraków, Poland
View GitHub Profile
@wsadrak
wsadrak / reverse-string-recursion.java
Created March 7, 2019 09:42
Description: Writea a program to the string entered by user and then reverse it
import java.util.Scanner;
public class ReverseStringRecursion {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please type a word to reverse: ");
String stringToReverse = scanner.nextLine();
scanner.close();
@wsadrak
wsadrak / top-ten-repeated-words.java
Created March 6, 2019 15:43
Description: Write a program to read words from a file. Count the repeated or duplicated words. Sort it by maximum repeated or duplicated word count and limit to 10.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.stream.Collectors;
@wsadrak
wsadrak / middle-index.java
Created March 6, 2019 14:01
Description: You are given an array of numbers. Find out the array index or position where sum of numbers preceeding the index is equals to sum of numbers succeeding the index.
public class MiddleIndex {
public static void main(String[] args) {
int[] array = { 2, 4, 4, 5, 4, 1 };
try {
System.out.println("Starting from index 0, adding numbers till index " + findMiddleIndex(array) + " and");
System.out.println("adding rest of the numbers can be equal");
} catch (Exception e) {
System.out.println(e.getMessage());
}
@wsadrak
wsadrak / common-values-between-sorted-arrays.java
Created March 6, 2019 09:35
Description: Write a program to find common integers between two sorted arrays. Both arrays are sorted in ASC order. Both arrays doesn't have any duplicate numbers.
public class CommonValues {
public static void main(String[] args) {
int[] array1 = { 2, 7, 17, 19, 20, 45, 56, 159, 239 };
int[] array2 = { 7, 12, 15, 19, 22, 34, 55, 150, 159 };
findAndPrintCommonValues(array1, array2);
}
private static void findAndPrintCommonValues(int[] array1, int[] array2) {
System.out.println("Common values: ");
@wsadrak
wsadrak / bubble-sort.java
Created March 4, 2019 18:33
Description: Write a program for Bubble Sort in java.
public class BubbleSort {
public static void main(String[] args) {
int[] array = { 3, 60, 35, 2, 45, 320, 5 };
printValues(array);
bubbleSort(array);
}
private static void printValues(int[] arrayToSort) {
for (int value : arrayToSort) {
@wsadrak
wsadrak / fibonacci-sequence.java
Created March 1, 2019 14:10
Description: Write a program to print fibonacci series.
public class FibonacciSequence {
public static void main(String[] args) {
int length = readValueFromUser();
int[] fibonacciSequenceArray = createFibonacciSequence(length);
printFullSequence(fibonacciSequenceArray);
}
private static void printFullSequence(int[] fibonacciSequenceArray) {
@wsadrak
wsadrak / perfect-number.java
Created March 1, 2019 13:41
Description: Write a program to find perfect number or not.
import java.util.Scanner;
public class PerfectNumber {
public static void main(String[] args) {
int value = readValueFromUser();
isPerfectValue(value);
}
private static int readValueFromUser() {
System.out.println("Type a value to check: ");
@wsadrak
wsadrak / duplicate-characters-in-string.java
Created March 1, 2019 09:01
Description: Write a program to find out duplicate or repeated characters in a string, and calculate the count of repeatation.
import java.util.Map;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
String word1 = "Philadelhpia";
String word2 = "Spring Framework";
String word3 = "InputMismatchException";
findAndPrintDuplicates(word1);
@wsadrak
wsadrak / floyds-triangle.java
Last active March 1, 2019 08:21
Description: This program will prompt user for number of rows and based on the input, it would print the Floyd’s triangle having the same number of rows.
import java.util.Scanner;
public class FloydsTriangle {
public static void main(String[] args) {
int howManyRows = readRowsFromUser();
printFloydsTriangle(howManyRows);
}
@wsadrak
wsadrak / leap-year.java
Last active February 28, 2019 14:20
Description: Write a program to check whether the input year is a leap year or not.
public class LeapYear {
private static final int LEAP_YEAR_CONDITION_VALUE = 4;
private static final int LEAP_YEAR_CONDITION_VALUE_2 = 100;
private static final int LEAP_YEAR_CONDITION_VALUE_3 = 400;
public static void main(String[] args) {
int year = readYearFromUser();
if (isLeapYear(year)) {
System.out.println(year + " - is leap year");