Quellatalo.CombineCode.CSharp 0.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Quellatalo.CombineCode.CSharp --version 0.0.1                
NuGet\Install-Package Quellatalo.CombineCode.CSharp -Version 0.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Quellatalo.CombineCode.CSharp" Version="0.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Quellatalo.CombineCode.CSharp --version 0.0.1                
#r "nuget: Quellatalo.CombineCode.CSharp, 0.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Quellatalo.CombineCode.CSharp as a Cake Addin
#addin nuget:?package=Quellatalo.CombineCode.CSharp&version=0.0.1

// Install Quellatalo.CombineCode.CSharp as a Cake Tool
#tool nuget:?package=Quellatalo.CombineCode.CSharp&version=0.0.1                

Quellatalo.CombineCode.CSharp

A library to combine multiple written CSharp source files into one file.

Author: Quellatalo Nin

Overview

The project is still in early stage. It does not scan the local folders as much as IDEs/compilers do.

Please see the requirements follow the expected code style.

Requirements

It's just some standard styling rules:

Basically, namespaces should have matching folders, and types should have matching .cs files.

Example

Imagine we want to challenge HackerRank's Euler project, starting with the first problem.

Still, we want to code in a standard environment, writing multiple code files with reusable types.

The written code

First of all, let's create a console project CodePractice for online practice code, and write an interface for all problems:

namespace CodePractice;

/// <summary>
/// Represents a solution for an online practice problem.
/// </summary>
public interface ISolution
{
    /// <summary>
    /// Solves the problem (by reading from Console.In, then printing the results to Console.Out).
    /// </summary>
    void Solve();
}

When identifying the E001 problem, we noticed that an implementation of SumOfArithmeticSeries would be very helpful. So, we decided to create a Computing library and write such utility method:

using System.Numerics;

namespace Computing.Extensions;

/// <summary>
/// Common computations.
/// </summary>
public static class ArithmeticExtensions
{
    /// <summary>
    /// Calculates the sum of an arithmetic sequence.
    /// </summary>
    /// <param name="firstNumber">The first term.</param>
    /// <param name="difference">The common difference.</param>
    /// <param name="numberOfSeries">The number of series.</param>
    /// <typeparam name="T">Number type.</typeparam>
    /// <returns>The sum of the series.</returns>
    /// <remarks>https://www.mathsisfun.com/algebra/sequences-sums-arithmetic.html.</remarks>
    public static T SumOfArithmeticSeries<T>(this T firstNumber, T difference, T numberOfSeries)
        where T : IBinaryInteger<T>
        => (numberOfSeries * ((firstNumber << 1) + ((numberOfSeries - T.One) * difference))) >> 1;
}

Now we solve the problem:

using System;
using System.Globalization;
using System.Text;
using Computing.Extensions;

namespace CodePractice.Euler.E001MultipleOf3And5;

class E001 : ISolution
{
    public const int MaxN = 1_000_000_000;
    public const int MaxT = 100_000;
    public const int Min = 1;

    public static long MultipleOf3Or5(int upToExclusive)
    {
        int largest = upToExclusive - 1;
        return 3L.SumOfArithmeticSeries(3, largest / 3) +
               5L.SumOfArithmeticSeries(5, largest / 5) -
               15L.SumOfArithmeticSeries(15, largest / 15);
    }

    public void Solve()
    {
        StringBuilder results = new();

        // up to 100,000
        int t = Convert.ToInt32(Console.ReadLine(), CultureInfo.InvariantCulture);
        for (int i = 0; i < t; i++)
        {
            // up to 1,000,000,000
            int n = Convert.ToInt32(Console.ReadLine(), CultureInfo.InvariantCulture);
            results.AppendLine(MultipleOf3Or5(n).ToString(CultureInfo.InvariantCulture));
        }

        Console.WriteLine(results.ToString());
    }
}

In CodePractice project's Program.cs, we can execute the scenario:

using CodePractice.Euler.E001MultipleOf3And5;

new E001().Solve();

