-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implementation class name: CalculateAverage_ashikur2146 Execution tim…
…e: 3 mins 32 seconds Device Specification: CPU: 11th Gen Intel(R) Core(TM) i5-1135G7 @2.40GHz Core: 8 RAM: 16 GB
- Loading branch information
mohammad.ashikur
committed
Jan 31, 2024
1 parent
bee7197
commit 1e6fb94
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/sh | ||
# | ||
# Copyright 2023 The original authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
JAVA_OPTS="" | ||
java $JAVA_OPTS --class-path target/average-1.0.0-SNAPSHOT.jar dev.morling.onebrc.CalculateAverage_ashikur2146 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/bin/bash | ||
# | ||
# Copyright 2023 The original authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
# Uncomment below to use sdk | ||
# source "$HOME/.sdkman/bin/sdkman-init.sh" | ||
# sdk use java 21.0.1-graal 1>&2 |
77 changes: 77 additions & 0 deletions
77
src/main/java/dev/morling/onebrc/CalculateAverage_ashikur2146.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2023 The original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.morling.onebrc; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.stream.Collectors; | ||
|
||
public class CalculateAverage_ashikur2146 { | ||
private static final String FILE_PATH = "./measurements.txt"; | ||
|
||
public static void main(String[] args) { | ||
try { | ||
Map<String, TemperatureStats> stationStats = processFile(FILE_PATH); | ||
System.out.println(stationStats); | ||
|
||
} | ||
catch (IOException e) { | ||
System.err.println("Error processing file: " + e.getMessage()); | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private static Map<String, TemperatureStats> processFile(String filePath) throws IOException { | ||
try { | ||
Path path = Paths.get(filePath); | ||
return Files.lines(path).map(line -> line.split(";")).parallel().filter(parts -> parts.length == 2) | ||
.collect(Collectors.groupingByConcurrent(parts -> parts[0], | ||
Collectors.mapping(parts -> new TemperatureStats(Double.parseDouble(parts[1])), | ||
Collectors.reducing(TemperatureStats::merge)))) | ||
.entrySet().stream() | ||
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().orElseThrow(), | ||
(e1, e2) -> e1, | ||
TreeMap::new)); | ||
} | ||
catch (IOException e) { | ||
throw new IOException("Error reading file: " + e.getMessage(), e); | ||
} | ||
} | ||
|
||
} | ||
|
||
record TemperatureStats(double min, double mean, double max, int count) { | ||
|
||
public TemperatureStats(double temperature) { | ||
this(temperature, temperature, temperature, 1); | ||
} | ||
|
||
public TemperatureStats merge(TemperatureStats other) { | ||
double newMin = Math.min(min, other.min); | ||
double newMax = Math.max(max, other.max); | ||
double newMean = ((mean * count) + (other.mean * other.count)) / (count + other.count); | ||
return new TemperatureStats(newMin, newMean, newMax, count + other.count); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("%.1f/%.1f/%.1f", min, mean, max); | ||
} | ||
} |