Life of student

All about Programming , C++ ,Fedora

Polyglot:single program runs on multiple languages

Posted by pankaj4u4m on October 4, 2009

Before it i never heard the name polyglot!!!! I saw this on one of online event in IITM tech fest. Initially I faced difficulty but when I did one program rest were seems very easy to do. and I stood first in this competition. Polyglot is not a programming language but it is way of programming such that it runs on two or more compiler (different languages) simultaneously without making any change on it.   “A polyglot is a computer program or script written in a valid form of multiple programming languages, which performs the same operations or output independently of the programming language used to compile or interpret it.”

See it very interesting:

#include<stdio.h>
#define print(x) main(){printf(x);}
print("Pankaj\n")

it can runs in perl and C and prints Pankaj

There were saveral problems which I did successfully and here are these:

1>here are 3 cities A B and C. City A and B are connected with p roads. City B and C are connected with q roads. City C and A are connected with r roads. In how many different ways can one reach city C starting from city A? (0<p,q,r<10)

sample input:

1 1 1

samlpe output:

2

the code is:

#include<stdio.h>/*
0 and length<<2;
"""
2
(*O/*_/
/# * ) 2>_$$;read -n2 p;read -n2 q;read -n1 r;((r=p*q+r));echo $r;exit;
/*0)unless
$p=getc();getc();$q=getc();getc();$r=getc();print$p*$q+$r;__END__
"""
p=raw_input()
print int(p[0])*int(p[2])+int(p[4])
#*/
#define X p;q;r;main(){scanf("%d%d%d",&p,&q,&r);printf("%d",p*q+r);}x
X=1;

it runs on BASH, PERL, PYTHON and C

2>Polyglot that takes as input a string of length n and prints the reverse of the string. (0<n<20)

sample input:

pankaj

sample output:

jaknap
Code:

#include<cstdio>
#include<cstring>
#include<algorithm>/*
#,----------[>,----------]<[++++++++++.<]++++++++++.
0 and length <<2;
"""
2
(*O/*_/
/# * ) 2>_$$;read n;echo $n|rev;exit;
/*0)unless
$p=<>;$p=~s/\s+$//;$p = reverse $p;print $p;__END__
"""
p=raw_input()
p=p[::-1];
print p
#*/
#define X int main(){char s[20];gets(s);std::reverse(s,s+strlen(s));puts(s);return 0;}int x
X=1;

runs on BRAINFUCK,BASH,PERL,PYTHON and C++

3>Given an array of ten numbers a[i] 1<=i<=10 and a number n.
Write a minimum 3 language polyglot that outputs 0 if the number n is not
found in the array or outputs 1 if the number n is found in the array.


#include<stdio.h>/*
0 and length <<2;
"""
2
(*O/*_/
/# * ) 2>_$$;i=0
while [ $i -lt 11 ]
do
read p[$i]
((i++))
done
i=0
while [ $i -lt 10 ]
do
if [ ${p[$i]} -eq ${p[10]} ];then
echo "1"
exit
fi
((i++))
done
echo "0"
exit
/*0)unless
$p[0]=<>;
for($i=1;$i<11;$i++){$p[$i]=<>;}for($i=0;$i<10;$i++){if($p[$i]==$p[10]){print "1";exit;}}print "0";__END__
"""
def h():
p=[0,0,0,0,0,0,0,0,0,0,0]
for i in range(0,11):
p[i]=input();
i=i+1;
for i in range(0,10):
if p[i]==p[10]:
return 1
return 0
print h()
#*/
#define X i;p[15];main(){for(i=0;i<11;i++)scanf("%d",&p[i]);for(i=0;i<10;i++)if(p[i]==p[10]){printf("1");return;}printf("0");}x
X=1;

BASH,PERL,PYTHON and C

4>Consider a two-pan balance, you are asked to weigh a piece of wood
known to have some integral weight between 1 gram and n grams (3<n<999).
If the weights can go in either pan, what is the minimum number of integral weights you need to weigh the wood.

sample input:

4

sample output:

2

code:

#include<stdio.h>/*
0 and length <<2;
"""
2
(*O/*_/
/# * ) 2>_$$;read s;r=1;n=0;((s=s*2+1));while [ $r -lt $s ];do ((r*=3));((n++));done;echo $n;exit;
/*0)unless
$s=<>;$r=1;$n=0;while($r<$s*2+1){$r*=3;$n+=1;}print$n;__END__
"""
s=input()
r=1
n=0
while r<s*2+1:
r*=3
n+=1
print n
#*/
#define X s,r=1,n=0;main(){scanf("%d",&s);while(r<s*2+1){r*=3;n++;}printf("%d",n);}x
X=1;

BASH,PERL,PYTHON and C

5>A quine is a program that produces its own source code as output.Polyglot quine program that takes as input a
integer n (0<n<10) and outputs its source code n times.


#include<stdio.h>/*
#<?php $b=file_get_contents(__FILE__);$a=trim(fgets(STDIN));$i=0;while($i<$a){$j=0;if($i==0)$j=1;while($j<strlen($b))echo $b[$j++];$i+=1;};return 0;?>
0 and length <<2;
"""
2
(*O/*_/
/# * ) 2>_$$;read n;while [ $n -gt 0 ];do cat $0;((n--));done;exit;
/*0)unless
open FILE, __FILE__;
$a=<>;
$b="";
while(<FILE>){$b=$b.$_;}
while($a--){print $b;}
__END__
"""
import sys
f=open(sys.argv[0],"r")
n=input()
while(n>0):
for i in f:
print i,
n=n-1
f.seek(0)
#*/
#define X n;c;main(){FILE *f;f=fopen(__FILE__,"r");scanf("%d",&n);while(n--){while((c=getc(f))!=EOF)putchar(c);fseek(f,0,0);}}x
X=1;

This one is very interesting it is in PHP,BASH,PERL,PYTHON and C (five language polyglot)

6>Polyglot program that will recognise its own
source code. The Program should output 1 if the input is its source code
itself and output 0 if the input is not its source code.


#include<stdio.h>/*
0 and length <<2;
"""
2
open FILE,__FILE__;$b=1;while(<FILE>){$a=<STDIN>;if($_ ne $a){$b=0;}}print $b;__END__
"""
import sys
def h():
f=open(sys.argv[0],"r")
for i in f:
x=raw_input();
if(cmp(i,x)==0):
return 0;
return 1;
print h()
#*/
#define X b;c;a=1;main(){FILE *f;f=fopen(__FILE__,"r");while((c=getc(f))!=EOF){b=getchar();if(c!=b)a=0;}printf("%d",a);}x
X=1;

on PERL,PYTHON and C

Here is a duine program in 5 languates:C,brainf**k,perl,ruby and python

At last I enjoyed this event a lot and had learnt different languages just in two days!!!
NOTE:this post and attachments are only for my future reference
NOTE:FOR ALL PROGRAM input will be STDIN and output will be STDOUT

For more information click here Introduction to polyglot

About these ads

3 Responses to “Polyglot:single program runs on multiple languages”

  1. This is awesome! I was working on a polyglot program, and your results are really great. I don’t know if I’ll be able to apply your techniques in my program, but I can try!

  2. nQRNUW said

    Long time lurker, first time poster. Great post, everyone on here seems to be in agreeance with what you wrote.
    .

  3. This is my Perl/C polyglot solution for http://golf.shinh.org/p.rb?Cut+Down+FIXED


    #include
    #include
    #define sub int
    #define my char
    #define ord
    #define chr(x) (x)
    #define eof feof
    #if 0
    sub isupper {
    $_[0] =~ /^[[:upper:]]/;
    }
    #endif
    sub main(){
    my $p,$u;
    while(!eof(stdin)){
    $p = ord(getc(stdin));
    $u = ($p > ord('A')-1 && $p < ord('Z')+1);
    if($u){
    $p+=32;
    printf(" ");
    }
    if($p<0){return 0;}
    printf("%c", $p);
    }
    }
    main();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: