Skip to content

Commit

Permalink
Add eliuds-eggs exercise (#2261)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored May 4, 2024
1 parent 0fa3cfd commit a7f7c1a
Show file tree
Hide file tree
Showing 12 changed files with 342 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2621,6 +2621,19 @@
"if-statements"
],
"difficulty": 5
},
{
"slug": "eliuds-eggs",
"name": "Eliud's Eggs",
"uuid": "c51655c7-a3a2-4e66-8e38-b6e8f5e33e8c",
"practices": [
"bit-manipulation"
],
"prerequisites": [
"numbers",
"bit-manipulation"
],
"difficulty": 3
}
],
"foregone": [
Expand Down
7 changes: 7 additions & 0 deletions exercises/Exercises.sln
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Zipper", "practice\zipper\Z
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
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1021,6 +1023,10 @@ Global
{D66F4A88-0765-4D6B-A8A4-933E5882EC92}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D66F4A88-0765-4D6B-A8A4-933E5882EC92}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D66F4A88-0765-4D6B-A8A4-933E5882EC92}.Release|Any CPU.Build.0 = Release|Any CPU
{2F3B7384-F4CA-4925-B07C-E05DB3FAC01B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2F3B7384-F4CA-4925-B07C-E05DB3FAC01B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2F3B7384-F4CA-4925-B07C-E05DB3FAC01B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2F3B7384-F4CA-4925-B07C-E05DB3FAC01B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1194,6 +1200,7 @@ Global
{FCB566EA-EDF0-4D97-8436-C101F384EB76} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
{82C5DF7B-6F86-40D3-8F65-4B63D41E23A5} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
{D66F4A88-0765-4D6B-A8A4-933E5882EC92} = {3303F74B-62AC-47B7-A8AA-F93A52A1C95C}
{2F3B7384-F4CA-4925-B07C-E05DB3FAC01B} = {E276EF69-669A-43E0-88AC-8ABB17A9C026}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AB4EA6C9-5461-4024-BDC7-2AE0C3A85CD1}
Expand Down
8 changes: 8 additions & 0 deletions exercises/practice/eliuds-eggs/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Instructions

Your task is to count the number of 1 bits in the binary representation of a number.

## Restrictions

Keep your hands off that bit-count functionality provided by your standard library!
Solve this one yourself using other basic tools instead.
47 changes: 47 additions & 0 deletions exercises/practice/eliuds-eggs/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Introduction

Your friend Eliud inherited a farm from her grandma Tigist.
Her granny was an inventor and had a tendency to build things in an overly complicated manner.
The chicken coop has a digital display showing an encoded number representing the positions of all eggs that could be picked up.

Eliud is asking you to write a program that shows the actual number of eggs in the coop.

The position information encoding is calculated as follows:

1. Scan the potential egg-laying spots and mark down a `1` for an existing egg or a `0` for an empty spot.
2. Convert the number from binary to decimal.
3. Show the result on the display.

Example 1:

```text
Chicken Coop:
_ _ _ _ _ _ _
|E| |E|E| | |E|
Resulting Binary:
1 0 1 1 0 0 1
Decimal number on the display:
89
Actual eggs in the coop:
4
```

Example 2:

```text
Chicken Coop:
_ _ _ _ _ _ _ _
| | | |E| | | | |
Resulting Binary:
0 0 0 1 0 0 0 0
Decimal number on the display:
16
Actual eggs in the coop:
1
```
141 changes: 141 additions & 0 deletions exercises/practice/eliuds-eggs/.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

[EliudsEggs.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
15 changes: 15 additions & 0 deletions exercises/practice/eliuds-eggs/.meta/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public static class EliudsEggs
{
public static int EggCount(int encodedCount)
{
var count = 0;

while (encodedCount > 0)
{
count += encodedCount & 1;
encodedCount >>= 1;
}

return count;
}
}
19 changes: 19 additions & 0 deletions exercises/practice/eliuds-eggs/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"EliudsEggs.cs"
],
"test": [
"EliudsEggsTests.cs"
],
"example": [
".meta/Example.cs"
]
},
"blurb": "Help Eliud count the number of eggs in her chicken coop by counting the number of 1 bits in a binary representation.",
"source": "Christian Willner, Eric Willigers",
"source_url": "https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5"
}
22 changes: 22 additions & 0 deletions exercises/practice/eliuds-eggs/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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.

[559e789d-07d1-4422-9004-3b699f83bca3]
description = "0 eggs"

[97223282-f71e-490c-92f0-b3ec9e275aba]
description = "1 egg"

[1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5]
description = "4 eggs"

[0c18be92-a498-4ef2-bcbb-28ac4b06cb81]
description = "13 eggs"
7 changes: 7 additions & 0 deletions exercises/practice/eliuds-eggs/EliudsEggs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public static class EliudsEggs
{
public static int EggCount(int encodedCount)
{
throw new System.NotImplementedException("You need to implement this method.");
}
}
26 changes: 26 additions & 0 deletions exercises/practice/eliuds-eggs/EliudsEggs.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

<ItemGroup>
<PackageReference Include="Exercism.Tests" Version="0.1.0-beta1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

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

</Project>
28 changes: 28 additions & 0 deletions exercises/practice/eliuds-eggs/EliudsEggsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Xunit;

public class EliudsEggsTests
{
[Fact]
public void Number_0_eggs()
{
Assert.Equal(0, EliudsEggs.EggCount(0));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Number_1_egg()
{
Assert.Equal(1, EliudsEggs.EggCount(16));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Number_4_eggs()
{
Assert.Equal(4, EliudsEggs.EggCount(89));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Number_13_eggs()
{
Assert.Equal(13, EliudsEggs.EggCount(2000000000));
}
}
9 changes: 9 additions & 0 deletions generators/Exercises/Generators/EliudsEggs.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 EliudsEggs : ExerciseGenerator
{
}

0 comments on commit a7f7c1a

Please sign in to comment.