Skip to content

Instantly share code, notes, and snippets.

@Banafasto
Created December 28, 2016 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Banafasto/adce9f70d26201d4dc06979c0c7a5c11 to your computer and use it in GitHub Desktop.
Save Banafasto/adce9f70d26201d4dc06979c0c7a5c11 to your computer and use it in GitHub Desktop.
package com.gmail.kudr641;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Lessons7Example1Lavel2 {
public static void main(String[] args) {
int[][] matrix = result(getList());
for (int j = 0; j < matrix.length; j += 1) {
System.out.println(Arrays.toString(matrix[j]));
}
}
static ArrayList<String> getList(){
Scanner scanner = null;
ArrayList<String> matrixList = new ArrayList<String>();
try {
scanner = new Scanner(new File("matrix.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
boolean yes = true;
while (yes) {
String line = scanner.nextLine();
if (line.isEmpty()) {
break;
}
matrixList.add(line);
}
return matrixList;
}
static int[][] result(ArrayList<String> matrixList){
int[][] matrix = new int[matrixList.size()][matrixList.size()];
for (int i = 0; i < matrixList.size(); i += 1) {
int how = 0;
for (int j = 1; j < matrixList.get(i).length(); j += 3) {
how += 1;
}
int[] array = new int[how];
for (int j = 1, k = 0; j < matrixList.get(i).length(); j += 3, k += 1) {
array[k] = Character.getNumericValue(matrixList.get(i).charAt(j));
}
for (int j = 0; j < matrix[i].length; j += 1) {
matrix[i][j] = array[j];
}
}
return matrix;
}
}
package com.gmail.kudr641;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;
public class Lessons7Example2Lavel2 {
public static void main(String[] args) {
File file = new File("AnglishText.txt");
ArrayList<Symbol> list = new ArrayList<Symbol>();
String text = getText(file);
list = getList(list, getTreeSet(text), text);
sortList(list);
printList(list);
}
static TreeSet<Character> getTreeSet(String text) {
TreeSet<Character> set = new TreeSet<Character>();
for (int i = 0; i < text.length(); i += 1) {
set.add(text.charAt(i));
}
return set;
}
static String getText(File file) {
String text;
Scanner scanner = null;
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
text = scanner.nextLine();
return text;
}
static ArrayList<Symbol> getList(ArrayList<Symbol> list, TreeSet<Character> set, String text) {
for (int i = 0; i < set.size(); i += 1) {
Symbol aas = new Symbol();
list.add(aas);
}
int h = 0;
for (Character c : set) {
int coint = 0;
for (int j = 0; j < text.length(); j += 1) {
if (c.equals(text.charAt(j))) {
coint += 1;
}
}
list.get(h).how = coint;
list.get(h).symbol = c;
h += 1;
}
return list;
}
static void sortList(ArrayList<Symbol> list) {
for (int i = 0; i < list.size(); i += 1) {
for (int j = 0; j < list.size() - 1; j += 1) {
if (list.get(j).how < list.get(j + 1).how) {
Symbol repository = list.get(j);
list.remove(j);
list.add(j + 1, repository);
}
}
}
}
static void printList(ArrayList<Symbol> list) {
for (int i = 0; i < list.size(); i += 1) {
System.out.println(list.get(i).how + " " + list.get(i).symbol);
}
}
}
class Symbol {
int how;
char symbol;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment