Compare commits
39 Commits
@ -1,16 +0,0 @@
|
||||
require_relative '../src/02_user_interface.rb'
|
||||
|
||||
RSpec.describe "Main" do
|
||||
before do
|
||||
allow(STDIN).to receive(:gets).and_return("ruby\n")
|
||||
end
|
||||
|
||||
it "greets the user and checks Ruby as language" do
|
||||
expect { main() }.to output("Hello my catgirl test_user! \nWhat is your love language?\nruby\nc++\npy\nПодлиза \n").to_stdout
|
||||
end
|
||||
|
||||
it "handles unknown language input" do
|
||||
allow(STDIN).to receive(:gets).and_return("java\n")
|
||||
expect { main() }.to output(/Неизвестный язык: java/).to_stdout
|
||||
end
|
||||
end
|
@ -1,2 +0,0 @@
|
||||
puts "Hello, world!"
|
||||
|
@ -1,30 +0,0 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
LANGUAGES = [
|
||||
"ruby",
|
||||
"c++",
|
||||
"py"
|
||||
]
|
||||
|
||||
def main()
|
||||
user_name = ARGV[0]
|
||||
puts "Hello my catgirl #{user_name}! \nWhat is your love language?"
|
||||
LANGUAGES.each { |language| puts "#{language}", " " }
|
||||
|
||||
user_lang = STDIN.gets.chomp
|
||||
|
||||
case user_lang
|
||||
when "ruby"
|
||||
puts "Подлиза \n"
|
||||
when "c++"
|
||||
puts "MATLAB скушал? \n"
|
||||
when "TS/JS"
|
||||
puts "Фронтендер, Фууу! \n"
|
||||
when "py"
|
||||
puts "Девопсер, иди ДАГИ писать \n"
|
||||
else
|
||||
puts "Неизвестный язык: #{user_lang}"
|
||||
end
|
||||
end
|
||||
|
||||
main()
|
@ -1,39 +0,0 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
LANGUAGES = [
|
||||
"ruby",
|
||||
"c++",
|
||||
"py"
|
||||
]
|
||||
|
||||
def main()
|
||||
user_name = ARGV[0]
|
||||
puts "Hello my catgirl #{user_name}! \nWhat is your love language?"
|
||||
LANGUAGES.each { |language| puts "#{language}", " " }
|
||||
|
||||
user_lang = STDIN.gets.chomp
|
||||
case user_lang
|
||||
when "ruby"
|
||||
puts "Подлиза \n"
|
||||
when "c++"
|
||||
puts "MATLAB скушал? \n"
|
||||
when "TS/JS"
|
||||
puts "Фронтендер, Фууу! \n"
|
||||
when "py"
|
||||
puts "Девопсер, иди ДАГИ писать \n"
|
||||
end
|
||||
|
||||
puts "Введите команду на языке Ruby для выполнения:"
|
||||
ruby_command = STDIN.gets.chomp
|
||||
begin
|
||||
eval(ruby_command)
|
||||
rescue Exception => e
|
||||
puts "Ошибка выполнения команды Ruby: #{e.message}"
|
||||
end
|
||||
|
||||
puts "Введите команду операционной системы для выполнения:"
|
||||
os_command = STDIN.gets.chomp
|
||||
system(os_command)
|
||||
end
|
||||
|
||||
main()
|
@ -1,68 +0,0 @@
|
||||
|
||||
|
||||
def find_min_element_for(arr)
|
||||
min_element = arr[0]
|
||||
for element in arr
|
||||
min_element = element if element < min_element
|
||||
end
|
||||
min_element
|
||||
end
|
||||
|
||||
|
||||
def find_min_element_while(arr)
|
||||
min_element = arr[0]
|
||||
index = 0
|
||||
while index < arr.size
|
||||
min_element = arr[index] if arr[index] < min_element
|
||||
index += 1
|
||||
end
|
||||
min_element
|
||||
end
|
||||
|
||||
|
||||
def find_first_positive_index_for(arr)
|
||||
for index in 0...arr.size
|
||||
return index if arr[index] > 0
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
|
||||
def find_first_positive_index_while(arr)
|
||||
index = 0
|
||||
while index < arr.size
|
||||
return index if arr[index] > 0
|
||||
index += 1
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
|
||||
def find_first_positive_for(arr)
|
||||
for element in arr
|
||||
return element if element > 0
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
|
||||
def find_first_positive_while(arr)
|
||||
index = 0
|
||||
while index < arr.size
|
||||
return arr[index] if arr[index] > 0
|
||||
index += 1
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
# INPUT
|
||||
array = [-10, -5, 0, 3, 5, -2]
|
||||
|
||||
puts "Минимальный элемент (for): #{find_min_element_for(array)}"
|
||||
puts "Минимальный элемент (while): #{find_min_element_while(array)}"
|
||||
|
||||
puts "Индекс первого положительного элемента (for): #{find_first_positive_index_for(array)}"
|
||||
puts "Индекс первого положительного элемента (while): #{find_first_positive_index_while(array)}"
|
||||
|
||||
puts "Первый положительный элемент (for): #{find_first_positive_for(array)}"
|
||||
puts "Первый положительный элемент (while): #{find_first_positive_while(array)}"
|
@ -1,62 +0,0 @@
|
||||
|
||||
|
||||
def find_min_element(arr)
|
||||
min_element = arr[0]
|
||||
arr.each do |element|
|
||||
min_element = element if element < min_element
|
||||
end
|
||||
min_element
|
||||
end
|
||||
|
||||
|
||||
def find_first_positive_index(arr)
|
||||
arr.each_with_index do |element, index|
|
||||
return index if element > 0
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
|
||||
def find_first_positive(arr)
|
||||
arr.each do |element|
|
||||
return element if element > 0
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
|
||||
def main
|
||||
method_name = ARGV[0]
|
||||
file_path = ARGV[1]
|
||||
|
||||
if method_name.nil? || file_path.nil?
|
||||
puts "Неправльный формат ввода. Использование: ruby program.rb <method_name> <file_path>"
|
||||
puts "method_name: 'min', 'first_positive_index' или 'first_positive'"
|
||||
exit
|
||||
end
|
||||
|
||||
begin
|
||||
array = File.read(file_path).split.map(&:to_i)
|
||||
rescue Errno::ENOENT
|
||||
puts "Файл не найден: #{file_path}"
|
||||
exit
|
||||
end
|
||||
|
||||
case method_name
|
||||
when 'min'
|
||||
result = find_min_element(array)
|
||||
puts "Минимальный элемент: #{result}"
|
||||
when 'first_positive_index'
|
||||
result = find_first_positive_index(array)
|
||||
puts "Индекс первого положительного элемента: #{result.nil? ? 'Нет положительного элемента' : result}"
|
||||
when 'first_positive'
|
||||
result = find_first_positive(array)
|
||||
puts "Первый положительный элемент: #{result.nil? ? 'Нет положительного элемента' : result}"
|
||||
else
|
||||
puts "Неизвестный метод: #{method_name}"
|
||||
puts "Доступные методы: 'min', 'first_positive_index', 'first_positive'"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
main if __FILE__ == $0
|
@ -1 +0,0 @@
|
||||
-10 -5 0 3 5 -2
|
@ -1,4 +1,88 @@
|
||||
# Lab 2
|
||||
|
||||
> "И тут я обнаружил что случайно сделал 3-5 задачи в 1-2"
|
||||
|
||||
## Диаграмма классов:
|
||||
|
||||
```mermaid
|
||||
classDiagram
|
||||
class BinarySearchTree {
|
||||
- root : Node
|
||||
+ add(student : Student) : void
|
||||
+ each(&block) : void
|
||||
- insert(node : Node, student : Student) : Node
|
||||
- in_order_traversal(node : Node, &block) : void
|
||||
}
|
||||
|
||||
class Node {
|
||||
- student : Student
|
||||
- left : Node
|
||||
- right : Node
|
||||
+ Node(student : Student)
|
||||
}
|
||||
|
||||
class Person {
|
||||
- id : String
|
||||
- git : String
|
||||
- phone : String?
|
||||
- telegram : String?
|
||||
- email : String?
|
||||
+ Person(id : String, git : String, phone: String, telegram: String, email: String)
|
||||
+ phone=(String) : Boolean
|
||||
+ telegram=(String) : Boolean
|
||||
+ email=(String) : Boolean
|
||||
+ surname_initials() : NotImplementedError
|
||||
+ valid_phone_number() : Boolean <<class>>
|
||||
+ valid_telegram() : Boolean <<class>>
|
||||
+ valid_email() : Boolean <<class>>
|
||||
+ git_present() : Boolean
|
||||
+ contact_present() : Boolean
|
||||
+ get_first_contact() : String
|
||||
+ git=(git : String) : void
|
||||
+ validate_id(id: String) : void <<class>>
|
||||
+ validate_git(git : String) : void <<class>>
|
||||
- valid_git?(git : String) : Boolean
|
||||
- valid_id?(id : String) : Boolean
|
||||
}
|
||||
|
||||
class StudentRepository {
|
||||
+ read_from_txt(file_path : String) : List~Student~ <<class>>
|
||||
+ write_to_txt(file_path : String, students : List~Student~) <<class>>
|
||||
}
|
||||
|
||||
class StudentShort {
|
||||
- surname_initials : String
|
||||
+ get_surname_initials() : String
|
||||
+ StudentShort(id : String, surname_initials : String, phone: String, telegram: String, email: String)
|
||||
+ from_student(student : Student) : StudentShort <<class>>
|
||||
+ from_string(id : String, info_string : String) : StudentShort <<class>>
|
||||
+ to_s() : String
|
||||
- parse_contact_string(contact_string: String) : Array<String> <<class>>
|
||||
}
|
||||
|
||||
class Student {
|
||||
- surname : String
|
||||
- name : String
|
||||
- patronymic : String
|
||||
- birth_date : Date
|
||||
- const NAME_REGEX : String
|
||||
+ Student(id : String, git : String, phone: String, telegram: String, email: String, surname : String, name : String, patronymic : String, birth_date : Date)
|
||||
+ from_string(student_string : String) : Student <<class>>
|
||||
+ surname_initials() : String
|
||||
+ to_s() : String
|
||||
+ get_info() : String
|
||||
+ surname=(surname : String)
|
||||
+ name=(name : String)
|
||||
+ patronymic=(patronymic : String)
|
||||
+ birth_date=(birthdate : String)
|
||||
+ valid_name?(name : String) : Boolean <<class>>
|
||||
- name_initial(name : String) : String
|
||||
- patronymic(patronymic : String) : String
|
||||
}
|
||||
|
||||
BinarySearchTree o-- Node
|
||||
Node o-- Student
|
||||
Person <|-- Student
|
||||
Person <|-- StudentShort
|
||||
Student <.. StudentRepository
|
||||
```
|
||||
|
||||
|
@ -0,0 +1,46 @@
|
||||
class BinarySearchTree
|
||||
include Enumerable
|
||||
|
||||
class Node
|
||||
attr_accessor :student, :left, :right
|
||||
|
||||
def initialize(student)
|
||||
@student = student
|
||||
@left = nil
|
||||
@right = nil
|
||||
end
|
||||
end
|
||||
|
||||
def initialize
|
||||
@root = nil
|
||||
end
|
||||
|
||||
def add(student)
|
||||
@root = insert(@root, student)
|
||||
end
|
||||
|
||||
def each(&block)
|
||||
in_order_traversal(@root, &block)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def insert(node, student)
|
||||
return Node.new(student) if node.nil?
|
||||
|
||||
if student.birth_date < node.student.birth_date
|
||||
node.left = insert(node.left, student)
|
||||
else
|
||||
node.right = insert(node.right, student)
|
||||
end
|
||||
node
|
||||
end
|
||||
|
||||
def in_order_traversal(node, &block)
|
||||
return if node.nil?
|
||||
|
||||
in_order_traversal(node.left, &block)
|
||||
yield node.student
|
||||
in_order_traversal(node.right, &block)
|
||||
end
|
||||
end
|
@ -0,0 +1,39 @@
|
||||
require_relative 'student'
|
||||
|
||||
class StudentRepository
|
||||
def self.read_from_txt(file_path)
|
||||
raise IOError, "File does not exist: #{file_path}" unless File.exist?(file_path)
|
||||
|
||||
students = []
|
||||
|
||||
File.foreach(file_path) do |line|
|
||||
line.strip!
|
||||
next if line.empty?
|
||||
|
||||
begin
|
||||
student = Student.from_string(line)
|
||||
students << student
|
||||
rescue ArgumentError => e
|
||||
puts "Error processing line: '#{line}'. Reason: #{e.message}"
|
||||
end
|
||||
end
|
||||
|
||||
students
|
||||
end
|
||||
|
||||
def self.write_to_txt(file_path, students)
|
||||
unless students.is_a?(Array) && students.all? { |s| s.is_a?(Student) }
|
||||
raise ArgumentError, 'Expected an array of Student objects'
|
||||
end
|
||||
|
||||
File.open(file_path, 'w') do |file|
|
||||
students.each do |student|
|
||||
file.puts student.to_s
|
||||
end
|
||||
end
|
||||
|
||||
puts "Data successfully written to #{file_path}"
|
||||
rescue IOError => e
|
||||
puts "An error occurred while writing to the file: #{e.message}"
|
||||
end
|
||||
end
|
@ -1,31 +1,59 @@
|
||||
require_relative 'person'
|
||||
|
||||
class StudentShort < Person
|
||||
attr_reader :surname_initials, :contact
|
||||
|
||||
def initialize(student)
|
||||
super(id: student.id, git: student.git_info)
|
||||
@surname_initials = student.surname_and_initials
|
||||
@contact = student.contact_info
|
||||
end
|
||||
|
||||
def self.from_string(id, info_string)
|
||||
parts = info_string.split(', ').map(&:strip)
|
||||
surname_initials = parts[0]
|
||||
git = parts[1].split(': ').last
|
||||
contact = parts[2].split(': ').last
|
||||
|
||||
new_instance = allocate
|
||||
new_instance.send(:initialize_from_data, id, surname_initials, git, contact)
|
||||
new_instance
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def initialize_from_data(id, surname_initials, git, contact)
|
||||
@id = id
|
||||
@surname_initials = surname_initials
|
||||
@git = git
|
||||
@contact = contact
|
||||
attr_reader :surname_initials
|
||||
|
||||
def initialize(id:, surname_initials:, phone: nil, telegram: nil, email: nil)
|
||||
super(id: id, phone: phone, telegram: telegram, email: email)
|
||||
@surname_initials = surname_initials
|
||||
freeze
|
||||
end
|
||||
|
||||
def self.from_student(student)
|
||||
new(
|
||||
id: student.id,
|
||||
surname_initials: student.surname_initials,
|
||||
phone: student.phone,
|
||||
telegram: student.telegram,
|
||||
email: student.email
|
||||
)
|
||||
end
|
||||
|
||||
def self.from_string(id, info_string)
|
||||
parts = info_string.split(',').map(&:strip)
|
||||
raise ArgumentError, 'Invalid info string format' if parts.size < 2
|
||||
|
||||
surname_initials = parts[0]
|
||||
contact_string = parts[1].split(': ', 2).last.strip
|
||||
|
||||
phone, telegram, email = parse_contact_string(contact_string)
|
||||
|
||||
new(
|
||||
id: id,
|
||||
surname_initials: surname_initials,
|
||||
phone: phone,
|
||||
telegram: telegram,
|
||||
email: email
|
||||
)
|
||||
end
|
||||
|
||||
def to_s
|
||||
contact_info = get_first_contact()
|
||||
"#{@surname_initials}, Contact: #{contact_info}"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def self.parse_contact_string(contact_string)
|
||||
case contact_string
|
||||
when /\APhone: (.+)\z/i
|
||||
[Regexp.last_match(1).strip, nil, nil]
|
||||
when /\ATelegram: (.+)\z/i
|
||||
[nil, Regexp.last_match(1).strip, nil]
|
||||
when /\AEmail: (.+)\z/i
|
||||
[nil, nil, Regexp.last_match(1).strip]
|
||||
else
|
||||
raise ArgumentError, "Invalid contact string format: #{contact_string}"
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,3 @@
|
||||
Иванов Иван Иванович | ID: 1 | Phone: +12345678901 | Telegram: @ivanov_user | Email: ivanov@example.com | Git: https://github.com/ivanov
|
||||
Петров Петр Петрович | ID: 2 | Phone: +98765432101 | Telegram: @petrov_user | Email: petrov@example.com | Git: https://github.com/petrov
|
||||
Сидоров Сидор Сидорович | ID: 3 | Phone: +56789012345 | Telegram: @sidorov_user | Email: sidorov@example.com | Git: https://github.com/sidorov
|
Loading…
Reference in new issue