programming languages – Knight tracking

def get_possibilities(x,y):
    possible = ()
    moves = ((2,1) , (1,2),(2,-1),(-1,2),(-2,1),(1,-2),(-2,-1),(-1,-2))
    for i in range(m):
        for j in range (n):
            for (dx,dy) in moves:
                if 0 < x+dx <= m and 0<y+dy<=n and (x+dx,y+dy) not in possible :
    return possible
def solv(m,n,x,y):
    for i in range (m):
        for j in range (n):
            pos = get_possibilities(x,y)
            mini = pos(0)
            for p in pos:
                    if len(get_possibilities(p(0), p(1))) <= len(get_possibilities(mini(0), mini(1))):
                        mini = p 
            (x,y) = (mini(0),mini(1))

I am trying to implement knight tour using Warnsdroff alogorithm , this is what I have tried to implement. I want to track knight position on chess board starting from Iniitial place like ((1,1),(2,3),…..) like this. But when I run my code it gives me weired output , please help me out. How can I incorportae backtracking in this ?

formal languages – How to evaluate a Kleene’s Closure through CFG and attribute grammars

For a CFG with the production rules that can represent a regular expression. How can one calculate all the set of strings that regular expression would produce.

For T = {a, b,*,(,)}
and an arbitrary production rule I created to represent a simple regular expression

S->SS | S* | (S) | a | b | βˆ…

What would be an attribute grammar which can calculate the set of all strings that the regex can evaluate.

For example if we generate b(a)*
Then the output generated by regular expression would be a set of infinte strings {b,baa,ba,baaa……}
using set valued expression in the attribute grammar.

I am not sure how to represent set valued concatenation and Kleene closure.

programming languages – Find the most frequent alphabet appearing in the given string. In case letter with same frequency, choose the one which comes first alphabetically

Thanks for contributing an answer to Computer Science Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.

Do any programming languages use types as values? Would there be any point?

You say:

One thing I haven’t seen (…) is using a type as a value that can be passed around, allowing it to instantiate new objects, call static functions etc, while still providing all the benefits of strong type checking

Emphasis mine.

For example, I have 2 classes, J and K, both of which implement interface I and in some circumstances may be used in the same place.


if the type itself can be passed I could have a function that takes a type implementing I and call whichever version of a static member function, based on the passed object

Emphasis mine.

Something that gets close is runtime interrogations. For example with simple C# pattern matching:

public static void Test<T>(T obj)
    where T: I
    if (obj is J objAsJ)
        // use J, including static methods on J

    if (obj is K objAsK)
        // use K, including static methods on K

This provides “all the benefits of strong type checking”. However, of course, has the drawback of forcing you to enumerate the possible types.

If the type is to be passed at runtime – for example, in a variable, as the question suggests – it means it is not known at compile time, so we lose that strong type checking. To pass a type at runtime but have some type information in compile time, we have generic type arguments and constraints, of course. Yet, that won’t give you access to static members.

I only see two paths to keep such strong type checking: We interrogate the object, as shown above. Or stronger generic constraints…

Another thing that gets close is static interface methods. If we can make a generic constraint to such interface, we could be able to use those static members.

Java has interfaces with static methods, but you can’t override those. So we need a language with has something like interfaces with static members that we can override. Rust is such language.

For example, I have 2 classes, J and K, both of which implement interface I and in some circumstances may be used in the same place.

I’ll have two types Dog and Sheep, and a trait Animal implemented for both.

if the type itself can be passed I could have a function that takes a type implementing I and call whichever version of a static member function, based on the passed object

Traits in Rust can have static functions, which we get to implement for each type.

I’ll show how to call both static and instance functions defined in a trait, getting a different result depending on the actual type. So it calls “whichever version of a static member function, based on the passed object”.

The reason I’m saying it gets very close is because I’ll never have a variable storing the type, which is what the title of the question suggests (“Do any programming languages use types as values?”).

To be fair, Rust has TypeID, which is just a number. It can be used to identify and compare types, but that’s about it.

Instead everything is type checked at compile time (which, according to comments, seems to be what you care about). Rust does not have runtime reflection.

Note: I’ll be using String (which is a heap allocated string), and i’ll be cloning it. Not efficient, but I don’t bother with lifetimes.

I’ll have two types Dog and Sheep:

struct Dog { name: String }
struct Sheep { wool: bool, name: String }

An Animal trait:

trait Animal {
    fn new(name: String) -> Self; // Self is the type that implements the trait
    fn name(&self) -> String;
    fn noise(&self) -> String;
    fn talk(&self) {
        println!("{} says {}",, self.noise()); // This is default impl.
    fn specie() -> String;

And we implement the trait for both types. This is Animal for Dog:

impl Animal for Dog {
    fn new(name: String) -> Dog {
        Dog { name: name }

    fn name(&self) -> String {

    fn noise(&self) -> String {
    fn specie() -> String

This is Animal for Sheep.

impl Animal for Sheep {
    fn new(name: String) -> Sheep {
        Sheep { name: name, wool: true }

    fn name(&self) -> String {

    fn noise(&self) -> String {
        if self.wool {
        } else {
    fn talk(&self) {
        println!("{} pauses briefly... {}",, self.noise());
    fn specie() -> String

Let us use them:

fn test<T: Animal>(animal: &T) {
    println!("{}", T::specie());;
    let clone = T::new("Clone of ".to_owned() + &;;

fn main() {
    let my_dog: Dog = Animal::new("Snuppy".to_string());
    let mut my_sheep: Sheep = Animal::new("Dolly".to_string());

As you can see the test function is generic. It has a type argument T that must have an implementation of Animal. And it borrows an argument of that type.

We are able to call static functions defined in the trait:

println!("{}", T::specie());

Which outputs "Canine" for Dog and "Ovine" for Sheep.

We are able to call instance functions defined in the trait:;

We are able to create new instances of the same type we are given (this is just another static function):

let clone = T::new("Clone of ".to_owned() + &;

And use those those instances:;

Everything is type checked at compile time.

This is the output of the program:

Snuppy says bark!
Clone of Snuppy says bark!
Dolly pauses briefly... baaaaah!
Clone of Dolly pauses briefly... baaaaah!

programming languages – How to sent a alert mail in php?

I have created a login page to perform a sql injection, whenever the sql injection happens in login page admin should automatically get a alert mail. But I’m getting mail even when the user logged in correctly.

    <!DOCTYPE html>
 <html lang="en" dir="ltr">
<meta charset="UTF-8">
<title>Login form </title>
 <link rel="stylesheet" type="text/css" href="style1.css">
     <div class="box">
     <form  action="insert.php" method="POST">
         <input type="text" name="username" placeholder="">
         <input type="text" name="password" placeholder="Password">
         <input type="submit" name="submit" value="Login">


    $conn = mysqli_connect('localhost:3308','root', '')or die("connection error");
$username =$_POST("username");
$query="SELECT * FROM users WHERE username='$username' AND password='$password'";
    $count = mysqli_num_rows($result);

if($count > 0)
    require 'PHPMailer/PHPMailerAutoload.php';
    $mail = new PHPMailer;

//$mail->SMTPDebug = 4;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = '';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('', 'abc');
$mail->addAddress('');     // Add a recipient
//$mail->addAddress('');               // Name is optional
//$mail->addReplyTo('', 'Information');

//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'sample';
$mail->Body    = 'sample checking';
//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {

    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo"<h1> success</h1>";
    echo 'Message has been sent';

formal languages – Show {0π‘š1𝑛|π‘šβ‰ π‘›} is not regular

So I have the question: show “Show {0^π‘š1^𝑛|π‘šβ‰ π‘›} is not regular”. I’ve already seen various proofs for this question, but they all have one step I don’t get.

They all take: “overline{L}∩(0βˆ—1βˆ—)” (overline{L} is the compliment of L) and show that it’s not regular. I don’t get why we can’t just take overline{L}. Because isn’t overline{L} = {0^π‘š1^𝑛|m=n} which is the same as {0^n1^n|n β‰₯ 0} which we know is not regular? What am I missing?

seo – What should the hreflang be for sites with more than 3 languages containing child pages?

We’ve got a domain:

  1. Right now the domain is pointing directly to the page. We are based in Germany. So actually the site should be directing to, right?

  2. We want to create a Spanish and a Italian version of it. let’s start the the Spanish version

Now do ALL of the hreflang-tag have to be included? (including the Italian version) or just the the Spanish version and the default German version? How would the href lang tags look like?

<link rel="alternate" href="" hreflang="x-default" />
<link rel="alternate" href="" hreflang="en" />
<link rel="alternate" href="h" hreflang="de" />
<link rel="alternate" href="" hreflang="it" />
<link rel="alternate" href="" hreflang="es" />

So do ALL of these 4 have to be in ANY page?

  1. Do they also have to be included in the child page?

  2. I’ve watched a google tutorial (which you can find here: )where someone tries to explain it. If you click here and open the video at 9:27 she says something about “canonical”. So when do I have to use “alternate” and when “canonical”?

Can a Turing machine or Push Down Automaton construct languages of type 3?

I am not quite sure, whether automata can construct languages over their types. For example, a Push down automaton can construct a language of type 2 – does that mean that a PDA also can construct a language of type 3? The same for turing machine?

formal languages – Let $Sigma = {a, b}$ and $L = {aa, bb}$. Use set notation to describe $overline L$

Let $Sigma = {a, b}$ and $L = {aa, bb}$. Use set notation to describe $overline L$.

This is exercise 6 (page 28) from “An Introduction to Formal Languages and Automata” by Peter Linz. The author provides the following answer: $overline L = {lambda, a, b, ab, ba} cup { w in {a, b}^+ : |w|geq 3}$. Mine on the other hand is the following: $overline L = Sigma^* – L = {w in Sigma^* : w neq aa, w neq bb}$. Is my answer wrong? Thanks in advance.

formal languages – Prove that $(L^R)^* = (L^*)^R$

Prove that $(L^R)^* = (L^*)^R$ for all languages $L$.

My attempt: Suppose $w in (L^R)^*$. So, $w = w_1dots w_l$, for some $w_1, dots , w_l in L^R$. Since $w^R in L$, then $w^R = w_ldots w_1$, for some $w_l, dots , w_1 in L^*$. Therefore, $(L^R)^* subseteq (L^*)^R$.

Honestly, I don’t know how to proceed onwards, because this part doesn’t sound right. I feel like I messed up with those symbols as this topic is really abstract. Any help? Thanks in advance.