In summary, we have the following code structure:

  • (Console) CodePractice
    • Euler
      • E001MultipleOf3And5
        • E001.cs
    • ISolution.cs
    • Program.cs
  • (Library) Computing
    • Extensions
      • ArithmeticExtensions.cs

Submitting to the online editor

Now to submit our solution, we can add nuget reference to this package (Quellatalo.CombineCode.CSharp) and change our Program.cs to:

using System.IO;
using Quellatalo.CombineCode.CSharp;

// The combined source code file to be generated
const string TransformedFile = "Transformed.cs";

// The rewriter to add Solution class and main method required by HackerRank
MainRewriter rewriter = new("Solution", $"new E001().Solve();");

File.WriteAllText(
    TransformedFile,
    CSharpSourceInfo
        .ReadFromFile(
            Path.Combine(
                CSharpSourceInfo.SolutionDirectory.FullName,
                "CodePractice",
                "Euler",
                "E001MultipleOf3And5",
                "E001.cs"))
        .CompileToOneSource(rewriter));

// Open file using the OS's default behavior.
FileUtils.OpenFile(TransformedFile);

Executing it will open up a Transformed.cs file with the following content:

namespace Computing.Extensions
{
    using System;
    using System.Linq;
    using System.Numerics;

    /// <summary>
    /// Common computations.
    /// </summary>
    public static class ArithmeticExtensions
    {
        /// <summary>
        /// Calculates the sum of an arithmetic sequence.
        /// </summary>
        /// <param name = "firstNumber">The first term.</param>
        /// <param name = "difference">The common difference.</param>
        /// <param name = "numberOfSeries">The number of series.</param>
        /// <typeparam name = "T">Number type.</typeparam>
        /// <returns>The sum of the series.</returns>
        /// <remarks>https://www.mathsisfun.com/algebra/sequences-sums-arithmetic.html.</remarks>
        public static T SumOfArithmeticSeries<T>(this T firstNumber, T difference, T numberOfSeries)
            where T : IBinaryInteger<T> => (numberOfSeries * ((firstNumber << 1) + ((numberOfSeries - T.One) * difference))) >> 1;
    }
}
namespace CodePractice
{
    /// <summary>
    /// Represents a solution for an online practice problem.
    /// </summary>
    public interface ISolution
    {
        /// <summary>
        /// Solves the problem.
        /// </summary>
        void Solve();
    }
}
namespace CodePractice.Euler.E001MultipleOf3And5
{
    using System;
    using System.Globalization;
    using System.Text;
    using Computing.Extensions;

    class E001 : ISolution
    {
        public const int MaxN = 1_000_000_000;
        public const int MaxT = 100_000;
        public const int Min = 1;
        public static long MultipleOf3Or5(int upToExclusive)
        {
            int largest = upToExclusive - 1;
            return 3L.SumOfArithmeticSeries(3, largest / 3) + 5L.SumOfArithmeticSeries(5, largest / 5) - 15L.SumOfArithmeticSeries(15, largest / 15);
        }

        public void Solve()
        {
            StringBuilder results = new();
            // up to 100,000
            int t = Convert.ToInt32(Console.ReadLine(), CultureInfo.InvariantCulture);
            for (int i = 0; i < t; i++)
            {
                // up to 1,000,000,000
                int n = Convert.ToInt32(Console.ReadLine(), CultureInfo.InvariantCulture);
                results.AppendLine(MultipleOf3Or5(n).ToString(CultureInfo.InvariantCulture));
            }

            Console.WriteLine(results.ToString());
        }
    }

    class Solution
    {
        public static void Main()
        {
            new E001().Solve();
        }
    }
}

Now we can copy its content and submit to HackerRank.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.8 110 11/13/2024
0.0.7 93 11/1/2024
0.0.6 157 10/16/2024
0.0.5 107 10/14/2024
0.0.4 111 10/14/2024
0.0.3 99 10/14/2024 0.0.3 is deprecated.
0.0.2 105 10/11/2024
0.0.1 119 10/8/2024