Go Back   CodeChef Discussion Forums > Questions About Specific Practice Problems
Please login here.

Reply
 
Thread Tools
  #1  
Old 02-23-2009, 12:26 PM
The Chef The Chef is offline
Administrator
 
Join Date: Jan 2009
Posts: 106
Default Solutions to TEST in 25 programming languages

By popular demand we have included a trivial sample problem. Solutions to TEST are given below in 25 programming languages. Feel free to respond with questions/comments/suggestions.

--- C++ ------------------------------------------------------------

#include <iostream>

int main(void) {
char c, d=10;
while(std::cin.get(c) && (c!='2' || d!='4') && std::cout.put(d))
d=c;
}

--- C --------------------------------------------------------------

#include <stdio.h>

int main(void) {
int x;
for(; scanf("%d",&x) > 0 && x != 42; printf("%d\n", x));
return 0;
}


--- Pascal ---------------------------------------------------------

program test;
var x: integer;
begin
repeat
readln(x);
if x<>42 then writeln(x);
until x=42
end.


--- Java -----------------------------------------------------------

public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}

--- Nice -----------------------------------------------------------

void main (String[] args)
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=notNull(r.readLine())).startsWith("42")) System.out.println(s);
}

--- Python ---------------------------------------------------------

k=raw_input()
while int(k)!=42:
print k
k=raw_input()

--- Lisp -----------------------------------------------------------

(loop for l = (read-line)
for n = (parse-integer l)
until (= n 42) do (format t "~D~%" n))

--- Scheme ---------------------------------------------------------

