-
Notifications
You must be signed in to change notification settings - Fork 0
/
P42CodedTriangleNumbers.java
62 lines (55 loc) · 1.41 KB
/
P42CodedTriangleNumbers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class P42CodedTriangleNumbers {
public ArrayList<Integer> triangleList = new ArrayList<Integer>();
public void Init()
{
for (int n=1; n<= 100; n++)
{
int t = n*(n+1)/2;
triangleList.add(t);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
P42CodedTriangleNumbers rv = new P42CodedTriangleNumbers();
rv.Init();
String file = "p042_words.txt";
BufferedReader br;
try {
br = new BufferedReader(new FileReader(file));
String line;
int count = 0;
int totalWord = 0;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
// process the line.
sb.append(line+",");
}
br.close();
String[] arr = sb.toString().split(",");
for (int i=0;i< arr.length;i++)
{
if (rv.IsTriangleWord(arr[i].replace('"',' ').trim())) count++;
}
totalWord = arr.length;
System.out.println(totalWord + " " + count);
System.out.println(rv.IsTriangleWord("SKY").toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Boolean IsTriangleWord(String str)
{
int sum = 0;
for (int i=0; i< str.length();i++)
{
sum = sum + str.charAt(i) - 'A' + 1;
}
if (triangleList.contains(sum)) return true;
else return false;
}
}