Category Archives: Programming languages

“One Edit Apart” task on Yandex interview

The task is to write one_edit_apart function that determines is it possible to make two given strings equal by replacing, adding or removing one symbol. The equal strings are considered to be equal, for example:

one_edit_apart("cat", "at") == true
one_edit_apart("cat", "cats") == true
one_edit_apart("cat", "cast") == true
one_edit_apart("cast", "cats") == false
one_edit_apart("cat", "cut") == true
one_edit_apart("cat", "dog") == false

The task is trivial, but Yandex interviewers require all the calculation to be done exactly in their inline editor without debugging and compiling and they do not accept the solution until it works for all possible input. The obvious solution is to truncate equal heads and equal tails and check if the lengths of remaining strings are not greater than 1, but if you forgot to handle the case when heads and tails intersect (with “a” and “aa”, for example) they will say “ooops…., you did a newbie mistake that 1-st year students usually do” and if this all takes more than 40 minutes you fail to pass the test. Below I provided the source code they are most likely to accept:

(more…)

Key knowledge about forwarding references in C++

The compiler deduces U as std::string& or as std::string depending on value category of the argument:

template <class U>
void f(U&& s)
{
    static_assert(std::is_same_v<U, const std::string&>);

    std::cout << std::forward<U>(s) << std::endl;
}

int main()
{
    const std::string& s = "abc";

    f(s);

    return 0;
}
(more…)

QML Application Style

There was qtquickcontrols2.conf in the root directory of my app sources with the following content:

; This file can be edited to change the style of the application
; See Styling Qt Quick Controls 2 in the documentation for details:
; https://doc.qt.io/qt-6/qtquickcontrols2-styles.html

[Controls]
;Style=Default
;Style=Universal
;Style=Material
Style=Fusion
;Style=iOS

[Universal]
Theme=Light
Accent=Steel

[Material]
;Theme=Light
Theme=Dark
;Accent=BlueGrey
;Primary=BlueGray
(more…)

Building BOOST with MSVC2022

I built BOOST with MSVC2022 in a similar way I built it with MSVC2017 except that I did install, but not stage:

cd C:\dev\repos\boost_1_80_0

set OPENSSL_ROOT_DIR=C:/dev/libs/OpenSSL
set OPENSSL_USE_STATIC_LIBS=ON

bootstrap.bat

b2 install --prefix=C:/dev/libs/boost_1_80_0 --toolset=msvc-14.3 link=static runtime-link=static variant=release address-model=64
b2 install --prefix=C:/dev/libs/boost_1_80_0 --toolset=msvc-14.3 link=static runtime-link=static variant=debug address-model=64

I am not sure if OPENSSL_ROOT_DIR takse an effect because boost::asio is header-only (but it depends on system and thread modules).

(more…)

Specialization of parameterless template function in C++

At least this compiles and works:

#include <iostream>
#include <memory>
#include <string>

template <class T>
std::shared_ptr<T> make_instance();

template <class T> requires std::is_default_constructible_v<T>
std::shared_ptr<T> make_instance()
{
    return std::make_shared<T>();
}
(more…)

How I fixed my contact form

When I switched to PHP 7.4 I forgot to specify sendmail_path parameter in php.ini and my contact form stopped working. Today I found sendmail_path parameter in PHP 7.0:

cd /etc/php
find . -name "php.ini"
./7.0/fpm/php.ini
./7.0/cgi/php.ini
./7.0/cli/php.ini
./7.4/fpm/php.ini
./7.4/cli/php.ini
grep sendmail ./7.0/fpm/php.ini
sendmail_path = "/usr/sbin/sendmail -t -f *****@yandex.ru -i"
(more…)

Throwing an exception from a function parameter in C++

There is no difference between lines 82 and 92 in the code below with both MSVC2022 and GCC11:

#include <utility>
#include <stdexcept>
#include <iostream>

struct Object
{
    virtual const char* name() = 0;

    virtual ~Object() = default;
};

struct A : public Object
{
    const char* name() override
    {
        return "A";
    }
};
(more…)

The rule of three/five/zero in C++

static_cast can’t convert from a virtual base class in C++

It converts only from non-virtual base class subobject:

struct B
{
    virtual ~B() {};
};

struct D : public virtual B { };

int main()
{
    D d;
    B& br1 = d;

    // cannot convert a 'B*' to a 'D*'; conversion from a virtual base class is implied
    // static_cast<D&>(br1); 

    dynamic_cast<D&>(br1);

    return 0;
}

Storage duration and linkage in C++

In the vernacular they say stack and heap, but the standard call this automatic and dynamic:

(more…)