Skip to content

Commit

Permalink
Generated Sample Programs website automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Feb 12, 2024
1 parent 7d1f4e2 commit d00a699
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ 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 1050 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 1051 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/).
1 change: 1 addition & 0 deletions docs/languages/beef/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ No 'Description' section available. [Please consider contributing](https://githu
- [Insertion Sort in Beef](https://sampleprograms.io/projects/insertion-sort/beef)
- [Josephus Problem in Beef](https://sampleprograms.io/projects/josephus-problem/beef)
- [Linear Search in Beef](https://sampleprograms.io/projects/linear-search/beef)
- [Longest Common Subsequence in Beef](https://sampleprograms.io/projects/longest-common-subsequence/beef)
- [Longest Palindromic Substring in Beef](https://sampleprograms.io/projects/longest-palindromic-substring/beef)
- [Longest Word in Beef](https://sampleprograms.io/projects/longest-word/beef)
- [Maximum Array Rotation in Beef](https://sampleprograms.io/projects/maximum-array-rotation/beef)
Expand Down
4 changes: 2 additions & 2 deletions docs/languages/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ layout: default
title: Programming Languages
---

Welcome to the Languages page! Here, you'll find a list of all of the languages represented in the collection. At this time, there are 153 languages, of which 152 are tested, 1 is untestable, and 860 code snippets.
Welcome to the Languages page! Here, you'll find a list of all of the languages represented in the collection. At this time, there are 153 languages, of which 152 are tested, 1 is untestable, and 861 code snippets.

## Language Collections by Letter

Expand Down Expand Up @@ -56,7 +56,7 @@ The 'A' collection contains 4 languages, of which 4 are tested, and 41 code snip

### B

The 'B' collection contains 9 languages, of which 9 are tested, and 62 code snippets.
The 'B' collection contains 9 languages, of which 9 are tested, and 63 code snippets.

- [Baik](https://sampleprograms.io/languages/baik)
- [Ballerina](https://sampleprograms.io/languages/ballerina)
Expand Down
183 changes: 183 additions & 0 deletions docs/projects/longest-common-subsequence/beef/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
---
authors:
- rzuckerm
date: 2024-02-11
featured-image: longest-common-subsequence-in-every-language.jpg
last-modified: 2024-02-11
layout: default
tags:
- beef
- longest-common-subsequence
title: Longest Common Subsequence in Beef
---

Welcome to the [Longest Common Subsequence](https://sampleprograms.io/projects/longest-common-subsequence) 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 LongestCommonSubsequence;

class Program
{
public static void Usage()
{
Console.WriteLine(
"""
Usage: please provide two lists 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;
}

// Source: https://en.wikipedia.org/wiki/Longest_common_subsequence#Computing_the_length_of_the_LCS
//
// However, instead of storing lengths, an index to the subsequence is stored
public static void LongestCommonSubsequence<T>(List<T> arr1, List<T> arr2, List<T> result)
where int : operator T <=> T
{
int m = arr1.Count;
int n = arr2.Count;

// Initialize all subsequences to the empty sequence
int[,] c = new .[m + 1, n + 1];
List<List<T>> subsequences = new .();
subsequences.Add(new List<T>());

// Find the longest common subsequence using prior subsequences
for (int i in 1...m)
{
for (int j in 1...n)
{
// If common element found, create new subsequence based on prior
// subsequence with the common element appended
if (arr1[i - 1] == arr2[j - 1])
{
c[i, j] = subsequences.Count;
List<T> newSubsequence = new .() ..
AddRange(subsequences[c[i - 1, j - 1]]) ..
Add(arr1[i - 1]);
subsequences.Add(newSubsequence);
}
// Else, reuse the longer of the two prior subsequences
else
{
int index1 = c[i, j - 1];
int index2 = c[i - 1, j];
c[i, j] = (subsequences[index1].Count > subsequences[index2].Count) ?
index1 : index2;
}
}
}

// Store result
result.Clear();
result.AddRange(subsequences[c[m, n]]);

// Deallocate subsequences
for (List<T> subsequence in subsequences)
{
delete subsequence;
}
delete subsequences;
delete c;
}

public static void ShowList<T>(List<T> arr)
{
String line = scope .();
for (T val in arr)
{
if (!line.IsEmpty)
{
line += ", ";
}

line.AppendF("{}", val);
}

Console.WriteLine(line);
}

public static int Main(String[] args)
{
if (args.Count < 2)
{
Usage();
}

List<int32> arr1 = scope .();
if (ParseIntList<int32>(args[0], arr1) case .Err)
{
Usage();
}

List<int32> arr2 = scope .();
if (ParseIntList<int32>(args[1], arr2) case .Err)
{
Usage();
}

List<int32> result = scope .();
LongestCommonSubsequence<int32>(arr1, arr2, result);
ShowList<int32>(result);
return 0;
}
}

```

{% endraw %}

Longest Common Subsequence 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).
3 changes: 2 additions & 1 deletion docs/projects/longest-common-subsequence/index.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
date: 2018-10-16
featured-image: longest-common-subsequence-in-every-language.jpg
last-modified: 2023-10-26
last-modified: 2024-02-11
layout: default
tags:
- longest-common-subsequence
Expand Down Expand Up @@ -88,6 +88,7 @@ Usage: please provide two lists in the format "1, 2, 3, 4, 5"
## Articles

- [Longest Common Subsequence in Algol68](https://sampleprograms.io/projects/longest-common-subsequence/algol68)
- [Longest Common Subsequence in Beef](https://sampleprograms.io/projects/longest-common-subsequence/beef)
- [Longest Common Subsequence in C](https://sampleprograms.io/projects/longest-common-subsequence/c)
- [Longest Common Subsequence in C#](https://sampleprograms.io/projects/longest-common-subsequence/c-sharp)
- [Longest Common Subsequence in C++](https://sampleprograms.io/projects/longest-common-subsequence/c-plus-plus)
Expand Down

0 comments on commit d00a699

Please sign in to comment.