Two-fer
or 2-fer
is short for two for one. One for you and one for me.
Given a name, return a string with the message:
One for X, one for me.
Where X is the given name.
However, if the name is missing, return the string:
One for you, one for me.
Here are some examples:
Name | String to return |
---|---|
Alice | One for Alice, one for me. |
Bob | One for Bob, one for me. |
One for you, one for me. | |
Zaphod | One for Zaphod, one for me. |
For installation and learning resources, refer to the exercism Racket page.
You can run the provided tests through DrRacket, or via the command line.
To run the test through DrRacket, simply open the test file and click the 'Run' button in the upper right.
To run the test from the command line, run the test from the exercise directory with the following command:
raco test two-fer-test.rkt
which will display the following:
raco test: (submod "two-fer-test.rkt" test)
2 success(es) 0 failure(s) 0 error(s) 2 test(s) run
0
2 tests passed
https://github.com/exercism/problem-specifications/issues/757
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
#lang racket
(require "two-fer.rkt")
(module+ test
(require rackunit rackunit/text-ui)
(define suite
(test-suite
"two fer tests"
(test-equal? "no name given"
(two-fer)
"One for you, one for me.")
(test-equal? "a name given"
(two-fer "Alice")
"One for Alice, one for me.")
(test-equal? "another name given"
(two-fer "Bob")
"One for Bob, one for me.")))
(run-tests suite))
#lang racket
(provide two-fer)
(define two-fer
(lambda name
(if (empty? name)
"One for you, one for me."
(format "One for ~a, one for me." (first name)))))
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments