Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
70
Task/Bulls-and-cows/Ceylon/bulls-and-cows.ceylon
Normal file
70
Task/Bulls-and-cows/Ceylon/bulls-and-cows.ceylon
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
import ceylon.random {
|
||||
|
||||
DefaultRandom
|
||||
}
|
||||
|
||||
shared void run() {
|
||||
|
||||
value random = DefaultRandom();
|
||||
|
||||
function generateDigits() =>
|
||||
random.elements(1..9).distinct.take(4).sequence();
|
||||
|
||||
function validate(String guess) {
|
||||
variable value ok = true;
|
||||
if(!guess.every((Character element) => element.digit)) {
|
||||
print("numbers only, please");
|
||||
ok = false;
|
||||
}
|
||||
if('0' in guess) {
|
||||
print("only 1 to 9, please");
|
||||
ok = false;
|
||||
}
|
||||
if(guess.distinct.shorterThan(guess.size)) {
|
||||
print("no duplicates, please");
|
||||
ok = false;
|
||||
}
|
||||
if(guess.size != 4) {
|
||||
print("4 digits please");
|
||||
ok = false;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
function score({Integer*} target, {Integer*} guess) {
|
||||
variable value bulls = 0;
|
||||
variable value cows = 0;
|
||||
for([a, b] in zipPairs(target, guess)) {
|
||||
if(a == b) {
|
||||
bulls++;
|
||||
} else if(target.contains(b)) {
|
||||
cows++;
|
||||
}
|
||||
}
|
||||
return [bulls, cows];
|
||||
}
|
||||
|
||||
while(true) {
|
||||
value digits = generateDigits();
|
||||
print("I have chosen my four digits, please guess what they are.
|
||||
Use only the digits 1 to 9 with no duplicates and enter them with no spaces. eg 1234
|
||||
Enter q or Q to quit.");
|
||||
while(true) {
|
||||
if(exists line = process.readLine()) {
|
||||
if(line.uppercased == "Q") {
|
||||
return;
|
||||
}
|
||||
if(validate(line)) {
|
||||
value guessDigits = line.map((Character element) => parseInteger(element.string)).coalesced;
|
||||
value [bulls, cows] = score(digits, guessDigits);
|
||||
if(bulls == 4) {
|
||||
print("You win!");
|
||||
break;
|
||||
} else {
|
||||
print("Bulls: ``bulls``, Cows: ``cows``");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
4
Task/Bulls-and-cows/Coco/bulls-and-cows-1.coco
Normal file
4
Task/Bulls-and-cows/Coco/bulls-and-cows-1.coco
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
say = print
|
||||
prompt = (str) ->
|
||||
putstr str
|
||||
readline! ? quit!
|
||||
22
Task/Bulls-and-cows/Coco/bulls-and-cows-2.coco
Normal file
22
Task/Bulls-and-cows/Coco/bulls-and-cows-2.coco
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
const SIZE = 4
|
||||
|
||||
secret = _.sample ['1' to '9'], SIZE
|
||||
|
||||
for ever
|
||||
var guess
|
||||
for ever
|
||||
guess := _.uniq prompt 'Enter a guess: '
|
||||
if guess.length === SIZE and not _.difference guess, ['1' to '9'] .length
|
||||
break
|
||||
say 'Malformed guess; try again.'
|
||||
bulls = cows = 0
|
||||
for i til SIZE
|
||||
if guess[i] === secret[i]
|
||||
++bulls
|
||||
else if _.contains secret, guess[i]
|
||||
++cows
|
||||
if bulls === SIZE
|
||||
break
|
||||
say "#bulls bull#{[if bulls !== 1 then 's']}, #cows cow#{[if cows !== 1 then 's']}."
|
||||
|
||||
say 'A winner is you!'
|
||||
26
Task/Bulls-and-cows/Hy/bulls-and-cows.hy
Normal file
26
Task/Bulls-and-cows/Hy/bulls-and-cows.hy
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
(import random)
|
||||
|
||||
(def +size+ 4)
|
||||
(def +digits+ "123456789")
|
||||
(def +secret+ (random.sample +digits+ +size+))
|
||||
|
||||
(while True
|
||||
(while True
|
||||
(setv guess (list (distinct (raw-input "Enter a guess: "))))
|
||||
(when (and
|
||||
(= (len guess) +size+)
|
||||
(all (map (fn [c] (in c +digits+)) guess)))
|
||||
(break))
|
||||
(print "Malformed guess; try again"))
|
||||
(setv bulls 0)
|
||||
(setv cows 0)
|
||||
(for [i (range +size+)] (cond
|
||||
[(= (get guess i) (get +secret+ i)) (setv bulls (inc bulls))]
|
||||
[(in (get guess i) +secret+) (setv cows (inc cows))]))
|
||||
(when (= bulls +size+)
|
||||
(break))
|
||||
(print (.format "{} bull{}, {} cows"
|
||||
bulls (if (= bulls 1) "" "s")
|
||||
cows (if (= cows 1) "" "s"))))
|
||||
|
||||
(print "A winner is you!")
|
||||
70
Task/Bulls-and-cows/Lasso/bulls-and-cows.lasso
Normal file
70
Task/Bulls-and-cows/Lasso/bulls-and-cows.lasso
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
[
|
||||
define randomizer() => {
|
||||
local(n = string)
|
||||
while(#n->size < 4) => {
|
||||
local(r = integer_random(1,9)->asString)
|
||||
#n !>> #r ? #n->append(#r)
|
||||
}
|
||||
return #n
|
||||
}
|
||||
define cowbullchecker(n::string,a::string) => {
|
||||
integer(#n) == integer(#a) ? return (:true,map('cows'=0,'bulls'=4,'choice'=#a))
|
||||
local(cowbull = map('cows'=0,'bulls'=0,'choice'=#a),'checked' = array)
|
||||
loop(4) => {
|
||||
if(#checked !>> integer(#a->values->get(loop_count))) => {
|
||||
#checked->insert(integer(#a->values->get(loop_count)))
|
||||
if(integer(#n->values->get(loop_count)) == integer(#a->values->get(loop_count))) => {
|
||||
#cowbull->find('bulls') += 1
|
||||
else(#n->values >> #a->values->get(loop_count))
|
||||
#cowbull->find('cows') += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
#cowbull->find('bulls') == 4 ? return (:true,map('cows'=0,'bulls'=4,'choice'=#a))
|
||||
return (:true,#cowbull)
|
||||
}
|
||||
session_start('user')
|
||||
session_addvar('user', 'num')
|
||||
session_addvar('user', 'historic_choices')
|
||||
// set up rand
|
||||
var(num)->isNotA(::string) ? var(num = randomizer)
|
||||
var(historic_choices)->isNotA(::array) ? var(historic_choices = array)
|
||||
local(success = false)
|
||||
// check answer
|
||||
if(web_request->param('a')->asString->size) => {
|
||||
local(success,result) = cowbullchecker($num,web_request->param('a')->asString)
|
||||
$historic_choices->insert(#result)
|
||||
}
|
||||
if(web_request->params->asStaticArray >> 'restart') => {
|
||||
$num = randomizer
|
||||
$historic_choices = array
|
||||
}
|
||||
]
|
||||
<h1>Bulls and Cows</h1>
|
||||
<p>Guess the 4-digit number...</p>
|
||||
<p>Your win if the guess is the same as the randomly chosen number.<br>
|
||||
- A score of one bull is accumulated for each digit in your guess that equals the corresponding digit in the randomly chosen initial number.<br>
|
||||
- A score of one cow is accumulated for each digit in your guess that also appears in the randomly chosen number, but in the wrong position.
|
||||
</p>
|
||||
[
|
||||
local(win = false)
|
||||
if($historic_choices->size) => {
|
||||
with c in $historic_choices do => {^
|
||||
'<p>'+#c->find('choice')+': Bulls: '+#c->find('bulls')+', Cows: '+#c->find('cows')
|
||||
if(#c->find('bulls') == 4) => {^
|
||||
' - YOU WIN!'
|
||||
#win = true
|
||||
^}
|
||||
'</p>'
|
||||
^}
|
||||
}
|
||||
if(not #win) => {^
|
||||
]
|
||||
<form action="?" method="post">
|
||||
<input name="a" value="[web_request->param('a')->asString]" size="5">
|
||||
<input type="submit" name="guess">
|
||||
<a href="?restart">Restart</a>
|
||||
</form>
|
||||
[else
|
||||
'<a href="?restart">Restart</a>'
|
||||
^}]
|
||||
35
Task/Bulls-and-cows/Nim/bulls-and-cows.nim
Normal file
35
Task/Bulls-and-cows/Nim/bulls-and-cows.nim
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import random, strutils, rdstdin
|
||||
randomize()
|
||||
|
||||
proc random(a: string): char = a[random(0..a.len)]
|
||||
|
||||
const
|
||||
digits = "123456789"
|
||||
size = 4
|
||||
|
||||
var digitsSet: set[char] = {}
|
||||
for d in digits: digitsSet.incl d
|
||||
|
||||
var chosen = newString(size)
|
||||
for i in 0..chosen.high: chosen[i] = random(digits)
|
||||
|
||||
echo """I have chosen a number from $# unique digits from 1 to 9 arranged in a random order.
|
||||
You need to input a $# digit, unique digit number as a guess at what I have chosen""".format(size, size)
|
||||
|
||||
var guesses = 0
|
||||
while true:
|
||||
inc guesses
|
||||
var guess = ""
|
||||
while true:
|
||||
guess = readLineFromStdin("\nNext guess [$#]: ".format(guesses)).strip()
|
||||
if guess.len == size and allCharsInSet(guess, digitsSet):
|
||||
break
|
||||
echo "Problem, try again. You need to enter $# unique digits from 1 to 9".format(size)
|
||||
if guess == chosen:
|
||||
echo "\nCongratulations you guessed correctly in ",guesses," attempts"
|
||||
break
|
||||
var bulls, cows = 0
|
||||
for i in 0 .. <size:
|
||||
if guess[i] == chosen[i]: inc bulls
|
||||
if guess[i] in chosen: inc cows
|
||||
echo " $# Bulls\n $# Cows".format(bulls, cows)
|
||||
18
Task/Bulls-and-cows/Oforth/bulls-and-cows.oforth
Normal file
18
Task/Bulls-and-cows/Oforth/bulls-and-cows.oforth
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
: bullsAndCows
|
||||
| numbers guess digits bulls cows |
|
||||
|
||||
ListBuffer new ->numbers
|
||||
while(numbers size 4 <>) [ 9 rand dup numbers include ifFalse: [ numbers add ] else: [ drop ] ]
|
||||
|
||||
while(true) [
|
||||
"Enter a number of 4 different digits between 1 and 9 : " print
|
||||
System.Console askln ->digits
|
||||
digits asInteger isNull digits size 4 <> or ifTrue: [ "Number of four digits needed" println continue ]
|
||||
digits map(#asDigit) ->guess
|
||||
|
||||
guess numbers zipWith(#==) occurrences(true) ->bulls
|
||||
bulls 4 == ifTrue: [ "You won !" println return ]
|
||||
|
||||
guess filter(#[numbers include]) size bulls - ->cows
|
||||
System.Out "Bulls = " << bulls << ", cows = " << cows << cr
|
||||
] ;
|
||||
50
Task/Bulls-and-cows/Phix/bulls-and-cows.phix
Normal file
50
Task/Bulls-and-cows/Phix/bulls-and-cows.phix
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
constant N = 4
|
||||
|
||||
function mask(integer ch)
|
||||
return power(2,ch-'1')
|
||||
end function
|
||||
|
||||
function score(sequence guess, sequence goal)
|
||||
integer bits = 0, bulls = 0, cows = 0, b
|
||||
for i=1 to N do
|
||||
b = goal[i]
|
||||
if guess[i]=b then
|
||||
bulls += 1
|
||||
else
|
||||
bits += mask(b)
|
||||
end if
|
||||
end for
|
||||
for i=1 to N do
|
||||
b = mask(guess[i])
|
||||
if and_bits(bits,b)!=0 then
|
||||
cows += 1
|
||||
bits -= b
|
||||
end if
|
||||
end for
|
||||
return {bulls, cows}
|
||||
end function
|
||||
|
||||
procedure game()
|
||||
sequence tgt = shuffle("123456789")[1..N]
|
||||
integer attempt = 1, bulls = 0, cows
|
||||
sequence guess
|
||||
while bulls<N do
|
||||
while 1 do
|
||||
printf(1,"Enter a %d digit number using only the digits 1 to 9:",N)
|
||||
guess = trim(gets(0))
|
||||
if length(guess)=N then exit end if
|
||||
if length(guess)=1 and lower(guess)="q" then
|
||||
puts(1,"\nthe secret number was:"&tgt)
|
||||
{} = wait_key()
|
||||
abort(0)
|
||||
end if
|
||||
printf(1," - length is not %d (enter q to give up)\n",N)
|
||||
end while
|
||||
{bulls, cows} = score(guess,tgt)
|
||||
printf(1," Guess %-2d (%s) bulls:%d cows:%d\n",{attempt,guess,bulls,cows})
|
||||
attempt += 1
|
||||
end while
|
||||
puts(1,"Well done!\n")
|
||||
end procedure
|
||||
|
||||
game()
|
||||
35
Task/Bulls-and-cows/Sidef/bulls-and-cows.sidef
Normal file
35
Task/Bulls-and-cows/Sidef/bulls-and-cows.sidef
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
var size = 4
|
||||
var num = @(1..9).shuffle.first(size)
|
||||
|
||||
for (var guesses = 1; true; guesses++) {
|
||||
|
||||
var bulls = 0
|
||||
var cows = 0
|
||||
|
||||
var input = Sys.scanln("Input: ").chars \
|
||||
.unique \
|
||||
.grep {.~~/^[1-9]$/} \
|
||||
.map {.to_i}
|
||||
|
||||
if (input.len != size) {
|
||||
warn "Invalid input!\n"
|
||||
guesses--
|
||||
next
|
||||
}
|
||||
|
||||
if (input == num) {
|
||||
printf("You did it in %d attempts!\n", guesses)
|
||||
break
|
||||
}
|
||||
|
||||
num.range.each { |i|
|
||||
if (num[i] == input[i]) {
|
||||
bulls++
|
||||
}
|
||||
elsif (num.contains(input[i])) {
|
||||
cows++
|
||||
}
|
||||
}
|
||||
|
||||
"Bulls: %d; Cows: %d\n".printf(bulls, cows)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue