Compare commits
17 Commits
Author | SHA1 | Date |
---|---|---|
|
71d128c71d | 6 months ago |
|
e603842e7c | 6 months ago |
|
d7c500f202 | 6 months ago |
|
8ccf7239f9 | 6 months ago |
|
6f9cd89526 | 6 months ago |
|
b1d90eab30 | 6 months ago |
|
9032b20783 | 6 months ago |
|
c9a56f66f4 | 7 months ago |
|
820a41173c | 7 months ago |
|
205b10ff9d | 7 months ago |
|
f015f43c99 | 7 months ago |
|
b9453a87e8 | 7 months ago |
|
988d9c83dd | 7 months ago |
|
a50ef4122e | 7 months ago |
|
44e77f3679 | 7 months ago |
|
8a8d9cfdd8 | 7 months ago |
|
8d8cc46bde | 10 months ago |
@ -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,4 +0,0 @@
|
|||||||
# Lab 2
|
|
||||||
|
|
||||||
> "И тут я обнаружил что случайно сделал 3-5 задачи в 1-2"
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
|||||||
class Student
|
|
||||||
attr_accessor :id, :surname, :name, :patronymic, :phone, :telegram, :email, :git
|
|
||||||
|
|
||||||
def self.valid_phone_number?(phone)
|
|
||||||
phone.match?(/\A\+?[0-9]{10,15}\z/)
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize(args = {})
|
|
||||||
@surname = args.fetch(:surname)
|
|
||||||
@name = args.fetch(:name)
|
|
||||||
@patronymic = args.fetch(:patronymic)
|
|
||||||
|
|
||||||
@id = args[:id] || nil
|
|
||||||
|
|
||||||
@phone = args[:phone]
|
|
||||||
if @phone && !Student.valid_phone_number?(@phone)
|
|
||||||
raise ArgumentError, "Invalid phone number format: #{@phone}"
|
|
||||||
end
|
|
||||||
|
|
||||||
@telegram = args[:telegram] || nil
|
|
||||||
@email = args[:email] || nil
|
|
||||||
@git = args[:git] || nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def to_s
|
|
||||||
"Student: #{@surname} #{@name} #{@patronymic}\n" \
|
|
||||||
"ID: #{@id || 'N/A'}\n" \
|
|
||||||
"Phone: #{@phone || 'N/A'}\n" \
|
|
||||||
"Telegram: #{@telegram || 'N/A'}\n" \
|
|
||||||
"Email: #{@email || 'N/A'}\n" \
|
|
||||||
"Git: #{@git || 'N/A'}"
|
|
||||||
end
|
|
||||||
end
|
|
@ -0,0 +1,2 @@
|
|||||||
|
source "https://rubygems.org"
|
||||||
|
gem "minitest", "~> 5.15"
|
@ -0,0 +1 @@
|
|||||||
|
5
|
@ -0,0 +1,27 @@
|
|||||||
|
# Run
|
||||||
|
|
||||||
|
### Run example
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ruby ./main.rb
|
||||||
|
```
|
||||||
|
|
||||||
|
### Run test
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ruby test/test_array_processor.rb
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
# Methods:
|
||||||
|
|
||||||
|
chunk - дробит массив на сущности по результату выполнения кода, even - четные число
|
||||||
|
|
||||||
|
include - содержит ли массив элемент
|
||||||
|
|
||||||
|
member - аналогично include
|
||||||
|
|
||||||
|
reduce - накапливает значения в аккумулияторе складывая все значения
|
||||||
|
|
||||||
|
filter - select элементов для который блок вернул true. odd - нечетное число
|
||||||
|
|
@ -0,0 +1,67 @@
|
|||||||
|
class ArrayProcessor
|
||||||
|
def initialize(array)
|
||||||
|
@array = array.dup.freeze
|
||||||
|
end
|
||||||
|
|
||||||
|
# Возвращает элементы массива
|
||||||
|
def elements
|
||||||
|
@array
|
||||||
|
end
|
||||||
|
|
||||||
|
# группировка элементов
|
||||||
|
def chunk
|
||||||
|
raise "No block given" unless block_given?
|
||||||
|
|
||||||
|
result = []
|
||||||
|
current_group = []
|
||||||
|
@array.each do |element|
|
||||||
|
if current_group.empty? || yield(element) == yield(current_group.last)
|
||||||
|
current_group << element
|
||||||
|
else
|
||||||
|
result << current_group
|
||||||
|
current_group = [element]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
result << current_group unless current_group.empty?
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
# проверка наличия элемента
|
||||||
|
def include?(value)
|
||||||
|
@array.each do |element|
|
||||||
|
return true if element == value
|
||||||
|
end
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
# свёртка массива
|
||||||
|
def reduce(initial = nil)
|
||||||
|
raise "No block given" unless block_given?
|
||||||
|
|
||||||
|
accumulator = initial
|
||||||
|
@array.each do |element|
|
||||||
|
if accumulator.nil?
|
||||||
|
accumulator = element
|
||||||
|
else
|
||||||
|
accumulator = yield(accumulator, element)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
accumulator
|
||||||
|
end
|
||||||
|
|
||||||
|
# проверка членства
|
||||||
|
def member?(value)
|
||||||
|
include?(value)
|
||||||
|
end
|
||||||
|
|
||||||
|
# фильтрация массива
|
||||||
|
def filter
|
||||||
|
raise "No block given" unless block_given?
|
||||||
|
|
||||||
|
result = []
|
||||||
|
@array.each do |element|
|
||||||
|
result << element if yield(element)
|
||||||
|
end
|
||||||
|
result
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,23 @@
|
|||||||
|
require_relative 'lib/array_processor'
|
||||||
|
|
||||||
|
|
||||||
|
processor = ArrayProcessor.new([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||||
|
|
||||||
|
# chunk
|
||||||
|
puts "Chunk example:"
|
||||||
|
p processor.chunk { |x| x.even? }
|
||||||
|
|
||||||
|
# include?
|
||||||
|
puts "Include example (5): #{processor.include?(5)}"
|
||||||
|
puts "Include example (10): #{processor.include?(10)}"
|
||||||
|
|
||||||
|
# reduce
|
||||||
|
puts "Reduce example (sum): #{processor.reduce(0) { |acc, x| acc + x }}"
|
||||||
|
|
||||||
|
# member?
|
||||||
|
puts "Member example (5): #{processor.member?(5)}"
|
||||||
|
puts "Member example (10): #{processor.member?(10)}"
|
||||||
|
|
||||||
|
# filter
|
||||||
|
puts "Filter example (odd numbers):"
|
||||||
|
p processor.filter { |x| x.odd? }
|
@ -0,0 +1,34 @@
|
|||||||
|
require 'minitest/autorun'
|
||||||
|
require_relative '../lib/array_processor'
|
||||||
|
|
||||||
|
|
||||||
|
class ArrayProcessorTest < Minitest::Test
|
||||||
|
def setup
|
||||||
|
@processor = ArrayProcessor.new([1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_chunk
|
||||||
|
result = @processor.chunk { |x| x.even? }
|
||||||
|
assert_equal [[1], [2], [3], [4], [5], [6], [7], [8], [9]], result
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_include
|
||||||
|
assert_equal true, @processor.include?(5)
|
||||||
|
assert_equal false, @processor.include?(10)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_reduce
|
||||||
|
assert_equal 45, @processor.reduce(0) { |acc, x| acc + x }
|
||||||
|
assert_equal 120, @processor.reduce(1) { |acc, x| acc * x }
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_member
|
||||||
|
assert_equal true, @processor.member?(5)
|
||||||
|
assert_equal false, @processor.member?(10)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_filter
|
||||||
|
result = @processor.filter { |x| x.odd? }
|
||||||
|
assert_equal [1, 3, 5, 7, 9], result
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,97 @@
|
|||||||
|
require 'nokogiri'
|
||||||
|
|
||||||
|
class HtmlTree
|
||||||
|
include Enumerable
|
||||||
|
|
||||||
|
def initialize(html)
|
||||||
|
@root = parse_html(html)
|
||||||
|
raise "Parsed HTML tree is empty" if @root.nil?
|
||||||
|
end
|
||||||
|
|
||||||
|
def each(order = :breadth_first, &block)
|
||||||
|
return enum_for(:each, order) unless block_given?
|
||||||
|
|
||||||
|
case order
|
||||||
|
when :breadth_first
|
||||||
|
queue = [@root].compact
|
||||||
|
until queue.empty?
|
||||||
|
current = queue.shift
|
||||||
|
yield current
|
||||||
|
queue.concat(current.children) if current.has_children?
|
||||||
|
end
|
||||||
|
when :depth_first
|
||||||
|
stack = [@root].compact
|
||||||
|
until stack.empty?
|
||||||
|
current = stack.pop
|
||||||
|
yield current
|
||||||
|
stack.concat(current.children.reverse) if current.has_children?
|
||||||
|
end
|
||||||
|
else
|
||||||
|
raise ArgumentError, "Unknown order: #{order}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def parse_html(html)
|
||||||
|
doc = Nokogiri::HTML::DocumentFragment.parse(html)
|
||||||
|
root_node = doc.at_css('body') || doc.children.find(&:element?) || doc.root
|
||||||
|
return nil if root_node.nil? || root_node.children.empty?
|
||||||
|
|
||||||
|
build_tree(root_node)
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_tree(node)
|
||||||
|
if node.element?
|
||||||
|
children = node.children.map { |child| build_tree(child) }.compact
|
||||||
|
HtmlTag.new(node.name, node.attributes.transform_values(&:value), children)
|
||||||
|
elsif node.text? && !node.content.strip.empty?
|
||||||
|
HtmlTag.new("text", { "content" => node.content.strip }, [])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class HtmlTag
|
||||||
|
attr_reader :name, :attributes, :children
|
||||||
|
|
||||||
|
def initialize(name, attributes, children = [])
|
||||||
|
@name = name
|
||||||
|
@attributes = attributes
|
||||||
|
@children = children
|
||||||
|
end
|
||||||
|
|
||||||
|
def has_children?
|
||||||
|
!@children.empty?
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
attr_str = attributes.map { |k, v| "#{k}=#{v.inspect}" }.join(", ")
|
||||||
|
"#{@name}(#{attr_str})"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
html = <<-HTML
|
||||||
|
<div id="root">
|
||||||
|
<p class="text">Hello, world!</p>
|
||||||
|
<ul>
|
||||||
|
<li>Item 1</li>
|
||||||
|
<li>Item 2</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
HTML
|
||||||
|
|
||||||
|
tree = HtmlTree.new(html)
|
||||||
|
|
||||||
|
puts "=== BFS Traversal ==="
|
||||||
|
tree.each(:breadth_first) { |tag| puts tag }
|
||||||
|
|
||||||
|
puts "\n=== DFS Traversal ==="
|
||||||
|
tree.each(:depth_first) { |tag| puts tag }
|
||||||
|
|
||||||
|
puts "\n=== Select Example (BFS) ==="
|
||||||
|
selected = tree.each(:breadth_first).select { |tag| tag.attributes["class"] == "text" }
|
||||||
|
puts "Selected tags: #{selected}"
|
||||||
|
|
||||||
|
puts "\n=== Reduce Example (DFS) ==="
|
||||||
|
reduced_value = tree.each(:depth_first).reduce("") { |acc, tag| acc + " " + tag.name }
|
||||||
|
puts "Reduced result: #{reduced_value.strip}"
|
@ -0,0 +1,30 @@
|
|||||||
|
|
||||||
|
numbers = [5, 3, 8, 1, 2]
|
||||||
|
|
||||||
|
# сортировка чисел
|
||||||
|
sorted_numbers = numbers.sort
|
||||||
|
puts "Sorted numbers using sort: #{sorted_numbers}"
|
||||||
|
|
||||||
|
# сортировка по алфавиту
|
||||||
|
words = ["apple", "orange", "banana", "grape"]
|
||||||
|
sorted_words = words.sort
|
||||||
|
puts "Sorted words using sort: #{sorted_words}"
|
||||||
|
|
||||||
|
people = [
|
||||||
|
{ name: "Alice", age: 30 },
|
||||||
|
{ name: "Bob", age: 25 },
|
||||||
|
{ name: "Charlie", age: 35 }
|
||||||
|
]
|
||||||
|
|
||||||
|
# сортировка по возрасту
|
||||||
|
sorted_people_by_age = people.sort { |a, b| a[:age] <=> b[:age] }
|
||||||
|
puts "Sorted people by age using sort: #{sorted_people_by_age}"
|
||||||
|
|
||||||
|
# сортировка по возрасту
|
||||||
|
sorted_people_by_age2 = people.sort_by { |person| person[:age] }
|
||||||
|
puts "Sorted people by age using sort_by: #{sorted_people_by_age2}"
|
||||||
|
|
||||||
|
# сортировка по длине строки
|
||||||
|
words = ["cat", "elephant", "dog", "hippopotamus"]
|
||||||
|
sorted_by_length = words.sort_by { |word| word.length }
|
||||||
|
puts "Sorted words by length using sort_by: #{sorted_by_length}"
|
Loading…
Reference in new issue