Skip to content

Instantly share code, notes, and snippets.

@oleksiilevzhynskyi
Created September 17, 2012 10:21
Show Gist options
  • Save oleksiilevzhynskyi/3736586 to your computer and use it in GitHub Desktop.
Save oleksiilevzhynskyi/3736586 to your computer and use it in GitHub Desktop.
Write a program, which outputs numbers from 0 to 1000 without:
loops (do, while, for, etc.)
jumps (goto)
exceptions
conditions
boolean operators (and, or, etc.)
comparison operators (<, >, etc.)
ranges
preprocessor shit
code-generating-code shit
it should end correctly
@oleksiilevzhynskyi
Copy link
Author

Задание со *
Решить общую задачу. Вывести числа от 0 до N.

@oleksiilevzhynskyi
Copy link
Author

  1. Самое кросс-языковое пришло от Максима Ромащенко на schema
#lang racket
(define step
 (list (lambda (x) #f)
       (lambda (x)
         (displayln (- 1000 x))
         ((list-ref step (sgn x)) (sub1 x)))))
((second step) 1000)

Аналоги:

// Вместо sgn floor
function r (x) {
  console.log(x);
  step[ Math.floor(x/1000) ](x + 1);
}
var step = [r, function(){}];
step[0](1);
# Вместо sgn div
step = []
r = Proc.new do |x|
    puts x
    step[ x.div(1000) ].call(x + 1)
end
step = [r, Proc.new {}]

step[0].call(1);
  1. Ruby way от Димы Кириенко
hash = Hash.new(Proc.new {|x| puts x; hash[x+1].call x+1})
hash[1000] = Proc.new {|x| puts x}
hash[1].call 1
class NoProblem
  def for_1000
    puts 1000
  end

  def method_missing name
    num = name.to_s.scan(/for_(\d+)/)[0][0].to_i
    puts num
    send("for_#{num + 1}")
  end
end

NoProblem.new.for_1
  1. Еще два отличных решения на Ruby и Python от Паши Митина
def f number
 p number
 sleep 1
 f number + 1
end

Thread.new do
 f 0
end

sleep 1000
import sys

sys.setrecursionlimit(1002)

def f(n):
   print n
   f(n + 1)

f(0)
  1. Не удовлетворяющее всем условиям, но все равно клевое решение на elang c паттерн-матчингом от Паши
-module(m).
-export([f/1]).

f(1000) ->  
  puts(1000);

f(N) ->
  puts(N),
  f(N + 1).

puts(N) ->
  io:format("~p~n", [N]).
  1. Как всегда лаконичное решение на bash от Ивана Трусова
cat /dev/urandom | head -1000 |  cat -n | cut -f1
  1. Еще несколько вариантов от Трусова
#C потоками

class Printer

   def initialize
     @c = 1
   end

   def print
     puts @c
     sleep 0.1
     @c = @c + 1
     print
   end
end

t1=Thread.new { Printer.new.print }
t2=Thread.new {
    sleep 10
    t1.exit
}

t1.join
#C переопределением NilClass

class Breaker
  def run
    Process.exit
  end
end

class NilClass
  def run
  end
end

@n = 1000
@a = Array.new @n + 1
@a[@n] = Breaker.new
@i = 0

def print
  puts @i
  @a[@i].run
  @i = @i + 1
  print
end

print
  1. Решение в лоб от Артема Цаплина :)
p 1

p 1000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment