├── LICENSE ├── README.md ├── about └── index.html ├── assets ├── andre-stackhouse-peace-ambassador.pdf ├── andre-stackhouse-resume.docx ├── andre-stackhouse-resume.pdf ├── andre-stackhouse-resume.png ├── andre-stackhouse.jpg ├── chess-scouter.png ├── chromattis.png ├── code-org-promote.png ├── email-logo.svg ├── environmental-enforcement-watch.png ├── github-logo.svg ├── handshake.ico ├── linkedin-logo.svg ├── ma-y-yo-kart.png ├── overcomp.png ├── public-stackhouse-favicon.png ├── public-stackhouse.png └── stack-overflow-logo.svg ├── data-structures ├── Array_Extensions.js ├── GraphNode.js ├── ListNode.js ├── Promise.js ├── Range.js └── TreeNode.js ├── index.html ├── lib ├── chai │ └── chai.js ├── mocha │ ├── mocha.css │ └── mocha.js └── prism │ ├── prism.css │ └── prism.js ├── object-instances ├── sample_arrays.js ├── sample_graphs.js ├── sample_linked_lists.js ├── sample_strings.js └── sample_trees.js ├── portfolio └── index.html ├── resume └── index.html ├── solutions ├── 01-arrays-and-strings │ ├── 01-02-check_permutation.js │ ├── 01-03-urlify.js │ ├── 01-05-one_away.js │ ├── 01-06-string_compression.js │ ├── 01-07-rotate_matrix.js │ ├── 01-08-zero_matrix.js │ ├── 01-09-string_rotation.js │ ├── corresponding_bracket_index.js │ ├── is_saturated.js │ ├── pretty_print.js │ ├── print_combinations.js │ ├── ransom_note.js │ ├── sum_pairs_to_target.js │ └── validate_brackets.js ├── 02-linked-lists │ ├── 02-01-remove_dupes.js │ ├── 02-02-return_kth_to_last.js │ ├── 02-03-delete_middle_node.js │ ├── 02-04-partition.js │ ├── 02-05-sum_lists.js │ ├── 02-06-palindrome.js │ └── 02-07-intersection.js ├── 03-stacks-and-queues │ └── 03-05-sort_stack.js ├── 04-trees-and-graphs │ ├── 04-01-route_between_nodes.js │ ├── 04-02-minimal_tree.js │ ├── 04-03-list_of_depths.js │ ├── 04-04-check_balanced.js │ ├── 04-08-first_common_ancestor.js │ └── is_binary_search_tree.js ├── 08-recursion-and-dynamic-programming │ ├── 08-01-triple_step.js │ ├── 08-03-magic_index.js │ ├── 08-04-power_set.js │ ├── 08-05-recursive_multiply.js │ └── max_nonadjacent_subset.js └── 10-sorting-and-searching │ ├── 10-01-sorted_merge.js │ ├── 10-03-search_in_rotated_array.js │ └── binary_search.js ├── test ├── check_permutation_test.js ├── corresponding_bracket_index_test.js ├── one_away_test.js ├── sort_stack_test.js └── string_compression_test.js └── ui ├── about.css ├── footer.css ├── header.css ├── home.css ├── main.css ├── portfolio.css ├── resume.css └── ui.js /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Public Stackhouse](https://captainstack.github.io/public-stackhouse/) 2 | Public Stackhouse began as a public repo for my solutions to common coding interview questions. It eventually became my full blown personal website. I am consistently implementing solutions to new questions and adding side projects to the portfolio section. The site is hosted on [GitHub Pages](https://captainstack.github.io/public-stackhouse/). 3 | 4 | Many are problems from [Cracking the Coding Interview](https://www.amazon.com/Cracking-Coding-Interview-Programming-Questions/dp/0984782850/ref=sr_1_1?ie=UTF8&qid=1487198960&sr=8-1&keywords=cracking+the+coding+interview), but I've also included problems my friends have thrown at me for practice, and problems I've been asked to implement in real-world interviews. These solutions are all written in JavaScript (with all the nice ES2015 features!) for now. Please feel free to use the problems and solutions for your own purposes. 5 | -------------------------------------------------------------------------------- /about/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | About | Public Stackhouse 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 | Public Stack<House> 17 | 25 |
26 |
27 |
28 |

About Andre Stackhouse

29 |
30 |
31 |

Education

32 |

University of Washington Seattle, 2014

33 |

BS in Informatics: Human-Computer Interaction (HCI)

34 |

Full Stack Developer

35 |

I've been programming for over 10 years and have worked professionally as a full stack software engineer specializing in web technology since graduating college in 2014.

36 |

Some companies I've worked at include Microsoft, education nonprofit Code.org, casual game company Zhurosoft, and sports software company Fencing Time. I program up and down the stack in frameworks including Ruby on Rails, C#/ASP.NET, Express, and Angular. Some of my professional work includes an email-HTML templating system, internal automation tools, and data front-ends.

37 |

Contact

38 | 45 |

Media coverage

46 |
    47 |
  • https://www.cnbc.com/2020/02/29/super-tuesday-2020-bernie-and-bloomberg-battle-over-big-tech.html
  • 48 |
  • https://www.latimes.com/california/story/2022-02-25/demise-of-single-payer-healthcare-in-california-trips-up-efforts-in-other-states
  • 49 |
  • https://www.realchangenews.org/news/2021/07/28/medicare-all-gaining-grassroots-traction
  • 50 |
  • https://stateofreform.com/featured/2022/05/proposition-would-establish-universal-health-care-insurance-plan/
  • 51 |
  • https://mynorthwest.com/3593173/campaign-for-wa-state-universal-healthcare-gains-momentum/
  • 52 |
  • https://www.thestranger.com/news/2022/12/13/78765296/seattle-city-council-endorses-whole-washingtons-initiative-for-universal-healthcare
  • 53 |
  • https://www.realchangenews.org/news/2023/01/11/advocates-push-washington-adopt-universal-health-care-2023#.Y8BjLCZpbEM.twitter
  • 54 |
  • https://southseattleemerald.com/2023/01/24/seattle-city-council-supports-universal-health-care-but-whats-next/
  • 55 |
56 |
57 |
58 | 59 |
60 |
61 | 62 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /assets/andre-stackhouse-peace-ambassador.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CaptainStack/public-stackhouse/daef6b70eb7b33316ca236a1ae0994a1906b9159/assets/andre-stackhouse-peace-ambassador.pdf -------------------------------------------------------------------------------- /assets/andre-stackhouse-resume.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CaptainStack/public-stackhouse/daef6b70eb7b33316ca236a1ae0994a1906b9159/assets/andre-stackhouse-resume.docx -------------------------------------------------------------------------------- /assets/andre-stackhouse-resume.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CaptainStack/public-stackhouse/daef6b70eb7b33316ca236a1ae0994a1906b9159/assets/andre-stackhouse-resume.pdf -------------------------------------------------------------------------------- /assets/andre-stackhouse-resume.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CaptainStack/public-stackhouse/daef6b70eb7b33316ca236a1ae0994a1906b9159/assets/andre-stackhouse-resume.png -------------------------------------------------------------------------------- /assets/andre-stackhouse.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CaptainStack/public-stackhouse/daef6b70eb7b33316ca236a1ae0994a1906b9159/assets/andre-stackhouse.jpg -------------------------------------------------------------------------------- /assets/chess-scouter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CaptainStack/public-stackhouse/daef6b70eb7b33316ca236a1ae0994a1906b9159/assets/chess-scouter.png -------------------------------------------------------------------------------- /assets/chromattis.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CaptainStack/public-stackhouse/daef6b70eb7b33316ca236a1ae0994a1906b9159/assets/chromattis.png -------------------------------------------------------------------------------- /assets/code-org-promote.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CaptainStack/public-stackhouse/daef6b70eb7b33316ca236a1ae0994a1906b9159/assets/code-org-promote.png -------------------------------------------------------------------------------- /assets/email-logo.svg: -------------------------------------------------------------------------------- 1 | email-logo -------------------------------------------------------------------------------- /assets/environmental-enforcement-watch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CaptainStack/public-stackhouse/daef6b70eb7b33316ca236a1ae0994a1906b9159/assets/environmental-enforcement-watch.png -------------------------------------------------------------------------------- /assets/github-logo.svg: -------------------------------------------------------------------------------- 1 | github-logo -------------------------------------------------------------------------------- /assets/handshake.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CaptainStack/public-stackhouse/daef6b70eb7b33316ca236a1ae0994a1906b9159/assets/handshake.ico -------------------------------------------------------------------------------- /assets/linkedin-logo.svg: -------------------------------------------------------------------------------- 1 | linkedin-logo -------------------------------------------------------------------------------- /assets/ma-y-yo-kart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CaptainStack/public-stackhouse/daef6b70eb7b33316ca236a1ae0994a1906b9159/assets/ma-y-yo-kart.png -------------------------------------------------------------------------------- /assets/overcomp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CaptainStack/public-stackhouse/daef6b70eb7b33316ca236a1ae0994a1906b9159/assets/overcomp.png -------------------------------------------------------------------------------- /assets/public-stackhouse-favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CaptainStack/public-stackhouse/daef6b70eb7b33316ca236a1ae0994a1906b9159/assets/public-stackhouse-favicon.png -------------------------------------------------------------------------------- /assets/public-stackhouse.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CaptainStack/public-stackhouse/daef6b70eb7b33316ca236a1ae0994a1906b9159/assets/public-stackhouse.png -------------------------------------------------------------------------------- /assets/stack-overflow-logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 9 | 10 | email-logo 11 | 12 | 13 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /data-structures/Array_Extensions.js: -------------------------------------------------------------------------------- 1 | Array.prototype.peek = function() { return this[this.length - 1] }; 2 | Array.prototype.isEmpty = function() { return this.length < 1 }; 3 | Array.prototype.swap = function(index1, index2) { let temp = this[index1]; this[index1] = this[index2]; this[index2] = temp }; 4 | Array.prototype.printMatrix = function() { 5 | let return_string = ''; 6 | for (row of this) { 7 | return_string += '['; 8 | return_string += row.join(', '); 9 | return_string += "] \n"; 10 | } 11 | console.log(return_string); 12 | } 13 | -------------------------------------------------------------------------------- /data-structures/GraphNode.js: -------------------------------------------------------------------------------- 1 | // Graph Node factory function 2 | const GraphNode = (data) => { 3 | return { 4 | data: data, 5 | children: [], 6 | printBFS: function(target) { 7 | let search_nodes = []; 8 | search_nodes.push(this); 9 | let message = "Target not in graph"; 10 | while (!search_nodes.isEmpty()) { 11 | let current = search_nodes.shift(); 12 | current.visited ? current.visited = current.visited + 1 : current.visited = 1; 13 | console.log(current.data); 14 | if (current === target) { 15 | message = "Target found"; 16 | break; 17 | } 18 | for (child of current.children) { 19 | if (!search_nodes.includes(child)) { 20 | search_nodes.push(child); 21 | } 22 | } 23 | } 24 | console.log(message); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /data-structures/ListNode.js: -------------------------------------------------------------------------------- 1 | // Linked List Node factory function 2 | const ListNode = (data) => { 3 | return { 4 | data: data, 5 | next: null, 6 | appendTail: function(new_node) { 7 | let current = this; 8 | while (current.next) { 9 | current = current.next; 10 | } 11 | current.next = new_node; 12 | }, 13 | toString: function() { 14 | let current = this; 15 | let return_string = ''; 16 | while (current) { 17 | if (current.next) { 18 | return_string += `[${current.data}] -> `; 19 | } else { 20 | return_string += `[${current.data}]`; 21 | } 22 | current = current.next; 23 | } 24 | return return_string; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /data-structures/Promise.js: -------------------------------------------------------------------------------- 1 | // This is a custom implementation of a JavaScript Promise for studying purposes. 2 | 3 | const Promise = { 4 | defer: () => { 5 | let errored = false; 6 | let resolved = false; 7 | let completed = false; 8 | let payload; 9 | 10 | return { 11 | resolve: data => { 12 | resolved = true; 13 | payload = data; 14 | }, 15 | reject: error => { 16 | errored = true; 17 | payload = error; 18 | }, 19 | promise: { 20 | then: function(on_success) { 21 | let interval = setInterval(function() { 22 | if (resolved && !completed) { 23 | on_success(payload); 24 | clearInterval(interval); 25 | completed = true; 26 | return this; 27 | } 28 | }.bind(this), 1000); 29 | return this; 30 | }, 31 | catch: on_fail => { 32 | let interval = setInterval(() => { 33 | if (errored && !completed) { 34 | on_fail(payload); 35 | completed = true; 36 | clearInterval(interval); 37 | } 38 | }, 1000); 39 | } 40 | } 41 | } 42 | } 43 | } 44 | 45 | const getCookie = numCookies => { 46 | const def = Promise.defer(); 47 | setTimeout(() => { 48 | if (numCookies > 0) { 49 | def.resolve(`You have ${numCookies} cookie(s)`); 50 | } else { 51 | def.reject('You have no cookies.'); 52 | } 53 | }, 1000); 54 | return def.promise; 55 | } 56 | 57 | // getCookie(4) 58 | // .then(cookie => console.log(cookie)) 59 | // .catch(error => console.log(error)); 60 | -------------------------------------------------------------------------------- /data-structures/Range.js: -------------------------------------------------------------------------------- 1 | const range = (start, end) => { 2 | return { 3 | start: start, 4 | end: end 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /data-structures/TreeNode.js: -------------------------------------------------------------------------------- 1 | // Tree Node factory function 2 | const TreeNode = (data) => { 3 | return { 4 | data: data, 5 | left: null, 6 | right: null, 7 | toString: function() { 8 | let lines = []; 9 | let helper = (branch, level) => { 10 | if (!lines[level]) lines[level] = ''; 11 | if (!lines[level + 1]) lines[level + 1] = ''; 12 | lines[level] += branch.data + ' '; 13 | if (branch.left) { 14 | lines[level + 1] += '/ ' 15 | helper(branch.left, level + 2); 16 | } 17 | if (branch.right) { 18 | lines[level + 1] += '\\ '; 19 | helper(branch.right, level + 2) 20 | } 21 | } 22 | helper(this, 0); 23 | for (line of lines) { 24 | console.log(line); 25 | } 26 | }, 27 | printBFS: function(target_node) { 28 | let node_queue = []; 29 | node_queue.push(this); 30 | let feedback_string = "Target not in tree"; 31 | while (!node_queue.isEmpty()) { 32 | let current_node = node_queue.shift(); 33 | console.log(current_node.data); 34 | if (current_node === target_node) { 35 | feedback_string = "Target found"; 36 | break; 37 | } 38 | if (current_node.left) { 39 | node_queue.push(current_node.left); 40 | } 41 | if (current_node.right) { 42 | node_queue.push(current_node.right); 43 | } 44 | } 45 | console.log(feedback_string); 46 | }, 47 | depthFirstSearch: function(target_node) { 48 | if (this === target_node) { 49 | return this; 50 | } else { 51 | let left_search = this.left ? this.left.depthFirstSearch(target_node) : -1; 52 | let right_search = this.right ? this.right.depthFirstSearch(target_node) : -1; 53 | if (left_search !== -1) { 54 | return left_search; 55 | } else if (right_search !== -1) { 56 | return right_search; 57 | } else { 58 | return -1; 59 | } 60 | } 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Public Stackhouse 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 |
23 | Public Stack<House> 24 | 32 |
33 |
34 |
35 | 36 |

Employment Status: Available

37 |

JavaScript functions are loaded into this page and can be run from the browser console.

38 |
39 |
40 |

Whiteboard

41 | 89 |
solutions_will_be_displayed_here(input) {
 90 |   // Problems will be specified in comments.
 91 | }
92 |
93 |
94 |

Mocha Test Output

95 | 96 |
97 |
98 |
99 | 100 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | -------------------------------------------------------------------------------- /lib/mocha/mocha.css: -------------------------------------------------------------------------------- 1 | @charset "utf-8"; 2 | 3 | #mocha { 4 | font: 20px/1.5 "Helvetica Neue", Helvetica, Arial, sans-serif; 5 | margin: 0px; 6 | } 7 | 8 | #mocha ul, 9 | #mocha li { 10 | margin: 0; 11 | padding: 0; 12 | } 13 | 14 | #mocha ul { 15 | list-style: none; 16 | } 17 | 18 | #mocha h1, 19 | #mocha h2 { 20 | margin: 0; 21 | } 22 | 23 | #mocha h1 { 24 | margin-top: 15px; 25 | font-size: 12pt; 26 | font-weight: 200; 27 | } 28 | 29 | #mocha h1 a { 30 | text-decoration: none; 31 | color: inherit; 32 | } 33 | 34 | #mocha h1 a:hover { 35 | text-decoration: underline; 36 | } 37 | 38 | #mocha .suite .suite h1 { 39 | margin-top: 0; 40 | font-size: .8em; 41 | } 42 | 43 | #mocha .hidden { 44 | display: none; 45 | } 46 | 47 | #mocha h2 { 48 | font-size: 12px; 49 | font-weight: normal; 50 | cursor: pointer; 51 | } 52 | 53 | #mocha .suite { 54 | margin-left: 15px; 55 | } 56 | 57 | #mocha .test { 58 | margin-left: 15px; 59 | overflow: hidden; 60 | } 61 | 62 | #mocha .test.pending:hover h2::after { 63 | content: '(pending)'; 64 | font-family: arial, sans-serif; 65 | } 66 | 67 | #mocha .test.pass.medium .duration { 68 | background: #c09853; 69 | } 70 | 71 | #mocha .test.pass.slow .duration { 72 | background: #b94a48; 73 | } 74 | 75 | #mocha .test.pass::before { 76 | content: '✓'; 77 | font-size: 12px; 78 | display: block; 79 | float: left; 80 | margin-right: 5px; 81 | color: #00d6b2; 82 | } 83 | 84 | #mocha .test.pass .duration { 85 | font-size: 9px; 86 | margin-left: 5px; 87 | padding: 2px 5px; 88 | color: #fff; 89 | -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.2); 90 | -moz-box-shadow: inset 0 1px 1px rgba(0,0,0,.2); 91 | box-shadow: inset 0 1px 1px rgba(0,0,0,.2); 92 | -webkit-border-radius: 5px; 93 | -moz-border-radius: 5px; 94 | -ms-border-radius: 5px; 95 | -o-border-radius: 5px; 96 | border-radius: 5px; 97 | } 98 | 99 | #mocha .test.pass.fast .duration { 100 | display: none; 101 | } 102 | 103 | #mocha .test.pending { 104 | color: #0b97c4; 105 | } 106 | 107 | #mocha .test.pending::before { 108 | content: '◦'; 109 | color: #0b97c4; 110 | } 111 | 112 | #mocha .test.fail { 113 | color: #c00; 114 | } 115 | 116 | #mocha .test.fail pre { 117 | color: black; 118 | } 119 | 120 | #mocha .test.fail::before { 121 | content: '✖'; 122 | font-size: 12px; 123 | display: block; 124 | float: left; 125 | margin-right: 5px; 126 | color: #c00; 127 | } 128 | 129 | #mocha .test pre.error { 130 | color: #c00; 131 | max-height: 300px; 132 | overflow: auto; 133 | } 134 | 135 | /** 136 | * (1): approximate for browsers not supporting calc 137 | * (2): 42 = 2*15 + 2*10 + 2*1 (padding + margin + border) 138 | * ^^ seriously 139 | */ 140 | #mocha .test pre { 141 | display: block; 142 | float: left; 143 | clear: left; 144 | font: 12px/1.5 monaco, monospace; 145 | margin: 5px; 146 | padding: 15px; 147 | border: 1px solid #eee; 148 | max-width: 85%; /*(1)*/ 149 | max-width: calc(100% - 42px); /*(2)*/ 150 | word-wrap: break-word; 151 | white-space: normal; 152 | border-bottom-color: #ddd; 153 | -webkit-border-radius: 3px; 154 | -webkit-box-shadow: 0 1px 3px #eee; 155 | -moz-border-radius: 3px; 156 | -moz-box-shadow: 0 1px 3px #eee; 157 | border-radius: 3px; 158 | } 159 | 160 | #mocha .test h2 { 161 | position: relative; 162 | } 163 | 164 | #mocha .test a.replay { 165 | position: absolute; 166 | top: 3px; 167 | right: 0; 168 | text-decoration: none; 169 | vertical-align: middle; 170 | display: block; 171 | width: 15px; 172 | height: 15px; 173 | line-height: 15px; 174 | text-align: center; 175 | background: #eee; 176 | font-size: 15px; 177 | -moz-border-radius: 15px; 178 | border-radius: 15px; 179 | -webkit-transition: opacity 200ms; 180 | -moz-transition: opacity 200ms; 181 | transition: opacity 200ms; 182 | opacity: 0.3; 183 | color: #888; 184 | } 185 | 186 | #mocha .test:hover a.replay { 187 | opacity: 1; 188 | } 189 | 190 | #mocha-report.pass .test.fail { 191 | display: none; 192 | } 193 | 194 | #mocha-report.fail .test.pass { 195 | display: none; 196 | } 197 | 198 | #mocha-report.pending .test.pass, 199 | #mocha-report.pending .test.fail { 200 | display: none; 201 | } 202 | #mocha-report.pending .test.pass.pending { 203 | display: block; 204 | } 205 | 206 | #mocha-error { 207 | color: #c00; 208 | font-size: 1.5em; 209 | font-weight: 100; 210 | letter-spacing: 1px; 211 | } 212 | 213 | #mocha-stats { 214 | position: fixed; 215 | bottom: 0px; 216 | right: 10px; 217 | font-size: 12px; 218 | margin: 0; 219 | color: #888; 220 | z-index: 1; 221 | } 222 | 223 | #mocha-stats .progress { 224 | float: right; 225 | padding-top: 0; 226 | } 227 | 228 | #mocha-stats em { 229 | color: black; 230 | } 231 | 232 | #mocha-stats a { 233 | text-decoration: none; 234 | color: inherit; 235 | } 236 | 237 | #mocha-stats a:hover { 238 | border-bottom: 1px solid #eee; 239 | } 240 | 241 | #mocha-stats li { 242 | display: inline-block; 243 | margin: 0 5px; 244 | list-style: none; 245 | padding-top: 11px; 246 | } 247 | 248 | #mocha-stats canvas { 249 | width: 40px; 250 | height: 40px; 251 | } 252 | 253 | #mocha code .comment { color: #ddd; } 254 | #mocha code .init { color: #2f6fad; } 255 | #mocha code .string { color: #5890ad; } 256 | #mocha code .keyword { color: #8a6343; } 257 | #mocha code .number { color: #2f6fad; } 258 | 259 | @media screen and (max-device-width: 480px) { 260 | #mocha { 261 | margin: 60px 0px; 262 | } 263 | 264 | #mocha #stats { 265 | position: absolute; 266 | } 267 | } 268 | -------------------------------------------------------------------------------- /lib/prism/prism.css: -------------------------------------------------------------------------------- 1 | /* http://prismjs.com/download.html?themes=prism-okaidia&languages=clike+javascript&plugins=line-highlight+line-numbers */ 2 | /** 3 | * okaidia theme for JavaScript, CSS and HTML 4 | * Loosely based on Monokai textmate theme by http://www.monokai.nl/ 5 | * @author ocodia 6 | */ 7 | 8 | code[class*="language-"], 9 | pre[class*="language-"] { 10 | color: #f8f8f2; 11 | background: none; 12 | text-shadow: 0 1px rgba(0, 0, 0, 0.3); 13 | font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; 14 | text-align: left; 15 | white-space: pre; 16 | word-spacing: normal; 17 | word-break: normal; 18 | word-wrap: normal; 19 | line-height: 1.5; 20 | 21 | -moz-tab-size: 4; 22 | -o-tab-size: 4; 23 | tab-size: 4; 24 | 25 | -webkit-hyphens: none; 26 | -moz-hyphens: none; 27 | -ms-hyphens: none; 28 | hyphens: none; 29 | } 30 | 31 | /* Code blocks */ 32 | pre[class*="language-"] { 33 | padding: 1em; 34 | margin: .5em 0; 35 | overflow: auto; 36 | border-radius: 0.3em; 37 | } 38 | 39 | :not(pre) > code[class*="language-"], 40 | pre[class*="language-"] { 41 | background: #272822; 42 | } 43 | 44 | /* Inline code */ 45 | :not(pre) > code[class*="language-"] { 46 | padding: .1em; 47 | border-radius: .3em; 48 | white-space: normal; 49 | } 50 | 51 | .token.comment, 52 | .token.prolog, 53 | .token.doctype, 54 | .token.cdata { 55 | color: slategray; 56 | } 57 | 58 | .token.punctuation { 59 | color: #f8f8f2; 60 | } 61 | 62 | .namespace { 63 | opacity: .7; 64 | } 65 | 66 | .token.property, 67 | .token.tag, 68 | .token.constant, 69 | .token.symbol, 70 | .token.deleted { 71 | color: #f92672; 72 | } 73 | 74 | .token.boolean, 75 | .token.number { 76 | color: #ae81ff; 77 | } 78 | 79 | .token.selector, 80 | .token.attr-name, 81 | .token.string, 82 | .token.char, 83 | .token.builtin, 84 | .token.inserted { 85 | color: #a6e22e; 86 | } 87 | 88 | .token.operator, 89 | .token.entity, 90 | .token.url, 91 | .language-css .token.string, 92 | .style .token.string, 93 | .token.variable { 94 | color: #f8f8f2; 95 | } 96 | 97 | .token.atrule, 98 | .token.attr-value, 99 | .token.function { 100 | color: #e6db74; 101 | } 102 | 103 | .token.keyword { 104 | color: #66d9ef; 105 | } 106 | 107 | .token.regex, 108 | .token.important { 109 | color: #fd971f; 110 | } 111 | 112 | .token.important, 113 | .token.bold { 114 | font-weight: bold; 115 | } 116 | .token.italic { 117 | font-style: italic; 118 | } 119 | 120 | .token.entity { 121 | cursor: help; 122 | } 123 | 124 | pre[data-line] { 125 | position: relative; 126 | padding: 1em 0 1em 3em; 127 | } 128 | 129 | .line-highlight { 130 | position: absolute; 131 | left: 0; 132 | right: 0; 133 | padding: inherit 0; 134 | margin-top: 1em; /* Same as .prism’s padding-top */ 135 | 136 | background: hsla(24, 20%, 50%,.08); 137 | background: linear-gradient(to right, hsla(24, 20%, 50%,.1) 70%, hsla(24, 20%, 50%,0)); 138 | 139 | pointer-events: none; 140 | 141 | line-height: inherit; 142 | white-space: pre; 143 | } 144 | 145 | .line-highlight:before, 146 | .line-highlight[data-end]:after { 147 | content: attr(data-start); 148 | position: absolute; 149 | top: .4em; 150 | left: .6em; 151 | min-width: 1em; 152 | padding: 0 .5em; 153 | background-color: hsla(24, 20%, 50%,.4); 154 | color: hsl(24, 20%, 95%); 155 | font: bold 65%/1.5 sans-serif; 156 | text-align: center; 157 | vertical-align: .3em; 158 | border-radius: 999px; 159 | text-shadow: none; 160 | box-shadow: 0 1px white; 161 | } 162 | 163 | .line-highlight[data-end]:after { 164 | content: attr(data-end); 165 | top: auto; 166 | bottom: .4em; 167 | } 168 | 169 | pre.line-numbers { 170 | position: relative; 171 | padding-left: 3.8em; 172 | counter-reset: linenumber; 173 | } 174 | 175 | pre.line-numbers > code { 176 | position: relative; 177 | } 178 | 179 | .line-numbers .line-numbers-rows { 180 | position: absolute; 181 | pointer-events: none; 182 | top: 0; 183 | font-size: 100%; 184 | left: -3.8em; 185 | width: 3em; /* works for line-numbers below 1000 lines */ 186 | letter-spacing: -1px; 187 | border-right: 1px solid #999; 188 | 189 | -webkit-user-select: none; 190 | -moz-user-select: none; 191 | -ms-user-select: none; 192 | user-select: none; 193 | 194 | } 195 | 196 | .line-numbers-rows > span { 197 | pointer-events: none; 198 | display: block; 199 | counter-increment: linenumber; 200 | } 201 | 202 | .line-numbers-rows > span:before { 203 | content: counter(linenumber); 204 | color: #999; 205 | display: block; 206 | padding-right: 0.8em; 207 | text-align: right; 208 | } 209 | -------------------------------------------------------------------------------- /lib/prism/prism.js: -------------------------------------------------------------------------------- 1 | /* http://prismjs.com/download.html?themes=prism-okaidia&languages=clike+javascript&plugins=line-highlight+line-numbers */ 2 | var _self = (typeof window !== 'undefined') 3 | ? window // if in browser 4 | : ( 5 | (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) 6 | ? self // if in worker 7 | : {} // if in node js 8 | ); 9 | 10 | /** 11 | * Prism: Lightweight, robust, elegant syntax highlighting 12 | * MIT license http://www.opensource.org/licenses/mit-license.php/ 13 | * @author Lea Verou http://lea.verou.me 14 | */ 15 | 16 | var Prism = (function(){ 17 | 18 | // Private helper vars 19 | var lang = /\blang(?:uage)?-(\w+)\b/i; 20 | var uniqueId = 0; 21 | 22 | var _ = _self.Prism = { 23 | manual: _self.Prism && _self.Prism.manual, 24 | util: { 25 | encode: function (tokens) { 26 | if (tokens instanceof Token) { 27 | return new Token(tokens.type, _.util.encode(tokens.content), tokens.alias); 28 | } else if (_.util.type(tokens) === 'Array') { 29 | return tokens.map(_.util.encode); 30 | } else { 31 | return tokens.replace(/&/g, '&').replace(/ text.length) { 302 | // Something went terribly wrong, ABORT, ABORT! 303 | break tokenloop; 304 | } 305 | 306 | if (str instanceof Token) { 307 | continue; 308 | } 309 | 310 | pattern.lastIndex = 0; 311 | 312 | var match = pattern.exec(str), 313 | delNum = 1; 314 | 315 | // Greedy patterns can override/remove up to two previously matched tokens 316 | if (!match && greedy && i != strarr.length - 1) { 317 | pattern.lastIndex = pos; 318 | match = pattern.exec(text); 319 | if (!match) { 320 | break; 321 | } 322 | 323 | var from = match.index + (lookbehind ? match[1].length : 0), 324 | to = match.index + match[0].length, 325 | k = i, 326 | p = pos; 327 | 328 | for (var len = strarr.length; k < len && p < to; ++k) { 329 | p += strarr[k].length; 330 | // Move the index i to the element in strarr that is closest to from 331 | if (from >= p) { 332 | ++i; 333 | pos = p; 334 | } 335 | } 336 | 337 | /* 338 | * If strarr[i] is a Token, then the match starts inside another Token, which is invalid 339 | * If strarr[k - 1] is greedy we are in conflict with another greedy pattern 340 | */ 341 | if (strarr[i] instanceof Token || strarr[k - 1].greedy) { 342 | continue; 343 | } 344 | 345 | // Number of tokens to delete and replace with the new match 346 | delNum = k - i; 347 | str = text.slice(pos, p); 348 | match.index -= pos; 349 | } 350 | 351 | if (!match) { 352 | continue; 353 | } 354 | 355 | if(lookbehind) { 356 | lookbehindLength = match[1].length; 357 | } 358 | 359 | var from = match.index + lookbehindLength, 360 | match = match[0].slice(lookbehindLength), 361 | to = from + match.length, 362 | before = str.slice(0, from), 363 | after = str.slice(to); 364 | 365 | var args = [i, delNum]; 366 | 367 | if (before) { 368 | args.push(before); 369 | } 370 | 371 | var wrapped = new Token(token, inside? _.tokenize(match, inside) : match, alias, match, greedy); 372 | 373 | args.push(wrapped); 374 | 375 | if (after) { 376 | args.push(after); 377 | } 378 | 379 | Array.prototype.splice.apply(strarr, args); 380 | } 381 | } 382 | } 383 | 384 | return strarr; 385 | }, 386 | 387 | hooks: { 388 | all: {}, 389 | 390 | add: function (name, callback) { 391 | var hooks = _.hooks.all; 392 | 393 | hooks[name] = hooks[name] || []; 394 | 395 | hooks[name].push(callback); 396 | }, 397 | 398 | run: function (name, env) { 399 | var callbacks = _.hooks.all[name]; 400 | 401 | if (!callbacks || !callbacks.length) { 402 | return; 403 | } 404 | 405 | for (var i=0, callback; callback = callbacks[i++];) { 406 | callback(env); 407 | } 408 | } 409 | } 410 | }; 411 | 412 | var Token = _.Token = function(type, content, alias, matchedStr, greedy) { 413 | this.type = type; 414 | this.content = content; 415 | this.alias = alias; 416 | // Copy of the full string this token was created from 417 | this.length = (matchedStr || "").length|0; 418 | this.greedy = !!greedy; 419 | }; 420 | 421 | Token.stringify = function(o, language, parent) { 422 | if (typeof o == 'string') { 423 | return o; 424 | } 425 | 426 | if (_.util.type(o) === 'Array') { 427 | return o.map(function(element) { 428 | return Token.stringify(element, language, o); 429 | }).join(''); 430 | } 431 | 432 | var env = { 433 | type: o.type, 434 | content: Token.stringify(o.content, language, parent), 435 | tag: 'span', 436 | classes: ['token', o.type], 437 | attributes: {}, 438 | language: language, 439 | parent: parent 440 | }; 441 | 442 | if (env.type == 'comment') { 443 | env.attributes['spellcheck'] = 'true'; 444 | } 445 | 446 | if (o.alias) { 447 | var aliases = _.util.type(o.alias) === 'Array' ? o.alias : [o.alias]; 448 | Array.prototype.push.apply(env.classes, aliases); 449 | } 450 | 451 | _.hooks.run('wrap', env); 452 | 453 | var attributes = Object.keys(env.attributes).map(function(name) { 454 | return name + '="' + (env.attributes[name] || '').replace(/"/g, '"') + '"'; 455 | }).join(' '); 456 | 457 | return '<' + env.tag + ' class="' + env.classes.join(' ') + '"' + (attributes ? ' ' + attributes : '') + '>' + env.content + ''; 458 | 459 | }; 460 | 461 | if (!_self.document) { 462 | if (!_self.addEventListener) { 463 | // in Node.js 464 | return _self.Prism; 465 | } 466 | // In worker 467 | _self.addEventListener('message', function(evt) { 468 | var message = JSON.parse(evt.data), 469 | lang = message.language, 470 | code = message.code, 471 | immediateClose = message.immediateClose; 472 | 473 | _self.postMessage(_.highlight(code, _.languages[lang], lang)); 474 | if (immediateClose) { 475 | _self.close(); 476 | } 477 | }, false); 478 | 479 | return _self.Prism; 480 | } 481 | 482 | //Get current script and highlight 483 | var script = document.currentScript || [].slice.call(document.getElementsByTagName("script")).pop(); 484 | 485 | if (script) { 486 | _.filename = script.src; 487 | 488 | if (document.addEventListener && !_.manual && !script.hasAttribute('data-manual')) { 489 | if(document.readyState !== "loading") { 490 | if (window.requestAnimationFrame) { 491 | window.requestAnimationFrame(_.highlightAll); 492 | } else { 493 | window.setTimeout(_.highlightAll, 16); 494 | } 495 | } 496 | else { 497 | document.addEventListener('DOMContentLoaded', _.highlightAll); 498 | } 499 | } 500 | } 501 | 502 | return _self.Prism; 503 | 504 | })(); 505 | 506 | if (typeof module !== 'undefined' && module.exports) { 507 | module.exports = Prism; 508 | } 509 | 510 | // hack for components to work correctly in node.js 511 | if (typeof global !== 'undefined') { 512 | global.Prism = Prism; 513 | } 514 | ; 515 | Prism.languages.clike = { 516 | 'comment': [ 517 | { 518 | pattern: /(^|[^\\])\/\*[\w\W]*?\*\//, 519 | lookbehind: true 520 | }, 521 | { 522 | pattern: /(^|[^\\:])\/\/.*/, 523 | lookbehind: true 524 | } 525 | ], 526 | 'string': { 527 | pattern: /(["'])(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/, 528 | greedy: true 529 | }, 530 | 'class-name': { 531 | pattern: /((?:\b(?:class|interface|extends|implements|trait|instanceof|new)\s+)|(?:catch\s+\())[a-z0-9_\.\\]+/i, 532 | lookbehind: true, 533 | inside: { 534 | punctuation: /(\.|\\)/ 535 | } 536 | }, 537 | 'keyword': /\b(if|else|while|do|for|return|in|instanceof|function|new|try|throw|catch|finally|null|break|continue)\b/, 538 | 'boolean': /\b(true|false)\b/, 539 | 'function': /[a-z0-9_]+(?=\()/i, 540 | 'number': /\b-?(?:0x[\da-f]+|\d*\.?\d+(?:e[+-]?\d+)?)\b/i, 541 | 'operator': /--?|\+\+?|!=?=?|<=?|>=?|==?=?|&&?|\|\|?|\?|\*|\/|~|\^|%/, 542 | 'punctuation': /[{}[\];(),.:]/ 543 | }; 544 | 545 | Prism.languages.javascript = Prism.languages.extend('clike', { 546 | 'keyword': /\b(as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/, 547 | 'number': /\b-?(0x[\dA-Fa-f]+|0b[01]+|0o[0-7]+|\d*\.?\d+([Ee][+-]?\d+)?|NaN|Infinity)\b/, 548 | // Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444) 549 | 'function': /[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*(?=\()/i, 550 | 'operator': /--?|\+\+?|!=?=?|<=?|>=?|==?=?|&&?|\|\|?|\?|\*\*?|\/|~|\^|%|\.{3}/ 551 | }); 552 | 553 | Prism.languages.insertBefore('javascript', 'keyword', { 554 | 'regex': { 555 | pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\\\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})]))/, 556 | lookbehind: true, 557 | greedy: true 558 | } 559 | }); 560 | 561 | Prism.languages.insertBefore('javascript', 'string', { 562 | 'template-string': { 563 | pattern: /`(?:\\\\|\\?[^\\])*?`/, 564 | greedy: true, 565 | inside: { 566 | 'interpolation': { 567 | pattern: /\$\{[^}]+\}/, 568 | inside: { 569 | 'interpolation-punctuation': { 570 | pattern: /^\$\{|\}$/, 571 | alias: 'punctuation' 572 | }, 573 | rest: Prism.languages.javascript 574 | } 575 | }, 576 | 'string': /[\s\S]+/ 577 | } 578 | } 579 | }); 580 | 581 | if (Prism.languages.markup) { 582 | Prism.languages.insertBefore('markup', 'tag', { 583 | 'script': { 584 | pattern: /()[\w\W]*?(?=<\/script>)/i, 585 | lookbehind: true, 586 | inside: Prism.languages.javascript, 587 | alias: 'language-javascript' 588 | } 589 | }); 590 | } 591 | 592 | Prism.languages.js = Prism.languages.javascript; 593 | (function(){ 594 | 595 | if (typeof self === 'undefined' || !self.Prism || !self.document || !document.querySelector) { 596 | return; 597 | } 598 | 599 | function $$(expr, con) { 600 | return Array.prototype.slice.call((con || document).querySelectorAll(expr)); 601 | } 602 | 603 | function hasClass(element, className) { 604 | className = " " + className + " "; 605 | return (" " + element.className + " ").replace(/[\n\t]/g, " ").indexOf(className) > -1 606 | } 607 | 608 | // Some browsers round the line-height, others don't. 609 | // We need to test for it to position the elements properly. 610 | var isLineHeightRounded = (function() { 611 | var res; 612 | return function() { 613 | if(typeof res === 'undefined') { 614 | var d = document.createElement('div'); 615 | d.style.fontSize = '13px'; 616 | d.style.lineHeight = '1.5'; 617 | d.style.padding = 0; 618 | d.style.border = 0; 619 | d.innerHTML = ' 
 '; 620 | document.body.appendChild(d); 621 | // Browsers that round the line-height should have offsetHeight === 38 622 | // The others should have 39. 623 | res = d.offsetHeight === 38; 624 | document.body.removeChild(d); 625 | } 626 | return res; 627 | } 628 | }()); 629 | 630 | function highlightLines(pre, lines, classes) { 631 | var ranges = lines.replace(/\s+/g, '').split(','), 632 | offset = +pre.getAttribute('data-line-offset') || 0; 633 | 634 | var parseMethod = isLineHeightRounded() ? parseInt : parseFloat; 635 | var lineHeight = parseMethod(getComputedStyle(pre).lineHeight); 636 | 637 | for (var i=0, range; range = ranges[i++];) { 638 | range = range.split('-'); 639 | 640 | var start = +range[0], 641 | end = +range[1] || start; 642 | 643 | var line = document.createElement('div'); 644 | 645 | line.textContent = Array(end - start + 2).join(' \n'); 646 | line.setAttribute('aria-hidden', 'true'); 647 | line.className = (classes || '') + ' line-highlight'; 648 | 649 | //if the line-numbers plugin is enabled, then there is no reason for this plugin to display the line numbers 650 | if(!hasClass(pre, 'line-numbers')) { 651 | line.setAttribute('data-start', start); 652 | 653 | if(end > start) { 654 | line.setAttribute('data-end', end); 655 | } 656 | } 657 | 658 | line.style.top = (start - offset - 1) * lineHeight + 'px'; 659 | 660 | //allow this to play nicely with the line-numbers plugin 661 | if(hasClass(pre, 'line-numbers')) { 662 | //need to attack to pre as when line-numbers is enabled, the code tag is relatively which screws up the positioning 663 | pre.appendChild(line); 664 | } else { 665 | (pre.querySelector('code') || pre).appendChild(line); 666 | } 667 | } 668 | } 669 | 670 | function applyHash() { 671 | var hash = location.hash.slice(1); 672 | 673 | // Remove pre-existing temporary lines 674 | $$('.temporary.line-highlight').forEach(function (line) { 675 | line.parentNode.removeChild(line); 676 | }); 677 | 678 | var range = (hash.match(/\.([\d,-]+)$/) || [,''])[1]; 679 | 680 | if (!range || document.getElementById(hash)) { 681 | return; 682 | } 683 | 684 | var id = hash.slice(0, hash.lastIndexOf('.')), 685 | pre = document.getElementById(id); 686 | 687 | if (!pre) { 688 | return; 689 | } 690 | 691 | if (!pre.hasAttribute('data-line')) { 692 | pre.setAttribute('data-line', ''); 693 | } 694 | 695 | highlightLines(pre, range, 'temporary '); 696 | 697 | document.querySelector('.temporary.line-highlight').scrollIntoView(); 698 | } 699 | 700 | var fakeTimer = 0; // Hack to limit the number of times applyHash() runs 701 | 702 | Prism.hooks.add('before-sanity-check', function(env) { 703 | var pre = env.element.parentNode; 704 | var lines = pre && pre.getAttribute('data-line'); 705 | 706 | if (!pre || !lines || !/pre/i.test(pre.nodeName)) { 707 | return; 708 | } 709 | 710 | /* 711 | * Cleanup for other plugins (e.g. autoloader). 712 | * 713 | * Sometimes blocks are highlighted multiple times. It is necessary 714 | * to cleanup any left-over tags, because the whitespace inside of the
715 | * tags change the content of the tag. 716 | */ 717 | var num = 0; 718 | $$('.line-highlight', pre).forEach(function (line) { 719 | num += line.textContent.length; 720 | line.parentNode.removeChild(line); 721 | }); 722 | 723 | // Remove extra whitespace 724 | if (num && /^( \n)+$/.test(env.code.slice(-num))) { 725 | env.code = env.code.slice(0, -num); 726 | } 727 | }); 728 | 729 | Prism.hooks.add('complete', function(env) { 730 | var pre = env.element.parentNode; 731 | var lines = pre && pre.getAttribute('data-line'); 732 | 733 | if (!pre || !lines || !/pre/i.test(pre.nodeName)) { 734 | return; 735 | } 736 | 737 | clearTimeout(fakeTimer); 738 | 739 | highlightLines(pre, lines); 740 | 741 | fakeTimer = setTimeout(applyHash, 1); 742 | }); 743 | 744 | if(window.addEventListener) { 745 | window.addEventListener('hashchange', applyHash); 746 | } 747 | 748 | })(); 749 | 750 | (function() { 751 | 752 | if (typeof self === 'undefined' || !self.Prism || !self.document) { 753 | return; 754 | } 755 | 756 | Prism.hooks.add('complete', function (env) { 757 | if (!env.code) { 758 | return; 759 | } 760 | 761 | // works only for wrapped inside
 (not inline)
762 | 	var pre = env.element.parentNode;
763 | 	var clsReg = /\s*\bline-numbers\b\s*/;
764 | 	if (
765 | 		!pre || !/pre/i.test(pre.nodeName) ||
766 | 			// Abort only if nor the 
 nor the  have the class
767 | 		(!clsReg.test(pre.className) && !clsReg.test(env.element.className))
768 | 	) {
769 | 		return;
770 | 	}
771 | 
772 | 	if (env.element.querySelector(".line-numbers-rows")) {
773 | 		// Abort if line numbers already exists
774 | 		return;
775 | 	}
776 | 
777 | 	if (clsReg.test(env.element.className)) {
778 | 		// Remove the class "line-numbers" from the 
779 | 		env.element.className = env.element.className.replace(clsReg, '');
780 | 	}
781 | 	if (!clsReg.test(pre.className)) {
782 | 		// Add the class "line-numbers" to the 
783 | 		pre.className += ' line-numbers';
784 | 	}
785 | 
786 | 	var match = env.code.match(/\n(?!$)/g);
787 | 	var linesNum = match ? match.length + 1 : 1;
788 | 	var lineNumbersWrapper;
789 | 
790 | 	var lines = new Array(linesNum + 1);
791 | 	lines = lines.join('');
792 | 
793 | 	lineNumbersWrapper = document.createElement('span');
794 | 	lineNumbersWrapper.setAttribute('aria-hidden', 'true');
795 | 	lineNumbersWrapper.className = 'line-numbers-rows';
796 | 	lineNumbersWrapper.innerHTML = lines;
797 | 
798 | 	if (pre.hasAttribute('data-start')) {
799 | 		pre.style.counterReset = 'linenumber ' + (parseInt(pre.getAttribute('data-start'), 10) - 1);
800 | 	}
801 | 
802 | 	env.element.appendChild(lineNumbersWrapper);
803 | 
804 | });
805 | 
806 | }());
807 | 


