CoddityTeam / movaicode

Concours mensuel du Pire Développeur de France
123 stars 10 forks source link

Random, ASCII et FileReader #75

Closed picressant closed 2 years ago

picressant commented 3 years ago
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.*;

public class MovaiCode2 {

    public static void main(String[] args) {

        List<String> values = Arrays.asList("cadeau", "pourri", "cadeau", "revendre", "écharpe", "transformer", "smartbox", "pyrénéens", "goldeneye64", "cadeau");
        System.out.println("occurences: " + countOccurences(values, "cadeau"));
    }

    private static int countOccurences(List<String> values, String searchValue) {
        List<Integer> visitedIndexes = new ArrayList<>();
        String foundValues = "";
        try {
            FileWriter fileWriter = new FileWriter("./occurences.txt");
            PrintWriter printWriter = new PrintWriter(fileWriter);

            while (values.size() != visitedIndexes.size()) {
                Random r = new Random();
                int low = 0;
                int high = values.size();
                int index = r.nextInt(high - low) + low;

                if (visitedIndexes.contains(index)) {
                    continue;
                }
                else {
                    visitedIndexes.add(index);
                    int asciiA = getAsciiCount(values.get(index), 0, 0);
                    int asciiB = getAsciiCount(searchValue, 0, 0);

                    if (asciiA == asciiB) {
                        boolean isSameWord = true;
                        for (int i = 0; i < values.get(index).split("").length; i++) {
                            if (!sameChar(values.get(index).substring(i, i + 1), searchValue.substring(i, i + 1))) {
                                isSameWord = false;
                            }
                        }
                        if (isSameWord) {
                            printWriter.print(String.valueOf(asciiA) + "\n");
                        }
                    }
                }
            }

            printWriter.close();

            File myObj = new File("./occurences.txt");
            Scanner myReader = new Scanner(myObj);
            int lines = 0;
            while (myReader.hasNextLine()) {
                String data = myReader.nextLine();
                lines++;
            }
#
            return lines;
        }
        catch (Exception e) {
            return 0;
        }
    }

    private static boolean sameChar(String a, String b) {
        return String.valueOf(getAsciiCount(a, 0, 0)).hashCode() == String.valueOf(getAsciiCount(b, 0, 0)).hashCode();
    }

    private static int getAsciiCount(String value, int index, int asciiCount) {
        if (value.length() <= index) {
            return asciiCount;
        }
        else {
            String s = value.substring(index, index + 1);
            char c = s.charAt(0);
            asciiCount += (int) c;

            return getAsciiCount(value, index + 1, asciiCount);
        }
    }
}
louismarslen commented 3 years ago

Hello Pierre, j'ai une bonne et une mauvaise (movaize) nouvelle :

Bravo et pas bravo donc ! 😊

PS : oui c'est un copié-collé, j'ai pas suffisamment d'inspiration pour faire 26 commentaires différents...

louismarslen commented 2 years ago

Je fais un peu de ménage dans les issues -> d'ailleurs go participer à l'édition 5