Hi folks, I am sharing this here because maybe it will help one of you in your homework or algorithm and logic lessons et cetera.
First of all, What Is The pq System?
The pq system is a simple system of three different symbols: p, q, and (the letter p, the letter q, and the hyphen). From these three symbols, you can generate system-specific or non-system symbol sequences like p, qq-p or --p-q.
A string belongs to the system if it is an axiom or can be derived from an axiom with a rule.
The pq system has (infinitely) axioms in the following case:
• All strings in the format Xp-qX-, where X represents the same number of non-zero hyphens, are part of the system.
And one rule of thumb to generate new sequences from these axioms:
Assume that X, Y, and Z represent strings containing only hyphens. If XpYqZ is part of the system, then XpY-qZ- is part of the system
Definition from: pq-System Explorer - Wolfram Demonstrations Project
Code To Check The String
First of all, What Is The pq System?
The pq system is a simple system of three different symbols: p, q, and (the letter p, the letter q, and the hyphen). From these three symbols, you can generate system-specific or non-system symbol sequences like p, qq-p or --p-q.
A string belongs to the system if it is an axiom or can be derived from an axiom with a rule.
The pq system has (infinitely) axioms in the following case:
• All strings in the format Xp-qX-, where X represents the same number of non-zero hyphens, are part of the system.
And one rule of thumb to generate new sequences from these axioms:
Assume that X, Y, and Z represent strings containing only hyphens. If XpYqZ is part of the system, then XpY-qZ- is part of the system
Definition from: pq-System Explorer - Wolfram Demonstrations Project
Code To Check The String
Python:
import re
hyphencounterforward=0
hyphencounterreverse=0
dashnumber=0
theorem=0
hyphennumber=0
i=0
j=0
qbefore = 0
pq=input("Enter the string: ")
p = "p" in pq
q = "q" in pq
dash= "-" in pq
if p==1 and q==1 and dash==1 and pq[-1] == "-" and pq[0] == "-":
reversepq=pq[::-1]
text1 = pq.partition("p")[0]
text2 = reversepq.partition("q")[0]
hyphencounterforward=len(text1)
hyphencounterreverse=len(text2)
if hyphencounterreverse==hyphencounterforward+1:
dashnumber=1
pqorder=pq.replace("-", "")
if pqorder[0]=="q":
qbefore=1
if qbefore==0:
hyphenbetween = re.search('p(.*)q', pq)
hyphennumber=hyphenbetween.group(1)
print()
if len(hyphennumber) >= 1:
hyphennumber=1
dashcheck=pq.replace("q", "")
dashcheck=dashcheck.replace("p", "")
if len(dashcheck)/2==hyphencounterreverse:
theorem=1
if qbefore==0 and dashnumber==1 and hyphennumber==1 or theorem==1:
print("This string is an element of the system")
else:
print("This string is not an element of the system")
else:
print("This string is not an element of the system")
Son düzenleme: