From 27775661f063127021f71ee15835c4e8bf08506d Mon Sep 17 00:00:00 2001 From: Jovial Joe Jayarson Date: Sat, 3 Jul 2021 15:54:22 +0530 Subject: [PATCH 1/8] feat: adds two sum in nim, minor fix in readme_en --- solution/0000-0099/0001.Two Sum/README.md | 18 ++++++++++++++++++ solution/0000-0099/0001.Two Sum/README_EN.md | 20 +++++++++++++++++++- solution/0000-0099/0001.Two Sum/Solution.nim | 16 ++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 solution/0000-0099/0001.Two Sum/Solution.nim diff --git a/solution/0000-0099/0001.Two Sum/README.md b/solution/0000-0099/0001.Two Sum/README.md index e2be4e9f3d355..7a4b1c69491ac 100644 --- a/solution/0000-0099/0001.Two Sum/README.md +++ b/solution/0000-0099/0001.Two Sum/README.md @@ -131,6 +131,24 @@ class Solution { } ``` +### **Nim** + +```nim +import std/enumerate + +proc twoSum(nums: seq[int], target: int): seq[int] = + var + bal: int + tdx: int + for idx, val in enumerate(nums): + bal = target - val + if bal in nums: + tdx = nums.find(bal) + if idx != tdx: + return @[idx, tdx] + +``` + ### **...** ``` diff --git a/solution/0000-0099/0001.Two Sum/README_EN.md b/solution/0000-0099/0001.Two Sum/README_EN.md index 2ef339a32b505..4022cf1347da9 100644 --- a/solution/0000-0099/0001.Two Sum/README_EN.md +++ b/solution/0000-0099/0001.Two Sum/README_EN.md @@ -16,7 +16,7 @@
 Input: nums = [2,7,11,15], target = 9
 Output: [0,1]
-Output: Because nums[0] + nums[1] == 9, we return [0, 1].
+Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
 

Example 2:

@@ -117,6 +117,24 @@ class Solution { } ``` +### **Nim** + +```nim +import std/enumerate + +proc twoSum(nums: seq[int], target: int): seq[int] = + var + bal: int + tdx: int + for idx, val in enumerate(nums): + bal = target - val + if bal in nums: + tdx = nums.find(bal) + if idx != tdx: + return @[idx, tdx] + +``` + ### **...** ``` diff --git a/solution/0000-0099/0001.Two Sum/Solution.nim b/solution/0000-0099/0001.Two Sum/Solution.nim new file mode 100644 index 0000000000000..e510b433f986f --- /dev/null +++ b/solution/0000-0099/0001.Two Sum/Solution.nim @@ -0,0 +1,16 @@ +#[ + Author: @joe733 +]# + +import std/enumerate + +proc twoSum(nums: seq[int], target: int): seq[int] = + var + bal: int + tdx: int + for idx, val in enumerate(nums): + bal = target - val + if bal in nums: + tdx = nums.find(bal) + if idx != tdx: + return @[idx, tdx] From 599199590b0de84fac538ac496ffd6eab3e53b95 Mon Sep 17 00:00:00 2001 From: Jovial Joe Jayarson Date: Tue, 6 Jul 2021 01:04:25 +0530 Subject: [PATCH 2/8] feat: nim solution to 'addtwonumbers' --- .../0000-0099/0002.Add Two Numbers/README.md | 37 ++++++++++++ .../0002.Add Two Numbers/README_EN.md | 36 ++++++++++++ .../0002.Add Two Numbers/Solution.nim | 56 +++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 solution/0000-0099/0002.Add Two Numbers/Solution.nim diff --git a/solution/0000-0099/0002.Add Two Numbers/README.md b/solution/0000-0099/0002.Add Two Numbers/README.md index 8ab330875783f..547b6edade581 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README.md +++ b/solution/0000-0099/0002.Add Two Numbers/README.md @@ -304,6 +304,43 @@ class Solution { } ``` +### **Nim** + +```nim +#[ + # Driver code in the solution file + # Definition for singly-linked list. + type + Node[int] = ref object + value: int + next: Node[int] + + SinglyLinkedList[T] = object + head, tail: Node[T] +]# + +# More efficient code churning ... +proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLinkedList[int] = + var + aggregate: SinglyLinkedList + psum: seq[char] + temp_la, temp_lb: seq[int] + + while not l1.head.isNil: + temp_la.add(l1.head.value) + l1.head = l1.head.next + + while not l2.head.isNil: + temp_lb.add(l2.head.value) + l2.head = l2.head.next + + psum = reversed($(reversed(temp_la).join("").parseInt() + reversed(temp_lb).join("").parseInt())) + for i in psum: aggregate.append(($i).parseInt()) + + result = aggregate +``` + + ### **...** ``` diff --git a/solution/0000-0099/0002.Add Two Numbers/README_EN.md b/solution/0000-0099/0002.Add Two Numbers/README_EN.md index 0a37b8dec7984..2011ed0f740a6 100644 --- a/solution/0000-0099/0002.Add Two Numbers/README_EN.md +++ b/solution/0000-0099/0002.Add Two Numbers/README_EN.md @@ -292,6 +292,42 @@ class Solution { } ``` +### **Nim** + +```nim +#[ + # Driver code in the solution file + # Definition for singly-linked list. + type + Node[int] = ref object + value: int + next: Node[int] + + SinglyLinkedList[T] = object + head, tail: Node[T] +]# + +# More efficient code churning ... +proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLinkedList[int] = + var + aggregate: SinglyLinkedList + psum: seq[char] + temp_la, temp_lb: seq[int] + + while not l1.head.isNil: + temp_la.add(l1.head.value) + l1.head = l1.head.next + + while not l2.head.isNil: + temp_lb.add(l2.head.value) + l2.head = l2.head.next + + psum = reversed($(reversed(temp_la).join("").parseInt() + reversed(temp_lb).join("").parseInt())) + for i in psum: aggregate.append(($i).parseInt()) + + result = aggregate +``` + ### **...** ``` diff --git a/solution/0000-0099/0002.Add Two Numbers/Solution.nim b/solution/0000-0099/0002.Add Two Numbers/Solution.nim new file mode 100644 index 0000000000000..149b2916d445c --- /dev/null +++ b/solution/0000-0099/0002.Add Two Numbers/Solution.nim @@ -0,0 +1,56 @@ +import std/[strutils, algorithm] + +type + Node[int] = ref object + value: int + next: Node[int] + + SinglyLinkedList[T] = object + head, tail: Node[T] + +proc append[T](list: var SinglyLinkedList[T], data: T = nil): void = + var node = Node[T](value: data) + if list.head.isNil: + list.head = node + list.tail = node + else: + list.tail.next = node + list.tail = node + +proc preview[T](list: SinglyLinkedList[T]): string = + var s: seq[T] + var n = list.head + while not n.isNil: + s.add n.value + n = n.next + result = s.join(" -> ") + +proc addTwoNumbers(l1: var SinglyLinkedList, l2: var SinglyLinkedList): SinglyLinkedList[int] = + var + aggregate: SinglyLinkedList + psum: seq[char] + temp_la, temp_lb: seq[int] + + while not l1.head.isNil: + temp_la.add(l1.head.value) + l1.head = l1.head.next + + while not l2.head.isNil: + temp_lb.add(l2.head.value) + l2.head = l2.head.next + + psum = reversed($(reversed(temp_la).join("").parseInt() + reversed(temp_lb).join("").parseInt())) + for i in psum: aggregate.append(($i).parseInt()) + + result = aggregate + +var list1: SinglyLinkedList[int] +var list2: SinglyLinkedList[int] + +for i in @[2, 4, 3]: list1.append(i) +for i in @[5, 6, 4]: list2.append(i) + +echo(preview(list1)) +echo(preview(list2)) +echo(preview(addTwoNumbers(list1, list2))) + From 728fe5f4840cef989cbc08e4392e77f6650d7160 Mon Sep 17 00:00:00 2001 From: Jovial Joe Jayarson Date: Thu, 30 Sep 2021 12:31:06 +0530 Subject: [PATCH 3/8] feat: adds nim-lang solution to longest substring --- .../README.md | 20 +++++++++++++++++ .../README_EN.md | 22 +++++++++++++++++++ .../Solution.nim | 19 ++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.nim diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md index 86bcd0d2ce250..e03b8cd34f603 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md @@ -202,6 +202,26 @@ func max(x, y int) int { } ``` +```nim +proc lengthOfLongestSubstring(s: string): int = + var + i = 0 + j = 0 + res = 0 + literals: set[char] = {} + + while i < s.len: + while s[i] in literals: + if s[j] in literals: + excl(literals, s[j]) + j += 1 + literals.incl(s[i]) # Uniform Function Call Syntax f(x) = x.f + res = max(res, i - j + 1) + i += 1 + + result = res # result has the default return value +``` + ### **...** ``` diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md index cf5b41d753e1a..d06e8edc82944 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md @@ -184,6 +184,28 @@ func max(x, y int) int { } ``` +### **Nim** + +```nim +proc lengthOfLongestSubstring(s: string): int = + var + i = 0 + j = 0 + res = 0 + literals: set[char] = {} + + while i < s.len: + while s[i] in literals: + if s[j] in literals: + excl(literals, s[j]) + j += 1 + literals.incl(s[i]) # Uniform Function Call Syntax f(x) = x.f + res = max(res, i - j + 1) + i += 1 + + result = res # result has the default return value +``` + ### **...** ``` diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.nim b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.nim new file mode 100644 index 0000000000000..03058aac47058 --- /dev/null +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/Solution.nim @@ -0,0 +1,19 @@ +proc lengthOfLongestSubstring(s: string): int = + var + i = 0 + j = 0 + res = 0 + literals: set[char] = {} + + while i < s.len: + while s[i] in literals: + if s[j] in literals: + excl(literals, s[j]) + j += 1 + literals.incl(s[i]) # Uniform Function Call Syntax f(x) = x.f + res = max(res, i - j + 1) + i += 1 + + result = res # result has the default return value + +echo lengthOfLongestSubstring("abcddabcf") From 2d1dfd63b73447eabb22f7bb4eacadfd6599b87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=8A=AD=E7=8F=91?= <382084620@qq.com> Date: Thu, 30 Sep 2021 15:34:29 +0800 Subject: [PATCH 4/8] doc: format README.md --- .../README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md index e03b8cd34f603..c4c05ef251a13 100644 --- a/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md +++ b/solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md @@ -202,6 +202,8 @@ func max(x, y int) int { } ``` +### **Nim** + ```nim proc lengthOfLongestSubstring(s: string): int = var From 02deb3fe85195199ee08c76de2730c2a5ef014e9 Mon Sep 17 00:00:00 2001 From: Jovial Joe Jayarson Date: Fri, 1 Oct 2021 08:22:26 +0530 Subject: [PATCH 5/8] feat: adds nim-lang soln to median of two arrays --- .../README.md | 59 +++++++++++++++++-- .../README_EN.md | 57 +++++++++++++++++- .../Solution.nim | 21 +++++++ 3 files changed, 128 insertions(+), 9 deletions(-) create mode 100644 solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.nim diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md index 0cb4332258f2e..bd328a0b56151 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md @@ -71,20 +71,67 @@ -### **Python3** - - +### [**Python3**](Solution.py) ```python +class Solution: + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + # concatenate the 2 lists and sort them + nums1 += nums2 + nums1.sort() + length = len(nums1) + value = length/2 + if length % 2 == 0: + value = int(value) + return (nums1[value-1] + nums1[value])/2 + else: + return nums1[int(value)] +``` + +### [**Java**](Solution.java) +```java +// Check the link ``` -### **Java** +### [**C++**](Solution.cpp) - +```cpp +// Check the link +``` -```java +### [**C#**](Solution.cs) + +```c# +// Check the link +``` + +### [**Go**](Solution.go) + +```go +// Check the link +``` + +### [**JavaScript**](Solution.js) + +```js +// Check the link +``` + +### [**Nim**](Solution.nim) + +```nim +proc medianOfTwoSortedArrays(nums1: seq[int], nums2: seq[int]): float = + var + fullList: seq[int] = concat(nums1, nums2) + value: int = fullList.len div 2 + + fullList.sort() + if fullList.len mod 2 == 0: + result = (fullList[value - 1] + fullList[value]) / 2 + else: + result = fullList[value].toFloat() ``` ### **...** diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md index dba8a7ad52c7d..f080ec6011651 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md @@ -63,16 +63,67 @@ -### **Python3** +### [**Python3**](Solution.py) ```python - +class Solution: + def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: + # concatenate the 2 lists and sort them + nums1 += nums2 + nums1.sort() + length = len(nums1) + value = length/2 + if length % 2 == 0: + value = int(value) + return (nums1[value-1] + nums1[value])/2 + else: + return nums1[int(value)] ``` -### **Java** +### [**Java**](Solution.java) ```java +// Check the link +``` + +### [**C++**](Solution.cpp) + +```cpp +// Check the link +``` + +### [**C#**](Solution.cs) + +```c# +// Check the link +``` + +### [**Go**](Solution.go) + +```go +// Check the link +``` + +### [**JavaScript**](Solution.js) + +```js +// Check the link +``` + +### [**Nim**](Solution.nim) + +```nim +proc medianOfTwoSortedArrays(nums1: seq[int], nums2: seq[int]): float = + var + fullList: seq[int] = concat(nums1, nums2) + value: int = fullList.len div 2 + + fullList.sort() + if fullList.len mod 2 == 0: + result = (fullList[value - 1] + fullList[value]) / 2 + else: + result = fullList[value].toFloat() ``` ### **...** diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.nim b/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.nim new file mode 100644 index 0000000000000..0ea829db0bef1 --- /dev/null +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/Solution.nim @@ -0,0 +1,21 @@ +import std/[algorithm, sequtils] + +proc medianOfTwoSortedArrays(nums1: seq[int], nums2: seq[int]): float = + var + fullList: seq[int] = concat(nums1, nums2) + value: int = fullList.len div 2 + + fullList.sort() + + if fullList.len mod 2 == 0: + result = (fullList[value - 1] + fullList[value]) / 2 + else: + result = fullList[value].toFloat() + +# Driver Code + +# var +# arrA: seq[int] = @[1, 2] +# arrB: seq[int] = @[3, 4, 5] +# echo medianOfTwoSortedArrays(arrA, arrB) + From 328c67100f847bcdb86c969b8d1c1735dbb65883 Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Thu, 7 Oct 2021 21:32:29 +0800 Subject: [PATCH 6/8] Update README.md --- .../README.md | 30 ++----------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md index bd328a0b56151..beab830c7258d 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md @@ -71,7 +71,7 @@ -### [**Python3**](Solution.py) +### **Python3** ```python class Solution: @@ -88,37 +88,13 @@ class Solution: return nums1[int(value)] ``` -### [**Java**](Solution.java) +### **Java** ```java -// Check the link -``` - -### [**C++**](Solution.cpp) - -```cpp -// Check the link -``` - -### [**C#**](Solution.cs) - -```c# -// Check the link -``` - -### [**Go**](Solution.go) - -```go -// Check the link -``` - -### [**JavaScript**](Solution.js) -```js -// Check the link ``` -### [**Nim**](Solution.nim) +### **Nim** ```nim proc medianOfTwoSortedArrays(nums1: seq[int], nums2: seq[int]): float = From 47e5bb0c170f76b7f1782b6566f3c96f06b1110e Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Thu, 7 Oct 2021 21:34:01 +0800 Subject: [PATCH 7/8] Update README_EN.md --- .../README_EN.md | 30 ++----------------- 1 file changed, 3 insertions(+), 27 deletions(-) diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md index f080ec6011651..e0351dcc63340 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md @@ -63,7 +63,7 @@ -### [**Python3**](Solution.py) +### **Python3** ```python class Solution: @@ -80,37 +80,13 @@ class Solution: return nums1[int(value)] ``` -### [**Java**](Solution.java) +### **Java** ```java -// Check the link -``` - -### [**C++**](Solution.cpp) - -```cpp -// Check the link -``` - -### [**C#**](Solution.cs) - -```c# -// Check the link -``` - -### [**Go**](Solution.go) - -```go -// Check the link -``` - -### [**JavaScript**](Solution.js) -```js -// Check the link ``` -### [**Nim**](Solution.nim) +### **Nim** ```nim proc medianOfTwoSortedArrays(nums1: seq[int], nums2: seq[int]): float = From 08714d2c9e7a1b580e3a70aac2ba1f239c72a67a Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Thu, 7 Oct 2021 21:35:30 +0800 Subject: [PATCH 8/8] Update README.md --- solution/0000-0099/0004.Median of Two Sorted Arrays/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md index beab830c7258d..ab57c524edf7d 100644 --- a/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md +++ b/solution/0000-0099/0004.Median of Two Sorted Arrays/README.md @@ -73,6 +73,8 @@ ### **Python3** + + ```python class Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: @@ -90,6 +92,8 @@ class Solution: ### **Java** + + ```java ```