--------------------------------------------------------------------------------
/object-instances/sample_arrays.js:
--------------------------------------------------------------------------------
 1 | let basic_list = [1, 2, 3, 4, 5, 6, 7, 8];
 2 | let moderate_list = [3, 4, 5, 7, 9, 14, 15, 16, 17, 18, 20];
 3 | let complex_list = [-3, -1, 0, 1, 2, 4, 99];
 4 | 
 5 | let A = [1, 1, 3, 7, 8, 9, null, null, null, null, null, null];
 6 | let B = [2, 3, 5, 5, 6, 10];
 7 | 
 8 | let character_array = 'Mr John Smith    '.split('');
 9 | 
10 | // Stacks
11 | let mixed_order = [1, 6, 3, 4, 5, 2];
12 | let sorted_order = [10, 9, 8, 7, 6, 5];
13 | let reverse_sorted = [90, 91, 92, 93, 94, 95];
14 | 
15 | // Matricies
16 | let four_by_four = [[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]];
17 | let three_by_three = [[9, 2, 1], [8, 4, 2], [7, 6, 3]];


--------------------------------------------------------------------------------
/object-instances/sample_graphs.js:
--------------------------------------------------------------------------------
 1 | let graph_without_cycles = GraphNode("A");
 2 | let b_node = GraphNode("B");
 3 | let c_node = GraphNode("C");
 4 | let d_node = GraphNode("D");
 5 | b_node.children = [c_node, d_node];
 6 | c_node.children = [d_node];
 7 | graph_without_cycles.children = [b_node, c_node];
 8 | 
 9 | let graph_with_cycle = GraphNode("1");
