package question6;

public class Cat extends Pet {

    public Cat(String name) {
        super(name);
    }

    @Override
    public String speak() {
        return "meow";
    }
}
package question6;

public class Dog extends Pet {

    public Dog(String name) {
        super(name);
    }

    @Override
    public String speak() {
        return "dog-sound";
    }
}
package question6;

import java.util.ArrayList;
import java.util.List;

public class Kennel {
    private ArrayList<Pet> petList;

    public Kennel(ArrayList<Pet> petList) {
        this.petList = petList;
    }

    public void allSpeak() {
        for (Pet pet : petList) {
            System.out.println(pet.getMyName() + " " + pet.speak());
        }
    }

    public static void main(String[] args) {
        ArrayList<Pet> petList = new ArrayList<>(List.of(
                new Cat("cat"),
                new Dog("dog"),
                new LoudDog("loud dog")));

        Kennel k = new Kennel(petList);
        k.allSpeak();
        ;
    }
}
package question6;

public class LoudDog extends Dog {

    public LoudDog(String name) {
        super(name);
    }

    @Override
    public String speak() {
        return (super.speak() + " " + super.speak());
    }
}
package question6;

public abstract class Pet {
    private String myName;

    public Pet(String name) {
        myName = name;
    }

    public String getMyName() {
        return myName;
    }

    public abstract String speak();
}
package question14;

public class Bicycle {
    private String name;
    private String type;
    private boolean assembled;

    public Bicycle(String name, String type, boolean assembled) {
        this.name = name;
        this.type = type;
        this.assembled = assembled;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

    public boolean isAssembled() {
        return assembled;
    }
}
package question14;

import java.util.ArrayList;
import java.util.List;

public class BicycleInventory {
    private ArrayList<Bicycle> bicycleList;

    public BicycleInventory(ArrayList<Bicycle> bicycleList) {
        this.bicycleList = new ArrayList<>(bicycleList);
    }

    public Bicycle[] getChoices(int n, String type, boolean assembled) {
        Bicycle[] bicycles = new Bicycle[n];
        int i = 0;

        for (Bicycle bicycle : bicycleList) {
            if (bicycle.getType().equals(type) && bicycle.isAssembled() == assembled) {
                bicycles[i] = bicycle;
                i++;
                if (i == n) {
                    break;
                }
            }
        }

        return bicycles;
    }

    public Bicycle chooseOne(int n, String type, boolean assembled) {
        Bicycle[] bicycles = getChoices(n, type, assembled);
        Bicycle bicycle;

        for (Bicycle bike : bicycles) {
            if (bike != null) {
                while (true) {
                    bicycle = bicycles[(int) (Math.random() * bicycles.length)];
                    if (bicycle != null) {
                        return bicycle;
                    }
                }
            }
        }

        return null;
    }

    public static void main(String[] args) {

        Bicycle bike1 = new Bicycle("bike1", "road", true);
        Bicycle bike2 = new Bicycle("bike2", "mountain", false);
        Bicycle bike3 = new Bicycle("bike3", "road", false);
        Bicycle bike4 = new Bicycle("bike4", "road", true);
        Bicycle bike5 = new Bicycle("bike5", "mountain", false);

        ArrayList<Bicycle> bicycleList = new ArrayList<>(List.of(bike1, bike2, bike3, bike4, bike5));

        BicycleInventory classicBike = new BicycleInventory(bicycleList);

        for (Bicycle bicycle : classicBike.getChoices(2, "mountain", false)) {
            if (bicycle != null) {
                System.out.print(bicycle.getName() + " ");
            } else {
                System.out.print(null + " ");
            }
        }
        System.out.println();

        for (Bicycle bicycle : classicBike.getChoices(3, "road", false)) {
            if (bicycle != null) {
                System.out.print(bicycle.getName() + " ");
            } else {
                System.out.print(null + " ");
            }
        }
        System.out.println();

        for (Bicycle bicycle : classicBike.getChoices(1, "road", true)) {
            if (bicycle != null) {
                System.out.print(bicycle.getName() + " ");
            } else {
                System.out.print(null + " ");
            }
        }
        System.out.println();
    }
}
package question16;

public class Converter {
    public static String convertToString(int num) {
        if (num == 1) {
            return "apple";
        }
        if (num == 2) {
            return "orange";
        }
        if (num == 3) {
            return "banana";
        }
        return "paper";
    }

    public static String buildString(int input) {
        StringBuilder sb = new StringBuilder();

        for (char c : Integer.toString(input).toCharArray()) {
            sb.append(convertToString(Character.getNumericValue(c)));
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(Converter.buildString(3));
        System.out.println(Converter.buildString(123));
        System.out.println(Converter.buildString(321));

    }
}
package question29;

public class StringAnalyzer {
    public static int countRepeat(String largeStr, String smallStr) {
        int ct = 0;

        for (int i = 0; i <= largeStr.length() - smallStr.length(); i++) {
            if (largeStr.startsWith(smallStr, i)) {
                ct++;
                i += smallStr.length() - 1;
            }
        }

        return ct;
    }

    public static void main(String[] args) {
        System.out.println(StringAnalyzer.countRepeat("BAAB", "AA"));
        System.out.println(StringAnalyzer.countRepeat("AAAAA", "AA"));
        System.out.println(StringAnalyzer.countRepeat("AABABABAA", "ABA"));
        System.out.println(StringAnalyzer.countRepeat("ABBAABB", "ABA"));
    }
}
package question31;

import java.util.ArrayList;

public class Delimiters {
    private String openDel;
    private String closeDel;

    public Delimiters(String open, String close) {
        openDel = open;
        closeDel = close;
    }

    public ArrayList<String> getDelimitersList(String[] tokens) {
        ArrayList<String> delimiters = new ArrayList<>();

        for (String token : tokens) {
            if (token.equals(openDel) || token.equals(closeDel)) {
                delimiters.add(token);
            }
        }

        return delimiters;
    }

    public boolean isBalanced(ArrayList<String> delimiters) {
        int openDelCt = 0;
        int closeDelCt = 0;

        for (String delimiter : delimiters) {
            if (delimiter.equals(openDel)) {
                openDelCt++;
            } else if (delimiter.equals(closeDel)) {
                closeDelCt++;
            }

            if (closeDelCt > openDelCt) {
                return false;
            }
        }

        return openDelCt == closeDelCt;
    }

    public static void main(String[] args) {
        Delimiters d1 = new Delimiters("(", ")");
        Delimiters d2 = new Delimiters("<q>", "</q>");

        System.out.println(d1.getDelimitersList(new String[]{"(", "x + y", ")", " * 5"}));
        System.out.println(d2.getDelimitersList(new String[]{"<q>", "yy", "</q>", "zz", "</q>"}));
    }
}
package question54;

import java.util.Arrays;

public class ArrayTester {
    public static int[] getColumn(int[][] arr2D, int c) {
        int[] col = new int[arr2D.length];

        for (int i = 0; i < arr2D.length; i++) {
            col[i] = arr2D[i][c];
        }

        return col;
    }

    public static boolean hasAllValues(int[] arr1, int[] arr2) {
        boolean f = false;

        for (int i : arr1) {
            f = false;
            for (int j : arr2) {
                if (i == j) {
                    f = true;
                    break;
                }
            }
            if (!f) {
                return false;
            }
        }

        return true;
    }

    public static boolean containsDuplicates(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] == arr[j]) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isLatin(int[][] square) {
        if (containsDuplicates(square[0])) {
            return false;
        }

        for (int[] row : square) {
            if (!hasAllValues(square[0], row)) {
                return false;
            }
        }

        for (int i = 0; i < square.length; i++) {
            if (!hasAllValues(square[0], getColumn(square, i))) {
                return false;
            }
        }

        return true;
    }

    public static void main(String[] args) {
        int[][] arr2D = {
                {0, 1, 2},
                {3, 4, 5},
                {6, 7, 8},
                {9, 5, 3}
        };

        System.out.println(Arrays.toString(ArrayTester.getColumn(arr2D, 1)));

        System.out.println(ArrayTester.isLatin(new int[][]{
                {1, 2, 3},
                {2, 3, 1},
                {3, 1, 2}
        }));

        System.out.println(ArrayTester.isLatin(new int[][]{
                {10, 30, 20, 0},
                {0, 20, 30, 10},
                {30, 0, 10, 20},
                {20, 10, 0, 30}
        }));

        System.out.println(ArrayTester.isLatin(new int[][]{
                {1, 2, 1},
                {2, 1, 1},
                {1, 1, 2}
        }));

        System.out.println(ArrayTester.isLatin(new int[][]{
                {1, 2, 3},
                {3, 1, 2},
                {7, 8, 9}
        }));

        System.out.println(ArrayTester.isLatin(new int[][]{
                {1, 2},
                {1, 2}
        }));
    }
}