Skip to content

Commit

Permalink
Add exercise: two-fer (#213)
Browse files Browse the repository at this point in the history
* Add exercise: two-fer

* Correct two-fer example

* Reduce indent to 2 spaces for consistency
  • Loading branch information
simisc committed Jul 20, 2023
1 parent db16e39 commit 82b73ae
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "two-fer",
"name": "Two-Fer",
"uuid": "0ee87521-e88e-4230-87da-26eba66db914",
"practices": [],
"prerequisites": [],
"difficulty": 1
}
]
}
Expand Down
25 changes: 25 additions & 0 deletions exercises/practice/two-fer/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Instructions

Your task is to determine what you will say as you give away the extra cookie.

If your friend likes cookies, and is named Do-yun, then you will say:

```text
One for Do-yun, one for me.
```

If your friend doesn't like cookies, you give the cookie to the next person in line at the bakery.
Since you don't know their name, you will say _you_ instead.

```text
One for you, one for me.
```

Here are some examples:

| Name | Dialogue |
| :----- | :-------------------------- |
| Alice | One for Alice, one for me. |
| Bohdan | One for Bohdan, one for me. |
| | One for you, one for me. |
| Zaphod | One for Zaphod, one for me. |
8 changes: 8 additions & 0 deletions exercises/practice/two-fer/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Introduction

In some English accents, when you say "two for" quickly, it sounds like "two fer".
Two-for-one is a way of saying that if you buy one, you also get one for free.
So the phrase "two-fer" often implies a two-for-one offer.

Imagine a bakery that has a holiday offer where you can buy two cookies for the price of one ("two-fer one!").
You go for the offer and (very generously) decide to give the extra cookie to a friend.
18 changes: 18 additions & 0 deletions exercises/practice/two-fer/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": [
"simisc"
],
"files": {
"solution": [
"two_fer.f90"
],
"test": [
"two_fer_test.f90"
],
"example": [
".meta/example.f90"
]
},
"blurb": "Create a sentence of the form \"One for X, one for me.\".",
"source_url": "https://github.com/exercism/problem-specifications/issues/757"
}
22 changes: 22 additions & 0 deletions exercises/practice/two-fer/.meta/example.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module two_fer
implicit none

contains

function twoFer(name) result(phrase)
character(*), intent(in), optional :: name
character(:), allocatable :: phrase
character(:), allocatable :: newname

if (present(name)) then
allocate(character(len(name)) :: newname)
newname = name
else
allocate(character(3) :: newname)
newname = "you"
end if

phrase = "One for " // newname // ", one for me."
end function twoFer

end module two_fer
19 changes: 19 additions & 0 deletions exercises/practice/two-fer/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[1cf3e15a-a3d7-4a87-aeb3-ba1b43bc8dce]
description = "no name given"

[b4c6dbb8-b4fb-42c2-bafd-10785abe7709]
description = "a name given"

[3549048d-1a6e-4653-9a79-b0bda163e8d5]
description = "another name given"
71 changes: 71 additions & 0 deletions exercises/practice/two-fer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Basic CMake project
cmake_minimum_required(VERSION 3.0.0)

# Name the project after the exercise
project(${exercise} Fortran)

# set debug build default
set(CMAKE_BUILD_TYPE Debug)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

# Activate Fortran compiler warnings
if(CMAKE_Fortran_COMPILER_ID MATCHES "Intel") # Intel fortran
if(WIN32)
set (CCMAKE_Fortran_FLAG ${CCMAKE_Fortran_FLAGS} "/warn:all")
else()
set (CMAKE_Fortran_FLAGS ${CCMAKE_Fortran_FLAGS} "-warn all")
endif()
endif()
if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU") # GFrotran
set (CMAKE_Fortran_FLAGS ${CCMAKE_Fortran_FLAGS} "-std=f2008 -W -Wall -Wextra -pedantic -fbacktrace")
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
add_definitions(-DEXERCISM_RUN_ALL_TESTS)
set(exercise_f90 ${file}_build_all.f90)
else()
# if building in exercise folder add testlib
set(testlib_path ${CMAKE_CURRENT_SOURCE_DIR}/testlib)
if (NOT EXISTS ${testlib_path})
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../../../testlib) # in git repo locally?
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../../../testlib DESTINATION ${CMAKE_CURRENT_SOURCE_DIR} PATTERN *)
else() # get from git with http
message("Downloading testlib from https://raw.githubusercontent.com/exercism/fortran/master")
file(MAKE_DIRECTORY ${testlib_path} )
file(DOWNLOAD https://raw.githubusercontent.com/exercism/fortran/master/testlib/TesterMain.f90 ${testlib_path}/TesterMain.f90 SHOW_PROGRESS)
file(DOWNLOAD https://raw.githubusercontent.com/exercism/fortran/master/testlib/CMakeLists.txt ${testlib_path}/CMakeLists.txt SHOW_PROGRESS)
endif()
endif()
# add lib to build
add_subdirectory(testlib)
include_directories(testlib ${CMAKE_CURRENT_BINARY_DIR}/testlib)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.f90)
set(exercise_f90 ${file}.f90)
endif()
endif()

# Get test_files if exercise needs it
file(GLOB test_files test_files/*.*)
foreach(test_file ${test_files})
message("Copying ${test_file} to binary directory for example ${file}")
configure_file(${test_file} . COPYONLY)
endforeach()

# Build executable from sources and headers
add_executable(${exercise} ${exercise_f90} ${file}_test.f90 )

target_link_libraries(${exercise} TesterMain)

include(CTest)

add_test (${exercise}_test ${exercise} )

# Run the tests on every build
#add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})

13 changes: 13 additions & 0 deletions exercises/practice/two-fer/two_fer.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module two_fer
implicit none

contains

function twoFer(name) result(phrase)
character(*), intent(in), optional :: name
character(:), allocatable :: phrase

phrase = name ! Replace this line with your implementation
end function twoFer

end module two_fer
19 changes: 19 additions & 0 deletions exercises/practice/two-fer/two_fer_test.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
! The tests were created from https://github.com/exercism/problem-specifications/blob/main/exercises/two-fer/canonical-data.json

program two_fer_test_main
use TesterMain
use two_fer
implicit none

! Test 1: no name given
call assert_equal("One for you, one for me.", twoFer(), "no name given")

! Test 2: a name given
call assert_equal("One for Alice, one for me.", twoFer("Alice"), "a name given")

! Test 3: another name given
call assert_equal("One for Bob, one for me.", twoFer("Bob"), "another name given")

call test_report()

end program

0 comments on commit 82b73ae

Please sign in to comment.