10 | let two_node = GraphNode("2");
11 | let three_node = GraphNode("3");
12 | let four_node = GraphNode("4");
13 | two_node.children = [three_node];
14 | three_node.children = [four_node];
15 | four_node.children = [two_node];
16 | graph_with_cycle.children = [two_node];
17 | 
18 | let single_node_graph = GraphNode("S");
19 | 


--------------------------------------------------------------------------------
/object-instances/sample_linked_lists.js:
--------------------------------------------------------------------------------
 1 | let list_with_duplicates = ListNode(1);
 2 | list_with_duplicates.appendTail(ListNode(1));
 3 | list_with_duplicates.appendTail(ListNode(2));
 4 | list_with_duplicates.appendTail(ListNode(2));
 5 | list_with_duplicates.appendTail(ListNode(3));
 6 | list_with_duplicates.appendTail(ListNode(3));
 7 | 
 8 | let single_node_list = ListNode(0);
 9 | 
10 | let all_duplicates = ListNode(7);
11 | all_duplicates.appendTail(ListNode(7));
12 | all_duplicates.appendTail(ListNode(7));
13 | all_duplicates.appendTail(ListNode(7));
14 | all_duplicates.appendTail(ListNode(7));
15 | 
16 | let ten_node_list = ListNode(0);
17 | ten_node_list.appendTail(ListNode(1));
18 | ten_node_list.appendTail(ListNode(2));
19 | ten_node_list.appendTail(ListNode(3));
20 | ten_node_list.appendTail(ListNode(4));
21 | ten_node_list.appendTail(ListNode(5));
22 | ten_node_list.appendTail(ListNode(6));
23 | ten_node_list.appendTail(ListNode(7));
24 | ten_node_list.appendTail(ListNode(8));
25 | ten_node_list.appendTail(ListNode(9));
26 | 
27 | let unordered_list = ListNode(5);
28 | unordered_list.appendTail(ListNode(10));
29 | unordered_list.appendTail(ListNode(2));
30 | unordered_list.appendTail(ListNode(6));
31 | unordered_list.appendTail(ListNode(3));
32 | unordered_list.appendTail(ListNode(3));
33 | unordered_list.appendTail(ListNode(7));
34 | 
35 | let reverse_ordered_list = ListNode(10);
36 | reverse_ordered_list.appendTail(ListNode(9));
37 | reverse_ordered_list.appendTail(ListNode(8));
38 | reverse_ordered_list.appendTail(ListNode(7));
39 | reverse_ordered_list.appendTail(ListNode(6));
40 | reverse_ordered_list.appendTail(ListNode(5));
41 | reverse_ordered_list.appendTail(ListNode(4));
42 | reverse_ordered_list.appendTail(ListNode(3));
43 | reverse_ordered_list.appendTail(ListNode(2));
44 | reverse_ordered_list.appendTail(ListNode(1));
45 | reverse_ordered_list.appendTail(ListNode(0));
46 | 
47 | let addend_1 = ListNode(7);
48 | addend_1.appendTail(ListNode(1));
49 | addend_1.appendTail(ListNode(6));
50 | 
51 | let addend_2 = ListNode(5);
52 | addend_2.appendTail(ListNode(9));
53 | addend_2.appendTail(ListNode(2));
54 | 
55 | let addend_3 = ListNode(2);
56 | addend_3.appendTail(ListNode(9));
57 | addend_3.appendTail(ListNode(0));
58 | addend_3.appendTail(ListNode(1));
59 | 
60 | let addend_4 = ListNode(7);
61 | addend_4.appendTail(ListNode(3));
62 | 
63 | let palindrome_list = ListNode('r');
64 | palindrome_list.appendTail(ListNode('a'));
65 | palindrome_list.appendTail(ListNode('c'));
66 | palindrome_list.appendTail(ListNode('e'));
67 | palindrome_list.appendTail(ListNode('c'));
68 | palindrome_list.appendTail(ListNode('a'));
69 | palindrome_list.appendTail(ListNode('r'));
70 | 
71 | let intersecting_list_1 = ListNode('a');
72 | intersecting_list_1.appendTail(ListNode('b'));
73 | let c_link = ListNode('c');
74 | intersecting_list_1.appendTail(c_link);
75 | intersecting_list_1.appendTail(ListNode('d'));
76 | intersecting_list_1.appendTail(ListNode('e'));
77 | 
78 | let intersecting_list_2 = ListNode(1);
79 | intersecting_list_2.appendTail(ListNode(2));
80 | intersecting_list_2.appendTail(ListNode(3));
81 | intersecting_list_2.appendTail(ListNode(4));
82 | intersecting_list_2.appendTail(ListNode(5));
83 | intersecting_list_2.appendTail(c_link);
84 | 


--------------------------------------------------------------------------------
/object-instances/sample_strings.js:
--------------------------------------------------------------------------------
 1 |   let valid_source_code     = '[[abc]][({Andre Stackhouse})]0';
 2 |   //                           012345678901234567890123456789
 3 |   let improper_nesting      = '12([)]444';
 4 |   //                           012345678
 5 |   let wrong_closing_bracket = '[a](3){#$}[)';
 6 |   //                           012345678901
 7 |   let unclosed_bracket      = '[][(){}';
 8 |   //                           0123456
 9 |   let no_braces             = 'abcdefg'; 
10 |   //                           0123456
11 | 
12 | let note = "I have your daughter";
13 | let string_repo = "I have your daughter and her birthday present";
14 | 
15 | 


--------------------------------------------------------------------------------
/object-instances/sample_trees.js:
--------------------------------------------------------------------------------
 1 | // Single node tree
 2 | let single_node = TreeNode(10);
 3 | 
 4 | // Valid BST
 5 | let valid_bst = TreeNode(8);
 6 | valid_bst.left = TreeNode(5);
 7 | valid_bst.right = TreeNode(11);
 8 | 
 9 | valid_bst.left.left = TreeNode(2);
10 | let shallow_left = TreeNode(7);
11 | valid_bst.left.right = shallow_left;
12 | 
13 | valid_bst.left.left.right = TreeNode(4);
14 | let far_left_node = TreeNode(3);
15 | valid_bst.left.left.right.left = far_left_node;
16 | 
17 | valid_bst.right.left = TreeNode(10);
18 | valid_bst.right.right = TreeNode(18);
19 | 
20 | let close_to_bottom = TreeNode(20);
21 | valid_bst.right.right.right = close_to_bottom;
22 | 
23 | // Invalid BST
24 | let invalid_bst = TreeNode(8);
25 | invalid_bst.left = TreeNode(4);
26 | invalid_bst.right = TreeNode(10);
27 | 
28 | invalid_bst.left.left = TreeNode(2);
29 | invalid_bst.left.right = TreeNode(12);
30 | 
31 | invalid_bst.right.right = TreeNode(20);
32 | 


