Skip to content

Instantly share code, notes, and snippets.

@Mat1s
Mat1s / 10_AllComandScores.rb
Last active December 16, 2018 22:46
AllComandScores
class Games
attr_accessor :array_of_score
def initialize(array_of_score)
@array_of_score = array_of_score
end
def check_attributes(rival)
return 'wrong attributes' unless @array_of_score.is_a?(Array) &&
rival.is_a?(Array)
@Mat1s
Mat1s / 9_check_palindrome.rb
Last active December 1, 2018 23:21
palindrome?
class String
def palindrome?
self.downcase == self.downcase.reverse
end
end
puts "erere".palindrome? # true
puts "ror5".palindrome? # false
str3 = 'fdsf fdJJfd ajk hlj fqjqfpot otirn dfsdl dskj vcbn cx'
puts str3.palindrome? # false
str4 = 'xc nbcv jksd ldsfd nrito otirn dfsdl dskj vcbn cx'
@Mat1s
Mat1s / 8_count_words.rb
Last active November 27, 2018 10:18
count_words
def count_words(str)
str.split.length
end
srt2 = 'fdsf fdsfd ajk hlj fqjqfpot otirn dfsdl dskj vcbn cx'
puts count_words srt2
@Mat1s
Mat1s / 7_sort_string.rb
Last active December 1, 2018 23:20
sort_string
def sort_string(str)
str.split.sort_by(&:length).join(' ')
end
srt1 = 'fdsf fdsfd ajk hlj fqjqfpot otirn dfsdl dskj vcbn cx'
puts sort_string srt1
@Mat1s
Mat1s / 6_sort_int.rb
Last active December 1, 2018 23:20
sort_int
numbers6 = [18, 234, 74, 47, -1163, -15, -1, 0, 71, 5143, 36]
puts numbers6.sort #immutable number_6
puts numbers6.sort! #mutable number_6
@Mat1s
Mat1s / 5_add_before_positive_elem.rb
Last active December 6, 2018 10:43
add_before_positive_elem
print a = [1, 2, -2, 9, -3, -8, 4, 5, -6, 7, -8]
print a.flat_map { |elem| elem > 0 ? [a[0], elem] : elem }
print a.reduce([]) { |temp, i| temp.push(a[0]) if i > 0; temp.push(i) }
def add_before_positive(arr, f = arr.first)
arr.map.with_index do |elem, i|
if i > 0 && elem > 0
arr_cut = arr.slice!(i..-1)
@Mat1s
Mat1s / 4_left_rotate.rb
Last active December 1, 2018 23:15
in_left
def left_rotate(arr)
arr.push(arr.shift)
end
numbers4 = [18, 234, 74, 47, 1163, -15, -1, 0, 71, 5143, 36]
p left_rotate numbers4
@Mat1s
Mat1s / 3_change_positive_on_first_elem.rb
Last active December 1, 2018 23:14
replace_positive_on_first_in_array
def change_positive_on_first_elem(num)
m = num.min
num.map { |n| n > 0 ? m : n }
end
numbers3 = [180, 204, 154, -167, -163, 154, -110, -171, -143, -196]
puts change_positive_on_first_elem numbers3
@Mat1s
Mat1s / 2_highest_student.rb
Last active December 1, 2018 23:14
highest_student
def highest_student(height, surnames)
surnames[height.index(height.max)]
end
height = [180, 204, 154, 167, 163, 154, 110, 171, 143, 196]
students = ['Dziatlov', 'Smith)', 'Borakov', 'Smirnov', 'Kot', 'Gorshenia',
'Sapozhnikav', 'Landay', 'Tolstoy', 'Mihailin']
puts highest_student height, students
@Mat1s
Mat1s / 1_win_or_lost_game.rb
Last active December 1, 2018 23:14
win_or_lost_game
def check_attributes(your, rival)
return 'wrong attributes' unless your.is_a?(Array) &&
rival.is_a?(Array)
(rival + your).each do |i|
return 'wrong type in Array' unless i.is_a?(Integer) && i >= 0
end
'different count game' unless rival.length == your.length
end
def win_or_lost_game(your, rival)