Wednesday, December 17, 2025

Python code

 # List of documents
# Each outer list represents one document
# Each document contains one or more sentences
documents = [
    ["Hello world", "Hello Python"],
    ["Python is great", "Hello again"],
    ["Enjoy coding in Python"]
]

# Dictionary to store the inverted index
# Key   -> word
# Value -> list of document IDs where the word appears
index = {}

# Loop through each document with its index (document ID)
for doc_id, doc in enumerate(documents):

    # Join all sentences in the current document into a single string
    # Convert text to lowercase to avoid case-sensitive duplicates
    text = " ".join(doc).lower()
    
    # Split the text into individual words using space as delimiter
    words = text.split()
    
    # Loop through each word in the current document
    for word in words:
        
        # If the word is not already in the index,
        # create a new entry with an empty list
        if word not in index:
            index[word] = []
        
        # Add the document ID only if it is not already present
        # This avoids duplicate document IDs for the same word
        if doc_id not in index[word]:
            index[word].append(doc_id)

# Print the final inverted index
print(index)

output:
{
    'hello': [0, 1],
    'world': [0],
    'python': [0, 1, 2],
    'is': [1],
    'great': [1],
    'again': [1],
    'enjoy': [2],
    'coding': [2],
    'in': [2]
}

=========================================

# Input data: a list of tuples
# Each tuple contains (name, score)
data = [('Alice', 88), ('Bob', 72), ('Alice', 91), ('Bob', 85)]

# Function to group scores by name
def group_scores(data):
    
    # Dictionary to store the grouped result
    # Key   -> name (e.g., 'Alice', 'Bob')
    # Value -> list of scores for that name
    result = {}
    
    # Loop through each (name, score) pair in the input data
    for name, score in data:
        
        # If the name is not already a key in the dictionary,
        # create a new key with an empty list
        if name not in result:
            result[name] = []
        
        # Add the score to the list for the corresponding name
        result[name].append(score)
    
    # Return the dictionary containing grouped scores
    return result

# Call the function and print the result
print(group_scores(data))

output:
{
    'Alice': [88, 91],
    'Bob': [72, 85]
}
============================================




Monday, December 8, 2025

Develop Azure AI services applications securely by using Azure Key Vault and Run Text Analytics (C#)


//Develop Azure AI services applications securely by using Azure Key Vault
and Run Text Analytics (C#)
//Create an Azure AI services account
//Create an Azure Key Vault

using Azure;
using Azure.AI.TextAnalytics;
using Azure.Core;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using System;
using System.Threading.Tasks;

namespace WhizConsoleApp1
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var keyVaultName = "mykeyvalut85"; //"your-key-vault-name";

            const string keySecretName = "CognitiveServicesKey"; //"Your-Key-Secret-Name";
            const string endpointSecretName = "CognitiveServicesEndpoint"; //"Your-Endpoint-Secret-Name";

            var kvUri = $"https://{keyVaultName}.vault.azure.net/";

            // Try DefaultAzureCredential first and fall back to
                InteractiveBrowserCredential if authentication is unavailable.
            TokenCredential credential = new DefaultAzureCredential();
            SecretClient keyVaultClient = new SecretClient(new Uri(kvUri), credential);

            Console.WriteLine($"Retrieving your secrets from {keyVaultName}.");

            KeyVaultSecret keySecret = null;
            KeyVaultSecret endpointSecret = null;

            try
            {
                var keyResponse = await keyVaultClient.GetSecretAsync(keySecretName);
                keySecret = keyResponse.Value;
                var endpointResponse = await keyVaultClient.GetSecretAsync(endpointSecretName);
                endpointSecret = endpointResponse.Value;
            }
            catch (Azure.Identity.CredentialUnavailableException)
            {
                Console.WriteLine("DefaultAzureCredential could not authenticate
                    with any of the configured credential sources.");
                Console.WriteLine("Falling back to InteractiveBrowserCredential.
                    A browser window will open for authentication.");

                credential = new InteractiveBrowserCredential();
                keyVaultClient = new SecretClient(new Uri(kvUri), credential);

                try
                {
                    var keyResponse =
                        await keyVaultClient.GetSecretAsync(keySecretName);
                    keySecret = keyResponse.Value;
                    var endpointResponse =
                        await keyVaultClient.GetSecretAsync(endpointSecretName);
                    endpointSecret = endpointResponse.Value;
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Failed to retrieve secrets after falling
                        back: {ex.Message}");
                    Console.WriteLine("Recommendations: run 'az login', or set
                        AZURE_CLIENT_ID / AZURE_TENANT_ID / AZURE_CLIENT_SECRET
                        environment variables for a service principal.");
                    Console.ReadLine();
                    return;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Failed to retrieve secrets: {ex.Message}");
                Console.ReadLine();
                return;
            }

            Console.WriteLine($"Your key secret value is: {keySecret.Value}");
            Console.WriteLine($"Your endpoint secret value is: {endpointSecret.Value}");
            Console.WriteLine("Secrets retrieved successfully");

            EntityRecognitionExample(keySecret.Value, endpointSecret.Value);

            Console.ReadLine();
        }

        private static void EntityRecognitionExample(
            string keySecret, string endpointSecret
        )
        {
            var exampleString = "I had a wonderful trip to Seattle last week.";

            AzureKeyCredential azureKeyCredential = new AzureKeyCredential(keySecret);
            Uri endpoint = new Uri(endpointSecret);
            var languageServiceClient =
                new TextAnalyticsClient(endpoint, azureKeyCredential);

            Console.WriteLine("Sending a Named Entity Recognition (NER) request");
            var response = languageServiceClient.RecognizeEntities(exampleString);

            Console.WriteLine("Named Entities:");
            foreach (var entity in response.Value)
            {
                Console.WriteLine($"\tText: {entity.Text},\tCategory:
                    {entity.Category},\tSub-Category: {entity.SubCategory}");
                Console.WriteLine($"\t\tScore: {entity.ConfidenceScore:F2},\tLength:
                    {entity.Length},\tOffset: {entity.Offset}\n");
            }
        }

    }
}


OutPut:



Friday, December 5, 2025

All bisect functions in Python

 Here’s a clean, practical tour of all bisect functions in Python – with code and scenario-based examples.

We’ll cover:

  • bisect_left

  • bisect_right (same as bisect)

  • insort_left

  • insort_right (same as insort)

All of these work on sorted lists. If the list is not sorted, results are wrong.


1. Basic idea: using bisect_left and bisect_right

from bisect import bisect_left, bisect_right

scores = [10, 20, 20, 20, 30, 40]  # sorted list

x = 20

pos_left = bisect_left(scores, x)
pos_right = bisect_right(scores, x)

print("scores:", scores)
print("bisect_left(20)  ->", pos_left)   # first position where 20 can go (leftmost)
print("bisect_right(20) ->", pos_right)  # position after the last 20 (rightmost+1)

Output (conceptually):

  • bisect_left(20)1

  • bisect_right(20)4

When to use which?

  • bisect_left(a, x)

    • Think: “insert before existing equals”

    • Good for lower bound, “first ≥ x”

  • bisect_right(a, x) / bisect(a, x)

    • Think: “insert after existing equals”

    • Good for upper bound, “first > x”


2. Scenario 1 – Grade boundaries (using bisect_right)

You have percentage marks, and you want to map them to grades based on boundary cutoffs.

from bisect import bisect_right

# boundaries: marks at which grade changes
boundaries = [40, 60, 75, 90]  # must be sorted
grades = ["F", "C", "B", "A", "A+"]  # one more than boundaries

def get_grade(score):
    idx = bisect_right(boundaries, score)
    return grades[idx]

for s in [35, 40, 59, 60, 74, 75, 89, 90, 99]:
    print(f"{s} -> {get_grade(s)}")

Logic:

  • bisect_right(boundaries, score) returns how many boundaries the score crosses.

  • We use that number as the index in grades.


3. Scenario 2 – Find position to insert (using bisect_left)

You want to place a new value in a sorted list but without actually inserting – just find where it would go.

from bisect import bisect_left

data = [3, 5, 8, 12, 15]

def find_insert_position(sorted_list, value):
    pos = bisect_left(sorted_list, value)
    return pos

for v in [1, 5, 7, 20]:
    pos = find_insert_position(data, v)
    print(f"Value {v} should go at index {pos} in {data}")

Use this when you just need the index, e.g., to split a list, or for binary search style lookup.


4. Inserting while keeping the list sorted – insort_left & insort_right

These are like bisect_... + list.insert(...) combined.

from bisect import insort_left, insort_right

nums = [10, 20, 20, 30]

print("Original:", nums)

# Insert with insort_left (before existing equals)
insort_left(nums, 20)
print("After insort_left(20): ", nums)

# Insert with insort_right (after existing equals)
insort_right(nums, 20)
print("After insort_right(20):", nums)

Behavior:

  • insort_left(a, x)
    Insert x at the position returned by bisect_left(a, x) (before equals)

  • insort_right(a, x) / insort(a, x)
    Insert x at the position returned by bisect_right(a, x) (after equals)


5. Scenario 3 – Maintaining a sorted list (online data)

Imagine you get sensor readings one by one, and you want to keep them in sorted order to quickly get median or percentiles.

from bisect import insort

readings = []

def add_reading(value):
    # insort == insort_right
    insort(readings, value)

def get_median():
    n = len(readings)
    if n == 0:
        return None
    mid = n // 2
    if n % 2 == 1:
        return readings[mid]
    else:
        return (readings[mid - 1] + readings[mid]) / 2

for r in [10, 5, 8, 15, 3, 12]:
    add_reading(r)
    print("After adding", r, "=> readings:", readings, "median:", get_median())

Here:

  • New values are inserted in O(n) time but still using binary search internally for the position.

  • Lookup for median is O(1) once sorted.


6. Scenario 4 – Counting elements in a range

Given a sorted list, how many values fall between low and high?

from bisect import bisect_left, bisect_right

data = [2, 4, 4, 4, 7, 9, 10, 10, 15]  # sorted

def count_in_range(sorted_list, low, high):
    # first index with value >= low
    left = bisect_left(sorted_list, low)
    # first index with value > high
    right = bisect_right(sorted_list, high)
    return right - left

print(count_in_range(data, 4, 10))   # how many values between 4 and 10 inclusive?
print(count_in_range(data, 5, 9))    # values between 5 and 9
print(count_in_range(data, 0, 100))  # all values

Key idea:

  • left = position of first value ≥ low

  • right = position of first value > high

  • count = right - left


7. Scenario 5 – Implementing your own binary search using bisect_left

You can use bisect_left as a helper to implement “does this value exist?” in a sorted list.

from bisect import bisect_left

def contains(sorted_list, value):
    i = bisect_left(sorted_list, value)
    # i is index where value *should* be inserted to stay sorted
    if i != len(sorted_list) and sorted_list[i] == value:
        return True
    return False

arr = [1, 3, 4, 7, 9, 11]

for v in [3, 5, 11, 12]:
    print(f"{v} exists? -> {contains(arr, v)}")

8. Quick summary of all bisect functions

from bisect import (
    bisect_left,
    bisect_right,  # same as bisect
    bisect,        # alias of bisect_right
    insort_left,
    insort_right,  # same as insort
    insort,        # alias of insort_right
)
  • Search / index only

    • bisect_left(a, x) → index of first element ≥ x

    • bisect_right(a, x) / bisect(a, x) → index of first element > x

  • Insert while keeping list sorted

    • insort_left(a, x) → insert at position bisect_left(a, x)

    • insort_right(a, x) / insort(a, x) → insert at position bisect_right(a, x)

All of them assume a is already sorted.



Featured Post

Study guide for Exam AZ-204: Developing Solutions for Microsoft Azure

  Azure: Implement Containerized Solutions (Points Only) 1) ✅ Create and Manage Container Images for Solutions ✅ What a container image is A...

Popular posts