A Code to Check Whether A String is An Element of The pq System or Not

Dolyetyus

Co Admin
21 Nis 2020
1,205
666
Delft
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

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:

rootibo

Kıdemli Üye
13 Mar 2023
2,168
1,459
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

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")
very good topic .
 

QuatrexDefacer

Black Hat Junior
15 Eki 2022
573
336
Baku
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

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")
nice topic.
 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.