Skip to content

Instantly share code, notes, and snippets.

View denis-mironov's full-sized avatar

Denis Mironov denis-mironov

View GitHub Profile
@denis-mironov
denis-mironov / LINUX commands and useful info.md
Last active February 26, 2018 13:47
LINUX often use commands

Unix shell commands

Main commands

  • uname -a - Linux kernel version
  • whoami - displays the username of the current user.
  • echo - output text to the screen
  • env - list environment variables. Use the dollar sign($) to refer.
  • unset - remove env variable

Info commands

  • man - manual page. man [command] opens manual for this command
@denis-mironov
denis-mironov / bubble_sort_in_ruby.md
Last active June 13, 2020 11:17
Bubble sorting algorithm implementation in Ruby

Visualisation: https://www.toptal.com/developers/sorting-algorithms

We will go through the array from left to right. If the current item is larger than the next, swap them. We do this until the array is sorted. Note that after the first iteration, the largest element will be at the end of the array, in the right place. After two iterations, the two largest elements will be in the right place, and so on. Obviously, after no more than n iterations, the array will be sorted. Thus, the asymptotic behavior in the worst and middle case is O(n2), in the best case - O(n). Note that bubble sorting works slowly on tests in which small elements are at the end (they are also called “turtles”). Such an element at each step of the algorithm will shift only one position to the left.

Будем идти по массиву слева направо. Если текущий элемент больше следующего, меняем их местами. Делаем так, пока массив не будет отсортирован. Заметим, что после первой итерации самый большой элемент будет находиться в конце массива, на правил

@denis-mironov
denis-mironov / insertion_sort_in_ruby.md
Created June 13, 2020 11:21
Insertion sort algorithm implementation in Ruby

Visualisation: https://www.toptal.com/developers/sorting-algorithms https://www.youtube.com/watch?v=SIrdTFF8-4s

When sorting by inserts, the array is sorted sequentially. Each next element under consideration is placed so as to be between the nearest minimum element and the nearest maximum.

При сортировке вставками, массив перебирается последовательно. Каждый следующий рассматриваемый элемент размещается так, чтобы оказаться между ближайшим минимальным элементом и ближайшим максимальным.

puts 'Please, enter array size'
arr_size = gets.chomp.to_i
@denis-mironov
denis-mironov / merge_sort_in_ruby.md
Last active June 13, 2020 11:35
Merge sort algorithm implementation in Ruby
@denis-mironov
denis-mironov / quick_sort_in_ruby.md
Created June 13, 2020 11:35
Quick sort algorithm implementation in Ruby

Visualisation: https://www.toptal.com/developers/sorting-algorithms

def quicksort(array, beg_index, end_index)
  if beg_index < end_index
    p_index = partition(array, beg_index, end_index)
    quicksort(array, beg_index, p_index - 1)
    quicksort(array, p_index + 1, end_index)
  end
@denis-mironov
denis-mironov / selection_sort_in_ruby.md
Created June 13, 2020 11:38
Selection sort algorithm implementation in Ruby

Visualisation: https://www.toptal.com/developers/sorting-algorithms

The main idea of this method is to create a sorted sequence by attaching one element after another in the correct order. The selection sorting algorithm consists of several successive steps. At each sorting step, the current array element is interchanged with the element with the smallest value. Thus, an array of values is obtained, sorted in ascending order.

Основная мысль этого метода заключается в том, чтобы создать отсортированную последовательность, присоединяя к ней один элемент за другим в правильном порядке. Алгоритм сортировки выбором состоит из нескольких последовательных шагов. На каждом шаге сортировки текущий элемент массива меняется местами с элементом с наименьшим значением. Таким образом, получается массив значений, отсортированный по возрастанию.

puts 'Please, enter array size'
arr_size = gets.chomp.to_i
array = Array.new(arr_size) { rand(1..100) }
@denis-mironov
denis-mironov / change_money_dp_in_ruby.md
Last active May 7, 2021 04:59
Change money problem in Ruby (Dynamic Programming)

As we already know, a natural greedy strategy for the change problem does not work correctly for any set of denominations. For example, if the available denominations are 1, 3, and 4, the greedy algorithm will change 6 cents using three coins (4 + 1 + 1) while it can be changed using just two coins (3 + 3). Your goal now is to apply dynamic programming for solving the Money Change Problem for denominations 1, 3, and 4.

Input Format.

Integer money.

Output Format.

The minimum number of coins with denominations 1, 3, 4 that changes money.

result = gets.chomp.to_i
coins = [1,3,4]
@denis-mironov
denis-mironov / edit_distance_dp_in_ruby.md
Created June 14, 2020 11:58
Edit distance between strings problem in Ruby (Dynamic Programming)

Problem Introduction

The edit distance between two strings is the minimum number of operations (insertions, deletions, and substitutions of symbols) to transform one string into another. It is a measure of similarity of two strings. Edit distance has applications, for example, in computational biology, natural language processing, and spell checking. Your goal in this problem is to compute the edit distance between two strings.

Problem Description

Task.

The goal of this problem is to implement the algorithm for computing the edit distance between two strings.

Input Format.

@denis-mironov
denis-mironov / knapsack_problem_dp_in_ruby.md
Last active May 30, 2022 21:23
Knapsack problem in Ruby (Dynamic Programming)

Maximum Amount of Gold

Problem Introduction

You are given a set of bars of gold and your goal is to take as much gold as possible into your bag. There is just one copy of each bar and for each bar you can either take it or not (hence you cannot take a fraction of a bar).

Problem Description

Task.

Given 𝑛 gold bars, find the maximum weight of gold that fits into a bag of capacity 𝑊.

Input Format.

The first line of the input contains the capacity 𝑊 of a knapsack and the number 𝑛 of bars

@denis-mironov
denis-mironov / partitioning_into_three_parts_in_ruby.md
Last active June 14, 2020 12:23
Partitioning into three parts problem in Ruby

Partitioning Souvenirs

You and two of your friends have just returned back home after visiting various countries. Now you would like to evenly split all the souvenirs that all three of you bought.

Problem Description

Input Format.

The first line contains an integer 𝑛. The second line contains integers 𝑣1, 𝑣2, . . . , 𝑣𝑛 separated by spaces.

Constraints.

1 ≤ 𝑛 ≤ 20, 1 ≤ 𝑣𝑖 ≤ 30 for all 𝑖.

Output Format.