SlideShare uma empresa Scribd logo
1 de 15
Write a program that prints itself By  Kiran Bhogadi http://en.wikipedia.org/wiki/Drawing_Hands
One simple rule Don’ts :    The program can not read its own source code file and output it. Do’s :    The program is set up to compute its own source code and output it.
A preliminary approach Divide program into 2 parts : A and B A B Program =
A preliminary approach In your program : A and B could be two different functions/methods Or A and B could each be just a line of code Assume that execution starts in A and ends with B START		                 END A B
A First Implementation Idea  A and B are two methods The method A() has a copy of method B()’s code (as a literal string). A() prints this string and then calls B() A “<B>”
A First Implementation Idea (contd.) Tempting to design method B() to contain the code of method A as a literal string too!  But that won’t work! Circular Dependency
A better approach Method A has a string literal copy of Method B code and it simply prints it.  Method B will compute the  string the represents Method A’s code A “<B>”
Method A’s code – contains Method B as a string literal Let’s say the method A is written like this : Method A () { Print(“<Code_for_Method_B>”) }
Can we compute the code for Method A …   … given the code in the “box” i.e., “<Code_for_Method_B> ? YES!!
Computing Method A’s code given <Code_for_Method_B> as input The code (method) that computes Method A’s code : Input_String S  For e.g.  “<Code_for_Method_B>” Returns : “Method A () {” + NEWLINE   + “Print(” + Input_String S + “)” + NEWLINE   + “}” Note the returned string represents Method A’s code! Method A () { Print(“<Code_for_Method_B>”) }
So how does the code for Method B look like? See the previous slide!
Putting it All together Method A () { 	   Print(<Code_for_Method_B>); Call B(…); } <Code_for_Method_B>is a string literal representing  Method B’s code : Method B(Input String S) {  String returnValue =   “Method A () {” + NEWLINE   + “Print(” + Input_String S + “);” + NEWLINE  + “Call B(…);” + NEWLINE   + “}”; Print returnValue; }
What happens when the program runs ? Execution starts with Method A() A() outputs the code (“literal”) for Method B A() calls B() B() computes the code for Method A and outputs it Method A()Method B() Step 2: calls Step 3 : outputs computed value Step 1 : outputs literal B A
Some Technicalities / Questions If the original program file contained the methods A and B in this order:     then the output from the program contained the methods in reverse order But this can be fixed easily! A B B A
Further Reading ,[object Object]

Mais conteúdo relacionado

Mais procurados

Copy of dti2143/dam31303 chap 1 problem solving and program design
Copy of dti2143/dam31303 chap 1 problem solving and program designCopy of dti2143/dam31303 chap 1 problem solving and program design
Copy of dti2143/dam31303 chap 1 problem solving and program design
alish sha
 
Android tutorials7 calulator_improve
Android tutorials7 calulator_improveAndroid tutorials7 calulator_improve
Android tutorials7 calulator_improve
Vlad Kolesnyk
 

Mais procurados (20)

CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
Programming presentation
Programming presentationProgramming presentation
Programming presentation
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markers
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 
C strings
C stringsC strings
C strings
 
Pointers operation day2
Pointers operation day2Pointers operation day2
Pointers operation day2
 
03 Operators and expressions
03 Operators and expressions03 Operators and expressions
03 Operators and expressions
 
Programming fundamentals
Programming fundamentalsProgramming fundamentals
Programming fundamentals
 
CP Handout#5
CP Handout#5CP Handout#5
CP Handout#5
 
C and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharC and C++ Industrial Training Jalandhar
C and C++ Industrial Training Jalandhar
 
Copy of dti2143/dam31303 chap 1 problem solving and program design
Copy of dti2143/dam31303 chap 1 problem solving and program designCopy of dti2143/dam31303 chap 1 problem solving and program design
Copy of dti2143/dam31303 chap 1 problem solving and program design
 
Android tutorials7 calulator_improve
Android tutorials7 calulator_improveAndroid tutorials7 calulator_improve
Android tutorials7 calulator_improve
 
Unit i intro-operators
Unit   i intro-operatorsUnit   i intro-operators
Unit i intro-operators
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
 
New lambdas
New lambdasNew lambdas
New lambdas
 
Flowchart and algorithm
Flowchart and algorithmFlowchart and algorithm
Flowchart and algorithm
 
CP Handout#8
CP Handout#8CP Handout#8
CP Handout#8
 
C Programming
C ProgrammingC Programming
C Programming
 
Algorithms and flowcharts
Algorithms and flowchartsAlgorithms and flowcharts
Algorithms and flowcharts
 