--------------------------------------------------------------------------------
/portfolio/index.html:
--------------------------------------------------------------------------------
  1 | 
  2 | 
  3 | 
  4 |   
  5 |     Portfolio | Public Stackhouse
  6 |     
  7 | 
  8 |     
  9 |     
 10 |     
 11 |     
 12 |     
 13 |   
 14 | 
 15 |   
 16 | 
 17 |     
18 |
19 | Public Stack<House> 20 | 28 |
29 |
30 |
31 | 32 |
33 | 44 | 55 | 56 | 67 | 68 | 79 | 80 | 91 | 92 | 103 | 104 | 115 | 116 |
117 | 118 | 138 | 139 | 140 | 141 | -------------------------------------------------------------------------------- /resume/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Resume | Public Stackhouse 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 | Public Stack<House> 17 | 25 |
26 |
27 |
28 |
29 | 30 | Andre Stackhouse Resume 31 | 32 |
33 | 37 | 38 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /solutions/01-arrays-and-strings/01-02-check_permutation.js: -------------------------------------------------------------------------------- 1 | function check_permutation(word1, word2) { 2 | // Cracking the Coding Interview problem 1.2 3 | // Given two strings, write a method to decide if one is a permutation of the other 4 | 5 | let character_map = new Map(); 6 | for (character of word1) { 7 | let quantity = character_map.get(character); 8 | character_map.set(character, quantity + 1 || 1); 9 | } 10 | for (character of word2) { 11 | let quantity = character_map.get(character); 12 | if (!quantity || quantity < 1) { 13 | return false; 14 | } else { 15 | character_map.set(character, quantity - 1); 16 | } 17 | } 18 | for (value of character_map.values()) { 19 | if (value > 0) { 20 | return false; 21 | } 22 | } 23 | return true; 24 | } 25 | -------------------------------------------------------------------------------- /solutions/01-arrays-and-strings/01-03-urlify.js: -------------------------------------------------------------------------------- 1 | function urlify(character_array, actual_length) { 2 | // Write a method to replace all spaces in a string with '%20'. You may assume that the string has 3 | // sufficient space at the end to hold the additional characters, and that you are given the "true" 4 | // length of the string. Use a character array so that you can perform this operation in place. 5 | // EXAMPLE: (input: "Mr John Smith ", 13) (output: "Mr%20John%20Smith") 6 | 7 | let current_string_index = actual_length - 1; 8 | let swap_index = character_array.length - 1; 9 | 10 | while (current_string_index >= 0) { 11 | if (character_array[current_string_index] !== ' ') { 12 | character_array.swap(current_string_index, swap_index); 13 | current_string_index--; 14 | swap_index--; 15 | } else { 16 | character_array[swap_index] = '0'; 17 | character_array[swap_index - 1] = '2'; 18 | character_array[swap_index - 2] = '%'; 19 | current_string_index--; 20 | swap_index -= 3; 21 | } 22 | } 23 | return character_array; 24 | } 25 | -------------------------------------------------------------------------------- /solutions/01-arrays-and-strings/01-05-one_away.js: -------------------------------------------------------------------------------- 1 | function one_away(s1, s2) { 2 | // Cracking the Coding Interview 1.5 3 | // There are three types of edits that can be performed on strings: insert a character, remove a 4 | // character, or replace a character. Given two strings, write a function to check if they are one 5 | // edit (or zero edits) away. 6 | // EXAMPLE 7 | // pale, ple -> true 8 | // pales, pale -> true 9 | // pale, bale -> true 10 | // pale, bake -> false 11 | 12 | s1_length = s1.length; 13 | s2_length = s2.length; 14 | // If one string is more than a character longer, then one operation cannot make them equivalent. 15 | if (Math.abs(s1_length - s2_length) > 1) { 16 | return false; 17 | } else { 18 | let i = 0; 19 | // Find the index of the first string difference. 20 | while (s1.charAt(i) === s2.charAt(i) && (i < s1_length) && (i < s2_length)) { 21 | i++; 22 | } 23 | // If the strings are the same length, and the strings are identical after i, 24 | // then a single replace edit will make them equivalent. 25 | if (s1_length === s2_length) { 26 | return s2.slice(i + 1, s2_length) === s1.slice(i + 1, s1_length); 27 | } else { 28 | // Remove the character at i from the longer string. 29 | // Remove and insert can be caught with one test. 30 | s1_length > s2_length ? 31 | s1 = s1.slice(0, i) + s1.slice(i + 1, s1_length) : 32 | s2 = s2.slice(0, i) + s2.slice(i + 1, s2_length); 33 | return s1 === s2; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /solutions/01-arrays-and-strings/01-06-string_compression.js: -------------------------------------------------------------------------------- 1 | function string_compression(string) { 2 | // Cracking the Coding Interview 1.6 3 | // Implement a method to perform basic string compression using the counts of repeated characters. 4 | // For example, the string aabcccccaaa would become a2b3c5a3. If the "compressed" string would 5 | // not become smaller than the original string, your method should return the original string. You 6 | // can assume the string has only uppercase and lowercase letters (a-z). 7 | 8 | let current_character = string.charAt(0); 9 | let character_count = 0; 10 | let original_length = string.length; 11 | let new_string = ''; 12 | 13 | for (character of string) { 14 | if (new_string.length >= original_length) { 15 | return string; 16 | } else if (character !== current_character) { 17 | new_string += current_character + character_count; 18 | current_character = character; 19 | character_count = 1; 20 | } else { 21 | character_count += 1; 22 | } 23 | } 24 | new_string += current_character + character_count; 25 | return new_string.length < original_length ? new_string : string; 26 | } 27 | -------------------------------------------------------------------------------- /solutions/01-arrays-and-strings/01-07-rotate_matrix.js: -------------------------------------------------------------------------------- 1 | function rotate_matrix(matrix) { 2 | // Cracking the Coding Interview 1.7 3 | // Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, 4 | // write a method to rotate the image by 90 degrees. Can you do this in place? 5 | 6 | let m_index = matrix.length - 1; 7 | let rotated_matrix = []; 8 | for (let i = 0; i <= m_index; i++) { 9 | rotated_matrix.push([]); 10 | } 11 | 12 | for (let [i, row] of matrix.entries()) { 13 | for (let [j, value] of row.entries()) { 14 | rotated_matrix[m_index - (m_index - j)][m_index - i] = value; 15 | } 16 | } 17 | return rotated_matrix; 18 | } 19 | -------------------------------------------------------------------------------- /solutions/01-arrays-and-strings/01-08-zero_matrix.js: -------------------------------------------------------------------------------- 1 | function zero_matrix(matrix) { 2 | // Cracking the Coding Interview 1.8 3 | // Write an algorithm such that if an element in an MxN matrix is 0, 4 | // its entire row and column are set to 0. 5 | 6 | let zero_rows = []; 7 | let zero_columns = []; 8 | 9 | // Find the row and column of every 0 in the original matrix 10 | for (let [r, row] of matrix.entries()) { 11 | for (let [c, value] of row.entries()) { 12 | if (value === 0) { 13 | zero_rows.push(r); 14 | zero_columns.push(c); 15 | } 16 | } 17 | } 18 | // Iterate through the matrix and replace the appropriate values with 0 19 | for (let [r, row] of matrix.entries()) { 20 | for (let [c, value] of row.entries()) { 21 | if (zero_rows.includes(r) || zero_columns.includes(c)) { 22 | matrix[r][c] = 0; 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /solutions/01-arrays-and-strings/01-09-string_rotation.js: -------------------------------------------------------------------------------- 1 | function string_rotation(s1, s2) { 2 | // Assume you have a method isSubstring which checks if one word is a substring of another. Given two 3 | // strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring 4 | // (e.g., "waterbottle" is a rotation of "erbottlewat"). 5 | 6 | let s2_sub = s2; 7 | let pivot_index = 0; 8 | while (s1.indexOf(s2_sub) === -1 && s2_sub.length > 0) { 9 | s2_sub = s2_sub.slice(1); 10 | pivot_index++; 11 | } 12 | return s1.replace(s2_sub, '') === s2.slice(0, pivot_index); 13 | } 14 | -------------------------------------------------------------------------------- /solutions/01-arrays-and-strings/corresponding_bracket_index.js: -------------------------------------------------------------------------------- 1 | function corresponding_bracket_index(source_code, caret_index) { 2 | // Write a function that takes a string that represents source code and an int that represents a 3 | // caret index in the source code. If the caret index is pointed at a bracket, curly brace, or 4 | // parenthesis, return the index of the corresponding brace. 5 | 6 | // Throw error if caret_index is not pointing to a bracket 7 | if (!'(){}[]'.includes(source_code[caret_index])) { 8 | throw new Error('Invalid caret index. Caret must point to a bracket.'); 9 | } 10 | 11 | // Store the indexes of opening brackets that have not been closed 12 | let active_opening_brackets = []; 13 | let corresponding_index; 14 | 15 | for (let current_index = 0; current_index < source_code.length; current_index++) { 16 | let character = source_code.charAt(current_index); 17 | if (character === '[' || character === '(' || character === '{') { 18 | active_opening_brackets.push({index: current_index, type: character}); 19 | } else if (character === ']' || character === ')' || character === '}') { 20 | let last_active_opening_bracket = active_opening_brackets.pop(); 21 | 22 | // Throw an error if closing brackets are not properly nested within opening ones 23 | // Or if a closing bracket does not have a corresponding opening one. 24 | if (!last_active_opening_bracket|| 25 | character === ']' && last_active_opening_bracket.type != '[' || 26 | character === ')' && last_active_opening_bracket.type != '(' || 27 | character === '}' && last_active_opening_bracket.type != '{') { 28 | throw new Error('Invalid brackets in source code.'); 29 | } 30 | 31 | if (last_active_opening_bracket.index === caret_index) { 32 | // If you reach a closing bracket and the last active opening bracket is at the caret, 33 | // Then the closing bracket you have reached is the corresponding closing bracket 34 | corresponding_index = current_index; 35 | } else if (current_index === caret_index) { 36 | // If the closing bracket is at the caret, 37 | // then the corresponding opening one is the last active opening bracket 38 | corresponding_index = last_active_opening_bracket.index; 39 | } 40 | // If neither conditional is met, deactivate the last active opening bracket by removing 41 | // it from the stack of active opening brackets. Continue to iterate through the source code. 42 | } 43 | } 44 | 45 | // Throw error if any brackets are unclosed 46 | if (!active_opening_brackets.isEmpty()) { 47 | throw new Error('Unclosed brackets in source code.'); 48 | } else { 49 | return corresponding_index; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /solutions/01-arrays-and-strings/is_saturated.js: -------------------------------------------------------------------------------- 1 | function is_saturated(range_array, head, tail) { 2 | // Write a function that takes an array of integer ranges, and an upper and lower bound. 3 | // It returns true if the ranges in the array include every value within the bounds. 4 | // It returnsfalse if any of the following conditions are met: 5 | // 1. The ranges do not cover every value between head and tail. 6 | // 2. Any ranges include values outside of head and tail. 7 | // 3. Any ranges overlap. The ranges are inclusive, but it is okay if they overlap at their edges. 8 | 9 | // EXAMPLES: 10 | // is_saturated([range(0, 5), range(5, 10)], 0, 10) -> true 11 | // is_saturated([range(0, 5), range(6, 10)], 0, 10) -> true 12 | // is_saturated([range(0, 5), range(3, 10)], 0, 10) -> false 13 | // is_saturated([range(0, 11)], 0, 10) -> false 14 | 15 | // Store the number of times a value has been encountered in encountered_values at the index of value. 16 | // E.g. let times_value_encountered = encountered_values[value]; 17 | let encountered_values = []; 18 | 19 | // Iterate through each range and check that its values are within head and tail. 20 | for (let range of range_array) { 21 | if (range.start < head || range.end > tail) { 22 | return false; 23 | } else { 24 | for (let i = range.start; i <= range.end; i++) { 25 | encountered_values[i] ? encountered_values[i] += 1 : encountered_values[i] = 1; 26 | if (encountered_values[i] == 2 && (encountered_values[i - 1] === 2 || 27 | encountered_values[i + 1] === 2)) { 28 | return false; 29 | } 30 | } 31 | } 32 | } 33 | 34 | for (let i = head; i <= tail; i++) { 35 | let quantity = encountered_values[i]; 36 | if (!quantity) { 37 | return false; 38 | } 39 | } 40 | return true; 41 | } 42 | -------------------------------------------------------------------------------- /solutions/01-arrays-and-strings/pretty_print.js: -------------------------------------------------------------------------------- 1 | function pretty_print(string) { 2 | /* Write a function that takes a string of curly braces and letters and prints a reformatted version 3 | in which the brackets sit on their own line and subsequent content is indented one level per bracket 4 | Example input: 5 | test1{test2} 6 | 7 | Would become: 8 | test1 9 | { 10 | test2 11 | } 12 | */ 13 | 14 | let output_string = ''; 15 | let indentation = ''; 16 | let last_char = ''; 17 | let active_opening_brackets = []; 18 | 19 | for (let char of string) { 20 | if (char !== '{' && char !== '}') { 21 | if (last_char === '{' || last_char === '}') { 22 | output_string += "\n"; 23 | output_string += indentation; 24 | } 25 | output_string += char; 26 | } else if (char === '{') { 27 | output_string += "\n"; 28 | active_opening_brackets.push(char); 29 | output_string += indentation; 30 | output_string += char; 31 | indentation += ' '; 32 | } else if (char === '}') { 33 | if (active_opening_brackets.pop()) { 34 | output_string += "\n"; 35 | indentation = indentation.slice(0, indentation.length - 2); 36 | output_string += indentation; 37 | output_string += char; 38 | } else { 39 | return; 40 | } 41 | } 42 | last_char = char; 43 | } 44 | if (active_opening_brackets.length === 0) { 45 | console.log(output_string); 46 | } else { 47 | return; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /solutions/01-arrays-and-strings/print_combinations.js: -------------------------------------------------------------------------------- 1 | function print_combinations() { 2 | // Write a function that prints all possible "combinations" to a lock in which a valid combination 3 | // is a four digit number that represents a month and day e.g. 0123 corresponds to January 23rd 4 | // a valid combination cannot have two repeating digits. Assume all months have 31 days. 5 | 6 | // Loop through months and days 7 | for (let i = 1; i <= 12; i++) { 8 | // November will never be valid because of repeating digits 9 | if (i === 11) { 10 | continue; 11 | } 12 | for (let j = 1; j <= 31; j++) { 13 | // Create both digits for day and month 14 | let m1 = Math.floor(i / 10); 15 | let m2 = i % 10; 16 | let d1 = Math.floor(j /10); 17 | let d2 = j % 10; 18 | 19 | // Check that no two digits are the same. 20 | // Only four digits means a Set would be too resource intensive 21 | if (!(m1 === m2 || m1 === d1 || m1 === d2 || 22 | m2 === d1 || m2 === d2 || d1 === d2)) { 23 | console.log(m1 + "" + m2 + "" + d1 + "" + d2); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /solutions/01-arrays-and-strings/ransom_note.js: -------------------------------------------------------------------------------- 1 | function ransom_note(note, string_repo) { 2 | // Given a string, a "ransom note," and a second string, a "collection of newspapers/magazines," 3 | // write a function that returns true if the second string contains the correct collection of 4 | // characters such that the ransom note can be recreated and return false otherwise. 5 | 6 | // Helper to convert a string into a map of characters to number of instances 7 | let convert_string_to_character_map = (string) => { 8 | let character_map = new Map(); 9 | for (let character of string) { 10 | let quantity = character_map.get(character) || 0; 11 | character_map.set(character, quantity + 1); 12 | } 13 | return character_map; 14 | } 15 | 16 | // Helper to check if a map has no values over 0 17 | let map_is_empty = (map) => { 18 | let values = map.values(); 19 | for (let value of values) { 20 | if (value > 0) { 21 | return false; 22 | } 23 | } 24 | return true; 25 | } 26 | 27 | let note_character_map = convert_string_to_character_map(note); 28 | 29 | // Iterate through the string repo and remove characters from the character map one at a time if 30 | // they were in the original ransom note. If the map is successfully emptied, the note can be 31 | // recreated and the function returns true. 32 | for (let character of string_repo) { 33 | if (map_is_empty(note_character_map)) { 34 | return true; 35 | } else { 36 | let quantity = note_character_map.get(character); 37 | if (quantity) { 38 | note_character_map.set(character, quantity - 1); 39 | } 40 | } 41 | } 42 | return false; 43 | } 44 | -------------------------------------------------------------------------------- /solutions/01-arrays-and-strings/sum_pairs_to_target.js: -------------------------------------------------------------------------------- 1 | function sum_pairs_to_target(values, target) { 2 | // Find all pairs of integers in an array that sum up to a given value. 3 | // Values in the integer array may repeat. 4 | 5 | // Example: [1,2,3,5,6], target value = 8 6 | // Answer: 7 | // 3 + 5 = 8 8 | // 2 + 6 = 8 9 | 10 | // Build a map of 11 | let value_map = new Map(); 12 | for (let value of values) { 13 | let quantity = value_map.get(value) || 0; 14 | value_map.set(value, quantity + 1); 15 | } 16 | 17 | // Iterate through the unique values 18 | for (let value of value_map.keys()) { 19 | let search_value = target - value; 20 | let quantity = value_map.get(value) || 0; 21 | 22 | // Decrement the quantity of each value in the map as they are used 23 | value_map.set(value, quantity - 1); 24 | let search_value_quantity = value_map.get(search_value); 25 | 26 | // Log the combination if a corresponding value is found. Decrement the found value's quantity. 27 | if (search_value_quantity > 0) { 28 | value_map.set(search_value, search_value_quantity - 1); 29 | console.log(`${value} + ${search_value} = ${target}`); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /solutions/01-arrays-and-strings/validate_brackets.js: -------------------------------------------------------------------------------- 1 | function validate_brackets(string) { 2 | // Given a string that represents source code, write a function that checks if the braces are 3 | // properly paired and nested. 4 | // E.g. [({aa})] <- valid [(aa]) <- invalid [aa} <- invalid [hello <- invalid 5 | 6 | let brackets = []; 7 | for (character of string) { 8 | if (character === "(" || character === "{" || character === "[") { 9 | brackets.push(character); 10 | } else if (character === ")" && brackets.pop() !== "(" || 11 | character === "}" && brackets.pop() !== "{" || 12 | character === "]" && brackets.pop() !== "[") { 13 | return false; 14 | } 15 | } 16 | return brackets.isEmpty(); 17 | } 18 | -------------------------------------------------------------------------------- /solutions/02-linked-lists/02-01-remove_dupes.js: -------------------------------------------------------------------------------- 1 | function remove_dupes(list) { 2 | // Cracking the Coding Interview problem 2.1 3 | // Write code to remove duplicates from an unsorted linked list 4 | // FOLLOW UP: How would you solve this problem if a temporary buffer is not allowed? 5 | 6 | let encountered_nodes = new Set(); 7 | let previous = list; 8 | let current = list.next; 9 | encountered_nodes.add(previous.data); 10 | 11 | while (current) { 12 | if (encountered_nodes.has(current.data)) { 13 | previous.next = current.next; 14 | current = previous.next; 15 | } else { 16 | encountered_nodes.add(current.data); 17 | previous = current; 18 | current = current.next; 19 | } 20 | } 21 | return list; 22 | } 23 | -------------------------------------------------------------------------------- /solutions/02-linked-lists/02-02-return_kth_to_last.js: -------------------------------------------------------------------------------- 1 | function return_kth_to_last(list, k) { 2 | // Cracking the Coding Interview problem 2.2 3 | // Implement an algorithm to find the kth to last element of a singly linked list. 4 | 5 | let size = 0; 6 | let current = list; 7 | while (current) { 8 | size++; 9 | current = current.next; 10 | } 11 | let index = 0; 12 | current = list; 13 | while (index < size - k) { 14 | current = current.next; 15 | index++; 16 | } 17 | return current; 18 | } 19 | -------------------------------------------------------------------------------- /solutions/02-linked-lists/02-03-delete_middle_node.js: -------------------------------------------------------------------------------- 1 | function delete_middle_node(list, node) { 2 | // Cracking the Coding Interview problem 2.3 3 | // Implement an algorithm to delete a node in the middle (i.e., any node but the first and last node, 4 | // not necessarily the exact middle) of a singly linked list, given only access to that node. 5 | // EXAMPLE 6 | // Input: the node c from the linked list a->b->c->d->e->f 7 | // Result: nothing is returned by the new linked list looks like a->b->d->e->f 8 | 9 | let previous = list; 10 | let current = list.next; 11 | 12 | while (current) { 13 | if (node === current) { 14 | previous.next = current.next; 15 | break; 16 | } else { 17 | previous = current; 18 | current = current.next; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /solutions/02-linked-lists/02-04-partition.js: -------------------------------------------------------------------------------- 1 | function partition(x, list) { 2 | // Cracking the Coding Interview 2.4 3 | // Write code to partition a linked list around a value x, such that all nodes less than x come 4 | // before all nodes greater than or exual to x. If x is contained within the list, the values of x 5 | // only need to be after the elements less than x (see below). The partition element x can appear 6 | // anywhere in the "right partition"; it does not need to appear between the left and right partitions. 7 | 8 | let right_partition; 9 | let previous = list; 10 | let current = list; 11 | let front = list; 12 | while (current) { 13 | if (current.data >= x) { 14 | if (current === front) { 15 | front = previous.next; 16 | current.next = right_partition; 17 | right_partition = current; 18 | previous = front; 19 | current = front; 20 | } else { 21 | previous.next = current.next; 22 | current.next = right_partition; 23 | right_partition = current; 24 | current = previous.next; 25 | } 26 | } else { 27 | previous = current; 28 | current = current.next; 29 | } 30 | } 31 | previous.next = right_partition; 32 | return front; 33 | } 34 | -------------------------------------------------------------------------------- /solutions/02-linked-lists/02-05-sum_lists.js: -------------------------------------------------------------------------------- 1 | function sum_lists(addend_1, addend_2) { 2 | // Cracking the Coding Interview 2.5 3 | // You have two numbers represented by a linked list, where each node contains a single digit. 4 | // The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write 5 | // a function that adds the two numbers and returns the sum as a linked list. 6 | // EXAMPLE Input: (7 -> 1 -> 6) + (5 -> 9 -> 2). That is, 617 + 295. 7 | // Output: 2 -> 1 -> 9. That is, 912. 8 | // FOLLOW UP Suppose the digits are stored in forward order. Repeat the above problem. 9 | 10 | let digit_node_1 = addend_1; 11 | let digit_node_2 = addend_2; 12 | let result; 13 | let carry = 0; 14 | 15 | while (digit_node_1 || digit_node_2) { 16 | let new_digit = 0; 17 | if (digit_node_1) { 18 | new_digit += digit_node_1.data; 19 | digit_node_1 = digit_node_1.next; 20 | } 21 | if (digit_node_2) { 22 | new_digit += digit_node_2.data; 23 | digit_node_2 = digit_node_2.next; 24 | } 25 | new_node = ListNode(new_digit % 10 + carry); 26 | carry = Math.floor(new_digit / 10); 27 | new_node.next = result; 28 | result = new_node; 29 | } 30 | return result; 31 | } 32 | -------------------------------------------------------------------------------- /solutions/02-linked-lists/02-06-palindrome.js: -------------------------------------------------------------------------------- 1 | function palindrome(list) { 2 | // Cracking the Coding Interview 2.6 3 | // Implement a function to check if a linked list is a palindrome. 4 | 5 | let reverse_list = null; 6 | let current = list; 7 | let length = 0; 8 | 9 | // Iterate through the original list and build a new one in reverse order. Keep track of length. 10 | while (current) { 11 | length++; 12 | let node_clone = ListNode(current.data); 13 | node_clone.next = reverse_list; 14 | reverse_list = node_clone; 15 | current = current.next; 16 | } 17 | 18 | current = list; 19 | reverse_current = reverse_list; 20 | let i = 0; 21 | // Compare the original list to the reverse list. If the first half is the same so is the second. 22 | while (i < Math.floor(length / 2)) { 23 | i++; 24 | if (current.data !== reverse_current.data) { 25 | return false; 26 | } 27 | current = current.next; 28 | reverse_current = reverse_current.next; 29 | } 30 | return true; 31 | } 32 | -------------------------------------------------------------------------------- /solutions/02-linked-lists/02-07-intersection.js: -------------------------------------------------------------------------------- 1 | function intersection (l1, l2) { 2 | // Cracking the Coding Interview 2.7 3 | // Given two (singly) linked lists, determine if the two lists intersect. Return the intersecting node. 4 | // Note that the intersection is defined based on reference, not value. That is, if the kth node of the 5 | // first linked list is the exact same node (by reference) as the jth node of the second linked list, 6 | // then they are intersecting. 7 | 8 | let current_1 = l1; 9 | let current_2 = l2; 10 | let visited_nodes = new Set(); 11 | while (current_1 || current_2) { 12 | // Check if either ListNode has been seen before. If so, return it. 13 | if (visited_nodes.has(current_1)) { 14 | return current_1; 15 | } else if (visited_nodes.has(current_2)) { 16 | return current_2; 17 | } 18 | 19 | // Continue traversing the lists and adding nodes to the visited set. 20 | if (current_1) { 21 | visited_nodes.add(current_1); 22 | current_1 = current_1.next; 23 | } else if (current_2) { 24 | visited_nodes.add(current_2); 25 | current_2 = current_2.next; 26 | } 27 | } 28 | return undefined; 29 | } 30 | -------------------------------------------------------------------------------- /solutions/03-stacks-and-queues/03-05-sort_stack.js: -------------------------------------------------------------------------------- 1 | function sort_stack(stack) { 2 | // Cracking the Coding Interview 3.5 3 | // Write a program to sort a stack such that the smallet itmes are on the top. You can use an 4 | // additional temporary stack, but you may not copy the elements into any other data structure 5 | // (such as an array). The stack supports the following operations: push, pop, peek, and isEmpty. 6 | 7 | let reverse_sorted = []; // Create a stack to place elements in reverse sorted order. 8 | let current = stack.pop(); 9 | 10 | // Sort items into reverse_sorted stack until all original items are in second stack. 11 | while (!stack.isEmpty() || current) { 12 | // If the top of stack is greater than the item on top of reverse_sorted, add it to reverse_sorted 13 | if (reverse_sorted.peek() < current || !reverse_sorted.peek()) { 14 | reverse_sorted.push(current); 15 | current = stack.pop(); 16 | } 17 | // If top of reverse_sorted is greater than current element, move items to 18 | // stack until there is a proper spot for current in reverse_sorted stack. 19 | while (reverse_sorted.peek() > current) { 20 | stack.push(reverse_sorted.pop()); 21 | } 22 | } 23 | 24 | // When all elements have been put into reverse_sorted in proper order, 25 | // pop them into stack to get them into sorted order. 26 | while(!reverse_sorted.isEmpty()) { 27 | stack.push(reverse_sorted.pop()); 28 | } 29 | return stack; 30 | } 31 | -------------------------------------------------------------------------------- /solutions/04-trees-and-graphs/04-01-route_between_nodes.js: -------------------------------------------------------------------------------- 1 | function route_between_nodes(start, destination) { 2 | // Cracking the Coding Interview problem 4.1 3 | // Given a directed graph, design an algorithm to find out whether this is a route between two nodes. 4 | 5 | let helper = function (current, destination) { 6 | if (current === destination) { 7 | return true; 8 | } else if (current.children.isEmpty() || current.visited) { 9 | return false; 10 | } else { 11 | current.visited = true; 12 | for (child of current.children) { 13 | let destination_found = helper(child, destination); 14 | if (destination_found) { 15 | return true; 16 | } 17 | } 18 | return false; 19 | } 20 | } 21 | return helper(start, destination); 22 | } 23 | -------------------------------------------------------------------------------- /solutions/04-trees-and-graphs/04-02-minimal_tree.js: -------------------------------------------------------------------------------- 1 | function minimal_tree(elements) { 2 | // Cracking the Coding Interview problem 4.2 3 | // Given a sorted (increasing order) array with unique integer elements, write an algorithm to create 4 | // a binary search tree with minimal height 5 | 6 | if (elements.length === 1) { 7 | return create_tree_node(elements[0]); 8 | } else { 9 | let root_index = Math.floor((elements.length - 1) / 2); 10 | let branch = create_tree_node(elements[root_index]); 11 | let left_elements = elements.slice(0, root_index); 12 | let right_elements = elements.slice(root_index + 1); 13 | if (!left_elements.isEmpty()) { 14 | branch.left = minimal_tree(left_elements); 15 | } 16 | if (!right_elements.isEmpty()) { 17 | branch.right = minimal_tree(right_elements); 18 | } 19 | return branch; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /solutions/04-trees-and-graphs/04-03-list_of_depths.js: -------------------------------------------------------------------------------- 1 | function list_of_depths(tree) { 2 | // Cracking the Coding Interview 4.3 3 | // Given a binary tree, design an algorithm which creates a linked list of all the nodes 4 | // at each depth (e.g., if you have a tree with depth D, you'll have D linked lists). 5 | 6 | let lists = []; 7 | let helper = (branch, depth) => { 8 | if (lists[depth]) { 9 | lists[depth].appendTail(ListNode(branch.data)); 10 | } else { 11 | lists[depth] = ListNode(branch.data); 12 | } 13 | if (branch.left) { 14 | helper(branch.left, depth + 1); 15 | } 16 | if (branch.right) { 17 | helper(branch.right, depth + 1); 18 | } 19 | } 20 | helper(tree, 0); 21 | return lists; 22 | } 23 | -------------------------------------------------------------------------------- /solutions/04-trees-and-graphs/04-04-check_balanced.js: -------------------------------------------------------------------------------- 1 | function check_balanced(root) { 2 | // Cracking the Coding Interview 4.4 3 | // Implement a function to check if a binary tree is balanced. For the purposes of this question, a 4 | // balanced tree is defined to be a tree such that the heights of the two subtrees of any node never 5 | // differ by more than one. 6 | 7 | let helper = (node) => { 8 | if (!node) { 9 | return 0; 10 | } 11 | let left_level = helper(node.left); 12 | if (left_level === -1) { 13 | return -1; 14 | } 15 | let right_level = helper(node.right); 16 | if (right_level === -1) { 17 | return -1; 18 | } 19 | if (Math.abs(left_level - right_level) > 1) { 20 | return -1; 21 | } 22 | return left_level > right_level ? left_level + 1 : right_level + 1; 23 | } 24 | 25 | return helper(root) !== -1; 26 | } 27 | -------------------------------------------------------------------------------- /solutions/04-trees-and-graphs/04-08-first_common_ancestor.js: -------------------------------------------------------------------------------- 1 | function first_common_ancestor(root, target_1, target_2) { 2 | let helper = (current) => { 3 | if (current.left && current.left.depthFirstSearch(target_1) !== -1 && current.left.depthFirstSearch(target_2) !== -1) { 4 | return helper(current.left); 5 | } else if (current.right && current.right.depthFirstSearch(target_1) !== -1 && current.right.depthFirstSearch(target_2) !== -1) { 6 | return helper(current.right); 7 | } else { 8 | return current; 9 | } 10 | } 11 | return helper(root); 12 | } 13 | -------------------------------------------------------------------------------- /solutions/04-trees-and-graphs/is_binary_search_tree.js: -------------------------------------------------------------------------------- 1 | function is_binary_search_tree(root) { 2 | // Write a function that takes the root node of a binary tree and returns 3 | // true if the tree is a binary search tree. 4 | 5 | let helper = function (node, min_value, max_value) { 6 | if (!node) { 7 | return true; 8 | } else { 9 | return node.data > min_value && 10 | node.data < max_value && 11 | helper(node.left, min_value, node.data) && 12 | helper(node.right, node.data, max_value); 13 | } 14 | } 15 | return helper(root, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER); 16 | } 17 | -------------------------------------------------------------------------------- /solutions/08-recursion-and-dynamic-programming/08-01-triple_step.js: -------------------------------------------------------------------------------- 1 | function triple_step(n) { 2 | // Cracking the Coding Interview problem 8.1 3 | // A child is running up a staircase with n steps and can hop either 1 step, 2 steps, or 3 steps at a 4 | // time. Implement a method to count how many possible ways the child can run up the stairs. 5 | 6 | let cache_table = []; 7 | let helper = (current_step, total_steps) => { 8 | if (current_step === total_steps) { 9 | return 1; 10 | } else if (current_step > total_steps) { 11 | return 0; 12 | } else { 13 | if (!cache_table[current_step]) { 14 | cache_table[current_step] = helper(current_step + 1, total_steps) + 15 | helper(current_step + 2, total_steps) + 16 | helper(current_step + 3, total_steps); 17 | return cache_table[current_step]; 18 | } else { 19 | return cache_table[current_step]; 20 | } 21 | } 22 | } 23 | return helper(0, n); 24 | } 25 | -------------------------------------------------------------------------------- /solutions/08-recursion-and-dynamic-programming/08-03-magic_index.js: -------------------------------------------------------------------------------- 1 | function magic_index(array) { 2 | // A magic index in an array A[0 ... n-1] is defined to be an index such that A[i] = i. Given a sorted 3 | // array of distinct integeres, write a method to find a magic index, if one exists in array A. 4 | // FOLLOW UP What if the values are not distinct? 5 | 6 | let helper = (array, index) => { 7 | let value_at_index = array[0]; 8 | if (value_at_index === index) { 9 | return index; 10 | } else if (array.length === 0) { 11 | return -1; 12 | } else { 13 | if (value_at_index > index) { 14 | return helper(array.slice(value_at_index - index), value_at_index); 15 | } else { 16 | return helper(array.slice(1), index + 1); 17 | } 18 | } 19 | } 20 | return helper(array, 0); 21 | } 22 | -------------------------------------------------------------------------------- /solutions/08-recursion-and-dynamic-programming/08-04-power_set.js: -------------------------------------------------------------------------------- 1 | function match_query(words, query) { 2 | let result = []; 3 | 4 | let query_components = query.split('?'); 5 | 6 | words.filter(word => { 7 | let word_copy = word; 8 | if (word.length !== query.length) { 9 | return false; 10 | } else { 11 | for (let component of query_components) { 12 | if (component === '') { 13 | word_copy.slice(1, word_copy.length); 14 | } else { 15 | if (word_copy.indexOf(component) !== 0) { 16 | return false; 17 | } 18 | } 19 | } 20 | return true; 21 | } 22 | }); 23 | 24 | return result; 25 | } 26 | 27 | 28 | function getAllKeys() { 29 | return(fs.ls); 30 | } 31 | 32 | function removeItem(key) { 33 | fs.remove(key); 34 | console.log('Deleted'); 35 | } 36 | 37 | function setItem(key, value, callback) { 38 | operations.add_operation_to_queue(fs.writeFile(hash(key), value) 39 | .done(result => { 40 | callback(); 41 | return result 42 | }) 43 | ); 44 | } 45 | 46 | function execute_file_operations(operations) { 47 | for (operation of operations) { 48 | operation().done(() => { 49 | continue; 50 | }); 51 | } 52 | } 53 | 54 | function add_operation_to_queue(key, value, operation) { 55 | operations.push(setItem(key, value, print_result)); 56 | } 57 | 58 | function execute_operations() { 59 | while (!operations.empty()) { 60 | execute_file_operations(operations); 61 | } 62 | } -------------------------------------------------------------------------------- /solutions/08-recursion-and-dynamic-programming/08-05-recursive_multiply.js: -------------------------------------------------------------------------------- 1 | function recursive_multiply(a, b) { 2 | // Cracking the Coding Interview 8.5 3 | // Write a recursive function to multiply two positive integers without using the * operator. You can 4 | // use addition, subtraction, and bit-shifting, but you should minimie the number of those operators. 5 | 6 | // Using the larger number for the addition will mean fewer recursive calls. 7 | // E.g. (3 * 2) === (2 + 2 + 2) === (3 + 3) <-- Fewer operations 8 | let [factor_1, factor_2] = a > b ? [a, b] : [b, a]; 9 | let helper = (factor_2, total) => { 10 | if (factor_2 === 0) { 11 | return total; 12 | } else if (factor_2 === 1) { 13 | return factor_1; 14 | } else if (factor_2 % 2 === 0) { 15 | // If factor_2 is even, calculate the product of half of factor_2 and factor_1 and double it. 16 | // E.g. 5 * 6 === 5(3 * 2) 17 | total += helper(factor_2 >> 1, total); 18 | return total + total; 19 | } else { 20 | // If factor_2 is odd, subtract 1 to make it even, do the same halving and doubling as above, and 21 | // add factor_1 at the end to calculate the final result. 22 | // E.g. 5 * 7 === 5(3 * 2) + 5 23 | total += helper((factor_2 - 1) >> 1, total); 24 | total += total; 25 | total += factor_1; 26 | return total; 27 | } 28 | } 29 | return helper(factor_2, 0); 30 | } 31 | -------------------------------------------------------------------------------- /solutions/08-recursion-and-dynamic-programming/max_nonadjacent_subset.js: -------------------------------------------------------------------------------- 1 | function max_nonadjacent_subset(numbers) { 2 | let one_previous = 0; 3 | let two_previous = 0; 4 | 5 | let helper = index => { 6 | let new_addend = numbers[index]; 7 | let new_max = one_previous > two_previous + new_addend ? one_previous : new_addend + two_previous; 8 | two_previous = one_previous; 9 | one_previous = new_max; 10 | } 11 | 12 | for (let i = 0; i < numbers.length; i++) { 13 | helper(i); 14 | } 15 | 16 | return one_previous; 17 | } 18 | -------------------------------------------------------------------------------- /solutions/10-sorting-and-searching/10-01-sorted_merge.js: -------------------------------------------------------------------------------- 1 | function sorted_merge(A, B) { 2 | // Cracking the Coding Interview problem 10.1 3 | // You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. 4 | // Write a method to merge B into A in sorted order. 5 | 6 | let swap_index = A.length - 1; 7 | let B_index = B.length - 1; 8 | let A_index = 0; 9 | for (let [index, value] of A.entries()) { 10 | if (!value) { 11 | A_index = index - 1; 12 | break; 13 | } 14 | } 15 | 16 | while (B_index >= 0) { 17 | if (B[B_index] >= A[A_index] || !A[A_index]) { 18 | A[swap_index] = B[B_index]; 19 | B_index--; 20 | swap_index--; 21 | } else { 22 | A[swap_index] = A[A_index]; 23 | A[A_index] = null; 24 | A_index--; 25 | swap_index--; 26 | } 27 | } 28 | return A; 29 | } 30 | -------------------------------------------------------------------------------- /solutions/10-sorting-and-searching/10-03-search_in_rotated_array.js: -------------------------------------------------------------------------------- 1 | function search_in_rotated_array(list, target) { 2 | // Cracking the Coding Interview 10.3 3 | // Given a sorted array of n integers that has been rotated an unknown number of times, write code to 4 | // find an element in the array. You may assume that the array was originally sorted in increasing order. 5 | // EXAMPLE: search_in_rotated_array([15, 16, 19, 20, 25, 1, 3, 4, 5, 7, 10, 14], 5) === 8 6 | 7 | let start = 0; 8 | let end = list.length - 1; 9 | let search_index = Math.floor((end - start) / 2); 10 | let search_element = list[search_index]; 11 | 12 | while (search_element !== target && search_index !== -1) { 13 | if (start === end) { 14 | search_index = -1; 15 | } else if (target > list[end] || target < search_element) { 16 | end = search_index - 1; 17 | search_index = start + Math.floor((end - start) / 2); 18 | search_element = list[search_index]; 19 | } else { 20 | start = search_index + 1; 21 | search_index = start + Math.floor((end - start) / 2); 22 | search_element = list[search_index]; 23 | } 24 | } 25 | 26 | return search_index; 27 | } 28 | -------------------------------------------------------------------------------- /solutions/10-sorting-and-searching/binary_search.js: -------------------------------------------------------------------------------- 1 | function binary_search(list, target) { 2 | let start = 0; 3 | let end = list.length - 1; 4 | let search_index = Math.floor((end - start) / 2); 5 | 6 | while (list[search_index] !== target && search_index !== -1) { 7 | if (start === end) { 8 | search_index = -1; 9 | } else if (list[search_index] > target) { 10 | end = search_index - 1; 11 | search_index = start + Math.floor((end - start) / 2); 12 | } else { 13 | start = search_index + 1; 14 | search_index = start + Math.floor((end - start) / 2); 15 | } 16 | } 17 | 18 | return search_index; 19 | } 20 | -------------------------------------------------------------------------------- /test/check_permutation_test.js: -------------------------------------------------------------------------------- 1 | mocha.setup('bdd'); 2 | 3 | describe('check_permutation_test', () => { 4 | describe('valid_permutation', () => { 5 | it('should return true for "read" and "dear"', () => { 6 | chai.assert.equal(check_permutation('read', 'dear'), true); 7 | }); 8 | it('should return true for equivalent strings', () => { 9 | chai.assert.equal(check_permutation('equivalent', 'equivalent'), true); 10 | }); 11 | it('should return true for two empty strings', () => { 12 | chai.assert.equal(check_permutation('', ''), true); 13 | }); 14 | }); 15 | describe('invalid_permutation', () => { 16 | it('should return false for "noon" and "moon"', () => { 17 | chai.assert.equal(check_permutation('noon', 'moon'), false); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /test/corresponding_bracket_index_test.js: -------------------------------------------------------------------------------- 1 | describe('corresponding_bracket_index_test', () => { 2 | describe('valid_source_code', () => { 3 | it('should return 6 when the caret index is 0', () => { 4 | chai.assert.equal(corresponding_bracket_index(valid_source_code, 0), 6); 5 | }); 6 | 7 | it('should return 0 when the caret index is 6', () => { 8 | chai.assert.equal(corresponding_bracket_index(valid_source_code, 6), 0); 9 | }); 10 | 11 | it('should return 26 when the caret index is 9', () => { 12 | chai.assert.equal(corresponding_bracket_index(valid_source_code, 9), 26); 13 | }); 14 | 15 | it('should return 8 when the caret index is 27', () => { 16 | chai.assert.equal(corresponding_bracket_index(valid_source_code, 27), 8); 17 | }); 18 | }); 19 | 20 | describe('invalid_source_code', () => { 21 | it('should throw "invalid brackets in source code" on improper nesting', () => { 22 | chai.assert.throws(() => { corresponding_bracket_index(improper_nesting, 2) }, Error, 'Invalid brackets in source code.'); 23 | }); 24 | 25 | it('should throw "invalid brackets in source code" on incorrect closing bracket', () => { 26 | chai.assert.throws(() => { corresponding_bracket_index(wrong_closing_bracket, 2) }, Error, 'Invalid brackets in source code.'); 27 | }); 28 | 29 | it('should throw "Unclosed brackets in source code" on unclosed bracket', () => { 30 | chai.assert.throws(() => { corresponding_bracket_index(unclosed_bracket, 2) }, Error, 'Unclosed brackets in source code.'); 31 | }); 32 | 33 | it('should throw "Invalid caret index. Caret must point to a bracket" if no braces', () => { 34 | chai.assert.throws(() => { corresponding_bracket_index(no_braces, 2) }, Error, 'Invalid caret index. Caret must point to a bracket.'); 35 | }); 36 | 37 | it('should throw "Invalid caret index. Caret must point to a bracket" on invalid caret index', () => { 38 | chai.assert.throws(() => { corresponding_bracket_index(valid_source_code, 2) }, Error, 'Invalid caret index. Caret must point to a bracket.'); 39 | }); 40 | }); 41 | }); 42 | -------------------------------------------------------------------------------- /test/one_away_test.js: -------------------------------------------------------------------------------- 1 | describe('one_away_test', () => { 2 | describe('insert/remove', () => { 3 | it('should return true for "pale and "ple"', () => { 4 | chai.assert.equal(one_away('pale', 'ple'), true); 5 | }); 6 | it('should return false for "pale and "pl"', () => { 7 | chai.assert.equal(one_away('pale', 'pl'), false); 8 | }); 9 | }); 10 | describe('replace', () => { 11 | it('should return true for "pale" and "bale"', () => { 12 | chai.assert.equal(one_away("pale", "pales"), true); 13 | }); 14 | it('should return false for "pale" and "bald"', () => { 15 | chai.assert.equal(one_away("pale", "bald"), false); 16 | }); 17 | }); 18 | describe('equivalent', function () { 19 | it('should return true for "hello" and "hello"', () => { 20 | chai.assert.equal(one_away("hello", "hello"), true); 21 | }); 22 | it('should return true for two empty strings', () => { 23 | chai.assert.equal(one_away("", ""), true); 24 | }); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /test/sort_stack_test.js: -------------------------------------------------------------------------------- 1 | describe('sort_stack_test', () => { 2 | describe('mixed_order', () => { 3 | it('should return [6, 5, 4, 3, 2, 1]', () => { 4 | chai.assert.deepEqual(sort_stack(mixed_order), [6, 5, 4, 3, 2, 1]); 5 | }); 6 | }); 7 | describe('sorted_order', () => { 8 | it('should return [10, 9, 8, 7, 6, 5]', () => { 9 | chai.assert.deepEqual(sort_stack(sorted_order), [10, 9, 8, 7, 6, 5]); 10 | }); 11 | }); 12 | describe('reverse_sorted', () => { 13 | it('should return [95, 94, 93, 92, 91, 90]', () => { 14 | chai.assert.deepEqual(sort_stack(reverse_sorted), [95, 94, 93, 92, 91, 90]); 15 | }); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /test/string_compression_test.js: -------------------------------------------------------------------------------- 1 | describe('string_compression_test', () => { 2 | describe('compressable_strings', () => { 3 | it('should return "a3b1c5a3" for "aaabcccccaaa"', () => { 4 | chai.assert.deepEqual(string_compression('aaabcccccaaa'), 'a3b1c5a3'); 5 | }); 6 | it('should return "r2e2d3i2t2" for "rreedddiitt"', () => { 7 | chai.assert.deepEqual(string_compression('rreedddiitt'), 'r2e2d3i2t2'); 8 | }); 9 | }); 10 | describe('uncompressable_strings', () => { 11 | it('should return "andre" for "andre"', () => { 12 | chai.assert.deepEqual(string_compression('andre'), 'andre'); 13 | }); 14 | it('should return "" for ""', () => { 15 | chai.assert.deepEqual(string_compression(''), ''); 16 | }); 17 | }); 18 | }); 19 | -------------------------------------------------------------------------------- /ui/about.css: -------------------------------------------------------------------------------- 1 | #bio { 2 | display: flex; 3 | flex-wrap: wrap-reverse; 4 | justify-content: center; 5 | } 6 | #left-column { 7 | width: 30em; 8 | margin-right: 3em; 9 | } 10 | #right-column img { 11 | width: 30em; 12 | } 13 | a p { 14 | margin: 0; 15 | display: inline; 16 | } 17 | ul { 18 | margin: 0; 19 | padding-left: 1em; 20 | } 21 | h1 { 22 | text-align: center; 23 | } 24 | -------------------------------------------------------------------------------- /ui/footer.css: -------------------------------------------------------------------------------- 1 | #footer-content { 2 | display: flex; 3 | justify-content: space-between; 4 | } 5 | footer { 6 | border-top: 1px solid #eee; 7 | margin-top: 2em; 8 | padding-top: 1em; 9 | padding-bottom: 2em; 10 | } 11 | #footer-content div p { 12 | margin: 0; 13 | padding: 0; 14 | font-size: 0.8em; 15 | color: #767676; 16 | } 17 | #footer-content div a p:hover { 18 | color: #4EC9B0; 19 | } 20 | #footer-content img:hover { 21 | opacity: 0.8; 22 | } 23 | footer img { 24 | width: 1.5em; 25 | } 26 | #footer-center { 27 | display: flex; 28 | justify-content: space-between; 29 | width: 8em; 30 | } 31 | -------------------------------------------------------------------------------- /ui/header.css: -------------------------------------------------------------------------------- 1 | #header-content { 2 | display: flex; 3 | flex-wrap: wrap; 4 | align-items: center; 5 | justify-content: space-between; 6 | font-family: "Courier New", monospace; 7 | font-size: 2em; 8 | font-weight: bold; 9 | white-space: nowrap; 10 | margin-top: 0.6em; 11 | margin-bottom: 0.6em; 12 | } 13 | nav ul { 14 | list-style: none; 15 | padding: 0; 16 | margin: 0; 17 | } 18 | nav ul li { 19 | display: inline-block; 20 | } 21 | #home-link:hover { 22 | color: black; 23 | } 24 | #home-link:hover span { 25 | color: #4EC9B0; 26 | } 27 | -------------------------------------------------------------------------------- /ui/home.css: -------------------------------------------------------------------------------- 1 | #main-content { 2 | display: flex; 3 | flex-wrap: wrap; 4 | } 5 | #code-container { 6 | min-width: 56em; 7 | width: 60%; 8 | flex-grow: 1; 9 | } 10 | #code-container h3, #test-output h3 { 11 | display: inline-block; 12 | margin-right: 0.5em; 13 | } 14 | #test-output { 15 | margin-left: 0.5em; 16 | min-width: 28em; 17 | width: 38%; 18 | flex-grow: 1; 19 | margin-left: 0.75em; 20 | } 21 | #employment-status { 22 | padding: 0.1em 0.3em 0.1em 0.3em; 23 | color: white; 24 | background-color: #4EC9B0; 25 | /*background-color: #B1364F;*/ 26 | border-radius: 0.3em; 27 | } 28 | pre { 29 | font-size: 0.9em; 30 | } 31 | -------------------------------------------------------------------------------- /ui/main.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding: 0 2em 0 2em; 3 | overflow-y: scroll; 4 | max-width: 120em; 5 | margin-left: auto; 6 | margin-right: auto; 7 | background-color: white; 8 | } 9 | p, h1, h2, h3 { 10 | font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; 11 | } 12 | .accent { 13 | color: #4EC9B0; 14 | } 15 | a { 16 | text-decoration: none; 17 | color: black; 18 | } 19 | a:hover { 20 | color: #4EC9B0; 21 | } 22 | /* These styles are duplicated here from Prism.css so the formatting loads more quickly. */ 23 | pre { 24 | background-color: #272822; 25 | padding: 1em; 26 | margin: .5em 0; 27 | overflow: auto; 28 | border-radius: 0.3em; 29 | } 30 | -------------------------------------------------------------------------------- /ui/portfolio.css: -------------------------------------------------------------------------------- 1 | #portfolio { 2 | display: flex; 3 | flex-direction: row; 4 | justify-content: space-around; 5 | flex-wrap: wrap; 6 | } 7 | #portfolio img { 8 | width: 100%; 9 | } 10 | .portfolio-item { 11 | border: 1px solid lightgrey; 12 | border-radius: 0.8em; 13 | width: 18em; 14 | padding: 1.2em; 15 | text-align: center; 16 | margin-top: 1.2em; 17 | } 18 | a:hover img { 19 | opacity: 0.5; 20 | } 21 | .portfolio-item h3 { 22 | margin: 0; 23 | margin-bottom: 1em; 24 | } 25 | -------------------------------------------------------------------------------- /ui/resume.css: -------------------------------------------------------------------------------- 1 | #resume { 2 | width: 60%; 3 | max-width: 50em; 4 | margin-left: auto; 5 | margin-right: auto; 6 | margin-top: 2em; 7 | border: 1px solid lightgrey; 8 | } 9 | #resume img { 10 | width: 100%; 11 | } 12 | #resume img:hover { 13 | opacity: 0.55; 14 | } 15 | #download-links { 16 | margin-top: 1em; 17 | text-align: center; 18 | } 19 | -------------------------------------------------------------------------------- /ui/ui.js: -------------------------------------------------------------------------------- 1 | document.addEventListener('DOMContentLoaded', () => { 2 | // Watch for a problem being selected from the select input. Look up selected solution and 3 | // display it in the code container. 4 | let problem_select = document.querySelector('select'); 5 | let selected_option; 6 | let all_tests_button = document.querySelector('#all-tests-button'); 7 | problem_select.addEventListener('change', () => { 8 | selected_option = problem_select[problem_select.selectedIndex].innerText; 9 | let fn = window[selected_option]; 10 | document.querySelector('code').textContent = fn.toString(); 11 | Prism.highlightAll(); 12 | }); 13 | 14 | // Watch for clicks on the 'run mocha tests' button and run them when clicked. 15 | all_tests_button.onclick = () => { 16 | mocha.run(); 17 | all_tests_button.disabled = true; 18 | all_tests_button.title = "Refresh page to run Mocha tests again."; 19 | }; 20 | }, false ); 21 | --------------------------------------------------------------------------------