Skip to content

Illya Starikov

Encapsulation

As I go throughout my undergraduate studies, I have gotten really good at digging deeper until I completely understand a subject; however, when I observed around me, I realized that I am in the minority. Often times, students only desired to know how to get from question to answer without any intermediate thinking. When trying to hypothesize an answer, I cannot think of a definitive one. However, I know one possible contribution: encapsulation[1].

The most famous example I came across is the derivative of any trigonometric function besides $\sin x$ or $\cos x$. As any student who’s taken Calculus can tell you, $$\frac{d}{dx} \left(\tan x\right) = \sec^2 x $$

However, most students couldn't tell you:

$$\begin{align*}
\frac{d}{dx} \left(\tan x\right) &= \frac{d}{dx} \left(\frac{\sin x}{\cos x}\right) \\
&= \frac{\cos x \cos x + \sin x \sin x}{\cos^2 x} \\
&= \frac{\cos^2 x + \sin^2 x}{\cos^2 x} \\
&= \frac{1}{\cos^2 x} \\
&= \sec^2 x
\end{align*}$$

At first glance, it's hardly useful information. As a matter of fact you might think of it as piece of trivia — until you see the amount of people that miss it on a quiz or test. And entry-level Calculus is not the only place this is relevant; physics, thermodynamics, mathematics of any kind, computer science, and the like.

A question you might be thinking, why does it matter? If it's worth a couple of points on a test, is it that harmful? Maybe not, until you realize you conceptually do not understand the subject — only mechanically.

The next time you're learning something, ask yourself: "why?". The outcome might delight you.


  1. The act of enclosing in a capsule; the growth of a membrane around (any part) so as to enclose it in a capsule. ↩︎

House of Cards (Kattis Problem)

It’s not so often I come across a problem that works out so beautifully yet requires so many different aspects in competitive programming. One particular problem, courtesy of Kattis, really did impress me.

The problem was House of Cards, and the problem was this (skip for TL;DR at bottom):

Brian and Susan are old friends, and they always dare each other to do reckless things. Recently Brian had the audacity to take the bottom right exit out of their annual maze race, instead of the usual top left one. In order to trump this, Susan needs to think big. She will build a house of cards so big that, should it topple over, the entire country would be buried by cards. It’s going to be huge!
The house will have a triangular shape. The illustration to the right shows a house of height 6 and Figure 1 shows a schematic figure of a house of height 5.

For aesthetic reasons, the cards used to build the tower should feature each of the four suits (clubs, diamonds, hearts, spades) equally often. Depending on the height of the tower, this may or may not be possible. Given a lower bound $h_0$ on the height of the tower, what is the smallest possible height $h \geq h_0$ such that it is possible to build the tower?

TL;DR: Using Figure 1 as a reference, you are given a lower bound on a height for a tower of cards. However, there must be an equal distribution of all four suites; clubs, diamonds, hearts, and spades.

This implies that the number of cards have to be divisible by $4$. Seeing as the input was huge $1 \leq h_0 \leq 10^{1000}$, there was no brute forcing this. So, first thought: turn this into a closed-form series, and solve the series.

Getting the values for the first five heights, I got the following set:

$${2, 7, 15, 25, 40, \ldots}$$

I was able to turn this set into a series quite easily:

$$\sum _{n = 1} ^{h_0} \left(3n - 1\right)$$

This turned into the following equation:

$$\frac{1}{2} h_0(3,h_0 + 1)$$

So, all I had to do was plug $h_0$ in the equation, and increment while the number was not divisible by $4$. Then, I realized how large the input really was. The input size ($1*10^{1000}$) was orders of magnitudes larger than typical, large data types would allow ($1.84 * 10^{19}$).

I realized this couldn’t be tested against a intensive data set, because there is only one number to calculate. I thought, since the series always subtracts one, the minimum times I must increment should roughly be four. Keeping this in mind, I decided to use Python. Python can work with arbitrarily large numbers, making it ideal in this situation.

I sat down, hoped for the best, and wrote the following code.

def getNumberOfCards(x):
    return (3*pow(x, 2) + x) // 2


height = int(input())
while getNumberOfCards(height) % 4 != 0:
    height += 1

print(height)

It worked.

Avoid The Mac App Store via The Terminal

I avoid the Mac App store as much as possible. It’s buggy. It’s slow. Apps are getting pulled from it left and right. But, I could not avoid it permanently because of software updates. As macOS Sierra 10.5 rolled out, I clicked on the App Store, followed by updates, and...

I could not get the updates page opened. Killed App Store. Restart computer. Everything.

After doing some research, I discovered there is a way to update software from the terminal: softwareupdate. So, after running one command (sudo softwareupdate -iv), I am writing this for the latest version of macOS Sierra.

Vim Autorun

One of the reasons I kept CodeRunner handy was its ability to quickly compile code. With a single click of a button, I could run any of my frequently used languages. It didn’t matter if it was an interpreted or compiled language, so I could virtually run anything, like:

  • C/C
  • Python
  • Lua
  • LaTeX
  • Perl

However, in the last few months I started using Vim. Heavily. So much so I was trying to use Vim command in the CodeRunner buffers. So I decided I wanted to have the functionality, and in vim-esque fashion, I mapped to my leader key: <leader>r. The mnemonic <leader>run helped me remember the command on the first few tries.

To get the functionality, just add the following to your .vimrc.

function! MakeIfAvailable()
    if filereadable("./makefile")
        make
    elseif (&filetype == "cpp")
        execute("!clang++ -std=c++14" + bufname("%"))
        execute("!./a.out")
    elseif (&filetype == "c")
        execute("!clang -std=c11" + bufname("%"))
        execute("!./a.out")
    elseif (&filetype == "tex")
        execute("!xelatex" + bufname("%"))
        execute("!open" + expand(%:r))
    endif
endfunction

augroup spaces
    autocmd!
    autocmd FileType c nnoremap <leader>r :call MakeIfAvailable()<cr>
    autocmd FileType cpp nnoremap <leader>r :call MakeIfAvailable()<cr>
    autocmd FileType tex nnoremap <leader>r :call MakeIfAvailable()<cr>
    autocmd FileType python nnoremap <leader>r :exec '!python' shellescape(@%, 1)<cr>
    autocmd FileType perl nnoremap <leader>r :exec '!perl' shellescape(@%, 1)<cr>
    autocmd FileType sh nnoremap <leader>r :exec '!bash' shellescape(@%, 1)<cr>
    autocmd FileType swift nnoremap <leader>r :exec '!swift' shellescape(@%, 1)<cr>
    nnoremap <leader>R :!<Up><CR>
augroup END

C++ Syntactic Peculiarities

After using a fairly large, matured language for a reasonable period of time, find peculiarities in the language or the libraries is guaranteed to happen. However, given it’s history, I have to say C++ definitely allows for some of the strangest peculiarities in it’s syntax. Below I list three that are my favorite.

Ternaries Returning lvalues

You might be familiar with ternaries as condition ? do something : do something else, and they become quite useful in comparison to the standard if-else. However, if you’ve dealt with ternaries a lot, you might have noticed that ternaries also return lvalues/rvalues. Now, as the name suggests suggests, you can assign to lvalues (lvalues are often referred to as locator values). So something like so is possible:

std::string x = "foo", y = "bar";

std::cout << "Before Ternary! ";
// prints x: foo, y: bar
std::cout << "x: " << x << ", y: " << y << "\n"; 

// Use the lvalue from ternary for assignment
(1 == 1 ? x : y) = "I changed";
(1 != 1 ? x : y) = "I also changed";

std::cout << "After Ternary! ";
// prints x: I changed, y: I also changed
std::cout << "x: " << x << ", y: " << y << "\n"; 

Although it makes sense, it’s really daunting; I can attest to never seeing it in the wild.

Commutative Bracket Operator

An interesting fact about C++ bracket operator, it’s simply pointer arithmetic. Writing array[42]() is actually the same as writing *(array + 42), and thinking in terms of x86/64 assembly, this makes sense! It’s simply an indexed addressing mode, a base (the beginning location of array) followed by an offset (42). If this doesn’t make sense, that’s okay. We will discuss the implications without any need for assembly programming.

So we can do something like *(array + 42), which is interesting; but we can do better. We know addition to be commutative, so wouldn’t saying *(42 + array) be the same? Indeed it is, and by transitivity, array[42]() is exactly the same as 42[array](). The following is a more concrete example.

std::string array[50];
42[array] = "answer";

// prints 42 is the answer
std::cout << "42 is the " << array[42] << ".";

Zero Width Space Identifiers

This one has the least to say, and could cause the most damage. The C++ standard allows for hidden white space in identifiers (i.e. variable names, method/property names, class names, etc.). So this makes the following possible.

int n​umber = 1;
int nu​mber = 2;
int num​ber = 3;