(do ((i (read) (read)))
((eq? i 42) '())
(begin
(display i)
(newline)))

--- OCaml ----------------------------------------------------------

while true do
let n = read_int () in
if n=42 then exit 0 else print_int n; print_newline ()
done

--- Haskell --------------------------------------------------------

main = interact f
where f = unlines . takeWhile (/="42") . words

--- Clips ----------------------------------------------------------

(defrule readin
?f<-(initial-fact)
=>
(retract ?f)
(assert (number (read)))
)

(defrule writeout
?f<-(number ?n)(test (<> ?n 42))
=>
(retract ?f)
(printout t ?n crlf)
(assert (initial-fact))
)

--- Prolog ---------------------------------------------------------

program :- get_char(X),get_char(Y),check(X,Y).
check('4','2'):-!.
check(X,Y):-write(X),get_char(Z),check(Y,Z).

--- C# -------------------------------------------------------------

using System;
public class Test
{
public static void Main()
{
int n;
while ((n = int.Parse(Console.ReadLine()))!=42)
Console.WriteLine(n);
}
}

--- BrainF**k ------------------------------------------------------

+[>>----------
[++++++++++<,----------]
>--------------------------------------------------
>----------------------------------------------------
>

[
<+++++++++++++++++++++++++++++++++++++++++++++++ ++ +++
<+++++++++++++++++++++++++++++++++++++++++++++++ ++ +
[>]<
[.<]++++++++++.---------->
[>]>>
]<

[++++++++++++++++++++++++++++++++++++++++++++++++++ ++
<+++++++++++++++++++++++++++++++++++++++++++++++ ++ +
[>]<
[.<]++++++++++.---------->
[>]>
]<

[>+++++++++++++++++++++++++++++++++++++++++++++++ ++ +++
<+++++++++++++++++++++++++++++++++++++++++++++++ ++ +
[>]<
[.<]++++++++++.---------->
[>]
]<

]

--- Perl -----------------------------------------------------------

print while($_=<>)!=42

--- Ruby -----------------------------------------------------------

print while gets != "42\n"

--- Pike -----------------------------------------------------------

int main() {
while (sscanf(Stdio.stdin->gets(), "%d", int n), n!=42 && write(n+"\n"));
return 0;
}

--- PHP ------------------------------------------------------------

<?php
while (true) {
$input = fgets(STDIN, 3);
if ($input == 42) {
break;
} else {
echo $input;
}
}
?>

--- Intercal -------------------------------------------------------

PLEASE DO ,1 <- #1
PLEASE DO .4 <- #0
PLEASE DO .5 <- #0
PLEASE DO .99 <- #0
DO COME FROM (30)
DO COME FROM (31)
DO WRITE IN ,1
DO .1 <- ,1SUB#1
DO .2 <- .4
DO (1000) NEXT
DO .4 <- .3~#255
DO (10) NEXT
(42) DO .1 <- .1
(20) DO .42 <- "&'&.4~#26'$#1"
PLEASE RESUME "?.42$#1"~#3
(10) DO (20) NEXT
DO FORGET #1
PLEASE COME FROM (42)
PLEASE STASH .1+.2+.3
DO .1 <- .4
DO .2 <- #50
DO (1010) NEXT
DO (100) NEXT
PLEASE STASH .1+.2+.3
DO .1 <- .99
DO .2 <- #52
DO (1010) NEXT
DO (101) NEXT
PLEASE GIVE UP
(201) DO .3 <- '.3~.3'~#1
PLEASE RESUME "?.3$#2"~#3
(101) DO (201) NEXT
DO FORGET #1
(32) PLEASE RETRIEVE .1+.2+.3
(200) DO .3 <- '.3~.3'~#1
PLEASE RESUME "?.3$#2"~#3
(100) DO (200) NEXT
DO FORGET #1
DO COME FROM (32)
PLEASE RETRIEVE .1+.2+.3
DO (102) NEXT
(31) DO .99 <- .4
(202) DO .98 <- '.99~.99'~#1
PLEASE RESUME "?.98$#2"~#3
(102) DO (202) NEXT
DO FORGET #1
DO .3 <- !99~#15'$!99~#240'
DO .3 <- !3~#15'$!3~#240'
DO .2 <- !3~#15'$!3~#240'
DO .1 <- .5
DO (1010) NEXT
DO .5 <- .2
DO ,1SUB#1 <- .3
PLEASE READ OUT ,1
(30) DO .99 <- .4

--- NASM -----------------------------------------------------------

global _start
section .data
buffer dw 0h
section .text
_start:
mov ecx, buffer
mov edx, 02h
call read
mov cx, word [buffer]
cmp cx, 3234h
je exit
cmp ch, 0ah
je one_dig
jmp two_dig
one_dig:
mov ecx, buffer
mov edx, 02h
call write
jmp _start
two_dig:
mov ecx, buffer
mov edx, 02h
call write
mov edx, 01h
mov ecx, buffer
call read ; read the 0ah
mov ecx, buffer
call write ; write the 0ah
jmp _start
exit:
mov eax, 01h ; exit()
xor ebx, ebx ; errno
int 80h
read:
mov eax, 03h ; read()
mov ebx, 00h ; stdin
int 80h
ret
write:
mov eax, 04h ; write()
mov ebx, 01h ; stdout
int 80h
ret

--- Ada95 ----------------------------------------------------------

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure test is
x : integer;
begin
loop
get(x);
if x/=42
then
put(x);
new_line(1);
else
exit;
end if;
end loop;
end test;

--- Bash -----------------------------------------------------------

while read a; do
if [ $a -eq 42 ]; then
exit
else
echo $a
fi
done

--- Smalltalk ------------------------------------------------------

|c number|
[
number:=0.
[ (c := stdin next) asciiValue ~= 10 ]
whileTrue:
[number := (number * 10) + (c asciiValue) - 48.].
number ~= 42
]
whileTrue:
[Transcript show: number printString; cr.]
!

--- Fortran --------------------------------------------------------

program TEST
integer ans
do
read (*,*) ans
if (ans.eq.42) stop
write (*,*) ans
enddo
stop
end

--- Icon -----------------------------------------------------------

procedure main ()
while (l := read()) ~= 42 do
write(l);
end
Reply With Quote
  #2  
Old 02-24-2009, 07:01 AM
khraghesh khraghesh is offline
Junior Member
 
Join Date: Feb 2009
Posts: 7
Default Thank you chef

Thanks for giving us a tit bit of your recipe.
Reply With Quote
  #3  
Old 08-31-2009, 01:03 PM
gunish gunish is offline
Junior Member
 
Join Date: Aug 2009
Posts: 1
Default Integers Between 0 and 99

I thought it was necessary to keep the integers between 0 and 99 ... correct me if i am wrong... but most of the programs above don't do that ..
Reply With Quote
  #4  
Old 08-31-2009, 01:45 PM
leppy
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by gunish View Post
I thought it was necessary to keep the integers between 0 and 99 ... correct me if i am wrong... but most of the programs above don't do that ..
What gave you that idea? Nothing in the problem statement states that.

Even if the constraints said that each of the numbers in the input were between 0 and 99, you aren't responsible for ensuring that.
Reply With Quote
  #5  
Old 08-31-2009, 09:40 PM
triplem triplem is offline
Senior Member
 
Join Date: Jul 2009
Posts: 221
Default

Quote:
Originally Posted by leppy View Post
What gave you that idea? Nothing in the problem statement states that.
Actually, it does

But as Leppy implied, that doesn't mean any of the programs above are wrong. They work perfectly fine when all numbers are between 0 and 99, so what is the problem?
Reply With Quote
  #6  
Old 09-01-2009, 01:11 AM
leppy
Guest
 
Posts: n/a
Default

Quote:
Originally Posted by TripleM View Post
Actually, it does
Appologies, I was looking for 0 <= N <= 99, not "All numbers at input are integers of one or two digits."

Cheers!
Reply With Quote
  #7  
Old 12-31-2009, 09:57 AM
skorned skorned is offline
Junior Member
 
Join Date: Aug 2009
Posts: 3
Default

Why isn't this C++ code working?
Code:
#include <iostream>

using namespace std;

int main(){
    int n;
    while(cin >> n && n!=42){
        cout << n;
    }
    return 0;
}
It works as expected on my compiler (Code::Blocks), but gives a 'Wrong Answer' on CodeChef
Reply With Quote
  #8  
Old 12-31-2009, 05:49 PM
leppyr64 leppyr64 is offline
Member
 
Join Date: Aug 2009
Posts: 44
Default

Input:
Code:
1
2
3
4
5
42
Expected Result:
Code:
1
2
3
4
5
Your result:
Code:
12345
Reply With Quote
  #9  
Old 12-31-2009, 06:29 PM
skorned skorned is offline
Junior Member
 
Join Date: Aug 2009
Posts: 3
Default

Are you serious? The reason my programs weren't working were a stupid new line character? In most programming competitions I've done, the automated checker just checks the various outputs of the program, not the formatting. And since my app will actually output each number separately, each number is a separate output. To a computer, the process is like
INPUT
OUTPUT
INPUT
OUTPUT
INPUT 42
END
So whether the numbers have newlines or tabs or spaces or nothing in between them shouldn't matter, should it?
Reply With Quote
  #10  
Old 12-31-2009, 07:50 PM
triplem triplem is offline
Senior Member
 
Join Date: Jul 2009
Posts: 221
Default

In the standard judge, any amount of whitespace is equal to any amount of whitespace. So you could put 20 new lines between outputs if you wanted; but of course you have to have SOMETHING. If your output file only contains 12345, there is no way the judge could tell that was 5 separate outputs. That is how all programming competitions work.
Reply With Quote
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:36 AM.