Learn A New Language
June 25, 2010
There are hundreds, probably thousands, of programming language. Some are general-purpose, others are intended for a limited domain. Some are useful, most get in your way. Some are just weird. We programmers frequently have to learn a new language, or re-learn an old one, or adapt to improvements from the vendor.
A good way to learn a new language is to write in the new language a familiar program from an old language with which you are experienced. Many people have written that they use the exercises at Programming Praxis as a way to get started with a new language.
Your task is to write a program that solves the Programming Praxis exercise of your choice in a programming language in which you are not proficient. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
[…] Praxis – Learn A New Language By Remco Niemeijer The goal in today’s Programming Praxis exercise is to solve a previous exercise in a language you’re not […]
Steve Yegge’s Phone-Screen Coding Exercises using Ruby (see http://bonsaicode.wordpress.com/2010/06/25/programming-praxis-learn-a-new-language/ for a version with comments):
def reverse(s) s.chars.inject {|rev, x| x + rev } end def fib(n) (2..n).inject([0,1]) {|fs, i| [fs[1], fs[0] + fs[1]]} [[1, n].min] end def timestable (1..12).map {|r| puts (1..12).map {|c| "%4d" % (r * c)}.join} end def sum_from_file(file) File.open(file) {|f| f.readlines.map(&:to_i).inject(:+)} end def odd_numbers p (1..99).find_all(&:odd?) end def maximum(array) array.inject {|max, i| i > max ? i : max} end def to_rgb(r, g, b) sprintf("%02x%02x%02x", r, g, b) endI did the Texas Hold’Em problem in Ruby. For the rest of the code go to http://codingjunkie.net
require ‘deck’
require ‘card’
require ‘player’
card_deck = Deck.new
keep_running = true
while keep_running
results = {}
card_deck.shuffle
seven_cards = card_deck.deal
seven_cards.sort!
copy = Array.new(seven_cards)
puts “Press enter to deal cards..”
line = gets
if line.chop == “quit”
keep_running = false
else
for i in 0..5
for j in i..5
copy.delete_at(i)
copy.delete_at(j)
hand = Player.check_hand(copy,0,0,nil)
if results[hand]
copy = Player.compare_hands(copy, results[hand], 4)
end
results[hand] = copy
copy = Array.new(seven_cards)
end
copy = Array.new(seven_cards)
end
max_score = -1
winning_hand = nil
best_cards = nil
results.each do |hand, cards|
score = Player.ranks[hand]
score = score.nil? ? cards[4].value : score
if score > max_score
max_score = score
score = Player.ranks[hand]
best_cards = cards
winning_hand = hand
end
end
puts “Best hand from #{seven_cards.join(“, “)} ”
puts “\n\t #{winning_hand} : #{best_cards.join(“, “)}”
end
end
The golden ratio exercise from July 10 2009 in Scheme, recursively:
(define (golden_r n) (if (= 0 n) 1 (+ 1 (/ 1 (golden (- n 1))))))and iteratively (I’ve started working my way through SICP as you can probably tell…):
(define (golden_i n) (define (golden-iter n result count) (if (= count n) result (golden-iter n (+ 1 (/ 1 result)) (+ count 1)))) (golden-iter n 1 0))