mirror of
https://github.com/ASDjonok/OOP_IO-2x_2023.git
synced 2026-04-07 23:21:51 +03:00
Compare commits
6 Commits
b00e090791
...
ІО-23/30-Ш
| Author | SHA1 | Date | |
|---|---|---|---|
| d43c1fedf4 | |||
| 698bfe413e | |||
| 59fc1ad596 | |||
| 342b1d50b9 | |||
| aadc3f5d05 | |||
| d5cd53f954 |
@@ -1,5 +1,8 @@
|
|||||||
package OOP.Java.lab_2;
|
package OOP.Java.lab_2;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Contract;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class lab_2 {
|
public class lab_2 {
|
||||||
@@ -23,13 +26,15 @@ public class lab_2 {
|
|||||||
return read_variable;
|
return read_variable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static String format(int number) {
|
public static String format(int number) {
|
||||||
int width = String.valueOf(number).length() + 1;
|
int width = String.valueOf(number).length() + 1;
|
||||||
|
|
||||||
return "|%" + width + "d ";
|
return "|%" + width + "d ";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double average(short[] row) {
|
@Contract(pure = true)
|
||||||
|
public static double average(@NotNull short[] row) {
|
||||||
short sum = 0;
|
short sum = 0;
|
||||||
|
|
||||||
for (short element : row) {
|
for (short element : row) {
|
||||||
|
|||||||
@@ -2,17 +2,31 @@ package OOP.Java.lab_6
|
|||||||
|
|
||||||
import kotlin.time.Duration
|
import kotlin.time.Duration
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an album by a specific artist, containing multiple tracks.
|
||||||
|
*
|
||||||
|
* @property albumName The name of the album.
|
||||||
|
* @property artist The artist or band associated with the album.
|
||||||
|
* @property tracks An [Array] of tracks included on the album.
|
||||||
|
*/
|
||||||
class Album(val albumName: String, val artist: String, val tracks: Array<Track>) {
|
class Album(val albumName: String, val artist: String, val tracks: Array<Track>) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array of tracks within the specified duration range.
|
||||||
|
*/
|
||||||
fun tracksInDurationRange(durationRange: ClosedRange<Duration>): Array<Track> {
|
fun tracksInDurationRange(durationRange: ClosedRange<Duration>): Array<Track> {
|
||||||
return this.tracks.filter { it.duration in durationRange }.toTypedArray()
|
return this.tracks.filter { it.duration in durationRange }.toTypedArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the total duration of all tracks on the album.
|
||||||
|
*/
|
||||||
fun totalDuration(): Duration {
|
fun totalDuration(): Duration {
|
||||||
var totalDuration: Duration = Duration.ZERO
|
var totalDuration: Duration = Duration.ZERO
|
||||||
this.tracks.forEach { totalDuration += it.duration }
|
this.tracks.forEach { totalDuration += it.duration }
|
||||||
return totalDuration
|
return totalDuration
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "${this.albumName} by ${this.artist}\nTotal duration: ${this.totalDuration()}\nTracks:\n${this.tracks.joinToString("\n")}"
|
return "${this.albumName} by ${this.artist}\nTotal duration: ${this.totalDuration()}\nTracks:\n${this.tracks.joinToString("\n")}"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
package OOP.Java.lab_6
|
|
||||||
|
|
||||||
import kotlin.time.Duration
|
|
||||||
|
|
||||||
class DancePop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
|
|
||||||
override val style = "Dance-pop"
|
|
||||||
}
|
|
||||||
15
Java/lab_6/Dancepop.kt
Normal file
15
Java/lab_6/Dancepop.kt
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package OOP.Java.lab_6
|
||||||
|
|
||||||
|
import kotlin.time.Duration
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a track in the Dance-pop style.
|
||||||
|
*
|
||||||
|
* @param numberInAlbum The position of the track on the album.
|
||||||
|
* @param trackName The name of the track.
|
||||||
|
* @param feature The featured artist(s) in the track (nullable if no featured artist).
|
||||||
|
* @param duration The [Duration] of the track.
|
||||||
|
*/
|
||||||
|
class Dancepop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
|
||||||
|
override val style = "Dance-pop"
|
||||||
|
}
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package OOP.Java.lab_6
|
|
||||||
|
|
||||||
import kotlin.time.Duration
|
|
||||||
|
|
||||||
class ElectroPop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
|
|
||||||
override val style = "Electropop"
|
|
||||||
}
|
|
||||||
15
Java/lab_6/Electropop.kt
Normal file
15
Java/lab_6/Electropop.kt
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package OOP.Java.lab_6
|
||||||
|
|
||||||
|
import kotlin.time.Duration
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a track in the Electropop style.
|
||||||
|
*
|
||||||
|
* @param numberInAlbum The position of the track on the album.
|
||||||
|
* @param trackName The name of the track.
|
||||||
|
* @param feature The featured artist(s) in the track (nullable if no featured artist).
|
||||||
|
* @param duration The [Duration] of the track.
|
||||||
|
*/
|
||||||
|
class Electropop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
|
||||||
|
override val style = "Electropop"
|
||||||
|
}
|
||||||
@@ -2,6 +2,14 @@ package OOP.Java.lab_6
|
|||||||
|
|
||||||
import kotlin.time.Duration
|
import kotlin.time.Duration
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an interlude track.
|
||||||
|
*
|
||||||
|
* @param numberInAlbum The position of the track on the album.
|
||||||
|
* @param trackName The name of the track.
|
||||||
|
* @param feature The featured artist(s) in the track (nullable if no featured artist).
|
||||||
|
* @param duration The [Duration] of the track.
|
||||||
|
*/
|
||||||
class Interlude(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
|
class Interlude(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
|
||||||
override val style = "Interlude"
|
override val style = "Interlude"
|
||||||
}
|
}
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package OOP.Java.lab_6
|
|
||||||
|
|
||||||
import kotlin.time.Duration
|
|
||||||
|
|
||||||
class SynthPop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
|
|
||||||
override val style = "Synth-pop"
|
|
||||||
}
|
|
||||||
15
Java/lab_6/Synthpop.kt
Normal file
15
Java/lab_6/Synthpop.kt
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package OOP.Java.lab_6
|
||||||
|
|
||||||
|
import kotlin.time.Duration
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a track in the Synth-pop style.
|
||||||
|
*
|
||||||
|
* @param numberInAlbum The position of the track on the album.
|
||||||
|
* @param trackName The name of the track.
|
||||||
|
* @param feature The featured artist(s) in the track (nullable if no featured artist).
|
||||||
|
* @param duration The [Duration] of the track.
|
||||||
|
*/
|
||||||
|
class Synthpop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
|
||||||
|
override val style = "Synth-pop"
|
||||||
|
}
|
||||||
@@ -2,9 +2,21 @@ package OOP.Java.lab_6
|
|||||||
|
|
||||||
import kotlin.time.Duration
|
import kotlin.time.Duration
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a track on an album.
|
||||||
|
*
|
||||||
|
* @param numberInAlbum The position of the track on the album.
|
||||||
|
* @param trackName The name of the track.
|
||||||
|
* @param feature The featured artist(s) in the track (nullable if no featured artist).
|
||||||
|
* @param duration The [Duration] of the track.
|
||||||
|
*/
|
||||||
open class Track(val numberInAlbum: Int, val trackName: String, val feature: String?, val duration: Duration) {
|
open class Track(val numberInAlbum: Int, val trackName: String, val feature: String?, val duration: Duration) {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The style or genre of the track.
|
||||||
|
*/
|
||||||
open val style = ""
|
open val style = ""
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return if (this.feature == null) {
|
return if (this.feature == null) {
|
||||||
"#${this.numberInAlbum}: ${this.trackName} (${this.duration})"
|
"#${this.numberInAlbum}: ${this.trackName} (${this.duration})"
|
||||||
|
|||||||
@@ -4,34 +4,33 @@ import kotlin.time.Duration.Companion.minutes
|
|||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
|
|
||||||
val chromatica = Album(
|
val chromatica = Album(
|
||||||
"Chromatica",
|
"Chromatica",
|
||||||
"Lady Gaga",
|
"Lady Gaga",
|
||||||
arrayOf(
|
arrayOf(
|
||||||
Interlude(1, "Chromatica I", null, 1.minutes),
|
Interlude(1, "Chromatica I", null, 1.minutes),
|
||||||
SynthPop(2, "Alice", null, 2.minutes + 57.seconds),
|
Synthpop(2, "Alice", null, 2.minutes + 57.seconds),
|
||||||
DancePop(3, "Stupid Love", null, 3.minutes + 13.seconds),
|
Dancepop(3, "Stupid Love", null, 3.minutes + 13.seconds),
|
||||||
DancePop(4, "Rain On Me", "Ariana Grande", 3.minutes + 2.seconds),
|
Dancepop(4, "Rain On Me", "Ariana Grande", 3.minutes + 2.seconds),
|
||||||
SynthPop(5, "Free Woman", null, 3.minutes + 11.seconds),
|
Synthpop(5, "Free Woman", null, 3.minutes + 11.seconds),
|
||||||
ElectroPop(6, "Fun Tonight", null, 2.minutes + 53.seconds),
|
Electropop(6, "Fun Tonight", null, 2.minutes + 53.seconds),
|
||||||
Interlude(7, "Chromatica II", null, 41.seconds),
|
Interlude(7, "Chromatica II", null, 41.seconds),
|
||||||
SynthPop(8, "911", null, 2.minutes + 52.seconds),
|
Synthpop(8, "911", null, 2.minutes + 52.seconds),
|
||||||
ElectroPop(9, "Plastic Doll", null, 3.minutes + 41.seconds),
|
Electropop(9, "Plastic Doll", null, 3.minutes + 41.seconds),
|
||||||
DancePop(10, "Sour Candy", "Blackpink", 2.minutes + 37.seconds),
|
Dancepop(10, "Sour Candy", "Blackpink", 2.minutes + 37.seconds),
|
||||||
DancePop(11, "Enigma", null, 2.minutes + 59.seconds),
|
Dancepop(11, "Enigma", null, 2.minutes + 59.seconds),
|
||||||
SynthPop(12, "Replay", null, 3.minutes + 6.seconds),
|
Synthpop(12, "Replay", null, 3.minutes + 6.seconds),
|
||||||
Interlude(13, "Chromatica III", null, 27.seconds),
|
Interlude(13, "Chromatica III", null, 27.seconds),
|
||||||
ElectroPop(14, "Sine From Above", "Elton John", 4.minutes + 4.seconds),
|
Electropop(14, "Sine From Above", "Elton John", 4.minutes + 4.seconds),
|
||||||
SynthPop(15, "1000 Doves", null, 3.minutes + 35.seconds),
|
Synthpop(15, "1000 Doves", null, 3.minutes + 35.seconds),
|
||||||
DancePop(16, "Babylon", null, 2.minutes + 41.seconds)
|
Dancepop(16, "Babylon", null, 2.minutes + 41.seconds)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
println("$chromatica\n")
|
println("$chromatica\n")
|
||||||
|
|
||||||
chromatica.tracks.sortBy { it.style }
|
println("${chromatica.albumName} tracks sorted by musical style:\n${chromatica.tracks.sortedBy { it.style }.joinToString("\n")}\n")
|
||||||
|
|
||||||
println("Album sorted by musical style:\n$chromatica\n")
|
|
||||||
|
|
||||||
val durationRange = 1.minutes.. 3.minutes + 30.seconds
|
val durationRange = 1.minutes.. 3.minutes + 30.seconds
|
||||||
|
|
||||||
|
|||||||
9
Rust/lab_2/Cargo.lock
generated
9
Rust/lab_2/Cargo.lock
generated
@@ -5,3 +5,12 @@ version = 3
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "lab_2"
|
name = "lab_2"
|
||||||
version = "0.1.0"
|
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"
|
||||||
|
|||||||
@@ -6,3 +6,4 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
text_io = "0.1.12"
|
||||||
|
|||||||
@@ -1,3 +1,140 @@
|
|||||||
fn main() {
|
// #[allow(dead_code)]
|
||||||
println!("Hello, world!");
|
fn capitalise(s: &str) -> String {
|
||||||
|
let mut c = s.chars();
|
||||||
|
match c.next() {
|
||||||
|
None => String::new(),
|
||||||
|
Some(f) => f.to_uppercase().collect::<String>() + c.as_str(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_row() -> Vec<i16> {
|
||||||
|
loop {
|
||||||
|
print!("Row (separated by spaces): ");
|
||||||
|
let raw_row: String = text_io::read!("{}\n");
|
||||||
|
let trimmed_row: &str = raw_row.trim();
|
||||||
|
match trimmed_row.split(" ").all(|value| {
|
||||||
|
value.chars().all(|char| char.is_numeric() || char == '-')
|
||||||
|
&& value.parse::<i32>().unwrap() <= i16::MAX as i32
|
||||||
|
&& value.parse::<i32>().unwrap() >= i16::MIN as i32
|
||||||
|
}) {
|
||||||
|
true => {
|
||||||
|
return trimmed_row
|
||||||
|
.split(" ")
|
||||||
|
.map(|value| value.parse::<i16>().unwrap())
|
||||||
|
.collect::<Vec<i16>>()
|
||||||
|
}
|
||||||
|
false => println!("[Error] The row must consist of short integers, try again."),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_row(row_index: u16, length: u16) -> Vec<i16> {
|
||||||
|
return (0..length)
|
||||||
|
.map(|element_index| (element_index as i16 + 1) * (row_index as i16 + 1))
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_matrix() -> Vec<Vec<i16>> {
|
||||||
|
let rows: u16 = protected_u16_read("the number of rows");
|
||||||
|
loop {
|
||||||
|
print!("Generate [s]quare or [r]ectangle matrix or [i]nput manually [s/r/i]: ");
|
||||||
|
let option: String = text_io::read!();
|
||||||
|
match option.to_lowercase().as_str() {
|
||||||
|
"s" => return (0..rows).map(|i| generate_row(i, rows)).collect(),
|
||||||
|
"r" => {
|
||||||
|
let columns: u16 = protected_u16_read("the number of columns");
|
||||||
|
return (0..rows).map(|i| generate_row(i, columns)).collect();
|
||||||
|
}
|
||||||
|
"i" => return (0..rows).map(|_| read_row()).collect(),
|
||||||
|
_ => println!("[Error] Not an option, try again."),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn protected_u16_read(variable_name: &str) -> u16 {
|
||||||
|
use text_io::try_read;
|
||||||
|
loop {
|
||||||
|
print!("Enter {}: ", variable_name);
|
||||||
|
let read_result: Result<u16, _> = try_read!();
|
||||||
|
match read_result {
|
||||||
|
Ok(read_integer) => return read_integer,
|
||||||
|
Err(_e) => println!(
|
||||||
|
"{} must be a short integer, try again.",
|
||||||
|
capitalise(variable_name)
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn multiply_matrix_by_number(matrix: &Vec<Vec<i16>>, multiplier: u16) -> Vec<Vec<i16>> {
|
||||||
|
return matrix
|
||||||
|
.iter()
|
||||||
|
.map(|row| {
|
||||||
|
row.iter()
|
||||||
|
.map(|element| element * multiplier as i16)
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn average_of_row(row: &Vec<i16>) -> f32 {
|
||||||
|
return row.iter().sum::<i16>() as f32 / row.len() as f32;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn format_matrix(matrix: &Vec<Vec<i16>>) -> String {
|
||||||
|
let max_width = matrix
|
||||||
|
.iter()
|
||||||
|
.map(|row| {
|
||||||
|
row.iter()
|
||||||
|
.map(|element| element.to_string().len())
|
||||||
|
.max()
|
||||||
|
.unwrap()
|
||||||
|
})
|
||||||
|
.max()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let mut formatted_matrix: String = matrix
|
||||||
|
.iter()
|
||||||
|
.map(|row| {
|
||||||
|
row.iter()
|
||||||
|
.map(|element| format!("| {:max_width$} ", element))
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join("")
|
||||||
|
})
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join("|\n");
|
||||||
|
formatted_matrix.push('|');
|
||||||
|
return formatted_matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn format_column(column: &Vec<f32>) -> String {
|
||||||
|
let max_width = column
|
||||||
|
.iter()
|
||||||
|
.map(|element| element.to_string().len())
|
||||||
|
.max()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
return column
|
||||||
|
.iter()
|
||||||
|
.map(|element| format!("| {:max_width$} |", element))
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let full_vec_test: Vec<Vec<i16>> = read_matrix();
|
||||||
|
println!("\nMatrix B:\n{}\n", format_matrix(&full_vec_test));
|
||||||
|
|
||||||
|
let mult_full_vec_test: Vec<Vec<i16>> =
|
||||||
|
multiply_matrix_by_number(&full_vec_test, protected_u16_read("matrix multiplier"));
|
||||||
|
println!("Matrix a×B:\n{}\n", format_matrix(&mult_full_vec_test));
|
||||||
|
|
||||||
|
let row_averages: Vec<f32> = mult_full_vec_test
|
||||||
|
.iter()
|
||||||
|
.map(|row| average_of_row(row))
|
||||||
|
.collect();
|
||||||
|
println!(
|
||||||
|
"Averages for each row in a×B:\n{}",
|
||||||
|
format_column(&row_averages)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user