forked from TheRenegadeCoder/sample-programs-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generated Sample Programs website automatically
on-behalf-of: @TheRenegadeCoder <[email protected]>
- Loading branch information
1 parent
d00a699
commit b79cde1
Showing
6 changed files
with
135 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
--- | ||
date: 2018-03-15 | ||
last-modified: 2024-02-11 | ||
last-modified: 2024-02-15 | ||
layout: default | ||
title: Sample Programs in Every Language | ||
--- | ||
|
||
Welcome to Sample Programs in Every Language, a collection of code snippets in as many languages as possible. Thanks for taking an interest in our collection which currently contains 1051 articles written by 230 authors. | ||
Welcome to Sample Programs in Every Language, a collection of code snippets in as many languages as possible. Thanks for taking an interest in our collection which currently contains 1052 articles written by 230 authors. | ||
|
||
If you'd like to contribute to this growing collection, check out our [contributing document](https://github.com/TheRenegadeCoder/sample-programs/blob/master/.github/CONTRIBUTING.md) for more information. In addition, you can explore our documentation which is organized by [project](/projects) and by [language](/languages). If you don't find what you're look for, check out our list of related [open-source projects](/related). Finally, if code isn't your thing but you'd still like to help, there are plenty of other ways to [support the project](https://therenegadecoder.com/updates/5-ways-you-can-support-the-renegade-coder/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
--- | ||
authors: | ||
- rzuckerm | ||
date: 2024-02-15 | ||
featured-image: maximum-subarray-in-every-language.jpg | ||
last-modified: 2024-02-15 | ||
layout: default | ||
tags: | ||
- beef | ||
- maximum-subarray | ||
title: Maximum Subarray in Beef | ||
--- | ||
|
||
Welcome to the [Maximum Subarray](https://sampleprograms.io/projects/maximum-subarray) in [Beef](https://sampleprograms.io/languages/beef) page! Here, you'll find the source code for this program as well as a description of how the program works. | ||
|
||
## Current Solution | ||
|
||
{% raw %} | ||
|
||
```beef | ||
using System; | ||
using System.Collections; | ||
|
||
namespace MaximumSubarray; | ||
|
||
class Program | ||
{ | ||
public static void Usage() | ||
{ | ||
Console.WriteLine( | ||
""" | ||
Usage: Please provide a list of integers in the format: "1, 2, 3, 4, 5" | ||
""" | ||
); | ||
Environment.Exit(0); | ||
} | ||
|
||
public static Result<T> ParseInt<T>(StringView str) | ||
where T : IParseable<T> | ||
{ | ||
StringView trimmedStr = scope String(str); | ||
trimmedStr.Trim(); | ||
|
||
// T.Parse ignores single quotes since they are treat as digit separators -- e.g. 1'000 | ||
if (trimmedStr.Contains('\'')) | ||
{ | ||
return .Err; | ||
} | ||
|
||
return T.Parse(trimmedStr); | ||
} | ||
|
||
public static Result<void> ParseIntList<T>(StringView str, List<T> arr) | ||
where T: IParseable<T> | ||
{ | ||
arr.Clear(); | ||
for (StringView item in str.Split(',')) | ||
{ | ||
switch (ParseInt<T>(item)) | ||
{ | ||
case .Ok(let val): | ||
arr.Add(val); | ||
|
||
case .Err: | ||
return .Err; | ||
} | ||
} | ||
|
||
return .Ok; | ||
} | ||
|
||
// Find maximum subarray using Kadane's algorithm. | ||
// Source: https://en.wikipedia.org/wiki/Maximum_subarray_problem#No_empty_subarrays_admitted | ||
public static T MaximumSubarray<T>(List<T> arr) | ||
where T : operator explicit int, operator T + T, IMinMaxValue<T> | ||
where int : operator T <=> T | ||
{ | ||
T bestSum = T.MinValue; | ||
T currentSum = default(T); | ||
for (T val in arr) | ||
{ | ||
currentSum = val + Math.Max<T>(default(T), currentSum); | ||
bestSum = Math.Max<T>(currentSum, bestSum); | ||
} | ||
|
||
return bestSum; | ||
} | ||
|
||
public static int Main(String[] args) | ||
{ | ||
if (args.Count < 1) | ||
{ | ||
Usage(); | ||
} | ||
|
||
List<int32> arr = scope .(); | ||
if (ParseIntList<int32>(args[0], arr) case .Err) | ||
{ | ||
Usage(); | ||
} | ||
|
||
int32 result = MaximumSubarray<int32>(arr); | ||
Console.WriteLine(result); | ||
|
||
return 0; | ||
} | ||
} | ||
|
||
``` | ||
|
||
{% endraw %} | ||
|
||
Maximum Subarray in [Beef](https://sampleprograms.io/languages/beef) was written by: | ||
|
||
- rzuckerm | ||
|
||
If you see anything you'd like to change or update, [please consider contributing](https://github.com/TheRenegadeCoder/sample-programs). | ||
|
||
## How to Implement the Solution | ||
|
||
No 'How to Implement the Solution' section available. [Please consider contributing](https://github.com/TheRenegadeCoder/sample-programs-website). | ||
|
||
## How to Run the Solution | ||
|
||
No 'How to Run the Solution' section available. [Please consider contributing](https://github.com/TheRenegadeCoder/sample-programs-website). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters