Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,45 @@
static Boolean canMakeWord(List<String> src_blocks, String word) {
if (String.isEmpty(word)) {
return true;
}
List<String> blocks = new List<String>();
for (String block : src_blocks) {
blocks.add(block.toUpperCase());
}
for (Integer i = 0; i < word.length(); i++) {
Integer blockIndex = -1;
String c = word.mid(i, 1).toUpperCase();
for (Integer j = 0; j < blocks.size(); j++) {
if (blocks.get(j).contains(c)) {
blockIndex = j;
break;
}
}
if (blockIndex == -1) {
return false;
} else {
blocks.remove(blockIndex);
}
}
return true;
}
List<String> blocks = new List<String>{
'BO', 'XK', 'DQ', 'CP', 'NA',
'GT', 'RE', 'TG', 'QD', 'FS',
'JW', 'HU', 'VI', 'AN', 'OB',
'ER', 'FS', 'LY', 'PC', 'ZM'
};
System.debug('"": ' + canMakeWord(blocks, ''));
System.debug('"A": ' + canMakeWord(blocks, 'A'));
System.debug('"BARK": ' + canMakeWord(blocks, 'BARK'));
System.debug('"book": ' + canMakeWord(blocks, 'book'));
System.debug('"treat": ' + canMakeWord(blocks, 'treat'));
System.debug('"COMMON": ' + canMakeWord(blocks, 'COMMON'));
System.debug('"SQuAd": ' + canMakeWord(blocks, 'SQuAd'));
System.debug('"CONFUSE": ' + canMakeWord(blocks, 'CONFUSE'));

View file

@ -0,0 +1 @@
module rosetta.abc "1.0.0" {}

View file

@ -0,0 +1,74 @@
shared void run() {
printAndCanMakeWord("A", blocks);
//True
printAndCanMakeWord("BARK", blocks);
//True
printAndCanMakeWord("BOOK", blocks);
//False
printAndCanMakeWord("TREAT", blocks);
//True
printAndCanMakeWord("COMMON", blocks);
//False
printAndCanMakeWord("SQUAD", blocks);
//True
printAndCanMakeWord("CONFUSE", blocks);
//True
}
Block[] blocks =
[
Block('B','O'),
Block('X','K'),
Block('D','Q'),
Block('C','P'),
Block('N','A'),
Block('G','T'),
Block('R','E'),
Block('T','G'),
Block('Q','D'),
Block('F','S'),
Block('J','W'),
Block('H','U'),
Block('V','I'),
Block('A','N'),
Block('O','B'),
Block('E','R'),
Block('F','S'),
Block('L','Y'),
Block('P','C'),
Block('Z','M')
];
void printAndCanMakeWord(String word, Block[] blocks) {
print("``word``:``canMakeWord(word, blocks)``");
}
class Block(Character firstLetter, Character secondLetter) {
shared Character firstLetterUpper = firstLetter.uppercased;
shared Character secondLetterUpper = secondLetter.uppercased;
shared Boolean containsLetter(Character letter)
=> let (letterUpper = letter.uppercased)
firstLetterUpper == letterUpper || secondLetterUpper == letterUpper;
shared actual String string = "``firstLetterUpper``,``secondLetterUpper``";
}
Boolean canMakeWord(String word, Block[] blocks)
=> canMakeWordRecursive(word.uppercased.sequence(), 0, blocks, word.indexes());
Boolean canMakeWordRecursive(Character[] word,
Integer index,
Block[] remainingBlocks,
Integer[] remainingLetterIndexes)
=> if (exists wordFirst = word.first, // first is the Ceylon attribute for head
exists remainingBlock = remainingBlocks.find((remainingBlock) => remainingBlock.containsLetter(wordFirst)))
then
let (myRemainingLetterIndexes = remainingLetterIndexes.filter((theIndex) => index != theIndex).sequence())
if (myRemainingLetterIndexes.empty)
then true
else canMakeWordRecursive(word.rest,// rest is the Ceylon attribute for tail
index+1, // move through the letter indexes
remainingBlocks.filter((block) => remainingBlock != block).sequence(), // one less block
myRemainingLetterIndexes)
else false;

View file

@ -0,0 +1,28 @@
PROGRAM BLOCKS
!$INCLUDE="PC.LIB"
PROCEDURE CANMAKEWORD(WORD$)
LOCAL B$,P%
B$=BLOCKS$
PRINT(WORD$;" -> ";)
P%=INSTR(B$,CHR$(ASC(WORD$) AND $DF))
WHILE P%>0 AND WORD$>"" DO
CHANGE(B$,P%-1+(P% MOD 2),".."->B$)
WORD$=MID$(WORD$,2)
EXIT IF WORD$=""
P%=INSTR(B$,CHR$(ASC(WORD$) AND $DF))
END WHILE
IF WORD$>"" THEN PRINT("False") ELSE PRINT("True") END IF
END PROCEDURE
BEGIN
BLOCKS$="BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
CANMAKEWORD("A")
CANMAKEWORD("BARK")
CANMAKEWORD("BOOK")
CANMAKEWORD("TREAT")
CANMAKEWORD("COMMON")
CANMAKEWORD("SQUAD")
CANMAKEWORD("Confuse")
END PROGRAM

View file

@ -0,0 +1,16 @@
(lib 'list) ;; list-delete
(define BLOCKS '("BO" "XK" "DQ" "CP" "NA" "GT" "RE" "TG" "QD" "FS"
"JW" "HU" "VI" "AN" "OB" "ER" "FS" "LY" "PC" "ZM" ))
(define WORDS '("A" "BARK" "BOOK" "TREAT" "COMMON" "SQUAD" "CONFUSE"))
(define (spell word blocks)
(cond
((string-empty? word) #t)
((empty? blocks) #f)
(else
(for/or [(block blocks)]
#:continue (not (string-match block (string-first word)))
(spell (string-rest word) (list-delete blocks block))))))

View file

@ -0,0 +1,32 @@
PROCEDURE Main()
LOCAL cStr
FOR EACH cStr IN { "A", "BARK", "BooK", "TrEaT", "comMON", "sQuAd", "Confuse" }
? PadL( cStr, 10 ), iif( Blockable( cStr ), "can", "cannot" ), "be spelled with blocks."
NEXT
RETURN
STATIC FUNCTION Blockable( cStr )
LOCAL blocks := { ;
"BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS", ;
"JW", "HU", "VI", "AN", "OB", "ER", "FS", "LY", "PC", "ZM" }
LOCAL cFinal := ""
LOCAL i, j
cStr := Upper( cStr )
FOR i := 1 TO Len( cStr )
FOR EACH j IN blocks
IF SubStr( cStr, i, 1 ) $ j
cFinal += SubStr( cStr, i, 1 )
j := ""
EXIT
ENDIF
NEXT
NEXT
RETURN cFinal == cStr

View file

@ -0,0 +1,33 @@
from strutils import contains, format, toUpper
from sequtils import delete
proc canMakeWord(s: string): bool =
var
abcs = @["BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS",
"JW", "HU", "VI", "AN", "OB", "ER", "FS", "LY", "PC", "ZM"]
matched = newSeq[string]()
if s.len > abcs.len:
return false
for i in 0 .. s.len - 1:
var
letter = s[i].toUpper
n = 0
for abc in abcs:
if contains(abc, letter):
delete(abcs, n, n)
matched = matched & abc
break
else:
inc(n)
if matched.len == s.len:
return true
else:
return false
var words = @["A", "bArK", "BOOK", "treat", "common", "sQuAd", "CONFUSE"]
for word in words:
echo format("Can the blocks make the word \"$1\"? $2", word,
(if canMakeWord(word): "yes" else: "no"))

View file

@ -0,0 +1,10 @@
["BO","XK","DQ","CP","NA","GT","RE","TG","QD","FS","JW","HU","VI","AN","OB","ER","FS","LY","PC","ZM"] const: ABCBlocks
: canMakeWord(w, blocks)
| i |
w isEmpty ifTrue: [ true return ]
blocks size loop: i [
blocks at(i) include(w first toUpper) ifFalse: [ continue ]
canMakeWord(w right(w size 1 -), blocks del(i, i)) ifTrue: [ true return ]
]
false ;

View file

@ -0,0 +1,44 @@
-- Here is my recursive solution which also solves the extra problems on the discussion page:
sequence blocks = {"BO","XK","DQ","CP","NA","GT","RE","TG","QD","FS",
"JW","HU","VI","AN","OB","ER","FS","LY","PC","ZM"}
sequence words = {"","A","BarK","BOOK","TrEaT","COMMON","SQUAD","CONFUSE"}
--sequence blocks = {"US","TZ","AO","QA"}
--sequence words = {"AuTO"}
--sequence blocks = {"AB","AB","AC","AC"}
--sequence words = {"abba"}
sequence used = repeat(0,length(blocks))
function ABC_Solve(sequence word, integer idx)
integer ch
integer res = 0
if idx>length(word) then
res = 1
else
ch = word[idx]
for k=1 to length(blocks) do
if used[k]=0
and find(ch,blocks[k]) then
used[k] = 1
res = ABC_Solve(word,idx+1)
used[k] = 0
if res then exit end if
end if
end for
end if
return res
end function
constant TF = {"False","True"}
procedure ABC_Problem()
for i=1 to length(words) do
printf(1,"%s: %s\n",{words[i],TF[ABC_Solve(upper(words[i]),1)+1]})
end for
if getc(0) then end if
end procedure
ABC_Problem()

View file

@ -0,0 +1,24 @@
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 ]
for x in words
see '>>> can_make_word("' + upper(x) + '")' + nl
if checkword(x,blocks) see "True" + nl
else see "False" + nl
ok
next
func CheckWord Word,Blocks
cBlocks = BLocks
for x in word
Found = false
for y = 1 to len(cblocks)
if x = cblocks[y][1] or x = cblocks[y][2]
cblocks[y] = "--"
found = true
exit
ok
next
if found = false return false ok
next
return true

View file

@ -0,0 +1,23 @@
blocks:List Tuple Symbol:= _
[(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)]
findComb(l:List List NNI):List List NNI ==
#l=0 => []
#l=1 => [[s] for s in first l]
r:List List NNI:=[]
for y in findComb(rest l) repeat
r:=concat(r,[concat(x,y) for x in first l])
return r
canMakeWord?(word,blocks) ==
word:=upperCase word
bchr:=[map(char,map(string,s::List(Symbol))) for s in blocks]
c:=[[j for j in 1..#blocks | member?(word.k,bchr.j)] for k in 1..#word]
reduce(_or,[test(#removeDuplicates(l)=#word) for l in findComb(c)])
Example:=["a","bark","book","treat","common","squad","confuse"]
[canMakeWord?(s,blocks) for s in Example]

View file

@ -0,0 +1,29 @@
import <Utilities/Conversion.sl>;
import <Utilities/Sequence.sl>;
main(args(2)) :=
let
result[i] := args[i] ++ ": " ++ boolToString(can_make_word(args[i], InitBlocks));
in
delimit(result, '\n');
InitBlocks := ["BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS", "JW", "HU", "VI", "AN", "OB", "ER", "FS", "LY", "PC", "ZM"];
can_make_word(word(1), blocks(2)) :=
let
choices[i] := i when some(blocks[i] = toUpper(head(word)));
blocksAfterChoice[i] := blocks[(1 ... (choices[i] - 1)) ++ ((choices[i] + 1) ... size(blocks))];
in
true when size(word) = 0
else
false when size(choices) = 0
else
some(can_make_word(tail(word), blocksAfterChoice));
toUpper(letter(0)) :=
let
ascii := asciiToInt(letter);
in
letter when ascii >= 65 and ascii <= 90
else
intToAscii(ascii - 32);

View file

@ -0,0 +1,31 @@
import <Utilities/Conversion.sl>;
import <Utilities/Sequence.sl>;
import <RegEx/RegEx.sl>;
main(args(2)) :=
let
result[i] := args[i] ++ ": " ++ boolToString(can_make_word(args[i], InitBlocks));
in
delimit(result, '\n');
InitBlocks := "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM";
can_make_word(word(1), blocks(1)) :=
let
regEx := "(\\a" ++ [toUpper(head(word))] ++ "|" ++ [toUpper(head(word))] ++ "\\a)";
newBlocks := replaceFirst(blocks, regEx, "");
in
true when size(word) = 0
else
false when size(newBlocks) = size(blocks)
else
can_make_word(tail(word), newBlocks);
toUpper(letter(0)) :=
let
ascii := asciiToInt(letter);
in
letter when ascii >= 65 and ascii <= 90
else
intToAscii(ascii - 32);

View file

@ -0,0 +1,17 @@
func can_make_word(word, blocks) {
blocks.map! { |b| b.uc.chars.sort.join }.freq!
func(word, blocks) {
var char = word.shift
var candidates = blocks.keys.grep { |k| 0 <= k.index(char) }
for candidate in candidates {
blocks{candidate} <= 0 && next;
local blocks{candidate} = (blocks{candidate} - 1);
return true if (word.is_empty || __FUNC__(word, blocks));
}
return false;
}(word.uc.chars, blocks)
}

View file

@ -0,0 +1,19 @@
var b1 = %w(BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM)
var b2 = %w(US TZ AO QA)
var tests = [
["A", true, b1],
["BARK", true, b1],
["BOOK", false, b1],
["TREAT", true, b1],
["COMMON", false, b1],
["SQUAD", true, b1],
["CONFUSE", true, b1],
["auto", true, b2],
];
tests.each { |t|
var bool = can_make_word(t[0], t[2]);
say ("%7s -> %s" % (t[0], bool));
assert(bool == t[1])
}

View file

@ -0,0 +1,33 @@
import Foundation
func Blockable(str: String) -> Bool {
var blocks = [
"BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS",
"JW", "HU", "VI", "AN", "OB", "ER", "FS", "LY", "PC", "ZM" ]
var strUp = str.uppercaseString
var final = ""
for char: Character in strUp {
var CharString: String = ""; CharString.append(char)
for j in 0..<blocks.count {
if blocks[j].hasPrefix(CharString) ||
blocks[j].hasSuffix(CharString) {
final.append(char)
blocks[j] = ""
break
}
}
}
return final == strUp
}
func CanOrNot(can: Bool) -> String {
return can ? "can" : "cannot"
}
for str in [ "A", "BARK", "BooK", "TrEaT", "comMON", "sQuAd", "Confuse" ] {
println("'\(str)' \(CanOrNot(Blockable(str))) be spelled with blocks.")
}

View file

@ -0,0 +1,29 @@
# when_index(cond;ary) returns the index of the first element in ary
# that satisfies cond; it uses a helper function that takes advantage
# of tail-recursion optimization in recent versions of jq.
def index_when(cond; ary):
# state variable: counter
def when: if . >= (ary | length) then null
elif ary[.] | cond then .
else (.+1) | when
end;
0 | when;
# Attempt to match a single letter with a block;
# return null if no match, else the remaining blocks
def match_letter(letter):
. as $ary | index_when( index(letter); $ary ) as $ix
| if $ix == null then null
else del( .[$ix] )
end;
# Usage: string | abc(blocks)
def abc(blocks):
if length == 0 then true
else
.[0:1] as $letter
| (blocks | match_letter( $letter )) as $blks
| if $blks == null then false
else .[1:] | abc($blks)
end
end;

View file

@ -0,0 +1,5 @@
def task:
["BO","XK","DQ","CP","NA","GT","RE","TG","QD","FS",
"JW","HU","VI","AN","OB","ER","FS","LY","PC","ZM"] as $blocks
| ("A", "BARK","BOOK","TREAT","COMMON","SQUAD","CONFUSE")
| "\(.) : \( .|abc($blocks) )" ;task