Problem 2: ruby rewrite
This commit is contained in:
parent
8f7ac22afe
commit
eda9b3a868
7 changed files with 66 additions and 140 deletions
|
@ -1 +0,0 @@
|
|||
../template/Makefile
|
|
@ -1,45 +0,0 @@
|
|||
#include <stdlib.h>
|
||||
#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;
|
||||
}
|
|
@ -1,8 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
struct ListNode {
|
||||
int val;
|
||||
struct ListNode *next;
|
||||
};
|
||||
|
||||
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2);
|
|
@ -1,86 +0,0 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#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; i<length; i++) {
|
||||
// First node
|
||||
if (current == NULL) {
|
||||
current = mkNode(nums[i]);
|
||||
first = current;
|
||||
}
|
||||
// Regular node
|
||||
else {
|
||||
current->next = 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;
|
||||
}
|
38
problems/2-add-two-numbers/main.rb
Normal file
38
problems/2-add-two-numbers/main.rb
Normal file
|
@ -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
|
20
problems/2-add-two-numbers/test.rb
Normal file
20
problems/2-add-two-numbers/test.rb
Normal file
|
@ -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
|
8
problems/2-add-two-numbers/types.rb
Normal file
8
problems/2-add-two-numbers/types.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue