Narcissist: Difference between revisions
m
→{{header|Phix}}: missing /, or type it in
(Added Kotlin) |
m (→{{header|Phix}}: missing /, or type it in) |
||
(35 intermediate revisions by 22 users not shown) | |||
Line 20:
Took code from [[Quine]], has to be in one line (could be done pretty printed, too, but not as simple).
<
=={{header|ALGOL 68}}==
Line 27:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''}}
<
Output: T or F depending on input.
=={{header|AppleScript}}==
As long as the script hasn't been exported as run-only (''ie.'' without its source code):
<syntaxhighlight lang="applescript">(display dialog "" default answer "")'s text returned = (do shell script ("osadecompile " & (path to me)'s POSIX path's quoted form))</syntaxhighlight>
{{output}}
Depending on the text entered and button clicked, one of:
<syntaxhighlight lang="applescript">true
false
error "User cancelled." number -128</syntaxhighlight>
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">blk:[print (("blk:" ++ (as .code blk) ++ "do blk") = input "") ? -> "accept" -> "reject"]do blk</syntaxhighlight>
{{out}}
<pre>$ arturo narcissist.art < narcissist.art
accept</pre>
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<
FileRead, Source, % A_ScriptFullPath
return Input == Source ? "accept" : "reject"
}</
'''Example Use:'''
<
. Narcissist("This isn't the text you're looking for.")</
{{Output}}
<pre>accept
Line 46 ⟶ 66:
{{works with|BBC BASIC for Windows}}
Prints '1' on success and '0' on failure.
<
=={{header|Befunge}}==
Line 53 ⟶ 73:
Outputs <tt>1</tt> if the given line of input matches the source code, and <tt>0</tt> if it doesn't.
<
=={{header|C}}==
Based upon the quine. Reads until EOF or newline from stdin, and writes "1" or "0" to stdout.
<
=={{header|C sharp}}==
<
using System;
using System.IO;
Line 89 ⟶ 109:
}
}
</syntaxhighlight>
=={{header|Common Lisp}}==
Only checks the first line of stdin:
<
=={{header|D}}==
<syntaxhighlight lang="d">import std.file; import std.stdio; import std.string; void main() { auto source = readText("narcissist.d").chomp; auto input = readln().chomp(); if (source == input) writeln("accept"); else writeln("reject"); }</syntaxhighlight>
=={{header|Déjà Vu}}==
<
=={{header|Forth}}==
<
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">Dim As String a, s = "Dim As String a, s = Input a : Print (a = Left(s, 21) + Chr(34) + s + Chr(34) + Mid(s, 47, 3) + Mid(s, 23, 108)) + Mid(s, 54, 3)" : Input a : Print (a = Left(s, 21) + Chr(34) + s + Chr(34) + Mid(s, 47, 3) + Mid(s, 23, 108))</syntaxhighlight>
=={{header|Go}}==
Line 105 ⟶ 134:
This version reads until EOF and expects a newline at the end of the input. If this is being checked from a file, make sure that the file has exactly one newline at the end of it.
<
A version respecting the 80 character line limit:
<
import (
Line 145 ⟶ 174:
}
var x = `</
=={{header|Haskell}}==
<
=={{header|Huginn}}==
<syntaxhighlight lang="huginn">#! /bin/sh
exec huginn --no-argv -E "${0}"
#! huginn
main() {
c = "#! /bin/sh{1}~"
"exec huginn --no-argv -E {3}${{0}}{3}{1}#! huginn{1}{1}~"
"main() {{{1}{2}c = {3}{0}{3};{1}~"
"{2}s = {3}{3};{1}~"
"{2}while ( ( line = input() ) != none ) {{{1}~"
"{2}{2}s += line;{1}~"
"{2}}}{1}~"
"{2}self = copy( c ).replace( {3}{5}{3}, {3}{3} )~"
".format({1}{2}{2}c.replace( {3}{5}{3}, ~"
"{3}{5}{4}{3}{4}n{4}t{4}t{4}{3}{3} ), ~"
"{3}{4}n{3}, {3}{4}t{3}, {3}{4}{3}{3}, {3}{4}{4}{3}, ~"
"{3}{5}{3}{1}{2});{1}~"
"{2}print( s == self ? {3}1{4}n{3} : {3}0{4}n{3} );{1}}}{1}{1}";
s = "";
while ( ( line = input() ) != none ) {
s += line;
}
self = copy( c ).replace( "~", "" ).format(
c.replace( "~", "~\"\n\t\t\"" ), "\n", "\t", "\"", "\\", "~"
);
print( s == self ? "1\n" : "0\n" );
}
</syntaxhighlight>
=={{header|Icon}} and {{header|Unicon}}==
{{trans|Go}}
Since we can't put a link statement and program on a single line, we implement a simplified inline sprintf so we don't have to deal with all the double quotes or substrings and offsets. If we'd needed to write multiple procedures on a line it can be done [[Icon%2BUnicon/Intro#Semi-colons| Semi-colons in the language intro]]
<
Example:<pre>./narcissist.exe < narcissist.icn
Accept</pre>
Line 159 ⟶ 218:
=={{header|J}}==
<
main=:3 : 0 NB. tested under j602
self=: '#!/j602/bin/jconsole',LF,'main=:',(5!:5<'main'),LF,'main''''',LF
echo self -: stdin''
)
main''</
Example use:
<syntaxhighlight lang="text">$ ./narcissist.ijs <narcissist.ijs
1
</
Note that this assumes a suitable os command line.
Line 176 ⟶ 235:
'''Alternative solution:'''
<
'''Example use:'''
<
0
(-:,~,2#{:)&'(-:,~,2#{:)&''' '(-:,~,2#{:)&''(-:,~,2#{:)&'''''''
1</
=={{header|Java}}==
<syntaxhighlight lang="java">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Narcissist {
private static final String SOURCE = "import java.io.BufferedReader;%nimport java.io.IOException;%nimport java.io.InputStreamReader;%n%npublic class Narcissist {%n private static final String SOURCE = %c%s%c;%n private static final char QUOTE = 0x22;%n%n public static void main(String[] args) throws IOException {%n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));%n StringBuilder sb = new StringBuilder();%n%n while (true) {%n String line = br.readLine();%n if (null == line) break;%n sb.append(line).append(System.lineSeparator());%n }%n%n String program = String.format(SOURCE, QUOTE, SOURCE, QUOTE, QUOTE, QUOTE, QUOTE, QUOTE);%n if (program.equals(sb.toString())) {%n System.out.println(%caccept%c);%n } else {%n System.out.println(%creject%c);%n }%n }%n}%n";
private static final char QUOTE = 0x22;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
while (true) {
String line = br.readLine();
if (null == line) break;
sb.append(line).append(System.lineSeparator());
}
String program = String.format(SOURCE, QUOTE, SOURCE, QUOTE, QUOTE, QUOTE, QUOTE, QUOTE);
if (program.equals(sb.toString())) {
System.out.println("accept");
} else {
System.out.println("reject");
}
}
}
</syntaxhighlight>
=={{header|JavaScript}}==
{{works with|SpiderMonkey|1.7.0}}
Based upon [[Quine#Using_eval|one of the quines]]. Outputs 'true' if source is equal to inputted line (newline terminated), 'false' otherwise.
<
{{works with|JScript}}
<
function readfile(fname) {
var h = oFSO.OpenTextFile(fname, 1, false);
Line 210 ⟶ 298:
// compare and contrast
WScript.Echo(self === whatever ? "Accept" : "Reject");
</syntaxhighlight>
{{works with|Julia|1.2}}
=={{header|Julia}}==
<syntaxhighlight lang="julia">mysource = Base.read(Base.source_path(), String)
println(Int(ARGS[1] == mysource))</syntaxhighlight>
=={{header|Kotlin}}==
<
fun main(args: Array<String>) {
Line 221 ⟶ 315:
val lines = Array<String>(n) { readLine()!! }
if (lines.joinToString("\r\n") == text) println("\naccept") else println("\nreject")
}</
First run (pasting in program text):
{{out}}
Line 255 ⟶ 349:
NOTE: You have to manually type in ALL of the code since the Input statement will not successfully input data from a paste event even though it will show up in the MainWin.
<syntaxhighlight lang="lb">
s$ = "s$ = Input a$ : Print (a$ = Left$(s$, 5) + chr$(34) + s$ + chr$(34) + Mid$(s$, 14, 3) + Mid$(s$, 6, 100)) + Mid$(s$, 23, 3)" : Input a$ : Print (a$ = Left$(s$, 5) + chr$(34) + s$ + chr$(34) + Mid$(s$, 14, 3) + Mid$(s$, 6, 100))
</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
{{in}}
<
Print[InputString[] == ToString[StringForm[prog, ToString[prog, InputForm]]]];</syntaxhighlight>
{{out}}
<pre>True</pre>
=={{header|Nanoquery}}==
<syntaxhighlight lang="nanoquery">import Nanoquery.IO
// get a file tied to this program's source
$f = new(File, $args[len($args) - 1])
// get the source and split across lines
$source = split($f.readAll(), "\n")
// read that amount of lines from the console
$in = list(len($source))
for ($i = 0) ($i < len($source)) ($i = $i + 1)
append $in input()
end
// check line by line
for ($i = 0) ($i < len($in)) ($i = $i + 1)
// check if the lines are equal
if ($in[$i] != $source[$i])
println "Reject"
exit
end
end
// if we got here, the lines are the same
println "Accept"</syntaxhighlight>
=={{header|Nim}}==
Program written on a single line to avoid problems with indentation. But we wanted to avoid the too easy solution consisting to read the source file. Input should be terminated by an end of file (Ctrl-D on Linux).
<syntaxhighlight lang="nim">
import strutils; let a = "import strutils; let a = $#; echo ord(stdin.readAll == a % (chr(34) & a & chr(34)) & chr(10))"; echo ord(stdin.readAll == a % (chr(34) & a & chr(34)) & chr(10))
</syntaxhighlight>
{{out}}
<pre>./narcissist
whatever...
0</pre>
<pre>
./narcissist <narcissist.nim
1</pre>
=={{header|PARI/GP}}==
<
Run narcissist():<pre>Hello
0</pre>
<pre>narcissist() = {input() == narcissist;}
1</pre>
=={{header|Perl}}==
<
local $/;
print do { open 0; <0> } eq <> ? "accept" : "reject";</
Run:
<syntaxhighlight lang="text">perl narc.pl < narc.pl</
=={{header|Phix}}==
<!--(notonline)-->
<syntaxhighlight lang="phix">
printf(1,"\n\n%t\n\n",get_text(command_line()[2],GT_LF_LAST)[1]=gets(0))
</syntaxhighlight>
{{out}}
Can be run using <code>p test.exw < test.exw</code> or the input manually typed in
<pre>
true
</pre>
<small>Aside: I suppose there should really be a leading <code>without js -- file i/o</code>, but I wanted to keep it a 1-liner.<br>
Interpreted-only, as is: to allow a compiled version to run, change <code>command_line()[2]</code> to (say) <code>"test.exw"</code><br>
or maybe <code>substitute(command_line()[2],".exe",".exw")</code>, and ship with/install the source code file.</small>
=={{header|PicoLisp}}==
<
(= Str (str narcissist)) )</
Output:
<pre>: (narcissist "(Str) (= Str (str narcissist))")
Line 305 ⟶ 438:
=={{header|PowerShell}}==
{{works with|PowerShell|2}}
<syntaxhighlight lang="powershell">
function Narcissist
{
Line 312 ⟶ 445:
Else { 'Reject' }
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
Narcissist 'Banana'
Line 323 ⟶ 456:
'@
</syntaxhighlight>
{{out}}
<pre>
Line 332 ⟶ 465:
=={{header|Python}}==
{{Works with|Python|2}}
<syntaxhighlight lang="python">
import sys
with open(sys.argv[0]) as quine:
Line 340 ⟶ 473:
else:
print("Reject")
</syntaxhighlight>
{{Works with|Python|3}}
<syntaxhighlight lang="python">
_='_=%r;print (_%%_==input())';print (_%_==input())
</syntaxhighlight>
=={{header|Quackery}}==
As a dialogue in the Quackery shell. (REPL)
<syntaxhighlight lang="quackery">Welcome to Quackery.
Enter "leave" to leave the shell.
/O> [ this copy unbuild = ] is narcissist
... $ "[ this copy unbuild = ]" narcissist
...
Stack: 1
/O> drop
... $ "Nothing is more important than my egomania. -- Clara Oswald" narcissist
...
Stack: 0
/O> leave
...
Cheerio.</syntaxhighlight>
=={{header|Racket}}==
Line 352 ⟶ 510:
typed when the code stops to read some input.
<syntaxhighlight lang="racket">
-> ((lambda (x) (equal? (read) (list x (list 'quote x))))
'(lambda (x) (equal? (read) (list x (list 'quote x)))))
Line 358 ⟶ 516:
'(lambda (x) (equal? (read) (list x (list 'quote x)))))
#t
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
For the narcissist to work you must be very careful with whitespace. The following version works if it is given to standard input as exactly one line terminated by a newline character.
Note how the code takes advantage of Raku's ability to nest quoting delimiters.
<syntaxhighlight lang="raku" line>EVAL my $self = q{say slurp() eq q[EVAL my $self = q{]~$self~q[}]~10.chr ?? q{Beautiful!} !! q{Not my type.}}</syntaxhighlight>
{{out}}
<pre>$ narcissist='EVAL my $self = q{say slurp() eq q[EVAL my $self = q{]~$self~q[}]~10.chr ?? q{Beautiful!} !! q{Not my type.}}'
$ raku -e "$narcissist" <<<"$narcissist"
Beautiful!
$ raku -e "$narcissist" <<<"$narcissist # a comment ruining it all"
Not my type.
</pre>
=={{header|REXX}}==
===version 1===
(returns '''1''' or '''0''')
<syntaxhighlight lang
===version 2===
(returns '''accept''' or '''reject''')
<
≪ ‘NAR6’ RCL == ≫
‘NAR6’ STO
{{in}}
<pre>
≪ ‘NAR6’ RCL == ≫ NAR6
</pre>
{{out}}
<pre>
1: 1
</pre>
=={{header|RPL}}==
« LASTARG RCL →STR == » '<span style="color:blue">NARCISSIST</span>' STO
"« LASTARG RCL →STR == »" '<span style="color:blue">NARCISSIST</span>' EVAL <span style="color:grey">@ returns 1</span>
"string" '<span style="color:blue">NARCISSIST</span>' EVAL <span style="color:grey">@ returns 0</span>
=={{header|Ruby}}==
Translation of the C version.
<
Output:
<pre>$ ruby narcissist.rb < narcissist.rb
accept</pre>
=={{header|Rust}}==
<syntaxhighlight lang="rust">use std::io::{stdin, prelude::*};
fn main() {
let src = include_str!("main.rs");
let mut input = String::new();
stdin()
.lock()
.read_to_string(&mut input)
.expect("Could not read from STDIN");
println!("{}", src == input);
}</syntaxhighlight>
=={{header|Scala}}==
<syntaxhighlight lang="scala">import scala.io.StdIn
object Narcissist extends App {
val text = scala.io.Source.fromFile("Narcissist.scala", "UTF-8").toStream
println("Enter the number of lines to be input followed by those lines:\n")
val n = StdIn.readInt()
val lines = Stream {
StdIn.readLine()
}
if (lines.mkString("\r\n") == text) println("\naccept") else println("\nreject")
}</syntaxhighlight>
=={{header|Sidef}}==
<
=={{header|Swift}}==
<syntaxhighlight lang="swift">#! /usr/bin/swift
import Foundation
let script = CommandLine.arguments[0]
print(script)
let mytext = try? String.init(contentsOfFile: script, encoding: .utf8)
var enteredtext = readLine()
if mytext == enteredtext {
print("Accept")
} else {
print("Reject")
}</syntaxhighlight>
=={{header|Tcl}}==
With the use of explicit reflexive introspection:
<
Without such commands, using pure generation of strings and lists:
<
=={{header|TXR}}==
<
@(next :args)
@filename
Line 405 ⟶ 636:
@result
@(end)
</syntaxhighlight>
{{out}}
Line 413 ⟶ 644:
=={{header|UNIX Shell}}==
<
=={{header|VBA}}==
Based on the quine
<syntaxhighlight lang="vb">Public Sub narcissist()
quote = Chr(34)
comma = Chr(44)
cont = Chr(32) & Chr(95)
rparen = Chr(41)
n = Array( _
"Public Sub narcissist()", _
" quote = Chr(34)", _
" comma = Chr(44)", _
" cont = Chr(32) & Chr(95)", _
" rparen = Chr(41)", _
" n = Array( _", _
"How many lines?", _
"Line ", _
" x = InputBox(n(5))", _
" flag = True", _
" For i = 0 To 5", _
" If InputBox(n(6) & i) <> n(i) Then flag = False", _
" Next i", _
" For i = 0 To 20", _
" If InputBox(n(6) & i + 6) <> quote & n(i) & quote & comma & cont Then flag = False", _
" Next i", _
" If InputBox(n(6) & 27) <> quote & n(21) & quote & rparen Then flag = False", _
" For i = 7 To 21", _
" If InputBox(n(6) & i + 21) <> n(i) Then flag = False", _
" Next i", _
" Debug.Print IIf(flag, 1, 0)", _
"End Sub")
x = InputBox(n(5))
flag = True
For i = 0 To 5
If InputBox(n(6) & i) <> n(i) Then flag = False
Next i
For i = 0 To 20
If InputBox(n(6) & i + 6) <> quote & n(i) & quote & comma & cont Then flag = False
Next i
If InputBox(n(6) & 27) <> quote & n(21) & quote & rparen Then flag = False
For i = 7 To 21
If InputBox(n(6) & i + 21) <> n(i) Then flag = False
Next i
Debug.Print IIf(flag, 1, 0)
End Sub</syntaxhighlight>
=={{header|Wren}}==
<syntaxhighlight lang="wren">import "os" for Process, Platform
import "io" for File, Stdin, Stdout
var args = Process.allArguments
var text = File.read(args[1]).trim()
System.print("Enter the number of lines to be input followed by those lines:\n")
Stdout.flush()
var n = Num.fromString(Stdin.readLine())
var lines = List.filled(n, null)
for (i in 0...n) lines[i] = Stdin.readLine()
var joiner = Platform.isWindows ? "\r\n" : "\n"
var text2 = lines.join(joiner)
System.print()
if (text2 == text) {
System.print("accept")
} else if (text.startsWith(text2)) {
System.print("not yet finished")
} else {
System.print("reject")
}</syntaxhighlight>
{{out}}
Sample session:
<pre>
Enter the number of lines to be input followed by those lines:
2
import "os" for Process, Platform
import "io" for File, Stdin, Stdout
not yet finished
</pre>
=={{header|zkl}}==
<
thisFileSrc:=File(System.argv[1]).read();
println((stdin==thisFileSrc) and "input matches "+System.argv[1] or "No match");</
Since zkl is [usually] compile-when-run, we just compare the contents of source file to stdin.
{{out}}
|