Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
78
Task/ABC-problem/Ada/abc-problem.adb
Normal file
78
Task/ABC-problem/Ada/abc-problem.adb
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
with Ada.Characters.Handling;
|
||||
use Ada.Characters.Handling;
|
||||
|
||||
|
||||
package Abc is
|
||||
type Block_Faces is array(1..2) of Character;
|
||||
type Block_List is array(positive range <>) of Block_Faces;
|
||||
function Can_Make_Word(W: String; Blocks: Block_List) return Boolean;
|
||||
end Abc;
|
||||
|
||||
|
||||
package body Abc is
|
||||
|
||||
function Can_Make_Word(W: String; Blocks: Block_List) return Boolean is
|
||||
Used : array(Blocks'Range) of Boolean := (Others => False);
|
||||
subtype wIndex is Integer range W'First..W'Last;
|
||||
wPos : wIndex;
|
||||
begin
|
||||
if W'Length = 0 then
|
||||
return True;
|
||||
end if;
|
||||
wPos := W'First;
|
||||
while True loop
|
||||
declare
|
||||
C : Character := To_Upper(W(wPos));
|
||||
X : constant wIndex := wPos;
|
||||
begin
|
||||
for I in Blocks'Range loop
|
||||
if (not Used(I)) then
|
||||
if C = To_Upper(Blocks(I)(1)) or C = To_Upper(Blocks(I)(2)) then
|
||||
Used(I) := True;
|
||||
if wPos = W'Last then
|
||||
return True;
|
||||
end if;
|
||||
wPos := wIndex'Succ(wPos);
|
||||
exit;
|
||||
end if;
|
||||
end if;
|
||||
end loop;
|
||||
if X = wPos then
|
||||
return False;
|
||||
end if;
|
||||
end;
|
||||
end loop;
|
||||
return False;
|
||||
end Can_Make_Word;
|
||||
|
||||
end Abc;
|
||||
|
||||
with Ada.Text_IO, Ada.Strings.Unbounded, Abc;
|
||||
use Ada.Text_IO, Ada.Strings.Unbounded, Abc;
|
||||
|
||||
procedure Abc_Problem is
|
||||
Blocks : Block_List := (
|
||||
('B','O'), ('X','K'), ('D','Q'), ('C','P')
|
||||
, ('N','A'), ('G','T'), ('R','E'), ('T','G')
|
||||
, ('Q','D'), ('F','S'), ('J','W'), ('H','U')
|
||||
, ('V','I'), ('A','N'), ('O','B'), ('E','R')
|
||||
, ('F','S'), ('L','Y'), ('P','C'), ('Z','M')
|
||||
);
|
||||
function "+" (S : String) return Unbounded_String renames To_Unbounded_String;
|
||||
words : array(positive range <>) of Unbounded_String := (
|
||||
+"A"
|
||||
, +"BARK"
|
||||
, +"BOOK"
|
||||
, +"TREAT"
|
||||
, +"COMMON"
|
||||
, +"SQUAD"
|
||||
, +"CONFUSE"
|
||||
-- Border cases:
|
||||
-- , +"CONFUSE2"
|
||||
-- , +""
|
||||
);
|
||||
begin
|
||||
for I in words'Range loop
|
||||
Put_Line ( To_String(words(I)) & ": " & Boolean'Image(Can_Make_Word(To_String(words(I)),Blocks)) );
|
||||
end loop;
|
||||
end Abc_Problem;
|
||||
64
Task/ABC-problem/COBOL/abc-problem.cob
Normal file
64
Task/ABC-problem/COBOL/abc-problem.cob
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. ABC-PROBLEM.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 INPUT-DATA.
|
||||
03 BLOCK-SET PIC X(40) VALUE
|
||||
'BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM'.
|
||||
03 WORD-DATA.
|
||||
05 FILLER PIC X(7) VALUE 'A'.
|
||||
05 FILLER PIC X(7) VALUE 'BARK'.
|
||||
05 FILLER PIC X(7) VALUE 'BOOK'.
|
||||
05 FILLER PIC X(7) VALUE 'TREAT'.
|
||||
05 FILLER PIC X(7) VALUE 'COMMON'.
|
||||
05 FILLER PIC X(7) VALUE 'SQUAD'.
|
||||
05 FILLER PIC X(7) VALUE 'CONFUSE'.
|
||||
03 WORDS PIC X(7) OCCURS 7 TIMES, INDEXED BY W,
|
||||
REDEFINES WORD-DATA.
|
||||
|
||||
01 OUTPUT-LINE.
|
||||
03 OUT-WORD PIC X(7).
|
||||
03 FILLER PIC XX VALUE ': '.
|
||||
03 RESULT PIC X(3).
|
||||
03 FOO PIC 999.
|
||||
|
||||
01 VARIABLES.
|
||||
03 BLOCK-SET PIC X(40).
|
||||
03 BLOCKS OCCURS 20 TIMES, INDEXED BY B,
|
||||
REDEFINES BLOCK-SET.
|
||||
05 FACE-A PIC X.
|
||||
05 FACE-B PIC X.
|
||||
03 WORD.
|
||||
05 LETTERS PIC X OCCURS 7 TIMES, INDEXED BY L.
|
||||
03 FAIL-FLAG PIC X.
|
||||
88 FAILED VALUE 'X'.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
BEGIN.
|
||||
PERFORM CHECK-WORD VARYING W FROM 1 BY 1
|
||||
UNTIL W IS GREATER THAN 7.
|
||||
STOP RUN.
|
||||
|
||||
CHECK-WORD.
|
||||
MOVE BLOCK-SET OF INPUT-DATA TO BLOCK-SET OF VARIABLES.
|
||||
|
||||
MOVE WORDS(W) TO WORD.
|
||||
SET L TO 1.
|
||||
MOVE SPACE TO FAIL-FLAG.
|
||||
PERFORM CHECK-LETTER VARYING L FROM 1 BY 1
|
||||
UNTIL FAILED
|
||||
OR L IS GREATER THAN 7
|
||||
OR LETTERS(L) IS EQUAL TO SPACE.
|
||||
|
||||
MOVE WORDS(W) TO OUT-WORD.
|
||||
IF FAILED, MOVE 'NO' TO RESULT, ELSE MOVE 'YES' TO RESULT.
|
||||
DISPLAY OUTPUT-LINE.
|
||||
|
||||
CHECK-LETTER.
|
||||
SET B TO 1.
|
||||
SEARCH BLOCKS VARYING B
|
||||
AT END
|
||||
MOVE 'X' TO FAIL-FLAG
|
||||
WHEN LETTERS(L) IS EQUAL TO FACE-A(B) OR FACE-B(B)
|
||||
MOVE SPACES TO BLOCKS(B).
|
||||
17
Task/ABC-problem/Crystal/abc-problem.cr
Normal file
17
Task/ABC-problem/Crystal/abc-problem.cr
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
def can_make_word? (word, blocks, i=0)
|
||||
return true if word.empty?
|
||||
ch = word[0].upcase
|
||||
blocks.each_with_index do |block, idx|
|
||||
if ch.in? block.upcase
|
||||
return true if can_make_word? word[1..], blocks[...idx] + blocks[(idx+1)..], i+1
|
||||
end
|
||||
end
|
||||
false
|
||||
end
|
||||
|
||||
blocks = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM".split
|
||||
|
||||
["A", "BARK", "BOOK", "TREAT", "COMMON", "SQUAD", "CONFUSE"].each do |word|
|
||||
print word, " -> ", can_make_word?(word, blocks), "\n"
|
||||
end
|
||||
print "abba", " -> ", can_make_word?("abba", ["AB", "AB", "AC", "AC"]), "\n"
|
||||
35
Task/ABC-problem/Euphoria/abc-problem.eu
Normal file
35
Task/ABC-problem/Euphoria/abc-problem.eu
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
include std/text.e
|
||||
|
||||
sequence blocks = {{'B','O'},{'X','K'},{'D','Q'},{'C','P'},{'N','A'},
|
||||
{'G','T'},{'R','E'},{'T','G'},{'Q','D'},{'F','S'},
|
||||
{'J','W'},{'H','U'},{'V','I'},{'A','N'},{'O','B'},
|
||||
{'E','R'},{'F','S'},{'L','Y'},{'P','C'},{'Z','M'}}
|
||||
sequence words = {"A","BarK","BOOK","TrEaT","COMMON","SQUAD","CONFUSE"}
|
||||
|
||||
sequence current_word
|
||||
sequence temp
|
||||
integer matches
|
||||
|
||||
for i = 1 to length(words) do
|
||||
current_word = upper(words[i])
|
||||
temp = blocks
|
||||
matches = 0
|
||||
for j = 1 to length(current_word) do
|
||||
for k = 1 to length(temp) do
|
||||
if find(current_word[j],temp[k]) then
|
||||
temp = remove(temp,k)
|
||||
matches += 1
|
||||
exit
|
||||
end if
|
||||
end for
|
||||
if length(current_word) = matches then
|
||||
printf(1,"%s: TRUE\n",{words[i]})
|
||||
exit
|
||||
end if
|
||||
end for
|
||||
if length(current_word) != matches then
|
||||
printf(1,"%s: FALSE\n",{words[i]})
|
||||
end if
|
||||
end for
|
||||
|
||||
if getc(0) then end if
|
||||
117
Task/ABC-problem/PowerShell/abc-problem.ps1
Normal file
117
Task/ABC-problem/PowerShell/abc-problem.ps1
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
<#
|
||||
.Synopsis
|
||||
ABC Problem
|
||||
.DESCRIPTION
|
||||
You are given a collection of ABC blocks. Just like the ones you had when you were a kid.
|
||||
There are twenty blocks with two letters on each block. You are guaranteed to have a
|
||||
complete alphabet amongst all sides of the blocks
|
||||
blocks = "BO","XK","DQ","CP","NA","GT","RE","TG","QD","FS","JW","HU","VI","AN","OB","ER","FS","LY","PC","ZM"
|
||||
The goal of this task is to write a function that takes a string and can determine whether
|
||||
you can spell the word with the given collection of blocks.
|
||||
|
||||
The rules are simple:
|
||||
1.Once a letter on a block is used that block cannot be used again
|
||||
2.The function should be case-insensitive
|
||||
3. Show your output on this page for the following words:
|
||||
>>> can_make_word("A")
|
||||
True
|
||||
>>> can_make_word("BARK")
|
||||
True
|
||||
>>> can_make_word("BOOK")
|
||||
False
|
||||
>>> can_make_word("TREAT")
|
||||
True
|
||||
>>> can_make_word("COMMON")
|
||||
False
|
||||
>>> can_make_word("SQUAD")
|
||||
True
|
||||
>>> can_make_word("CONFUSE")
|
||||
True
|
||||
|
||||
Using the examples below you can either see just the value or
|
||||
status and the values using the verbose switch
|
||||
|
||||
.EXAMPLE
|
||||
test-blocks -testword confuse
|
||||
|
||||
.EXAMPLE
|
||||
test-blocks -testword confuse -verbose
|
||||
|
||||
#>
|
||||
|
||||
function test-blocks
|
||||
{
|
||||
[CmdletBinding()]
|
||||
# [OutputType([int])]
|
||||
Param
|
||||
(
|
||||
# word to test against blocks
|
||||
[Parameter(Mandatory = $true,
|
||||
ValueFromPipelineByPropertyName = $true)]
|
||||
$testword
|
||||
|
||||
)
|
||||
|
||||
$word = $testword
|
||||
|
||||
#define array of blocks
|
||||
[System.Collections.ArrayList]$blockarray = "BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS", "JW", "HU", "VI", "AN", "OB", "ER", "FS", "LY", "PC", "ZM"
|
||||
|
||||
#send word to chararray
|
||||
$chararray = $word.ToCharArray()
|
||||
$chars = $chararray
|
||||
|
||||
#get the character count
|
||||
$charscount = $chars.count
|
||||
|
||||
#get the initial count of the blocks
|
||||
$blockcount = $blockarray.Count
|
||||
|
||||
#find out how many blocks should be left from the difference
|
||||
#of the blocks and characters in the word - 1 letter/1 block
|
||||
$correctblockcount = $blockcount - $charscount
|
||||
|
||||
#loop through the characters in the word
|
||||
foreach ($char in $chars)
|
||||
{
|
||||
|
||||
#loop through the blocks
|
||||
foreach ($block in $blockarray)
|
||||
{
|
||||
|
||||
#check the current character against each letter on the current block
|
||||
#and break if found so the array can reload
|
||||
if ($char -in $block[0] -or $char -in $block[1])
|
||||
{
|
||||
|
||||
write-verbose "match for letter - $char - removing block $block"
|
||||
$blockarray.Remove($block)
|
||||
break
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
#get final count of blocks left in array to determine if the word was
|
||||
#correctly made
|
||||
$finalblockcount = $blockarray.count
|
||||
if ($finalblockcount -ne $correctblockcount)
|
||||
{
|
||||
write-verbose "$word : $false "
|
||||
return $false
|
||||
}
|
||||
else
|
||||
{
|
||||
write-verbose "$word : $true "
|
||||
return $true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#loop all the words and pass them to the function
|
||||
$wordlist = "a", "bark", "book", "treat", "common", "squad", "confuse"
|
||||
foreach ($word in $wordlist)
|
||||
{
|
||||
test-blocks -testword $word -Verbose
|
||||
}
|
||||
38
Task/ABC-problem/Rebol/abc-problem.rebol
Normal file
38
Task/ABC-problem/Rebol/abc-problem.rebol
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: ABC problem"
|
||||
file: %ABC_problem.r3
|
||||
url: https://rosettacode.org/wiki/ABC_problem
|
||||
needs: 3.10.0 ;; or something like that
|
||||
note: "Based on Red language solution"
|
||||
]
|
||||
;; Define the function 'test' that takes a string argument 's'
|
||||
test: function [s [string!]][
|
||||
;; Make a copy of the input string to work on (avoid mutation of original)
|
||||
s: copy s
|
||||
;; Initialize 'p' as a copy of a specific character sequence
|
||||
p: copy "BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
|
||||
;; Start an infinite loop
|
||||
forever [
|
||||
;; If 's' is empty, return true (all characters matched/removed)
|
||||
if 0 = length? s [return true]
|
||||
;; If 'p' is at the tail (all pairs tried), return false (no match)
|
||||
if tail? p [return false]
|
||||
;; Create a parsing rule of the current two characters in 'p'
|
||||
rule: reduce [first p '| second p]
|
||||
;; Try to parse 's' according to the current rule:
|
||||
;; If parsing succeeds, remove that rule from the string
|
||||
p: either parse s [to rule remove rule to end ] [
|
||||
;; If parsing succeeded, remove the current pair from 'p'
|
||||
head remove/part p 2
|
||||
][
|
||||
;; If parsing failed, skip the current pair in 'p' (move to next pair)
|
||||
skip p 2
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
;; Test the 'test' function on each word split from a string (split by space).
|
||||
foreach word split {A bark book TrEAT COmMoN SQUAD conFUsE} space [
|
||||
;; Print the word and its result from 'test'
|
||||
printf [8 ": "] reduce [word test word]
|
||||
]
|
||||
4
Task/ABC-problem/Uiua/abc-problem.uiua
Normal file
4
Task/ABC-problem/Uiua/abc-problem.uiua
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
s ← "BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
|
||||
Flt ← ⊂⊙↘₂⊃↙↘-⊸◿2⊸˜⨂ # remove block holding letter
|
||||
{"A" "BARK" "BOOK" "TREAT" "COMMON" "SQUAD" "CONFUSE"}
|
||||
≡⊂⟜⍚(◌◌⍥(⍥(⊙⊙⋅0)⊸≍⟜˜Flt⊙°⊂)◡⋅⧻ s ⊙1)
|
||||
|
|
@ -1,13 +1,12 @@
|
|||
const
|
||||
(
|
||||
blocks = ["BO","XK","DQ","CP","NA","GT","RE","TG","QD","FS","JW","HU","VI","AN","OB","ER","FS","LY","PC","ZM"]
|
||||
words = ["A", BARK","BOOK","TREAT","COMMON","SQUAD","CONFUSE"]
|
||||
)
|
||||
const blocks = ["BO","XK","DQ","CP","NA","GT","RE","TG","QD","FS",
|
||||
"JW","HU","VI","AN","OB","ER","FS","LY","PC","ZM"]
|
||||
|
||||
const words = ["A", "BARK","BOOK","TREAT","COMMON","SQUAD","CONFUSE"]
|
||||
|
||||
fn main() {
|
||||
for word in words {
|
||||
println('>>> can_make_word("${word.to_upper()}"): ')
|
||||
if check_word(word, blocks) == true {println('True')} else {println('False')}
|
||||
if check_word(word, blocks) == true { println("True") } else { println("False") }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -18,12 +17,12 @@ fn check_word(word string, blocks []string) bool {
|
|||
found = false
|
||||
for idx, _ in tblocks {
|
||||
if tblocks[idx].contains(chr.ascii_str()) == true {
|
||||
tblocks[idx] =''
|
||||
tblocks[idx] =""
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if found == false {return found}
|
||||
if found == false { return found }
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
|
|
|||
54
Task/ABC-problem/Zig/abc-problem.zig
Normal file
54
Task/ABC-problem/Zig/abc-problem.zig
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
const std = @import("std");
|
||||
|
||||
const Block: type = struct { Char1: u8, Char2: u8 };
|
||||
const BLOCK_SIZE: comptime_int = 20;
|
||||
const blocks: [BLOCK_SIZE]Block = .{
|
||||
.{ .Char1 = 'B', .Char2 = 'O' }, .{ .Char1 = 'X', .Char2 = 'K' },
|
||||
.{ .Char1 = 'D', .Char2 = 'Q' }, .{ .Char1 = 'C', .Char2 = 'P' },
|
||||
.{ .Char1 = 'N', .Char2 = 'A' }, .{ .Char1 = 'G', .Char2 = 'T' },
|
||||
.{ .Char1 = 'R', .Char2 = 'E' }, .{ .Char1 = 'T', .Char2 = 'G' },
|
||||
.{ .Char1 = 'Q', .Char2 = 'D' }, .{ .Char1 = 'F', .Char2 = 'S' },
|
||||
.{ .Char1 = 'J', .Char2 = 'W' }, .{ .Char1 = 'H', .Char2 = 'U' },
|
||||
.{ .Char1 = 'V', .Char2 = 'I' }, .{ .Char1 = 'A', .Char2 = 'N' },
|
||||
.{ .Char1 = 'O', .Char2 = 'B' }, .{ .Char1 = 'E', .Char2 = 'R' },
|
||||
.{ .Char1 = 'F', .Char2 = 'S' }, .{ .Char1 = 'L', .Char2 = 'Y' },
|
||||
.{ .Char1 = 'P', .Char2 = 'C' }, .{ .Char1 = 'Z', .Char2 = 'M' },
|
||||
};
|
||||
|
||||
fn can_make_word(word: []const u8) bool {
|
||||
var marked: [BLOCK_SIZE]bool = [_]bool{false} ** BLOCK_SIZE;
|
||||
|
||||
for (word) |char| {
|
||||
var hasBlock: bool = false;
|
||||
|
||||
for (blocks, 0..) |block, index| {
|
||||
if ((block.Char1 == char or block.Char2 == char) and !marked[index]) {
|
||||
hasBlock = true;
|
||||
marked[index] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasBlock) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
const words: []const []const u8 = &.{
|
||||
"A",
|
||||
"BARK",
|
||||
"BOOK",
|
||||
"TREAT",
|
||||
"COMMON",
|
||||
"SQUAD",
|
||||
"CONFUSE",
|
||||
};
|
||||
|
||||
for (words) |word| {
|
||||
std.debug.print("{s}\t{any}\n", .{ word, can_make_word(word) });
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue