Naloga 2 WIP

This commit is contained in:
Gašper Dobrovoljc 2025-03-31 11:59:36 +02:00
parent 469fa70a87
commit ed25569920
No known key found for this signature in database
GPG Key ID: 0E7E037018CFA5A5
12 changed files with 226 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
**/.DS_Store

6
naloga1/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

29
naloga2/.gitignore vendored Normal file
View File

@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
naloga2/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,12 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="tests.smoke.test_absences_sliding.TestAbsencesSliding.*" />
</list>
</option>
</inspection_tool>
</profile>
</component>

6
naloga2/.idea/misc.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="homebrew-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
naloga2/.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/naloga2.iml" filepath="$PROJECT_DIR$/naloga2.iml" />
</modules>
</component>
</project>

6
naloga2/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

11
naloga2/naloga2.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,8 @@
import java.util.Comparator;
class AlphabeticalComparator implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
}

22
naloga2/src/Main.java Normal file
View File

@ -0,0 +1,22 @@
public class Main {
public static void main(String[] args) {
AlphabeticalComparator comparator = new AlphabeticalComparator();
TabelaTabel tabelaTabel = new TabelaTabel(comparator);
tabelaTabel.izpisi();
tabelaTabel.vstavi("1");
tabelaTabel.vstavi("12");
tabelaTabel.vstavi("123");
tabelaTabel.izpisi();
tabelaTabel.vstavi("321");
tabelaTabel.vstavi("6383");
tabelaTabel.vstavi("43");
tabelaTabel.vstavi("023");
tabelaTabel.izpisi();
}
}

View File

@ -0,0 +1,109 @@
import java.util.Comparator;
class Item {
String value;
int count;
Item(String value) {
this.value = value;
this.count = 1;
}
}
public class TabelaTabel {
Comparator<String> comparator;
Item[] array = new Item[32];
int size = 0;
TabelaTabel(Comparator<String> comparator) {
this.comparator = comparator;
}
private boolean isFull() {
return size >= array.length;
}
private void resize() {
Item[] tmp = new Item[array.length * 2];
System.arraycopy(array, 0, tmp, 0, array.length);
array = tmp;
}
private int getSubarrayCount() {
if (size == 0) {
return 0;
}
return (int) (Math.log(size) / Math.log(2)) + 1;
}
private boolean isSubarrayFull(int i) {
return ((size >> i) & 1) != 0;
}
void vstavi(String element) {
Item[] tmp = {new Item(element)};
boolean success = false;
int k = getSubarrayCount();
for (int i = 0; i < k; i++) {
if (isSubarrayFull(i)) {
// Append subarray to tmp
int subArrLen = 1 << i;
Item[] newTmp = new Item[tmp.length + subArrLen];
System.arraycopy(tmp, 0, newTmp, 0, tmp.length);
System.arraycopy(array, subArrLen - 1, newTmp, tmp.length, subArrLen);
tmp = newTmp;
} else {
// Set subarray to tmp
System.arraycopy(tmp, 0, array, i, tmp.length);
success = true;
break;
}
}
if (!success) {
// Crate new subarray with size k and add tmp to it
if (isFull()) {
resize();
}
System.arraycopy(tmp, 0, array, k, tmp.length);
}
size++;
}
void najdi(String element) {
}
void izbrisi(String element) {
}
void izpisi() {
if (size == 0) {
System.out.println("prazen");
return;
}
for (int i = 0; i < getSubarrayCount(); i++) {
if (!isSubarrayFull(i)) {
System.out.println("...");
continue;
}
int startIndex = i == 0 ? 0 : 1 << (i - 1);
int subArrLen = 1 << i;
for (int j = startIndex; j < startIndex + subArrLen; j++) {
if (j > startIndex) {
System.out.print(", ");
}
if (array[j] == null) {
System.out.print("x");
} else {
System.out.printf("%s/%d", array[j].value, array[j].count);
}
}
System.out.println();
}
}
}