HW3: Fixed heresy

This commit is contained in:
Claudio Maggioni 2019-12-17 09:23:19 +01:00
parent 601abefbcf
commit 2dda0a2bff
2 changed files with 72 additions and 86 deletions

View File

@ -2,8 +2,9 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="cf07aeb8-47a7-429b-9024-1f8bf10e85cc" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/ex3_test.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/../hw2/Ex2/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../hw2/Ex2/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/streams/FileLinesProcessor.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/streams/FileLinesProcessor.java" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
@ -16,14 +17,11 @@
</component>
<component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="true">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/submission.tex">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="367">
<state relative-caret-position="795">
<caret line="53" column="25" selection-start-line="53" selection-start-column="25" selection-end-line="53" selection-end-column="25" />
<folding>
<element signature="e#39#66#2" expanded="true" />
</folding>
</state>
</provider>
</entry>
@ -31,8 +29,8 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/src/fp/IntOperatorsUtil.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="165">
<caret line="12" column="5" selection-start-line="12" selection-start-column="5" selection-end-line="12" selection-end-column="5" />
<state relative-caret-position="285">
<caret line="22" column="48" selection-start-line="22" selection-start-column="48" selection-end-line="22" selection-end-column="48" />
</state>
</provider>
</entry>
@ -50,11 +48,11 @@
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/src/streams/FileLinesProcessor.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="280">
<caret line="99" column="22" selection-start-line="99" selection-start-column="22" selection-end-line="99" selection-end-column="22" />
<state relative-caret-position="333">
<caret line="72" column="71" lean-forward="true" selection-start-line="72" selection-start-column="71" selection-end-line="72" selection-end-column="71" />
<folding>
<element signature="imports" expanded="true" />
</folding>
@ -95,8 +93,8 @@
<option value="$PROJECT_DIR$/src/fp/IntOperatorsUtil.java" />
<option value="$PROJECT_DIR$/src/fp/FunctionsUtil.java" />
<option value="$PROJECT_DIR$/ex3_test.txt" />
<option value="$PROJECT_DIR$/src/streams/FileLinesProcessor.java" />
<option value="$PROJECT_DIR$/submission.tex" />
<option value="$PROJECT_DIR$/src/streams/FileLinesProcessor.java" />
</list>
</option>
</component>
@ -154,6 +152,7 @@
</component>
<component name="PropertiesComponent">
<property name="ASKED_SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
@ -175,7 +174,7 @@
</list>
</option>
</component>
<component name="RunManager" selected="LaTeX.submission">
<component name="RunManager" selected="Application.IntOperatorsUtil">
<configuration name="FileLinesProcessor" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="streams.FileLinesProcessor" />
<module name="hw3" />
@ -222,7 +221,7 @@
<compiler-path />
<compiler-arguments />
<main-file>$PROJECT_DIR$/submission.tex</main-file>
<aux-dir />
<aux-dir>$APPLICATION_HOME_DIR$/bin</aux-dir>
</texify-bibtex>
<method v="2" />
</configuration>
@ -266,9 +265,9 @@
</list>
<recent_temporary>
<list>
<item itemvalue="Application.IntOperatorsUtil" />
<item itemvalue="Application.FileLinesProcessor" />
<item itemvalue="Application.FunctionsUtil" />
<item itemvalue="Application.IntOperatorsUtil" />
<item itemvalue="LaTeX.submission (1)" />
</list>
</recent_temporary>
@ -285,18 +284,22 @@
<updated>1575886303360</updated>
<workItem from="1575886304499" duration="94000" />
<workItem from="1575886414622" duration="3616000" />
<workItem from="1575903832348" duration="10727000" />
<workItem from="1575903832348" duration="11015000" />
<workItem from="1575967348523" duration="1386000" />
<workItem from="1576140010540" duration="28000" />
<workItem from="1576528996442" duration="1593000" />
<workItem from="1576569898500" duration="909000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="14437000" />
<option name="totallyTimeSpent" value="18641000" />
</component>
<component name="ToolWindowManager">
<frame x="0" y="0" width="1015" height="1050" extended-state="0" />
<frame x="0" y="0" width="1920" height="1200" extended-state="0" />
<editor active="true" />
<layout>
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.43062693" />
<window_info active="true" content_ui="combo" id="Project" order="0" visible="true" weight="0.25612354" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info id="Designer" order="2" />
<window_info id="Image Layers" order="3" />
@ -306,7 +309,7 @@
<window_info id="Favorites" order="7" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info anchor="bottom" id="Run" order="2" visible="true" weight="0.32959184" />
<window_info anchor="bottom" id="Run" order="2" weight="0.32959184" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
@ -332,20 +335,6 @@
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="jar:///Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk/Contents/Home/lib/src.zip!/java.base/java/util/stream/Collectors.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="318">
<caret line="317" column="28" selection-start-line="317" selection-start-column="28" selection-end-line="317" selection-end-column="28" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/ex3_test.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="60">
<caret line="4" selection-start-line="4" selection-end-line="4" />
</state>
</provider>
</entry>
<entry file="jar:///Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk/Contents/Home/lib/src.zip!/java.base/java/util/Collection.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="2">
@ -364,18 +353,18 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/Cose.java">
<entry file="file://$PROJECT_DIR$/src/Cose.java" />
<entry file="file://$PROJECT_DIR$/submission.tex">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="315">
<caret line="21" lean-forward="true" selection-start-line="21" selection-end-line="21" />
<folding>
<element signature="e#292#293#0" expanded="true" />
<element signature="e#347#348#0" expanded="true" />
<element signature="e#395#396#0" expanded="true" />
<element signature="e#490#491#0" expanded="true" />
<element signature="e#521#522#0" expanded="true" />
<element signature="e#607#608#0" expanded="true" />
</folding>
<state relative-caret-position="795">
<caret line="53" column="25" selection-start-line="53" selection-start-column="25" selection-end-line="53" selection-end-column="25" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/fp/IntOperatorsUtil.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="22" column="48" selection-start-line="22" selection-start-column="48" selection-end-line="22" selection-end-column="48" />
</state>
</provider>
</entry>
@ -390,33 +379,30 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/ex3_test.txt">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="60">
<caret line="4" selection-start-line="4" selection-end-line="4" />
</state>
</provider>
</entry>
<entry file="jar:///Library/Java/JavaVirtualMachines/openjdk-12.0.1.jdk/Contents/Home/lib/src.zip!/java.base/java/util/stream/Collectors.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="368">
<caret line="1067" column="31" selection-start-line="1067" selection-start-column="31" selection-end-line="1067" selection-end-column="31" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/streams/FileLinesProcessor.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="280">
<caret line="99" column="22" selection-start-line="99" selection-start-column="22" selection-end-line="99" selection-end-column="22" />
<state relative-caret-position="333">
<caret line="72" column="71" lean-forward="true" selection-start-line="72" selection-start-column="71" selection-end-line="72" selection-end-column="71" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/fp/IntOperatorsUtil.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="165">
<caret line="12" column="5" selection-start-line="12" selection-start-column="5" selection-end-line="12" selection-end-column="5" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/submission.tex">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="367">
<caret line="53" column="25" selection-start-line="53" selection-start-column="25" selection-end-line="53" selection-end-column="25" />
<folding>
<element signature="e#39#66#2" expanded="true" />
</folding>
</state>
</provider>
</entry>
</component>
<component name="masterDetails">
<states>
@ -473,7 +459,6 @@
<option name="proportions">
<list>
<option value="0.2" />
<option value="0.6" />
</list>
</option>
</splitter-proportions>

View File

@ -46,40 +46,41 @@ public class FileLinesProcessor {
public Map<Character, Long> countCharsOccurrences() throws IOException {
return getLines()
.map(String::chars)
.reduce(IntStream.of(), IntStream::concat)
.flatMapToInt(String::chars)
.boxed()
.collect(Collectors.groupingBy(i -> (char) (i & 0xFFFF), Collectors.counting()));
.collect(Collectors.groupingBy(i -> (char) i.intValue(), Collectors.counting()));
}
public Map<Character, Long> countCharsOccurrences(int n) throws IOException {
return getLines()
.map(String::chars)
.reduce(IntStream.of(), IntStream::concat)
.boxed()
.collect(Collectors.groupingBy(i -> (char) (i & 0xFFFF), Collectors.counting()))
return countCharsOccurrences()
.entrySet()
.stream()
.filter(e -> e.getValue() > n)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
private static <T> void assertEquals(T o1, T o2) {
if (o1 != o2 && o2 == null || !o2.equals(o1)) {
throw new AssertionError(o1 + " != " + o2);
}
}
public static void main(final String... args) throws IOException {
final FileLinesProcessor processor = new FileLinesProcessor("ex3_test.txt");
assert processor.fileLinesAsBytes()[0][1] == 'b';
assert processor.fileLinesAsBytes()[1][2] == 'z';
assert processor.countByNumberOfSpaces(0) == 2; // (i.e., "abc" and "xyz")
assert processor.countByNumberOfSpaces(2) == 1; // (i.e., "play with streams")
assert "axhp".equals(processor.concatNth(0));
assert "byel".equals(processor.concatNth(1));
assert "ly".equals(processor.concatNth(3));
assertEquals(processor.fileLinesAsBytes()[0][1], (byte) 'b');
assertEquals(processor.fileLinesAsBytes()[1][2], (byte) 'z');
assertEquals(processor.countByNumberOfSpaces(0), 2L); // (i.e., "abc" and "xyz")
assertEquals(processor.countByNumberOfSpaces(2), 1L); // (i.e., "play with streams")
assertEquals(processor.concatNth(0), "axhp");
assertEquals(processor.concatNth(1), "byel");
assertEquals(processor.concatNth(3), "ly");
final Map<Integer, List<String>> expected = new HashMap<>();
expected.put(3, Arrays.asList("abc", "xyz"));
expected.put(11, Collections.singletonList("hello world"));
expected.put(17, Collections.singletonList("play with streams"));
assert expected.equals(processor.groupByLineLength()); // Expected result: {3=[abc, xyz], 11=[hello world],
// 17=[play with streams]}
assertEquals(processor.groupByLineLength(), expected); // Expected result: {3=[abc, xyz], 11=[hello world],
// 17=[play with streams]}
final Map<Character, Long> expected2 = new HashMap<>();
expected2.put(' ', 3L);
@ -101,14 +102,14 @@ public class FileLinesProcessor {
expected2.put('x', 1L);
expected2.put('y', 2L);
expected2.put('z', 1L);
assert expected2.equals(processor.countCharsOccurrences()); // Expected result { =3, a=3, b=1, c=1, d=1, e=2,
// h=2, i=1, l=4, m=1, o=2, p=1, r=2, s=2, t=2, w=2,
// x=1, y=2, z=1}
assertEquals(processor.countCharsOccurrences(), expected2); // Expected result { =3, a=3, b=1, c=1, d=1, e=2,
// h=2, i=1, l=4, m=1, o=2, p=1, r=2, s=2, t=2, w=2,
// x=1, y=2, z=1}
final Map<Character, Long> expected3 = new HashMap<>();
expected3.put(' ', 3L);
expected3.put('a', 3L);
expected3.put('l', 4L);
assert expected3.equals(processor.countCharsOccurrences(2)); // Expected result: { =3, a=3, l=4}
assertEquals(processor.countCharsOccurrences(2), expected3); // Expected result: { =3, a=3, l=4}
System.out.println("OK");
}