Skip to content

Commit bd76773

Browse files
committed
Updates
1 parent 69095f0 commit bd76773

16 files changed

+532
-6
lines changed

rosetta-code/solutions/100-doors.factor

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
! Opening only those doors is an optimization that may also be expressed;
2323
! however, as should be obvious, this defeats the intent of comparing
2424
! implementations across programming languages.
25+
!
26+
! Why doesn't syntax highlighting work on this page ?:
27+
!
28+
! Currently, there is a limit on how many <syntaxhighlight> tags can
29+
! appear on a page, so only the first few languages get highlighting, the
30+
! rest are shown in monochrome.
31+
! You could try "manual highlighting", possibly using one of the
32+
! highlighters on Syntax highlighting using Mediawiki formatting or
33+
! something similar.
2534

2635
USING: bit-arrays formatting fry kernel math math.ranges
2736
sequences ;

rosetta-code/solutions/abc-correlation.factor

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
!
33
! Task:
44
!
5-
! Get a string/input and store it into a variable or something and compare
6-
! the amount of "a", "b" and "c" . Other characters like "r", "h" or "5"
7-
! MUST be ignored. If the amount of "a"'s, "b"'s and "c"'s are equal
8-
! return true. Otherwise return false
5+
! Get a string input and store it into a variable or something and compare
6+
! the number of occurrences of the letters "a", "b" and "c". All other
7+
! characters MUST be ignored. If the "a"'s, "b"'s and "c"'s occur with
8+
! exactly equal frequency, return true; otherwise return false.
99

1010

rosetta-code/solutions/append-numbers-at-same-position-in-strings.factor

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
!
99
! list3 = [19,20,21,22,23,24,25,26,27]
1010
!
11-
! Turn the numbers them into strings and concatenate them.
11+
! At each index, turn the numbers into strings and concatenate them.
1212
!
1313
! The result should be:
1414
!

rosetta-code/solutions/brilliant-numbers.factor

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
!
2828
! See also
2929
! * Numbers Aplenty - Brilliant numbers
30+
! * - Like the task upper Limit 1E201
3031
! * OEIS:A078972 - Brilliant numbers: semiprimes whose prime factors have the same number of decimal digits
3132

3233
USING: assocs formatting grouping io kernel lists lists.lazy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
! Description
2+
!
3+
! The Brzozowski Algebraic Method is a technique used in automata theory,
4+
! specifically for constructing the minimal deterministic finite automaton
5+
! (DFA) from a given nondeterministic finite automaton (NFA). It was
6+
! introduced by Janusz Brzozowski in 1962 and involves the use of
7+
! algebraic transformations to simplify and optimize automata.
8+
!
9+
! As the workings of the algorithm are clearly described in the linked
10+
! article they will not be repeated here.
11+
!
12+
! Task:
13+
!
14+
! Implement the Brzozowski Algebraic Method to convert an NFA
15+
! representation into a regular expression. The implementation should:
16+
!
17+
! 1. Take as input:
18+
!
19+
!   - A 2D array 'a' representing the transition matrix of the NFA
20+
!   - An array 'b' representing the final states
21+
!
22+
! 2. Include functionality to:
23+
!
24+
!   - Handle basic regular expression operations (Empty, Epsilon, Character, Union, Concatenation, Star)
25+
!   - Print regular expressions in a readable format
26+
!   - Simplify regular expressions using algebraic rules
27+
!
28+
! 3. Apply the Brzozowski algorithm to:
29+
!
30+
!   - Process the transition matrix in reverse order
31+
!   - Update transitions using Star and Concatenation operations
32+
!   - Combine paths using Union operations
33+
!   - Generate a final regular expression representing the language accepted by the NFA
34+
!
35+
! 4. Include simplification rules for:
36+
!
37+
!   - Eliminating redundant unions (e + e → e)
38+
!   - Handling identity elements (e·1 → e, 1·e → e)
39+
!   - Handling zero elements (e·0 → 0, 0·e → 0)
40+
!   - Simplifying star operations (0* → 1, 1* → 1)
41+
!
42+
! The implementation should be able to process the given example input and
43+
! produce both the raw and simplified regular expressions as output.
44+
45+

rosetta-code/solutions/csv-data-manipulation.factor

+4
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,9 @@
2626
!
2727
! If possible, illustrate the use of built-in or standard functions,
2828
! methods, or libraries, that handle generic CSV files.
29+
!
30+
! Related tasks:
31+
!
32+
! - Convert_CSV_records_to_TSV
2933

3034

rosetta-code/solutions/cumulative-standard-deviation.factor

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
! Task
2+
!
13
! Write a stateful function, class, generator or co-routine that takes a
24
! series of floating point numbers, one at a time, and returns the running
35
! standard deviation of the series.

rosetta-code/solutions/exactly-three-adjacent-3-in-lists.factor

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
! list[5] = [4,6,8,7,2,3,3,3,1]
1414
!
1515
! For each list, print 'true' if the list contains exactly three '3's that
16-
! form a consecutive subsequence, otherwise print 'false'.
16+
! form a consecutive subsequence, otherwise print 'false'. That is, print
17+
! false unless there is exactly one run of three '3's and there is no
18+
! other occurrence of a '3'.
1719

1820

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
! The discovery of the Harriss Spiral was first publicized about 2015. It
2+
! is the brainchild of Edmund Orme Harriss, a British mathematician,
3+
! writer and artist. Since 2010 he has been at the Fulbright College of
4+
! Arts & Sciences at The University of Arkansas in Fayetteville, Arkansas,
5+
! where he is an Assistant Professor of Arts & Sciences (ARSC) and
6+
! Mathematical Sciences (MASC). He does research in the Geometry of
7+
! Tilings and Patterns,a branch of Convex and Discrete Geometry.
8+
!
9+
! In Harriss' own words, his namesake spiral, "…is constructed by taking a
10+
! rectangle with height 1 and length the real root of x^3-x-1=0. With this
11+
! rectangle you can cut off a similar rectangle, and then a square to get
12+
! another similar rectangle. You can repeat this construction on the
13+
! smaller similar rectangles, in each case getting a square and two more
14+
! similar rectangles. Now simply adding an arc of a circle to each square
15+
! (in the right way) gives the spiral, or more correctly the nest of
16+
! spirals." [Fractal spiral discovered by Edmund Harriss]
17+
!
18+
! The Harriss Spiral is a variant of the decomposition for the golden
19+
! spiral in which a rectangle is decomposed into three smaller units: a
20+
! rectangle similar to the original rotated 90◦, a square, and a similar
21+
! rectangle in the same orientation as the original rectangle. As in the
22+
! golden-spiral decomposition, the individual non-square units can be
23+
! decomposed further along these lines to create a cascading filling of
24+
! the rectangle with ever-smaller squares. Unlike in the golden spiral,
25+
! each square is incident on two smaller regions appearing in the same
26+
! generation; if arcs are drawn between each square and the square which
27+
! appeared in its previous generation The result is a branching structure,
28+
! shown at right.
29+
!
30+
! As with the golden spiral decomposition, the Harriss spiral requires a
31+
! specific aspect ratio for the original rectangle. While the golden
32+
! spiral requires an aspect ratio which is a solution to 𝜙2 = 𝜙 + 1 — the
33+
! Golden Ratio of 1:1.618 — the Harriss spiral requires an aspect ratio 𝜌
34+
! satisfying 𝜌3 = 𝜌 + 1, whose real solution is known as the plastic ratio
35+
! and equals 1:1.3247.
36+
!
37+
! Task:
38+
!
39+
! Create and display a Harriss Spiral in your language.
40+
41+

rosetta-code/solutions/hash-join.factor

+6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
! this operation is the nested loop join algorithm, but a more scalable
44
! alternative is the hash join algorithm.
55
!
6+
! Task
7+
!
68
! Implement the "hash join" algorithm, and demonstrate that it passes the
79
! test-case listed below.
810
!
911
! You should represent the tables as data structures that feel natural in
1012
! your programming language.
1113
!
14+
! Guidance
15+
!
1216
! The "hash join" algorithm consists of two steps:
1317
!
1418
! 1. Hash phase: Create a multimap from one of the two tables, mapping
@@ -41,6 +45,8 @@
4145
!       let c = the concatenation of row a and row b
4246
!       place row c in table C
4347
!
48+
! Test case
49+
!
4450
! +----------------------------------+----------------------------------+
4551
! | Input | Output |
4652
! +==================================+==================================+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
! Description
2+
!
3+
! Hierholzer's Algorithm is an efficient way to find Eulerian circuits in
4+
! a graph.
5+
!
6+
! Algorithm: Hierholzer's Algorithm
7+
! Input: Graph G = (V, E) represented as adjacency list
8+
! Output: Eulerian circuit as ordered list of vertices
9+
!
10+
! Function FindEulerianCircuit(Graph G):
11+
! // Initialize empty circuit and stack
12+
! Circuit = empty list
13+
! Stack = empty stack
14+
!
15+
! // Start from any vertex with non-zero degree
16+
! current = any vertex v in V where degree(v) > 0
17+
! Push current to Stack
18+
!
19+
! While Stack is not empty:
20+
! if OutDegree(current) == 0:
21+
! // Add to circuit when no more unused edges
22+
! Add current to front of Circuit
23+
! current = Pop from Stack
24+
! else:
25+
! // Follow an unused edge
26+
! Push current to Stack
27+
! next = any adjacent vertex with unused edge from current
28+
! Remove edge (current, next) from G
29+
! current = next
30+
!
31+
! Return Circuit
32+
!
33+
! Function VerifyEulerianCircuit(Graph G):
34+
! For each vertex v in G:
35+
! If InDegree(v) ≠ OutDegree(v):
36+
! Return false
37+
! If Graph is not strongly connected:
38+
! Return false
39+
! Return true
40+
!
41+
! Main Algorithm:
42+
! 1. If not VerifyEulerianCircuit(G):
43+
! Return "No Eulerian circuit exists"
44+
!
45+
! 2. Circuit = FindEulerianCircuit(G)
46+
!
47+
! 3. If Circuit contains all edges:
48+
! Return Circuit
49+
! Else:
50+
! Return "No Eulerian circuit exists"
51+
!
52+
! Preconditions:
53+
! - Graph must be connected
54+
! - All vertices must have equal in-degree and out-degree
55+
!
56+
! Time Complexity: O(|E|) where E is the number of edges
57+
! Space Complexity: O(|V|) where V is the number of vertices
58+
!
59+
! Task
60+
!
61+
! Implement the Hierholze's Algorithm in your language and test it using
62+
! the same examples as in the OCaml entry.
63+
64+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
! Description
2+
!
3+
! [http://cgosorio.es/Seshat/thompson?expr=a.(a%7Cb.a)*%7Cc*.a The
4+
! McNaughton-Yamada-Thompson algorithm] (aka. Thompson algorithm) is
5+
! designed to transform a regular expression into a Non-Deterministic
6+
! Finite Automaton (NDFA) that recognizes the language represented by the
7+
! regular expression.
8+
!
9+
! As the workings of the algorithm are clearly described in the linked
10+
! article they will not be repeated here.
11+
!
12+
! Task:
13+
!
14+
! Implement Thompson's Construction Algorithm to convert regular
15+
! expressions into NFAs and match strings. The implementation should:
16+
!
17+
! 1. Support the following operations:
18+
!
19+
!   - Concatenation (.)
20+
!   - Union/Alternation (|)
21+
!   - Kleene Star (*)
22+
!   - Plus (+)
23+
!   - Optional (?)
24+
!   - Basic character literals
25+
!
26+
! 2. Include the following components:
27+
!
28+
!   - A regex-to-postfix converter using Shunting Yard algorithm
29+
!   - NFA state and transition representation
30+
!   - Epsilon closure computation
31+
!   - Pattern matching functionality
32+
!
33+
! 3. Implement the following features:
34+
!
35+
!   - State structure with:
36+
!     * Character label
37+
!     * Two possible transitions (edge1, edge2)
38+
!     * Support for epsilon transitions
39+
!
40+
!   - NFA structure containing:
41+
!     * Initial state pointer
42+
!     * Accept state pointer
43+
!
44+
!   - String matching that:
45+
!     * Follows epsilon transitions
46+
!     * Tracks current possible states
47+
!     * Processes input characters
48+
!     * Determines if final state is accepting
49+
!
50+
! 4. Handle test cases demonstrating:
51+
!
52+
!   - Basic concatenation (a.b.c)
53+
!   - Star operations (c*)
54+
!   - Alternation (b|d)
55+
!   - Complex combinations ((a.(b|d))*)
56+
!   - Empty strings and various input patterns
57+
!
58+
! The implementation should efficiently construct NFAs from regular
59+
! expressions and correctly determine whether input strings match the
60+
! given patterns.
61+
62+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
! In the area of number decomposition algorithms, Pollard's rho algorithm
2+
! is a fast, yet fairly simple method to find factors of a number, using a
3+
! pseudorandom number generator modulo the number.
4+
!
5+
! Except for trivial factors like 2, 3, 5 etc., it is much faster than
6+
! trial division, finding a factor quite often, but may terminate without
7+
! a factor. Thus it cannot prove that a number is prime. If it is known
8+
! that a number is not prime, the algorithm can be re-iterated with
9+
! changed parameters to finally find a factor (not necessarily a prime
10+
! factor) with quite high probability.
11+
!
12+
! Task:
13+
!
14+
! Write a function to demonstrate the algorithm (see pseudocode in
15+
! Wikipedia). The intent is to show the principle as clear as possible in
16+
! the respective language, but avoid an endless loop. Enhanced versions
17+
! may be presented as extra examples.
18+
!
19+
! Warning:
20+
!
21+
! The variables 'x' and 'y' may be equal during an iteration, calling
22+
! 'gcd()' with first argument 0, while the GCD is only defined for
23+
! postitive numbers, so a library gdc() function might fail. If in this
24+
! case, the other argument is returned, the algorithm reports failure,
25+
! which is acceptable. The gcd() functions supplied for AWK and C have
26+
! this property.
27+
!
28+
! Example numbers, if supported:
29+
!
30+
! - 4294967213 = 75167 * 57139 (32 bits)
31+
! - 9759463979 = 98779 * 98801 (34 bits)
32+
! - 34225158206557151 = 130051349 * 263166499 (55 bits)
33+
! - 763218146048580636353 = 25998278833 * 29356487441 (70 bits)
34+
!
35+
! Related tasks:
36+
!
37+
! - prime decomposition
38+
! - AKS test for primes
39+
! - factors of an integer
40+
! - factors of a Mersenne number
41+
! - trial factoring of a Mersenne number
42+
! - partition an integer X into N primes
43+
! - sequence of primes by Trial Division
44+
!
45+
! References:
46+
!
47+
! - Wikipedia: Pollard's rho algorithm
48+
49+

0 commit comments

Comments
 (0)