1
0
Fork 0

new solution: emacs-lisp, binary.

main
Gökmen Görgen 2 months ago
parent 2a6c176131
commit a9ec7a2f64
  1. 41
      emacs-lisp/binary/HELP.md
  2. 46
      emacs-lisp/binary/README.md
  3. 34
      emacs-lisp/binary/binary-test.el
  4. 21
      emacs-lisp/binary/binary.el

@ -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.

@ -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-

@ -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

@ -0,0 +1,21 @@
;;; binary.el --- Binary exercise (exercism)
;;; Commentary:
;;; Code:
(defun to-decimal-from-list (remaining &optional total)
"Convert number list to its decimal equivalent."
(if (or (null remaining) (member nil remaining)) (or total 0)
(to-decimal-from-list (cdr remaining)
(+ (or total 0) (* (car remaining) (expt 2 (1- (length remaining))))))))
(defun to-decimal (number)
"Convert a binary number, represented as a string to its decimal equivalent."
(to-decimal-from-list (mapcar (lambda (n)
(if (or (< n 48) (> n 57)) nil
(- n 48)))
(string-to-list number))))
(provide 'binary)
;;; binary.el ends here
Loading…
Cancel
Save