std::cout << n​umber << std::endl; // prints 1
std::cout << nu​mber << std::endl; // prints 2
std::cout << num​ber << std::endl; // prints 3

Using \u as a proxy for hidden whitespace character, the above code can be re-written as such:

int n\uumber = 1;
int nu\umber = 2;
int num\uber = 3;

std::cout << n\uumber << std::endl; // prints 1
std::cout << nu\umber << std::endl; // prints 2
std::cout << num\uber << std::endl; // prints 3

So if you’re feeling like watching the world burn, this would be the way to go.

Trailing Whitespace

One of the most frustrating things to deal with is the trailing space. Trailing spaces are such a pain because

  • They can screw up string literals
  • They can break expectations in a text editor (i.e. jumping to a new line or the end of the line)
  • They can actually break programming languages
  • They are just unflattering

However, in Vim, it takes one autocmd to alleviate this.

augroup spaces
autocmd!
autocmd BufWritePre \* %s/s+$//e
augroup END

On every buffer save substitute spaces at the end of the line with nothing. Easy!

Mercedes-Benz NTG5 STAR2

Fully integrated high-end multimedia system with 6-disc DVD changer, hard-drive navigation system, internet browser, telephony plus DVD video and Music Register, shown on high-resolution 21.3 cm colour TFT display.

The NTG Star2 is a mid-2010s Mercedes-Benz in‑car navigation/infotainment system (part of the COMAND/Audio20 series). In practice it was the NTG5‑generation system used in models like the W205 C‑Class, W222 S‑Class, GLC, etc. For audio and navigation it typically used the standard 7″ (17.8 cm) TFT display and console controller. Importantly, NTG Star2 used Garmin’s “Map Pilot” technology for GPS navigation in Audio20-equipped cars – inserting a Garmin SD‑card lets the Audio20 unit run 3D maps and routing​. (The COMAND head unit itself was Mercedes-made, but Garmin supplied the map/database software.)

NTG Star2 debuted around 2014–2015 as the successor to the NTG4.5 COMAND system. Mercedes introduced it on new models (for example, the 2015–2020 C‑Class W205 and later E‑Class and GLC) as COMAND Online (NTG5) hardware​. These NTG5/Star2 units featured a built‑in hard drive for media and optional CD/DVD storage. Around 2018–2019 Mercedes began updating Star2 to a new software version called “Star1” (this was essentially a software revision to enable features like Apple CarPlay). As one forum noted, “my new [head unit] is called NTG Star1 and CarPlay works – the old one was NTG Star2”​. (In other words, Star2 was the original NTG5 software on older Audio20 hardware; Star1 was the newer update with smartphone integration.)

Today NTG Star2 systems remain supported through Mercedes accessories and parts. Mercedes still sells Garmin Map Pilot SD navigation cards and software updates for NTG5/Star2 systems​. For vehicles that came with NTG Star2, a dealer or owner can purchase official updates (e.g. Europe 2020/2021 map packs) for the Garmin Map Pilot. Mercedes also offered an accessory COMAND Online head unit upgrade with DVD changer – a high-end multimedia unit with a 6‑disc changer and hard‑drive nav that replaces the original unit. In short, the NTG Star2 system remains available on its original models (C‑Class, S‑Class, etc.) and can be serviced or upgraded using genuine Mercedes parts (for example, the COMAND Online unit with DVD changer or Garmin Map Pilot module)​.

While Mercedes-Benz designed the NTG Star2 hardware and user interface, Garmin’s involvement was confined to the navigation content. On Audio20/NTG5 Star2 cars, Garmin supplied the Map Pilot navigation module (SD card and software) that plugs into the Mercedes system​. There is no ongoing Garmin development of NTG Star2 itself – Garmin’s input was the map database and guidance engine. All system updates (DVD/DVD changer upgrades or map updates) are handled through Mercedes-Benz parts (including Garmin-branded map cards sold by Mercedes).


More Mercedes - Technical Accessories and Collection Products from Mercedes-Benz
More Mercedes - Technical Accessories and Collection Products from Mercedes-Benz

Avoiding eqnarray

Since reading about its faults in The Not So Short Introduction to LaTeX (highly recommended read), I have avidly stayed away form eqnarray. However, I feel as if the book didn’t covey the ideas as well as it should; also, it suggestion was IEEEeqnarray, apposed to the classically recommended align. This article on TeX User Groups conveys ideas more eloquently and provides better alternatives.