Skip to content

Instantly share code, notes, and snippets.

@gceylan
Created July 17, 2012 07:51
Show Gist options
  • Save gceylan/3127863 to your computer and use it in GitHub Desktop.
Save gceylan/3127863 to your computer and use it in GitHub Desktop.
ahmet salih' e sevgilerle...
package com.ykb.eulerDüzeltme;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
public class Duzeltme3 {
public void writeFile(String filePath, ArrayList<Object> myArray) {
PrintWriter pw = null;
try {
pw = new PrintWriter(new File(filePath));
pw.println(new Date());
for (Object s : myArray) {
pw.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
pw.close();
}
}
public ArrayList<String[]> readFile(String filePath) {
String foo = null;
BufferedReader br = null;
ArrayList<String[]> wordsArry = new ArrayList<String[]>();
try {
br = new BufferedReader(new FileReader(filePath));
while ((foo = br.readLine()) != null) {
String[] words = foo.toLowerCase().replaceAll("\"", "").split(",");
wordsArry.add(words);
}
} catch (IOException ioEx) {
ioEx.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return wordsArry;
}
public void control(String control_word, ArrayList<Object> triangle, ArrayList<Object> notTriangle) {
int sum = getSum(control_word);
int triageNumber = getTriageNumber(sum);
if (triageNumber != 0) {
// System.out.println("word: " + foo + ", sum: " + sum + ", T" +
// triageNumber);
triangle.add((control_word.toUpperCase() + " " + "T" + triageNumber));
} else {
notTriangle.add(control_word.toUpperCase());
}
}
public int getSum(String word) {
char[] chars = word.toLowerCase().toCharArray();
int sum = 0;
for (char c : chars) {
sum += c - 96;
}
return sum;
}
public int getTriageNumber(int sum) {
double delta = (1 + (8 * sum));
double kok = (((-1) + (Math.sqrt(delta))) / 2);
return ((int) kok == kok) ? (int) kok : 0;
}
public static void main(String[] args) {
Duzeltme3 o = new Duzeltme3();
// depo dosyaları
String triangleFile = "text/triangle.txt";
String notTriangleFile = "text/not_triangle.txt";
// depo arraylistleri
ArrayList<Object> triangle = new ArrayList<Object>();
ArrayList<Object> notTriangle = new ArrayList<Object>();
ArrayList<String[]> wordsArray = o.readFile("text/words.txt");
for (String[] array : wordsArray) {
for (String word : array) {
o.control(word, triangle, notTriangle);
}
}
o.writeFile(triangleFile, triangle);
o.writeFile(notTriangleFile, notTriangle);
// hatalıysam ara.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment