Human Readable Binary Search

In this post, I used

  • Introduce explaining boolean variables
  • Given attention to variable naming

Please review the below code or visit https://gist.github.com/way2datta/7d32ee72289f7f5c5ea2d4068139f48d
and let me know your thoughts?

public class IntArray
{
public static int BinarySearch(int[] items, int searchTerm)
{
return SearchRecursively(items, searchTerm, 0, items.Length 1);
}
private static int SearchRecursively(int[] items, int searchTerm, int lowerIndex, int upperIndex)
{
var scannedAllItems = lowerIndex > upperIndex;
if (scannedAllItems) return 1;
var middleIndex = (lowerIndex + upperIndex) / 2;
var middleItem = items[middleIndex];
var isItemPresentAtMiddleIndex = items[middleIndex] == searchTerm;
if (isItemPresentAtMiddleIndex) return middleIndex;
var searchItemInLowerHalf = middleItem.CompareTo(searchTerm) > 0;
if (searchItemInLowerHalf) return SearchRecursively(items, searchTerm, lowerIndex, middleIndex 1);
var searchItemInUpperHalf = middleItem.CompareTo(searchTerm) < 0;
if (searchItemInUpperHalf) return SearchRecursively(items, searchTerm, middleIndex + 1, upperIndex);
return 1;
}
}
view raw BinarySearch.cs hosted with ❤ by GitHub

Witness Running Many Programs At Once from Windows Command Prompt

In this blog post, you will witness how multi-core CPU executes different processes and do context switching.

We will use the following program, this program merely takes input from the user and prints input string after some time periodically.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
internal class Program
{
[DllImport("Kernel32.dll"), SuppressUnmanagedCodeSecurity]
public static extern int GetCurrentProcessorNumber();
private static void Main()
{
var currentProcess = Process.GetCurrentProcess();
Console.WriteLine($"Please enter any string for process {currentProcess.Id} which is executed by {GetCurrentProcessorNumber()}: ");
var readline = Console.ReadLine();
var i = 0;
while (i++ < 12)
{
Console.WriteLine($"For process {currentProcess.Id} which is executed by {GetCurrentProcessorNumber()}, you have entered: {readline}");
Thread.Sleep(1000);
}
}
}
OSPlayground,cs

I presume after compiling the above file, we get the output file: ‘OSPlayground.exe’.

Now open a command prompt and navigate to the path that contains the above binary. Run the following command and witness the magic.

Command to use: start /b OSPlayground && OSPlayground

Output:

The command prompt will ask you to enter the string a couple of times since we executed the program twice.
I had inputted EARTH! for the first process from the command prompt and MARS! for another one.

The above program is executed on a multicore processor.

Local stack quick setup on my Windows 10 machine

Prerequisite:

Steps:

  • Create a file with the name docker-compose.yml.
  • Copy the following content and paste it into a docker-compose.yml.
version: "3.1"
services:
  localstack:
    image: localstack/localstack:latest
    container_name: localstack_demo
    ports:
      - "4563-4599:4563-4599"
      - "8000:8080"
    environment:
      - SERVICES=${SERVICES- }
      - DEBUG=1
      - DATA_DIR=/tmp/localstack/data
    volumes:
      - "./.localstack:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

  • If there is any problem due to indentation, you can download the original file from here.
  • Open PowerShell from the folder where above docker-compose.yml is kept.
  • Run docker-compose up command.
  • Builds, (re)creates, starts, and attaches to containers for all LocalStack services.
  • Now you can work with Localstack services like S3.


How to make PowerShell shorten the long path of the current directory.

Are you getting less space on the PowerShell due to the long path of the current directory?

You can increase the signal to noise ratio by hiding that long path. How?

Follow the steps:

  • Navigate to PowerShell and type $profile.
  • It will give you a path to the PowerShell profile file.
  • Navigate to the directory of that PowerShell profile file.
    It was “C:\Users\{user}\Documents\WindowsPowerShell” for me.
  • Check if the file “Microsoft.PowerShell_profile.ps1” exists on the disk.
  • If NOT then create one “Microsoft.PowerShell_profile.ps1”.
  • Open “Microsoft.PowerShell_profile.ps1” file.
  • Paste the following code in it and save.
function prompt {
  $p = Split-Path -leaf -path (Get-Location)
  "$p> "
}
  • Restart PowerShell.
  • It will show the current folder name only.