summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Orbekk <kj@orbekk.com>2021-11-28 13:03:48 -0500
committerKjetil Orbekk <kj@orbekk.com>2021-11-28 13:03:48 -0500
commit17049953c08b7c783bf73fcb18ddbb7b05c8a671 (patch)
tree134b4db7e924b4cd803584bef1aef1c0de57d163
parent65951ec5972aec2abbfc8d0dfa2a7227a0fa7223 (diff)
add exercises
-rw-r--r--exercism/emacs-lisp/binary/.exercism/config.json19
-rw-r--r--exercism/emacs-lisp/binary/.exercism/metadata.json1
-rw-r--r--exercism/emacs-lisp/binary/HELP.md41
-rw-r--r--exercism/emacs-lisp/binary/README.md46
-rw-r--r--exercism/emacs-lisp/binary/binary-test.el34
-rw-r--r--exercism/emacs-lisp/binary/binary.el21
-rw-r--r--exercism/emacs-lisp/difference-of-squares/.exercism/config.json24
-rw-r--r--exercism/emacs-lisp/difference-of-squares/.exercism/metadata.json1
-rw-r--r--exercism/emacs-lisp/difference-of-squares/HELP.md41
-rw-r--r--exercism/emacs-lisp/difference-of-squares/README.md38
-rw-r--r--exercism/emacs-lisp/difference-of-squares/difference-of-squares-test.el40
-rw-r--r--exercism/emacs-lisp/difference-of-squares/difference-of-squares.el21
12 files changed, 327 insertions, 0 deletions
diff --git a/exercism/emacs-lisp/binary/.exercism/config.json b/exercism/emacs-lisp/binary/.exercism/config.json
new file mode 100644
index 0000000..8515e32
--- /dev/null
+++ b/exercism/emacs-lisp/binary/.exercism/config.json
@@ -0,0 +1,19 @@
+{
+ "blurb": "Convert a binary number, represented as a string (e.g. '101010'), to its decimal equivalent using first principles",
+ "authors": [
+ "canweriotnow"
+ ],
+ "files": {
+ "solution": [
+ "binary.el"
+ ],
+ "test": [
+ "binary-test.el"
+ ],
+ "example": [
+ ".meta/example.el"
+ ]
+ },
+ "source": "All of Computer Science",
+ "source_url": "http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-"
+}
diff --git a/exercism/emacs-lisp/binary/.exercism/metadata.json b/exercism/emacs-lisp/binary/.exercism/metadata.json
new file mode 100644
index 0000000..30df7c8
--- /dev/null
+++ b/exercism/emacs-lisp/binary/.exercism/metadata.json
@@ -0,0 +1 @@
+{"track":"emacs-lisp","exercise":"binary","id":"7bb1929aef2b4c1b990fd4ec71b1b5f2","url":"https://exercism.org/tracks/emacs-lisp/exercises/binary","handle":"orbekk","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/exercism/emacs-lisp/binary/HELP.md b/exercism/emacs-lisp/binary/HELP.md
new file mode 100644
index 0000000..5f70cca
--- /dev/null
+++ b/exercism/emacs-lisp/binary/HELP.md
@@ -0,0 +1,41 @@
+# Help
+
+## Running the tests
+
+Tests can be run several ways:
+
+1. Interactively and individually, with `M-x ert RET test-name RET`
+2. Interactively and all at once, with `M-x ert RET t RET`
+3. From the terminal, in batch mode, with `emacs -batch -l ert -l my-test.el -f ert-run-tests-batch-and-exit`
+4. Other options can be found in the docs, `C-h i m ert RET`
+
+## Submitting your solution
+
+You can submit your solution using the `exercism submit binary.el` command.
+This command will upload your solution to the Exercism website and print the solution page's URL.
+
+It's possible to submit an incomplete solution which allows you to:
+
+- See how others have completed the exercise
+- Request help from a mentor
+
+## Need to get help?
+
+If you'd like help solving the exercise, check the following pages:
+
+- The [Emacs Lisp track's documentation](https://exercism.org/docs/tracks/emacs-lisp)
+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
+
+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
+
+To get help if you're having trouble, you can use one of the following resources:
+
+- [The Emacs Wiki](http://emacswiki.org/) is invaluable. Spend lots of time here.
+- [The Emacs Editor](http://www.gnu.org/software/emacs/manual/html_node/emacs/index.html) is the official manual for GNU Emacs.
+- IRC - there are [freenode](https://freenode.net/) channels for `#emacs`, `#prelude`, and many Emacs
+ packages, and many helpful folks around. And with emacs, IRC is as close as
+ `M-x erc`.
+- [Exercism Support](https://gitter.im/exercism/support) Gitter chat is also a good place to get help from the
+ exercism community.
+- [StackOverflow](http://stackoverflow.com/questions/tagged/elisp) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions. \ No newline at end of file
diff --git a/exercism/emacs-lisp/binary/README.md b/exercism/emacs-lisp/binary/README.md
new file mode 100644
index 0000000..577576b
--- /dev/null
+++ b/exercism/emacs-lisp/binary/README.md
@@ -0,0 +1,46 @@
+# Binary
+
+Welcome to Binary on Exercism's Emacs Lisp Track.
+If you need help running the tests or submitting your code, check out `HELP.md`.
+
+## Instructions
+
+Convert a binary number, represented as a string (e.g. '101010'), to its decimal equivalent using first principles.
+
+Implement binary to decimal conversion. Given a binary input
+string, your program should produce a decimal output. The
+program should handle invalid inputs.
+
+## Note
+
+- Implement the conversion yourself.
+ Do not use something else to perform the conversion for you.
+
+## About Binary (Base-2)
+
+Decimal is a base-10 system.
+
+A number 23 in base 10 notation can be understood
+as a linear combination of powers of 10:
+
+- The rightmost digit gets multiplied by 10^0 = 1
+- The next number gets multiplied by 10^1 = 10
+- ...
+- The *n*th number gets multiplied by 10^*(n-1)*.
+- All these values are summed.
+
+So: `23 => 2*10^1 + 3*10^0 => 2*10 + 3*1 = 23 base 10`
+
+Binary is similar, but uses powers of 2 rather than powers of 10.
+
+So: `101 => 1*2^2 + 0*2^1 + 1*2^0 => 1*4 + 0*2 + 1*1 => 4 + 1 => 5 base 10`.
+
+## Source
+
+### Created by
+
+- @canweriotnow
+
+### Based on
+
+All of Computer Science - http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld- \ No newline at end of file
diff --git a/exercism/emacs-lisp/binary/binary-test.el b/exercism/emacs-lisp/binary/binary-test.el
new file mode 100644
index 0000000..ad8476d
--- /dev/null
+++ b/exercism/emacs-lisp/binary/binary-test.el
@@ -0,0 +1,34 @@
+;;; binary-test.el --- Tests for Binary exercise (exercism)
+
+;;; Commentary:
+
+;;; Code:
+
+(load-file "binary.el")
+
+(ert-deftest binary-1-is-decimal-1 ()
+ (should (= 1 (to-decimal "1"))))
+
+(ert-deftest binary-10-is-decimal-2 ()
+ (should (= 2 (to-decimal "10"))))
+
+(ert-deftest binary-11-is-decimal-3 ()
+ (should (= 3 (to-decimal "11"))))
+
+(ert-deftest binary-100-is-decimal-4 ()
+ (should (= 4 (to-decimal "100"))))
+
+(ert-deftest binary-1001-is-decimal-9 ()
+ (should (= 9 (to-decimal "1001"))))
+
+(ert-deftest binary-11010-is-decimal-26 ()
+ (should (= 26 (to-decimal "11010"))))
+
+(ert-deftest binary-10001101000-is-decimal-1128 ()
+ (should (= 1128 (to-decimal "10001101000"))))
+
+(ert-deftest invalid-binary-is-decimal-0 ()
+ (should (= 0 (to-decimal "carrot"))))
+
+(provide 'binary-test)
+;;; binary-test.el ends here
diff --git a/exercism/emacs-lisp/binary/binary.el b/exercism/emacs-lisp/binary/binary.el
new file mode 100644
index 0000000..2a2fba3
--- /dev/null
+++ b/exercism/emacs-lisp/binary/binary.el
@@ -0,0 +1,21 @@
+;;; binary.el --- Binary exercise (exercism)
+
+;;; Commentary:
+
+;;; Code:
+
+(require 'cl)
+
+(defun to-decimal-helper (str)
+ (cl-loop with result = 0
+ for c across str
+ do (setq result (+ (- c ?0) (* 2 result)))
+ finally return result))
+
+(defun to-decimal (str)
+ (if (string-match "[01]+" str)
+ (to-decimal-helper str)
+ 0))
+
+(provide 'binary)
+;;; binary.el ends here
diff --git a/exercism/emacs-lisp/difference-of-squares/.exercism/config.json b/exercism/emacs-lisp/difference-of-squares/.exercism/config.json
new file mode 100644
index 0000000..7d5d1e5
--- /dev/null
+++ b/exercism/emacs-lisp/difference-of-squares/.exercism/config.json
@@ -0,0 +1,24 @@
+{
+ "blurb": "Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.",
+ "authors": [
+ "canweriotnow"
+ ],
+ "contributors": [
+ "Scientifica96",
+ "vermiculus",
+ "yurrriq"
+ ],
+ "files": {
+ "solution": [
+ "difference-of-squares.el"
+ ],
+ "test": [
+ "difference-of-squares-test.el"
+ ],
+ "example": [
+ ".meta/example.el"
+ ]
+ },
+ "source": "Problem 6 at Project Euler",
+ "source_url": "http://projecteuler.net/problem=6"
+}
diff --git a/exercism/emacs-lisp/difference-of-squares/.exercism/metadata.json b/exercism/emacs-lisp/difference-of-squares/.exercism/metadata.json
new file mode 100644
index 0000000..7051a2b
--- /dev/null
+++ b/exercism/emacs-lisp/difference-of-squares/.exercism/metadata.json
@@ -0,0 +1 @@
+{"track":"emacs-lisp","exercise":"difference-of-squares","id":"d839accd4cc147648cf015b4a41e496d","url":"https://exercism.org/tracks/emacs-lisp/exercises/difference-of-squares","handle":"orbekk","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/exercism/emacs-lisp/difference-of-squares/HELP.md b/exercism/emacs-lisp/difference-of-squares/HELP.md
new file mode 100644
index 0000000..23a6380
--- /dev/null
+++ b/exercism/emacs-lisp/difference-of-squares/HELP.md
@@ -0,0 +1,41 @@
+# Help
+
+## Running the tests
+
+Tests can be run several ways:
+
+1. Interactively and individually, with `M-x ert RET test-name RET`
+2. Interactively and all at once, with `M-x ert RET t RET`
+3. From the terminal, in batch mode, with `emacs -batch -l ert -l my-test.el -f ert-run-tests-batch-and-exit`
+4. Other options can be found in the docs, `C-h i m ert RET`
+
+## Submitting your solution
+
+You can submit your solution using the `exercism submit difference-of-squares.el` command.
+This command will upload your solution to the Exercism website and print the solution page's URL.
+
+It's possible to submit an incomplete solution which allows you to:
+
+- See how others have completed the exercise
+- Request help from a mentor
+
+## Need to get help?
+
+If you'd like help solving the exercise, check the following pages:
+
+- The [Emacs Lisp track's documentation](https://exercism.org/docs/tracks/emacs-lisp)
+- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
+
+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
+
+To get help if you're having trouble, you can use one of the following resources:
+
+- [The Emacs Wiki](http://emacswiki.org/) is invaluable. Spend lots of time here.
+- [The Emacs Editor](http://www.gnu.org/software/emacs/manual/html_node/emacs/index.html) is the official manual for GNU Emacs.
+- IRC - there are [freenode](https://freenode.net/) channels for `#emacs`, `#prelude`, and many Emacs
+ packages, and many helpful folks around. And with emacs, IRC is as close as
+ `M-x erc`.
+- [Exercism Support](https://gitter.im/exercism/support) Gitter chat is also a good place to get help from the
+ exercism community.
+- [StackOverflow](http://stackoverflow.com/questions/tagged/elisp) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions. \ No newline at end of file
diff --git a/exercism/emacs-lisp/difference-of-squares/README.md b/exercism/emacs-lisp/difference-of-squares/README.md
new file mode 100644
index 0000000..ae757fa
--- /dev/null
+++ b/exercism/emacs-lisp/difference-of-squares/README.md
@@ -0,0 +1,38 @@
+# Difference Of Squares
+
+Welcome to Difference Of Squares on Exercism's Emacs Lisp Track.
+If you need help running the tests or submitting your code, check out `HELP.md`.
+
+## Instructions
+
+Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.
+
+The square of the sum of the first ten natural numbers is
+(1 + 2 + ... + 10)² = 55² = 3025.
+
+The sum of the squares of the first ten natural numbers is
+1² + 2² + ... + 10² = 385.
+
+Hence the difference between the square of the sum of the first
+ten natural numbers and the sum of the squares of the first ten
+natural numbers is 3025 - 385 = 2640.
+
+You are not expected to discover an efficient solution to this yourself from
+first principles; research is allowed, indeed, encouraged. Finding the best
+algorithm for the problem is a key skill in software engineering.
+
+## Source
+
+### Created by
+
+- @canweriotnow
+
+### Contributed to by
+
+- @Scientifica96
+- @vermiculus
+- @yurrriq
+
+### Based on
+
+Problem 6 at Project Euler - http://projecteuler.net/problem=6 \ No newline at end of file
diff --git a/exercism/emacs-lisp/difference-of-squares/difference-of-squares-test.el b/exercism/emacs-lisp/difference-of-squares/difference-of-squares-test.el
new file mode 100644
index 0000000..56c5559
--- /dev/null
+++ b/exercism/emacs-lisp/difference-of-squares/difference-of-squares-test.el
@@ -0,0 +1,40 @@
+;;; difference-of-squares-test.el --- Tests for difference-of-squares (exercism)
+
+;;; Commentary:
+
+;;; Code:
+
+(load-file "difference-of-squares.el")
+
+(ert-deftest square-of-sum-to-5 ()
+ (should (= 225 (square-of-sum 5))))
+
+(ert-deftest sum-of-squares-to-5 ()
+ (should (= 55 (sum-of-squares 5))))
+
+(ert-deftest difference-of-squares-to-5 ()
+ (should (= 170 (difference 5))))
+
+
+(ert-deftest square-of-sum-to-10 ()
+ (should (= 3025 (square-of-sum 10))))
+
+(ert-deftest sum-of-squares-to-10 ()
+ (should (= 385 (sum-of-squares 10))))
+
+(ert-deftest difference-of-squares-to-10 ()
+ (should (= 2640 (difference 10))))
+
+
+(ert-deftest square-of-sum-to-100 ()
+ (should (= 25502500 (square-of-sum 100))))
+
+(ert-deftest sum-of-squares-to-100 ()
+ (should (= 338350 (sum-of-squares 100))))
+
+(ert-deftest difference-of-squares-to-100 ()
+ (should (= 25164150 (difference 100))))
+
+
+(provide 'difference-of-squares-test)
+;;; difference-of-squares-test.el ends here
diff --git a/exercism/emacs-lisp/difference-of-squares/difference-of-squares.el b/exercism/emacs-lisp/difference-of-squares/difference-of-squares.el
new file mode 100644
index 0000000..4b7e452
--- /dev/null
+++ b/exercism/emacs-lisp/difference-of-squares/difference-of-squares.el
@@ -0,0 +1,21 @@
+;;; difference-of-squares.el --- Difference of Squares (exercism)
+
+;;; Commentary:
+
+;;; Code:
+(require 'cl)
+
+(defun square-of-sum (n)
+ (cl-loop for i from 1 to n
+ sum i into s
+ finally return (* s s)))
+
+(defun sum-of-squares (n)
+ (cl-loop for i from 1 to n
+ sum (* i i)))
+
+(defun difference (n)
+ (- (square-of-sum n) (sum-of-squares n)))
+
+(provide 'difference-of-squares)
+;;; difference-of-squares.el ends here