03. operators and-expressions
03. operators and-expressions03. operators and-expressions
03. operators and-expressions
 

Semelhante a Quine - A program that computes & prints its own source code

Arithmetic operator
Arithmetic operatorArithmetic operator
Arithmetic operator
Md Masudur Rahman
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
Hattori Sidek
 
C language questions_answers_explanation
C language questions_answers_explanationC language questions_answers_explanation
C language questions_answers_explanation
srinath v
 
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Simplilearn
 
Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2
hassaanciit
 

Semelhante a Quine - A program that computes & prints its own source code (20)

Arithmetic operator
Arithmetic operatorArithmetic operator
Arithmetic operator
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
 
Algorithm pseudocode flowchart program notes
Algorithm pseudocode flowchart program notesAlgorithm pseudocode flowchart program notes
Algorithm pseudocode flowchart program notes
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
Chapter2
Chapter2Chapter2
Chapter2
 
Basics Of C++.pptx
Basics Of C++.pptxBasics Of C++.pptx
Basics Of C++.pptx
 
Building Simple C Program
Building Simple  C ProgramBuilding Simple  C Program
Building Simple C Program
 
c++
c++c++
c++
 
Session 1
Session 1Session 1
Session 1
 
Session 1
Session 1Session 1
Session 1
 
C language questions_answers_explanation
C language questions_answers_explanationC language questions_answers_explanation
C language questions_answers_explanation
 
Programming Fundamentals lecture 5
Programming Fundamentals lecture 5Programming Fundamentals lecture 5
Programming Fundamentals lecture 5
 
C introduction
C introductionC introduction
C introduction
 
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++
 
Unit-IV.pptx
Unit-IV.pptxUnit-IV.pptx
Unit-IV.pptx
 
Compiling and linking of a c code
Compiling and linking of a c codeCompiling and linking of a c code
Compiling and linking of a c code
 
Basic of c++ programming
Basic of c++ programmingBasic of c++ programming
Basic of c++ programming
 
Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2Introduction to Computer and Programing - Lab2
Introduction to Computer and Programing - Lab2
 
1 c introduction
1 c introduction1 c introduction
1 c introduction
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

Quine - A program that computes & prints its own source code

  • 1. Write a program that prints itself By Kiran Bhogadi http://en.wikipedia.org/wiki/Drawing_Hands
  • 2. One simple rule Don’ts : The program can not read its own source code file and output it. Do’s : The program is set up to compute its own source code and output it.
  • 3. A preliminary approach Divide program into 2 parts : A and B A B Program =
  • 4. A preliminary approach In your program : A and B could be two different functions/methods Or A and B could each be just a line of code Assume that execution starts in A and ends with B START END A B
  • 5. A First Implementation Idea A and B are two methods The method A() has a copy of method B()’s code (as a literal string). A() prints this string and then calls B() A “<B>”
  • 6. A First Implementation Idea (contd.) Tempting to design method B() to contain the code of method A as a literal string too! But that won’t work! Circular Dependency
  • 7. A better approach Method A has a string literal copy of Method B code and it simply prints it. Method B will compute the string the represents Method A’s code A “<B>”
  • 8. Method A’s code – contains Method B as a string literal Let’s say the method A is written like this : Method A () { Print(“<Code_for_Method_B>”) }
  • 9. Can we compute the code for Method A … … given the code in the “box” i.e., “<Code_for_Method_B> ? YES!!
  • 10. Computing Method A’s code given <Code_for_Method_B> as input The code (method) that computes Method A’s code : Input_String S For e.g. “<Code_for_Method_B>” Returns : “Method A () {” + NEWLINE + “Print(” + Input_String S + “)” + NEWLINE + “}” Note the returned string represents Method A’s code! Method A () { Print(“<Code_for_Method_B>”) }
  • 11. So how does the code for Method B look like? See the previous slide!
  • 12. Putting it All together Method A () { Print(<Code_for_Method_B>); Call B(…); } <Code_for_Method_B>is a string literal representing Method B’s code : Method B(Input String S) { String returnValue = “Method A () {” + NEWLINE + “Print(” + Input_String S + “);” + NEWLINE + “Call B(…);” + NEWLINE + “}”; Print returnValue; }
  • 13. What happens when the program runs ? Execution starts with Method A() A() outputs the code (“literal”) for Method B A() calls B() B() computes the code for Method A and outputs it Method A()Method B() Step 2: calls Step 3 : outputs computed value Step 1 : outputs literal B A
  • 14. Some Technicalities / Questions If the original program file contained the methods A and B in this order: then the output from the program contained the methods in reverse order But this can be fixed easily! A B B A
  • 15.