mirror of
https://github.com/ASDjonok/OOP_IO-2x_2023.git
synced 2026-04-07 15:21:49 +03:00
Compare commits
8 Commits
59f315cacc
...
master_bef
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6fc3cc244e | ||
|
|
3bbb7c8e54 | ||
|
|
1af299f8f7 | ||
|
|
4924bbe62f | ||
|
|
29e63163a4 | ||
|
|
399941844f | ||
|
|
f385bff73d | ||
|
|
6d8171ad4c |
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
# Project exclude paths
|
||||
/out/
|
||||
@@ -1,33 +0,0 @@
|
||||
package OOP.Java.lab_1;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class lab_1 {
|
||||
|
||||
public static int protectedInput(String variableToRead, Scanner input) {
|
||||
do {
|
||||
try {
|
||||
System.out.printf("Enter %s: ", variableToRead);
|
||||
return input.nextInt();
|
||||
} catch (Exception e) {
|
||||
System.out.printf("%s must be an integer.\n", variableToRead.toUpperCase());
|
||||
input.nextLine();
|
||||
}
|
||||
} while (true);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
|
||||
final int n = protectedInput("n", input);
|
||||
final int m = protectedInput("m", input);
|
||||
final int a = protectedInput("a", input);
|
||||
final int b = protectedInput("b", input);
|
||||
|
||||
input.close();
|
||||
|
||||
final float s = ((float) (b + m) / 2) * (m - b + 1) * (n - a + 1);
|
||||
|
||||
System.out.println("S = " + s);
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package OOP.Java.lab_1
|
||||
|
||||
fun protectedInput(variableName: String): Int {
|
||||
do {
|
||||
try {
|
||||
print("Enter $variableName: ")
|
||||
return readln().toInt()
|
||||
} catch (e: Exception) {
|
||||
println("${variableName.uppercase()} must be an integer!")
|
||||
}
|
||||
} while (true)
|
||||
}
|
||||
|
||||
fun main() {
|
||||
val n: Int = protectedInput("n")
|
||||
val m: Int = protectedInput("m")
|
||||
val a: Int = protectedInput("a")
|
||||
val b: Int = protectedInput("b")
|
||||
|
||||
val s: Float = (b + m).toFloat() / 2 * (m - b + 1) * (n - a + 1)
|
||||
|
||||
println("S = $s")
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
package OOP.Java.lab_2;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class lab_2 {
|
||||
|
||||
public static short protectedInput(String inputPrompt, String errorMessage, Scanner input) {
|
||||
short read_variable;
|
||||
|
||||
do {
|
||||
try {
|
||||
System.out.println();
|
||||
System.out.print(inputPrompt);
|
||||
|
||||
read_variable = input.nextShort();
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
System.out.println(errorMessage);
|
||||
input.nextLine();
|
||||
}
|
||||
} while (true);
|
||||
|
||||
return read_variable;
|
||||
}
|
||||
|
||||
public static String format(int number) {
|
||||
int width = String.valueOf(number).length() + 1;
|
||||
|
||||
return "|%" + width + "d ";
|
||||
}
|
||||
|
||||
public static double average(short[] row) {
|
||||
short sum = 0;
|
||||
|
||||
for (short element : row) {
|
||||
sum += element;
|
||||
}
|
||||
|
||||
return (double) sum / row.length;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
short rows, columns;
|
||||
|
||||
Scanner input = new Scanner(System.in);
|
||||
|
||||
final short a = protectedInput("Input a constant to multiply a matrix by: ",
|
||||
"A constant must be a short-data type integer, try again.", input);
|
||||
|
||||
System.out.println();
|
||||
System.out.println("Input size of the matrix.");
|
||||
|
||||
do {
|
||||
rows = protectedInput("Rows: ",
|
||||
"A number of rows must be a short-data type integer, try again.", input);
|
||||
} while (rows <= 0);
|
||||
|
||||
do {
|
||||
columns = protectedInput("Columns: ",
|
||||
"A number of columns must be a short-data type integer, try again.", input);
|
||||
} while (columns <= 0);
|
||||
|
||||
input.close();
|
||||
|
||||
short[][] matrix_B = new short[rows][columns];
|
||||
|
||||
System.out.println();
|
||||
System.out.println("Matrix B:");
|
||||
|
||||
String format = format(rows * columns);
|
||||
|
||||
for (short i = 0; i < rows; i++) {
|
||||
for (short j = 0; j < columns; j++) {
|
||||
matrix_B[i][j] = (short) ((i + 1) * (j + 1));
|
||||
|
||||
System.out.printf(format, matrix_B[i][j]);
|
||||
}
|
||||
|
||||
System.out.println("|");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("Matrix a×B:");
|
||||
|
||||
format = format(rows * columns * a);
|
||||
|
||||
for (short i = 0; i < matrix_B.length; i++) {
|
||||
for (short j = 0; j < matrix_B[i].length; j++) {
|
||||
matrix_B[i][j] *= (a);
|
||||
|
||||
System.out.printf(format, matrix_B[i][j]);
|
||||
}
|
||||
|
||||
System.out.println("|");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
System.out.println("Averages of each row:");
|
||||
|
||||
for (short[] row : matrix_B) {
|
||||
System.out.println(average(row));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
package OOP.Java.lab_3;
|
||||
|
||||
public class lab_3 {
|
||||
static int maxStrLength;
|
||||
static String result;
|
||||
|
||||
public static void cSubUtil(StringBuilder stringbuilder, int leftBoundary, int rightBoundary) {
|
||||
String string = stringbuilder.toString().toLowerCase().replaceAll("[^a-z]","");
|
||||
// check if the indices lie in the range of string
|
||||
// and also if it is palindrome
|
||||
while (leftBoundary >= 0 && rightBoundary < string.length() && string.toLowerCase().charAt(leftBoundary) == string.toLowerCase().charAt(rightBoundary)) {
|
||||
// expand the boundary
|
||||
leftBoundary--;
|
||||
rightBoundary++;
|
||||
}
|
||||
// if it's length is greater than maxStrLength update
|
||||
// maxLength and result
|
||||
if (rightBoundary - leftBoundary - 1 >= maxStrLength) {
|
||||
result = string.substring(leftBoundary + 1, rightBoundary);
|
||||
maxStrLength = rightBoundary - leftBoundary - 1;
|
||||
}
|
||||
}
|
||||
|
||||
public static int longestPalSubstr(StringBuilder string)
|
||||
{
|
||||
result = "";
|
||||
maxStrLength = 1;
|
||||
// for every index in the string check palindromes
|
||||
// starting from that index
|
||||
for (int i = 0; i < string.length(); i++) {
|
||||
// check for odd length palindromes
|
||||
cSubUtil(string, i, i);
|
||||
// check for even length palindromes
|
||||
cSubUtil(string, i, i + 1);
|
||||
}
|
||||
System.out.println("Longest palindrome substring is: " + compareStrings(string));
|
||||
return compareStrings(string).length();
|
||||
}
|
||||
|
||||
public static String compareStrings(StringBuilder builder) {
|
||||
for (int leftBoundary = 0; leftBoundary <= builder.length(); leftBoundary++) {
|
||||
for (int rightBoundary = builder.length(); rightBoundary >= leftBoundary; rightBoundary--) {
|
||||
if (result.equals(builder.substring(leftBoundary, rightBoundary).toLowerCase().replaceAll("[^a-z]",""))) {
|
||||
return builder.substring(leftBoundary, rightBoundary);
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
StringBuilder stringToDetect = new StringBuilder("Eva, can I see bees in a cave?");
|
||||
|
||||
System.out.println("Initial string: " + stringToDetect);
|
||||
System.out.println("Length is: " + longestPalSubstr(stringToDetect));
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package OOP.Java.lab_4
|
||||
|
||||
class Furniture(val name: String, val material: String, val price: Int, val length: Int, val width: Int, val height: Int) {
|
||||
fun print(nameWidth: Int, materialWidth: Int, priceWidth: Int){
|
||||
print("Furniture stats: {")
|
||||
print("Name: ${this.name.padEnd(nameWidth)} ")
|
||||
print("Material: ${this.material.padEnd(materialWidth)} ")
|
||||
print("Price: ${(this.price.toString() + " cu").padEnd(priceWidth + 3)} ")
|
||||
print("Size: ${this.length}×${this.width}×${this.height}};\n")
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package OOP.Java.lab_4
|
||||
|
||||
fun main() {
|
||||
val furnitureArray = arrayOf(
|
||||
Furniture("Chair", "Wood", 10, 5, 5, 10),
|
||||
Furniture("Counter-top", "Marble", 1_000, 10, 5, 1),
|
||||
Furniture("Dinner table", "Glass", 500, 15, 10, 1),
|
||||
Furniture("Office table", "Wood", 200, 10, 7, 1),
|
||||
Furniture("Refrigerator", "Stainless steel", 20_000, 8, 4, 10)
|
||||
)
|
||||
|
||||
val maxNameWidth = furnitureArray.maxWith(Comparator.comparingInt { it.name.length }).name.length
|
||||
val maxMaterialWidth = furnitureArray.maxWith(Comparator.comparingInt { it.material.length }).material.length
|
||||
val maxPriceWidth = furnitureArray.maxWith(Comparator.comparingInt { it.price }).price.toString().length
|
||||
|
||||
println("\nUnsorted array:")
|
||||
for (item in furnitureArray) item.print(maxNameWidth, maxMaterialWidth, maxPriceWidth)
|
||||
|
||||
println("\nSorted alphabetically by name:")
|
||||
furnitureArray.sortBy { it.name }
|
||||
for (item in furnitureArray) item.print(maxNameWidth, maxMaterialWidth, maxPriceWidth)
|
||||
|
||||
println("\nSorted alphabetically by material:")
|
||||
furnitureArray.sortBy { it.material }
|
||||
for (item in furnitureArray) item.print(maxNameWidth, maxMaterialWidth, maxPriceWidth)
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package OOP.Java.lab_5
|
||||
|
||||
class Letter(val character: Char) {
|
||||
override fun toString(): String {
|
||||
return this.character.toString()
|
||||
}
|
||||
|
||||
fun equals(letter: Letter, ignoreCase: Boolean): Boolean {
|
||||
return this.character.toString().equals(letter.toString(), ignoreCase)
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package OOP.Java.lab_5
|
||||
|
||||
class Punctuation(var punctuationMark: String) {
|
||||
private val punctuationMarkOptions: Regex = Regex("\\p{Punct}")
|
||||
|
||||
init {
|
||||
if (!punctuationMarkOptions.containsMatchIn(punctuationMark)) {
|
||||
this.punctuationMark = ""
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return punctuationMark
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package OOP.Java.lab_5
|
||||
|
||||
class Sentence(sentenceString: String) {
|
||||
var sentenceArray = Pair<Array<Word>, Array<Punctuation>>(splitWords(sentenceString), splitPunctuation(sentenceString))
|
||||
|
||||
init {
|
||||
// val arrayOfWords: Array<Word> = splitWords(sentenceString)
|
||||
// val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(sentenceString)
|
||||
|
||||
}
|
||||
|
||||
private fun splitWords(sentenceString: String): Array<Word> {
|
||||
return sentenceString.split("[\\p{Punct}\\s]+".toRegex()).filter { it.isNotEmpty() }.map { word -> Word(word) }.toTypedArray()
|
||||
}
|
||||
|
||||
private fun splitPunctuation(sentenceString: String): Array<Punctuation> {
|
||||
return sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray()
|
||||
}
|
||||
|
||||
fun getAllLetters(): Array<Letter> {
|
||||
var allLetters = arrayOf<Letter>()
|
||||
this.sentenceArray.first.forEach { allLetters += it.letters }
|
||||
/*for (i: Word in this.sentenceArray.first) {
|
||||
println("${i} ${i::class.simpleName} ${i.letters}")
|
||||
}*/
|
||||
return allLetters
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
var sentence = arrayOf<String>()
|
||||
for (i in this.sentenceArray.first.indices) {
|
||||
sentence += this.sentenceArray.first[i].toString() + this.sentenceArray.second[i].toString()
|
||||
}
|
||||
return sentence.joinToString(" ")
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
package OOP.Java.lab_5
|
||||
|
||||
class Text(textString: String) {
|
||||
var textArray = Pair(splitSentences(textString), splitPunctuation(textString))
|
||||
|
||||
private fun splitSentences(textString: String): Array<Sentence> {
|
||||
return textString.split("[.!?] ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Sentence(sentence) }.toTypedArray()
|
||||
}
|
||||
|
||||
private fun splitPunctuation(textString: String): Array<Punctuation> {
|
||||
return textString.split("(?<=[.!?]) ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Punctuation(sentence.last().toString()) }.toTypedArray()
|
||||
}
|
||||
|
||||
fun getAllLetters(): Array<Letter> {
|
||||
var allLetters = arrayOf<Letter>()
|
||||
this.textArray.first.forEach { allLetters += it.getAllLetters() }
|
||||
return allLetters
|
||||
}
|
||||
|
||||
fun palindromeSearch(): String {
|
||||
var results = arrayOf<String>()
|
||||
val letters = this.getAllLetters()
|
||||
for (leftBoundary in letters.indices) {
|
||||
for (rightBoundary in letters.lastIndex downTo leftBoundary + 1) {
|
||||
val subToC = letters.sliceArray(leftBoundary..rightBoundary)
|
||||
// println(subToC.joinToString(""))
|
||||
|
||||
if (subToC.first().equals(subToC.last(), true) && subToC.size >= 3 && this.checkReverse(subToC)) {
|
||||
// println(subToC.joinToString(""))
|
||||
results += subToC.joinToString("")
|
||||
}
|
||||
}
|
||||
}
|
||||
return results.maxBy { it.length }
|
||||
}
|
||||
|
||||
private fun checkReverse(substring: Array<Letter>): Boolean {
|
||||
var leftBoundary = 0
|
||||
var rightBoundary = substring.lastIndex
|
||||
var result = false
|
||||
var correction = 0
|
||||
|
||||
if (substring.size % 2 != 0) {
|
||||
correction = 1
|
||||
}
|
||||
|
||||
while (leftBoundary < substring.size / 2 && rightBoundary >= substring.size / 2 + correction) {
|
||||
leftBoundary++
|
||||
rightBoundary--
|
||||
result = substring[leftBoundary].equals(substring[rightBoundary], true)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
override fun toString(): String {
|
||||
var text = arrayOf<String>()
|
||||
for (i in this.textArray.first.indices) {
|
||||
text += this.textArray.first[i].toString() + this.textArray.second[i].toString()
|
||||
}
|
||||
return text.joinToString(" ")
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package OOP.Java.lab_5
|
||||
|
||||
class Word(var letters: Array<Letter>) {
|
||||
override fun toString(): String {
|
||||
val wordString = StringBuilder()
|
||||
this.letters.forEach { wordString.append(it) }
|
||||
|
||||
return wordString.toString()
|
||||
}
|
||||
constructor(
|
||||
word: String
|
||||
) : this((word.toCharArray().map { letter -> Letter(letter) }).toTypedArray())
|
||||
}
|
||||
16
Rust/lab_1/Cargo.lock
generated
16
Rust/lab_1/Cargo.lock
generated
@@ -1,16 +0,0 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "lab_1"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"text_io",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "text_io"
|
||||
version = "0.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d5f0c8eb2ad70c12a6a69508f499b3051c924f4b1cfeae85bfad96e6bc5bba46"
|
||||
@@ -1,9 +0,0 @@
|
||||
[package]
|
||||
name = "lab_1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
text_io = "0.1.12"
|
||||
@@ -1,22 +0,0 @@
|
||||
fn protected_read(variable_name: &str) -> i32 {
|
||||
use text_io::try_read;
|
||||
loop {
|
||||
print!("Enter {}: ", variable_name);
|
||||
let read_result: Result<i32, _> = try_read!();
|
||||
match read_result {
|
||||
Ok(read_integer) => return read_integer,
|
||||
Err(_e) => println!("{} must be an integer!", variable_name.to_uppercase()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let n: i32 = protected_read("n");
|
||||
let m: i32 = protected_read("m");
|
||||
let a: i32 = protected_read("a");
|
||||
let b: i32 = protected_read("b");
|
||||
|
||||
let s: f32 = ((b + m) as f32 / 2f32) * ((m - b + 1) * (n - a + 1)) as f32;
|
||||
|
||||
println!("S = {}", s);
|
||||
}
|
||||
7
Rust/lab_2/Cargo.lock
generated
7
Rust/lab_2/Cargo.lock
generated
@@ -1,7 +0,0 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "lab_2"
|
||||
version = "0.1.0"
|
||||
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "lab_2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
@@ -1,3 +0,0 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
Reference in New Issue
Block a user