Skip to main content
added 111 characters in body
Source Link
wumpz
  • 131
  • 1
  • 3

To refine @dpnmn stream answer. One should avoid shared mutability, meaning to change stream external variables within the stream processing. Therefore I propose for streams this solution. First it builds a sorted list from all available names, second it calculates and sums the name scores.

public static void main(String args[]) throws UnsupportedEncodingException, IOException {
    final List<String> sortedNames = Files.lines(Paths.get("target/classes/org/tw/namescan/names.txt").toAbsolutePath())
            .flatMap(line -> Stream.of(line.split(",")))
            .sorted()
            .collect(toList());
    
    int sum = IntStream.range(0, sortedNames.size())
            .map(i -> alphascore(sortedNames.get(i))*i)
            .sum();
    
    System.out.println(sum);
} 

private static int alphascore(String s) {
    return s.chars().map(c -> c - 'A' + 1).sum();
}

To refine @dpnmn stream answer. One should avoid shared mutability, meaning to change stream external variables within the stream processing. Therefore I propose for streams this solution. First it builds a sorted list from all available names, second it calculates and sums the name scores.

public static void main(String args[]) throws UnsupportedEncodingException, IOException {
    final List<String> sortedNames = Files.lines(Paths.get("target/classes/org/tw/namescan/names.txt").toAbsolutePath())
            .flatMap(line -> Stream.of(line.split(",")))
            .sorted()
            .collect(toList());
    
    int sum = IntStream.range(0, sortedNames.size())
            .map(i -> alphascore(sortedNames.get(i))*i)
            .sum();
    
    System.out.println(sum);
}

To refine @dpnmn stream answer. One should avoid shared mutability, meaning to change stream external variables within the stream processing. Therefore I propose for streams this solution. First it builds a sorted list from all available names, second it calculates and sums the name scores.

public static void main(String args[]) throws UnsupportedEncodingException, IOException {
    final List<String> sortedNames = Files.lines(Paths.get("target/classes/org/tw/namescan/names.txt").toAbsolutePath())
            .flatMap(line -> Stream.of(line.split(",")))
            .sorted()
            .collect(toList());
    
    int sum = IntStream.range(0, sortedNames.size())
            .map(i -> alphascore(sortedNames.get(i))*i)
            .sum();
    
    System.out.println(sum);
} 

private static int alphascore(String s) {
    return s.chars().map(c -> c - 'A' + 1).sum();
}
Source Link
wumpz
  • 131
  • 1
  • 3

To refine @dpnmn stream answer. One should avoid shared mutability, meaning to change stream external variables within the stream processing. Therefore I propose for streams this solution. First it builds a sorted list from all available names, second it calculates and sums the name scores.

public static void main(String args[]) throws UnsupportedEncodingException, IOException {
    final List<String> sortedNames = Files.lines(Paths.get("target/classes/org/tw/namescan/names.txt").toAbsolutePath())
            .flatMap(line -> Stream.of(line.split(",")))
            .sorted()
            .collect(toList());
    
    int sum = IntStream.range(0, sortedNames.size())
            .map(i -> alphascore(sortedNames.get(i))*i)
            .sum();
    
    System.out.println(sum);
}