Skip to content

Commit

Permalink
Add square-root exercise (#2349)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Jan 22, 2025
1 parent 52853b5 commit 83284a2
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2649,6 +2649,19 @@
"tuples"
],
"difficulty": 5
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "9855aaff-979d-4cab-9487-d0b8750c014a",
"practices": [
"while-loops"
],
"prerequisites": [
"numbers",
"while-loops"
],
"difficulty": 2
}
],
"foregone": [
Expand Down
8 changes: 8 additions & 0 deletions exercises/Exercises.sln
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,11 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LogAnalysis", "concept\log-analysis\LogAnalysis.csproj", "{D66F4A88-0765-4D6B-A8A4-933E5882EC92}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EliudsEggs", "practice\eliuds-eggs\EliudsEggs.csproj", "{2F3B7384-F4CA-4925-B07C-E05DB3FAC01B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Knapsack", "practice\knapsack\Knapsack.csproj", "{90C70CAA-A225-4D66-8B42-6AC82AD1D5DC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SquareRoot", "practice\square-root\SquareRoot.csproj", "{5C05051E-D46C-4544-9CF8-F2A748F63172}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1032,6 +1035,10 @@ Global
{90C70CAA-A225-4D66-8B42-6AC82AD1D5DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90C70CAA-A225-4D66-8B42-6AC82AD1D5DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90C70CAA-A225-4D66-8B42-6AC82AD1D5DC}.Release|Any CPU.Build.0 = Release|Any CPU
{5C05051E-D46C-4544-9CF8-F2A748F63172}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C05051E-D46C-4544-9CF8-F2A748F63172}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C05051E-D46C-4544-9CF8-F2A748F63172}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C05051E-D46C-4544-9CF8-F2A748F63172}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1207,6 +1214,7 @@ Global
{D66F4A88-0765-4D6B-A8A4-933E5882EC92} = {3303F74B-62AC-47B7-A8AA-F93A52A1C95C}
{2F3B7384-F4CA-4925-B07C-E05DB3FAC01B} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
{90C70CAA-A225-4D66-8B42-6AC82AD1D5DC} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
{5C05051E-D46C-4544-9CF8-F2A748F63172} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AB4EA6C9-5461-4024-BDC7-2AE0C3A85CD1}
Expand Down
18 changes: 18 additions & 0 deletions exercises/practice/square-root/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Instructions

Your task is to calculate the square root of a given number.

- Try to avoid using the pre-existing math libraries of your language.
- As input you'll be given a positive whole number, i.e. 1, 2, 3, 4…
- You are only required to handle cases where the result is a positive whole number.

Some potential approaches:

- Linear or binary search for a number that gives the input number when squared.
- Successive approximation using Newton's or Heron's method.
- Calculating one digit at a time or one bit at a time.

You can check out the Wikipedia pages on [integer square root][integer-square-root] and [methods of computing square roots][computing-square-roots] to help with choosing a method of calculation.

[integer-square-root]: https://en.wikipedia.org/wiki/Integer_square_root
[computing-square-roots]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
10 changes: 10 additions & 0 deletions exercises/practice/square-root/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Introduction

We are launching a deep space exploration rocket and we need a way to make sure the navigation system stays on target.

As the first step in our calculation, we take a target number and find its square root (that is, the number that when multiplied by itself equals the target number).

The journey will be very long.
To make the batteries last as long as possible, we had to make our rocket's onboard computer very power efficient.
Unfortunately that means that we can't rely on fancy math libraries and functions, as they use more power.
Instead we want to implement our own square root calculation.
141 changes: 141 additions & 0 deletions exercises/practice/square-root/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
###############################
# Core EditorConfig Options #
###############################

; This file is for unifying the coding style for different editors and IDEs.
; More information at:
; https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference?view=vs-2017
; https://docs.microsoft.com/en-us/visualstudio/ide/create-portable-custom-editor-options?view=vs-2017

root = true

[*]
indent_style = space

[SquareRoot.cs]
indent_size = 4

###############################
# .NET Coding Conventions #
###############################

# Organize usings
dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = true

# this. preferences
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion

# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion

# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:none
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion

# Modifier preferences
dotnet_style_require_accessibility_modifiers = always:suggestion
dotnet_style_readonly_field = true:suggestion

# Expression-level preferences
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion

###############################
# Naming Conventions #
###############################

# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case

# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const

###############################
# C# Code Style Rules #
###############################

# var preferences
csharp_style_var_for_built_in_types = true:none
csharp_style_var_when_type_is_apparent = true:none
csharp_style_var_elsewhere = true:none

# Expression-bodied members
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion

# Pattern-matching preferences
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion

# Null-checking preferences
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion

# Modifier preferences
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion

# Expression-level preferences
csharp_prefer_braces = true:none
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion

###############################
# C# Formatting Rules #
###############################

# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = false
csharp_new_line_before_members_in_anonymous_types = false
csharp_new_line_between_query_expression_clauses = true

# Indentation preferences
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = flush_left

# Space preferences
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false

# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = true
16 changes: 16 additions & 0 deletions exercises/practice/square-root/.meta/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public static class SquareRoot
{
public static int Root(int number)
{
var i = 1;
var result = 1;

while (result <= number)
{
i++;
result = i * i;
}

return i - 1;
}
}
19 changes: 19 additions & 0 deletions exercises/practice/square-root/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"SquareRoot.cs"
],
"test": [
"SquareRootTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"blurb": "Given a natural radicand, return its square root.",
"source": "wolf99",
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
}
28 changes: 28 additions & 0 deletions exercises/practice/square-root/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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.

[9b748478-7b0a-490c-b87a-609dacf631fd]
description = "root of 1"

[7d3aa9ba-9ac6-4e93-a18b-2e8b477139bb]
description = "root of 4"

[6624aabf-3659-4ae0-a1c8-25ae7f33c6ef]
description = "root of 25"

[93beac69-265e-4429-abb1-94506b431f81]
description = "root of 81"

[fbddfeda-8c4f-4bc4-87ca-6991af35360e]
description = "root of 196"

[c03d0532-8368-4734-a8e0-f96a9eb7fc1d]
description = "root of 65025"
9 changes: 9 additions & 0 deletions exercises/practice/square-root/SquareRoot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

public static class SquareRoot
{
public static int Root(int number)
{
throw new NotImplementedException("You need to implement this method.");
}
}
22 changes: 22 additions & 0 deletions exercises/practice/square-root/SquareRoot.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
40 changes: 40 additions & 0 deletions exercises/practice/square-root/SquareRootTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Xunit;

public class SquareRootTests
{
[Fact]
public void Root_of_1()
{
Assert.Equal(1, SquareRoot.Root(1));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Root_of_4()
{
Assert.Equal(2, SquareRoot.Root(4));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Root_of_25()
{
Assert.Equal(5, SquareRoot.Root(25));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Root_of_81()
{
Assert.Equal(9, SquareRoot.Root(81));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Root_of_196()
{
Assert.Equal(14, SquareRoot.Root(196));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Root_of_65025()
{
Assert.Equal(255, SquareRoot.Root(65025));
}
}
9 changes: 9 additions & 0 deletions generators/Exercises/Generators/SquareRoot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

using Exercism.CSharp.Output;

namespace Exercism.CSharp.Exercises.Generators;

internal class SquareRoot : ExerciseGenerator
{
}

0 comments on commit 83284a2

Please sign in to comment.