Compare commits
3 commits
78d155ab83
...
5f7bcbc2f0
Author | SHA1 | Date | |
---|---|---|---|
5f7bcbc2f0 | |||
6b0cdf9561 | |||
845b138856 |
12 changed files with 375 additions and 357 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1,3 @@
|
|||
.jupyter
|
||||
|
||||
.ipynb_checkpoints
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
{
|
||||
"cells": [],
|
||||
"metadata": {},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
110
leetcode/1/haskell.ipynb
Normal file
110
leetcode/1/haskell.ipynb
Normal file
|
@ -0,0 +1,110 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "674f782d-c014-4749-ba16-5f107f8c31d9",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import qualified Data.IntMap.Strict as Map"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"id": "6dd70e30-9203-45a8-957b-a0a8459a3a28",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"enumerate = zip [0..]\n",
|
||||
"\n",
|
||||
"twoSum' :: Map.IntMap Int -> [(Int, Int)] -> Int -> [Int]\n",
|
||||
"twoSum' hm [] target = []\n",
|
||||
"twoSum' hm ((i, n):xs) target =\n",
|
||||
" let remainder = target - n in\n",
|
||||
" let index = Map.lookup remainder hm in\n",
|
||||
" case index of\n",
|
||||
" Nothing -> twoSum' (Map.insert n i hm) xs target\n",
|
||||
" Just k -> [k, i]\n",
|
||||
"\n",
|
||||
"twoSum :: [Int] -> Int -> [Int]\n",
|
||||
"twoSum nums target =\n",
|
||||
" let hm = Map.empty in\n",
|
||||
" let nums' = enumerate nums in\n",
|
||||
" twoSum' hm nums' target"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"id": "0fd587ec-b060-4722-9bbd-d4f6dd83efe2",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"-- Open Assistant:\n",
|
||||
"-- Define a custom function to check assertions.\n",
|
||||
"assert :: Bool -> String -> IO ()\n",
|
||||
"assert True msg = pure ()\n",
|
||||
"assert False msg = ioError $ userError msg"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "85cd567a-8124-4a77-8108-0183a9e6b471",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"\"All Done\""
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"assert (twoSum [3,3] 6 == [0,1]) \"Case 1 Failed\"\n",
|
||||
"assert (twoSum [3,2,4] 6 == [1,2]) \"Case 2 Failed\"\n",
|
||||
"assert (twoSum [2,7,11,15] 9 == [0,1]) \"Case 3 Failed\"\n",
|
||||
"\n",
|
||||
"print \"All Done\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c631b602-1a58-478b-8a73-294f2e34176d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "haskell-Haskell kernel",
|
||||
"language": "haskell",
|
||||
"name": "haskell-haskell"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": "Haskell",
|
||||
"file_extension": ".hs",
|
||||
"mimetype": "text/x-haskell",
|
||||
"name": "haskell",
|
||||
"pygments_lexer": "Haskell",
|
||||
"version": "9.0.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
|
@ -5,12 +5,12 @@
|
|||
"id": "92b94045-a058-4b99-bfb6-7709a2a96f1a",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# First variant O(n^2)"
|
||||
"# Idea 1: O(n^2)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 27,
|
||||
"id": "764a6c5e-5fa8-41be-9ec2-2b097ed29af6",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
|
@ -36,12 +36,12 @@
|
|||
"id": "4278ee69-8a33-480c-a33b-1049bcec1eed",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Second variant O(n)"
|
||||
"# Idea 2: O(n)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"execution_count": 28,
|
||||
"id": "7ad2762a-1deb-46ce-93fb-84b6ef637951",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
|
@ -74,7 +74,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"execution_count": 29,
|
||||
"id": "8bdf4378-b7b4-4edb-a409-4e3f98517c41",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
|
|
39
leetcode/2/README.md
Normal file
39
leetcode/2/README.md
Normal file
|
@ -0,0 +1,39 @@
|
|||
# 2. Add Two Numbers (Medium)
|
||||
|
||||
You are given two non-empty linked lists representing two non-negative integers.
|
||||
The digits are stored in reverse order, and each of their nodes contains a
|
||||
single digit. Add the two numbers and return the sum as a linked list.
|
||||
|
||||
You may assume the two numbers do not contain any leading zero, except the
|
||||
number 0 itself.
|
||||
|
||||
## Example 1:
|
||||
|
||||
![Add two number](assets/addtwonumber1.jpg)
|
||||
|
||||
```
|
||||
Input: l1 = [2,4,3], l2 = [5,6,4]
|
||||
Output: [7,0,8]
|
||||
Explanation: 342 + 465 = 807.
|
||||
```
|
||||
|
||||
## Example 2:
|
||||
|
||||
```
|
||||
Input: l1 = [0], l2 = [0]
|
||||
Output: [0]
|
||||
```
|
||||
|
||||
## Example 3:
|
||||
|
||||
```
|
||||
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
|
||||
Output: [8,9,9,9,0,0,0,1]
|
||||
```
|
||||
|
||||
## Constraints:
|
||||
|
||||
- The number of nodes in each linked list is in the range [1, 100].
|
||||
- 0 <= Node.val <= 9
|
||||
- It is guaranteed that the list represents a number that does not have leading
|
||||
zeros.
|
BIN
leetcode/2/assets/addtwonumber1.jpg
Normal file
BIN
leetcode/2/assets/addtwonumber1.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
219
leetcode/2/rust.ipynb
Normal file
219
leetcode/2/rust.ipynb
Normal file
|
@ -0,0 +1,219 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "feab2236-6685-4f83-9d50-054bf952f66e",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"// Definition for singly-linked list.\n",
|
||||
"#[derive(PartialEq, Eq, Clone, Debug)]\n",
|
||||
"pub struct ListNode {\n",
|
||||
" pub val: i32,\n",
|
||||
" pub next: Option<Box<ListNode>>\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"impl ListNode {\n",
|
||||
" #[inline]\n",
|
||||
" fn new(val: i32) -> Self {\n",
|
||||
" ListNode {\n",
|
||||
" next: None,\n",
|
||||
" val\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "65dd529b-49f6-4a98-b69d-12871b081a40",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Idea 1: O(n + m)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"id": "167e31a3-7844-4cdf-a563-241a40b07212",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"pub fn internal_add_two_numbers(extra_val: i32, l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {\n",
|
||||
" match (extra_val, l1.clone(), l2.clone()) {\n",
|
||||
" (0, None, None) => None,\n",
|
||||
" (_, l1, l2) => {\n",
|
||||
" let val =\n",
|
||||
" l1.as_ref().map(|n1| n1.val).unwrap_or_default()\n",
|
||||
" + l2.as_ref().map(|n2| n2.val).unwrap_or_default()\n",
|
||||
" + extra_val;\n",
|
||||
" let mut nn = ListNode::new(val % 10);\n",
|
||||
" let extra_val = if val > 9 { 1 } else { 0 };\n",
|
||||
" nn.next = internal_add_two_numbers(\n",
|
||||
" extra_val,\n",
|
||||
" l1.and_then(|n1| n1.next),\n",
|
||||
" l2.and_then(|n2| n2.next)\n",
|
||||
" );\n",
|
||||
" Some(Box::new(nn))\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {\n",
|
||||
" internal_add_two_numbers(0, l1, l2)\n",
|
||||
"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "f9dab3ca-0d78-4050-b94b-ad0d19348b09",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Test Utils"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"id": "33f9c78d-98f5-4d95-9ed0-e0c1961dc7b6",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "Error",
|
||||
"evalue": "duplicate definitions with name `with_next`",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[31m[E0592] Error:\u001b[0m duplicate definitions with name `with_next`",
|
||||
" \u001b[38;5;246m╭\u001b[0m\u001b[38;5;246m─\u001b[0m\u001b[38;5;246m[\u001b[0mcommand_17:1:1\u001b[38;5;246m]\u001b[0m",
|
||||
" \u001b[38;5;246m│\u001b[0m",
|
||||
" \u001b[38;5;246m3 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;54mf\u001b[0m\u001b[38;5;54mn\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mw\u001b[0m\u001b[38;5;54mi\u001b[0m\u001b[38;5;54mt\u001b[0m\u001b[38;5;54mh\u001b[0m\u001b[38;5;54m_\u001b[0m\u001b[38;5;54mn\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54mx\u001b[0m\u001b[38;5;54mt\u001b[0m\u001b[38;5;54m(\u001b[0m\u001b[38;5;54mv\u001b[0m\u001b[38;5;54ma\u001b[0m\u001b[38;5;54ml\u001b[0m\u001b[38;5;54m:\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mi\u001b[0m\u001b[38;5;54m3\u001b[0m\u001b[38;5;54m2\u001b[0m\u001b[38;5;54m,\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mn\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54mx\u001b[0m\u001b[38;5;54mt\u001b[0m\u001b[38;5;54m:\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mL\u001b[0m\u001b[38;5;54mi\u001b[0m\u001b[38;5;54ms\u001b[0m\u001b[38;5;54mt\u001b[0m\u001b[38;5;54mN\u001b[0m\u001b[38;5;54mo\u001b[0m\u001b[38;5;54md\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54m)\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54m-\u001b[0m\u001b[38;5;54m>\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mS\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54ml\u001b[0m\u001b[38;5;54mf\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m{\u001b[0m",
|
||||
" \u001b[38;5;246m ·\u001b[0m \u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m┬\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m ",
|
||||
" \u001b[38;5;246m ·\u001b[0m \u001b[38;5;54m╰\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m other definition for `with_next`",
|
||||
"\u001b[38;5;246m───╯\u001b[0m"
|
||||
]
|
||||
},
|
||||
{
|
||||
"ename": "Error",
|
||||
"evalue": "duplicate definitions with name `fromVec`",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[31m[E0592] Error:\u001b[0m duplicate definitions with name `fromVec`",
|
||||
" \u001b[38;5;246m╭\u001b[0m\u001b[38;5;246m─\u001b[0m\u001b[38;5;246m[\u001b[0mcommand_17:1:1\u001b[38;5;246m]\u001b[0m",
|
||||
" \u001b[38;5;246m│\u001b[0m",
|
||||
" \u001b[38;5;246m10 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;54mf\u001b[0m\u001b[38;5;54mn\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mf\u001b[0m\u001b[38;5;54mr\u001b[0m\u001b[38;5;54mo\u001b[0m\u001b[38;5;54mm\u001b[0m\u001b[38;5;54mV\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54mc\u001b[0m\u001b[38;5;54m(\u001b[0m\u001b[38;5;54mv\u001b[0m\u001b[38;5;54m:\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mV\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54mc\u001b[0m\u001b[38;5;54m<\u001b[0m\u001b[38;5;54mi\u001b[0m\u001b[38;5;54m3\u001b[0m\u001b[38;5;54m2\u001b[0m\u001b[38;5;54m>\u001b[0m\u001b[38;5;54m)\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54m-\u001b[0m\u001b[38;5;54m>\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mO\u001b[0m\u001b[38;5;54mp\u001b[0m\u001b[38;5;54mt\u001b[0m\u001b[38;5;54mi\u001b[0m\u001b[38;5;54mo\u001b[0m\u001b[38;5;54mn\u001b[0m\u001b[38;5;54m<\u001b[0m\u001b[38;5;54mB\u001b[0m\u001b[38;5;54mo\u001b[0m\u001b[38;5;54mx\u001b[0m\u001b[38;5;54m<\u001b[0m\u001b[38;5;54mS\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54ml\u001b[0m\u001b[38;5;54mf\u001b[0m\u001b[38;5;54m>\u001b[0m\u001b[38;5;54m>\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m{\u001b[0m",
|
||||
" \u001b[38;5;246m ·\u001b[0m \u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m┬\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m ",
|
||||
" \u001b[38;5;246m ·\u001b[0m \u001b[38;5;54m╰\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m other definition for `fromVec`",
|
||||
"\u001b[38;5;246m────╯\u001b[0m"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"impl ListNode {\n",
|
||||
" #[inline]\n",
|
||||
" fn with_next(val: i32, next: ListNode) -> Self {\n",
|
||||
" ListNode {\n",
|
||||
" next: Some(Box::new(next)),\n",
|
||||
" val,\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
" \n",
|
||||
" fn fromVec(v: Vec<i32>) -> Option<Box<Self>> {\n",
|
||||
" v.into_iter()\n",
|
||||
" .rev()\n",
|
||||
" .fold(None, |acc, val| {\n",
|
||||
" match acc {\n",
|
||||
" None => Some(Box::new(ListNode::new(val))),\n",
|
||||
" Some(node) => Some(Box::new(ListNode::with_next(val, *node)))\n",
|
||||
" }\n",
|
||||
" })\n",
|
||||
" }\n",
|
||||
"}\n",
|
||||
"\n",
|
||||
"assert_eq!(\n",
|
||||
" ListNode::fromVec(vec![7, 0, 8]),\n",
|
||||
" Some(Box::new(ListNode::with_next(7, ListNode::with_next(0, ListNode::new(8)))))\n",
|
||||
");"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "69eca992-8fc9-4507-b636-d64b5561be21",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Test Cases"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"id": "30adc3ca-7327-4336-b294-3e8fd3380c01",
|
||||
"metadata": {
|
||||
"tags": []
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"assert_eq!(\n",
|
||||
" add_two_numbers(\n",
|
||||
" ListNode::fromVec(vec![2,4,3]),\n",
|
||||
" ListNode::fromVec(vec![5,6,4])\n",
|
||||
" ),\n",
|
||||
" ListNode::fromVec(vec![7,0,8])\n",
|
||||
");\n",
|
||||
"assert_eq!(\n",
|
||||
" add_two_numbers(\n",
|
||||
" ListNode::fromVec(vec![0]),\n",
|
||||
" ListNode::fromVec(vec![0])\n",
|
||||
" ),\n",
|
||||
" ListNode::fromVec(vec![0])\n",
|
||||
");\n",
|
||||
"\n",
|
||||
"assert_eq!(\n",
|
||||
" add_two_numbers(\n",
|
||||
" ListNode::fromVec(vec![9,9]),\n",
|
||||
" ListNode::fromVec(vec![9])\n",
|
||||
" ),\n",
|
||||
" ListNode::fromVec(vec![8,0,1])\n",
|
||||
");\n",
|
||||
"assert_eq!(\n",
|
||||
" add_two_numbers(\n",
|
||||
" ListNode::fromVec(vec![9,9,9,9,9,9,9]),\n",
|
||||
" ListNode::fromVec(vec![9,9,9,9])\n",
|
||||
" ),\n",
|
||||
" ListNode::fromVec(vec![8,9,9,9,0,0,0,1])\n",
|
||||
");"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "9377916a-51fc-4534-b53e-ec9ac5e3c81e",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "rust-Rust kernel",
|
||||
"language": "rust",
|
||||
"name": "rust-rust"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": "rust",
|
||||
"file_extension": ".rs",
|
||||
"mimetype": "text/rust",
|
||||
"name": "Rust",
|
||||
"pygment_lexer": "rust",
|
||||
"version": ""
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
|
@ -1,89 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "f6a5bb29-6834-41a0-a929-8e035675eb9d",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"predict x = x `rem` 3 == 0 || x `rem` 5 == 0"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "3ce07c68-c750-4d39-b593-dcdb6e22fade",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"filtered = filter predict [1..999]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "52551454-c27c-4ac7-8967-02d9f1704fc8",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[3,5,6,9,10,12,15,18,20,21,24,25,27,30,33,35,36,39,40,42,45,48,50,51,54,55,57,60,63,65,66,69,70,72,75,78,80,81,84,85,87,90,93,95,96,99,100,102,105,108,110,111,114,115,117,120,123,125,126,129,130,132,135,138,140,141,144,145,147,150,153,155,156,159,160,162,165,168,170,171,174,175,177,180,183,185,186,189,190,192,195,198,200,201,204,205,207,210,213,215,216,219,220,222,225,228,230,231,234,235,237,240,243,245,246,249,250,252,255,258,260,261,264,265,267,270,273,275,276,279,280,282,285,288,290,291,294,295,297,300,303,305,306,309,310,312,315,318,320,321,324,325,327,330,333,335,336,339,340,342,345,348,350,351,354,355,357,360,363,365,366,369,370,372,375,378,380,381,384,385,387,390,393,395,396,399,400,402,405,408,410,411,414,415,417,420,423,425,426,429,430,432,435,438,440,441,444,445,447,450,453,455,456,459,460,462,465,468,470,471,474,475,477,480,483,485,486,489,490,492,495,498,500,501,504,505,507,510,513,515,516,519,520,522,525,528,530,531,534,535,537,540,543,545,546,549,550,552,555,558,560,561,564,565,567,570,573,575,576,579,580,582,585,588,590,591,594,595,597,600,603,605,606,609,610,612,615,618,620,621,624,625,627,630,633,635,636,639,640,642,645,648,650,651,654,655,657,660,663,665,666,669,670,672,675,678,680,681,684,685,687,690,693,695,696,699,700,702,705,708,710,711,714,715,717,720,723,725,726,729,730,732,735,738,740,741,744,745,747,750,753,755,756,759,760,762,765,768,770,771,774,775,777,780,783,785,786,789,790,792,795,798,800,801,804,805,807,810,813,815,816,819,820,822,825,828,830,831,834,835,837,840,843,845,846,849,850,852,855,858,860,861,864,865,867,870,873,875,876,879,880,882,885,888,890,891,894,895,897,900,903,905,906,909,910,912,915,918,920,921,924,925,927,930,933,935,936,939,940,942,945,948,950,951,954,955,957,960,963,965,966,969,970,972,975,978,980,981,984,985,987,990,993,995,996,999]"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"-- filtered"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "1437aa9e-c83a-4bbb-a5c3-be4415f8b3f2",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"233168"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sum filtered"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "311e677a-7185-4718-a839-ace350ecb502",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Haskell - haskell",
|
||||
"language": "haskell",
|
||||
"name": "ihaskell_haskell"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": "ihaskell",
|
||||
"file_extension": ".hs",
|
||||
"mimetype": "text/x-haskell",
|
||||
"name": "haskell",
|
||||
"pygments_lexer": "Haskell",
|
||||
"version": "9.0.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
|
@ -1,169 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "bff727d0-70bf-42cf-a12c-ee5823037315",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
":l <nixpkgs>\n",
|
||||
"Added 17051 variables.\u001b[0m\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
":l <nixpkgs>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "1de5854d-a1dd-4c88-bad5-571c0e4dab8e",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# fake rem because nix doesn't support this operation"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "8773113c-494f-45d8-a8c1-4f1ac8b60266",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"rem = a: b:\n",
|
||||
" let\n",
|
||||
" x = builtins.div a b;\n",
|
||||
" cx = builtins.ceil x;\n",
|
||||
" in x == cx\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"rem = a: b:\n",
|
||||
" let\n",
|
||||
" x = builtins.div a b;\n",
|
||||
" cx = builtins.ceil x;\n",
|
||||
" in x == cx"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "25c08a9f-b2ef-451a-9791-f0fd75cf6f00",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"predicate = x: (rem x 3.) || (rem x 5.)\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"predicate = x: (rem x 3.) || (rem x 5.)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"id": "754560b4-01c3-401c-a9ab-14fe56a9a80c",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"input = lib.range 1 999\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"input = lib.range 1 999"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"id": "1bbc464a-bedc-4742-ae1d-d06039df53f5",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"filtered = lib.filter predicate input\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"filtered = lib.filter predicate input"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"id": "5a08e129-b2fe-43b5-a22f-bb6a0e69adb6",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"sum = list: lib.foldr builtins.add 0 list\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sum = list: lib.foldr builtins.add 0 list"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"id": "1dcaf52d-5526-4755-9454-d79238b9454f",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"sum filtered\n",
|
||||
"\u001b[36;1m233168\u001b[0m\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sum filtered"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Nix - nix",
|
||||
"language": "Nix",
|
||||
"name": "inix_nix"
|
||||
},
|
||||
"language_info": {
|
||||
"file_extension": ".nix",
|
||||
"mimetype": "text/nix",
|
||||
"name": "nix"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"id": "0f99fcb7-762f-447c-833d-f0e61b01cd3c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"fibs = 0 : 1 : zipWith (+) fibs (tail fibs)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "21cd7022-93ad-44e4-af07-695f2831ca47",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"it looks like the following"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"id": "202121f5-b7d3-4a23-b29b-d0ad679cc5d2",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "e49c7c8c-8f40-4429-91c7-6f8ffd6ec261",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"get first item of each iteration and then drop 3 items from the beginning"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "raw",
|
||||
"id": "b92a154b-99ad-4558-aeb1-635a4ad04570",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"0,2,8,34,144,610,2584"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"id": "9265e55e-64e6-4aff-a541-5896a032f66b",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"4613732"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
"output_type": "display_data"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"sum . takeWhile (<= 4000000) . map head . iterate (drop 3) $ fibs"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Haskell - haskell",
|
||||
"language": "haskell",
|
||||
"name": "ihaskell_haskell"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": "ihaskell",
|
||||
"file_extension": ".hs",
|
||||
"mimetype": "text/x-haskell",
|
||||
"name": "haskell",
|
||||
"pygments_lexer": "Haskell",
|
||||
"version": "9.0.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
|
||||
|
||||
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
|
||||
|
||||
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
|
Loading…
Reference in a new issue