This site aggregates some content from Rosetta Code, which is a website that presents solutions to programming tasks in many different languages.
In particular, we focus on the CoffeeScript programming language. Most of the content here was originally posted on Rosetta Code, and this content remains licensed under the GNU Free Documentation Licence version 1.2.
You can see the code used for crawling Rosetta by following this link.
Be a contributor! You can enhance the Rosetta Code site by implementing new tasks for CoffeeScript.
If you enjoy this page, please tweet it and /cc @shizowell.
# This program tries to find some way to turn four digits into an arithmetic # expression that adds up to 24. # # Example solution for 5, 7, 8, 8: # (((8 + 7) * 8) / 5) solve_24_game = (digits...) -> # Create an array of objects for our helper functions arr = for digit in digits { val: digit expr: digit } combo4 arr... combo4 = (a, b, c, d) -> arr = [a, b, c, d] # Reduce this to a three-node problem by combining two # nodes from the array. permutations = [ [0, 1, 2, 3] [0, 2, 1, 3] [0, 3, 1, 2] [1, 2, 0, 3] [1, 3, 0, 2] [2, 3, 0, 1] ] for permutation in permutations [i, j, k, m] = permutation for combo in combos arr[i], arr[j] answer = combo3 combo, arr[k], arr[m] return answer if answer null combo3 = (a, b, c) -> arr = [a, b, c] permutations = [ [0, 1, 2] [0, 2, 1] [1, 2, 0] ] for permutation in permutations [i, j, k] = permutation for combo in combos arr[i], arr[j] answer = combo2 combo, arr[k] return answer if answer null combo2 = (a, b) -> for combo in combos a, b return combo.expr if combo.val == 24 null combos = (a, b) -> [ val: a.val + b.val expr: "(#{a.expr} + #{b.expr})" , val: a.val * b.val expr: "(#{a.expr} * #{b.expr})" , val: a.val - b.val expr: "(#{a.expr} - #{b.expr})" , val: b.val - a.val expr: "(#{b.expr} - #{a.expr})" , val: a.val / b.val expr: "(#{a.expr} / #{b.expr})" , val: b.val / a.val expr: "(#{b.expr} / #{a.expr})" , ] # test do -> rand_digit = -> 1 + Math.floor (9 * Math.random()) for i in [1..15] a = rand_digit() b = rand_digit() c = rand_digit() d = rand_digit() solution = solve_24_game a, b, c, d console.log "Solution for #{[a,b,c,d]}: #{solution ? 'no solution'}"
accumulator = (sum) -> (n) -> sum += n f = accumulator(1) console.log f(5) console.log f(2.3)
ackermann = (m, n) -> if m is 0 then n + 1 else if m > 0 and n is 0 then ackermann m - 1, 1 else ackermann m - 1, ackermann m, n - 1
# CoffeeScript is dynamic, just like the Javascript it compiles to. # You can dynamically add attributes to objects. # First create an object very simply. e = {} e.foo = "bar" e.yo = -> "baz" console.log e.foo, e.yo() # CS also has class syntax to instantiate objects, the details of which # aren't shown here. The mechanism to add members is the same, though. class Empty # empty class e = new Empty() e.foo = "bar" e.yo = -> "baz" console.log e.foo, e.yo()
pad = (n) -> s = '' while n > 0 s += ' ' n -= 1 s align = (input, alignment = 'center') -> tokenized_lines = (line.split '$' for line in input) col_widths = {} for line in tokenized_lines for token, i in line if !col_widths[i]? or token.length > col_widths[i] col_widths[i] = token.length padders = center: (s, width) -> excess = width - s.length left = Math.floor excess / 2 right = excess - left pad(left) + s + pad(right) right: (s, width) -> excess = width - s.length pad(excess) + s left: (s, width) -> excess = width - s.length s + pad(excess) padder = padders[alignment] for line in tokenized_lines padded_tokens = (padder(token, col_widths[i]) for token, i in line) console.log padded_tokens.join ' ' input = [ "Given$a$text$file$of$many$lines,$where$fields$within$a$line$" "are$delineated$by$a$single$'dollar'$character,$write$a$program" "that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$" "column$are$separated$by$at$least$one$space." "Further,$allow$for$each$word$in$a$column$to$be$either$left$" "justified,$right$justified,$or$center$justified$within$its$column." ] for alignment in ['center', 'right', 'left'] console.log "\n----- #{alignment}" align input, alignment
http = require 'http' show_large_anagram_sets = (word_lst) -> anagrams = {} max_size = 0 for word in word_lst key = word.split('').sort().join('') anagrams[key] ?= [] anagrams[key].push word size = anagrams[key].length max_size = size if size > max_size for key, variations of anagrams if variations.length == max_size console.log variations.join ' ' get_word_list = (process) -> options = host: "www.puzzlers.org" path: "/pub/wordlists/unixdict.txt" req = http.request options, (res) -> s = '' res.on 'data', (chunk) -> s += chunk res.on 'end', -> process s.split '\n' req.end() get_word_list show_large_anagram_sets
> coffee anagrams.coffee [ 'abel', 'able', 'bale', 'bela', 'elba' ] [ 'alger', 'glare', 'lager', 'large', 'regal' ] [ 'angel', 'angle', 'galen', 'glean', 'lange' ] [ 'caret', 'carte', 'cater', 'crate', 'trace' ] [ 'elan', 'lane', 'lean', 'lena', 'neal' ] [ 'evil', 'levi', 'live', 'veil', 'vile' ]
http = require 'http' is_derangement = (word1, word2) -> for c, i in word1 return false if c == word2[i] true show_longest_derangement = (word_lst) -> anagrams = {} max_len = 0 for word in word_lst continue if word.length < max_len key = word.split('').sort().join('') if anagrams[key] for prior in anagrams[key] if is_derangement(prior, word) max_len = word.length result = [prior, word] else anagrams[key] = [] anagrams[key].push word console.log "Longest derangement: #{result.join ' '}" get_word_list = (process) -> options = host: "www.puzzlers.org" path: "/pub/wordlists/unixdict.txt" req = http.request options, (res) -> s = '' res.on 'data', (chunk) -> s += chunk res.on 'end', -> process s.split '\n' req.end() get_word_list show_longest_derangement
# This is a rather obscure technique to have an anonymous # function call itself. fibonacci = (n) -> throw "Argument cannot be negative" if n < 0 do (n) -> return n if n <= 1 arguments.callee(n-2) + arguments.callee(n-1) # Since it's pretty lightweight to assign an anonymous # function to a local variable, the idiom below might be # more preferred. fibonacci2 = (n) -> throw "Argument cannot be negative" if n < 0 recurse = (n) -> return n if n <= 1 recurse(n-2) + recurse(n-1) recurse(n)
map = (arr, f) -> (f(e) for e in arr) arr = [1, 2, 3, 4, 5] f = (x) -> x * x console.log map arr, f # prints [1, 4, 9, 16, 25]
# create an immutable Complex type class Complex constructor: (@r=0, @i=0) -> @magnitude = @r*@r + @i*@i plus: (c2) -> new Complex( @r + c2.r, @i + c2.i ) times: (c2) -> new Complex( @r*c2.r - @i*c2.i, @r*c2.i + @i*c2.r ) negation: -> new Complex( -1 * @r, -1 * @i ) inverse: -> throw Error "no inverse" if @magnitude is 0 new Complex( @r / @magnitude, -1 * @i / @magnitude ) toString: -> return "#{@r}" if @i == 0 return "#{@i}i" if @r == 0 if @i > 0 "#{@r} + #{@i}i" else "#{@r} - #{-1 * @i}i" # test do -> a = new Complex(5, 3) b = new Complex(4, -3) sum = a.plus b console.log "(#{a}) + (#{b}) = #{sum}" product = a.times b console.log "(#{a}) * (#{b}) = #{product}" negation = b.negation() console.log "-1 * (#{b}) = #{negation}" diff = a.plus negation console.log "(#{a}) - (#{b}) = #{diff}" inverse = b.inverse() console.log "1 / (#{b}) = #{inverse}" quotient = product.times inverse console.log "(#{product}) / (#{b}) = #{quotient}"
# like in JavaScript a = [1, 2, 3] b = [4, 5, 6] c = a.concat b
array1 = [] array1[0] = "Dillenidae" array1[1] = "animus" array1[2] = "Kona&gipoco.com is neither affiliated with the authors of this page or responsible
for its contents. This is a safe-cache copy of the original web site.gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.