From eda9b3a868567cbdc802c48b4f27c3c4ceba0dc3 Mon Sep 17 00:00:00 2001 From: Ivan Reshetnikov Date: Mon, 28 Nov 2022 22:15:37 +0500 Subject: [PATCH] Problem 2: ruby rewrite --- problems/2-add-two-numbers/Makefile | 1 - problems/2-add-two-numbers/lib.c | 45 --------------- problems/2-add-two-numbers/lib.h | 8 --- problems/2-add-two-numbers/main.c | 86 ----------------------------- problems/2-add-two-numbers/main.rb | 38 +++++++++++++ problems/2-add-two-numbers/test.rb | 20 +++++++ problems/2-add-two-numbers/types.rb | 8 +++ 7 files changed, 66 insertions(+), 140 deletions(-) delete mode 120000 problems/2-add-two-numbers/Makefile delete mode 100644 problems/2-add-two-numbers/lib.c delete mode 100644 problems/2-add-two-numbers/lib.h delete mode 100644 problems/2-add-two-numbers/main.c create mode 100644 problems/2-add-two-numbers/main.rb create mode 100644 problems/2-add-two-numbers/test.rb create mode 100644 problems/2-add-two-numbers/types.rb diff --git a/problems/2-add-two-numbers/Makefile b/problems/2-add-two-numbers/Makefile deleted file mode 120000 index 25eaf27..0000000 --- a/problems/2-add-two-numbers/Makefile +++ /dev/null @@ -1 +0,0 @@ -../template/Makefile \ No newline at end of file diff --git a/problems/2-add-two-numbers/lib.c b/problems/2-add-two-numbers/lib.c deleted file mode 100644 index e0ab860..0000000 --- a/problems/2-add-two-numbers/lib.c +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include "lib.h" - -struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { - struct ListNode* current = NULL; - struct ListNode* first_node = NULL; - - int reminder = 0; - while (l1 != NULL || l2 != NULL || reminder != 0) { - // Read digits - int a = 0; - int b = 0; - if (l1 != NULL) - a = l1->val; - if (l2 != NULL) - b = l2->val; - - // Move pointers to the next node - if (l1 != NULL) - l1 = l1->next; - if (l2 != NULL) - l2 = l2->next; - - // Calculate the result - int digit = (a + b + reminder) % 10; - reminder = (a + b + reminder) / 10; - - // Create the first node - if (current == NULL) { - current = malloc(sizeof(struct ListNode)); - first_node = current; - } - // Create regular node - else { - current->next = malloc(sizeof(struct ListNode)); - current = current->next; - } - - // Save the result - current->val = digit; - current->next = NULL; - } - - return first_node; -} diff --git a/problems/2-add-two-numbers/lib.h b/problems/2-add-two-numbers/lib.h deleted file mode 100644 index 425301f..0000000 --- a/problems/2-add-two-numbers/lib.h +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -struct ListNode { - int val; - struct ListNode *next; -}; - -struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2); diff --git a/problems/2-add-two-numbers/main.c b/problems/2-add-two-numbers/main.c deleted file mode 100644 index fc7c750..0000000 --- a/problems/2-add-two-numbers/main.c +++ /dev/null @@ -1,86 +0,0 @@ -#include -#include -#include "lib.h" - -// Creates single node -struct ListNode* mkNode(int val) { - struct ListNode* node = malloc(sizeof(struct ListNode)); - node->val = val; - node->next = NULL; - return node; -} - -// Creates linked list -struct ListNode* mkList(int* nums, int length) { - struct ListNode* first = NULL; - struct ListNode* current = NULL; - for (int i=0; inext = mkNode(nums[i]); - current = current->next; - } - } - return first; -} - -// Returns 1 if linked lists are equal. -// Returns 0 otherwise. -int cmpLists(struct ListNode* a, struct ListNode* b) { - while (a != NULL || b != NULL) { - // Both nodes exist - if (a == NULL || b == NULL) - return 0; - - // Check values - if (a->val != b->val) - return 0; - - // Go to next node - a = a->next; - b = b->next; - } - - return 1; -} - -// Free allocated memory -void freeLinkedList(struct ListNode* list) { - struct ListNode* current; - while (list != NULL) { - current = list; - list = list->next; - free(current); - } -} - -int main() { - // First list - int a[] = {2, 4, 3}; - struct ListNode* list_a = mkList(a, 3); - - // Second list - int b[] = {5, 6, 4}; - struct ListNode* list_b = mkList(b, 3); - - // Expected sum - int c[] = {7, 0, 8}; - struct ListNode* list_c = mkList(c, 3); - - // Computed result - struct ListNode* res = addTwoNumbers(list_a, list_b); - - printf("Test 1: %d\n", cmpLists(res, list_c)); - - freeLinkedList(list_a); - freeLinkedList(list_b); - freeLinkedList(list_c); - freeLinkedList(res); - - return 0; -} diff --git a/problems/2-add-two-numbers/main.rb b/problems/2-add-two-numbers/main.rb new file mode 100644 index 0000000..2ff2245 --- /dev/null +++ b/problems/2-add-two-numbers/main.rb @@ -0,0 +1,38 @@ +require_relative 'types' + +# @param {ListNode} l1 +# @param {ListNode} l2 +# @return {ListNode} +def add_two_numbers(l1, l2) + reminder = 0 + current = nil + first_node = nil + + while !l1.nil? || !l2.nil? || reminder != 0 + # Read digits + a = 0 + b = 0 + a = l1.val unless l1.nil? + b = l2.val unless l2.nil? + + # Move pointers to the next node + l1 = l1.next unless l1.nil? + l2 = l2.next unless l2.nil? + + # Calculate the result + digit = (a + b + reminder) % 10 + reminder = (a + b + reminder) / 10 + + # Create the first node + if current.nil? + current = ListNode.new(digit) + first_node = current + # Create regular node + else + current.next = ListNode.new(digit) + current = current.next + end + end + + first_node +end diff --git a/problems/2-add-two-numbers/test.rb b/problems/2-add-two-numbers/test.rb new file mode 100644 index 0000000..3efc0fe --- /dev/null +++ b/problems/2-add-two-numbers/test.rb @@ -0,0 +1,20 @@ +require_relative 'main' +require 'test/unit' + +class TestAddTwoNumbers < Test::Unit::TestCase + def test_case_1 + a3 = ListNode.new(3) + a2 = ListNode.new(4, a3) + a1 = ListNode.new(2, a2) + + b3 = ListNode.new(4) + b2 = ListNode.new(6, b3) + b1 = ListNode.new(5, b2) + + c = add_two_numbers(a1, b1) + + assert_equal(7, c.val) + assert_equal(0, c.next.val) + assert_equal(8, c.next.next.val) + end +end diff --git a/problems/2-add-two-numbers/types.rb b/problems/2-add-two-numbers/types.rb new file mode 100644 index 0000000..c27f145 --- /dev/null +++ b/problems/2-add-two-numbers/types.rb @@ -0,0 +1,8 @@ +# Definition for singly-linked list. +class ListNode + attr_accessor :val, :next + def initialize(val = 0, _next = nil) + @val = val + @next = _next + end +end