summaryrefslogtreecommitdiff
path: root/exercism/emacs-lisp/binary
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 /exercism/emacs-lisp/binary
parent65951ec5972aec2abbfc8d0dfa2a7227a0fa7223 (diff)
add exercises
Diffstat (limited to 'exercism/emacs-lisp/binary')
-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
6 files changed, 162 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