Exclusive Clothing Sale On Flipkart

Get up to 50% off on our latest collection.

Shop Now

Job Sequencing Problem in C#

  In this article we will discuss the Job sequencing problem in C#, we will achieve it using a greedy approach algorithm.

Problem statement

The job sequencing problem is a common problem that contains assigning a set of jobs to a set of machines, where each job has a deadline and a profit, and we have to get the maximum profit before the job deadline.

Given an array of jobs where each job has its deadline and profit. And Profit can get only if the job is finished before the job deadline also every job takes a single unit of time, so the minimum possible deadline for any job is 1 so we have to get the maximum profit before the job deadline.

For example,

JobIDDeadlineProfit
1390
2120
3230
4125
5215

Output

JobID:-> 4,3,1

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace JobsSequencingProblemInCSharp {
    public class Jobs {
        public int JobsID {
            get;
            set;
        }
        public int Deadline {
            get;
            set;
        }
        public int Profit {
            get;
            set;
        }
    }
    public class JobsSequencingProblem {
        public static void JobsSequencing() {
            List < Jobs > Jobs = new List < Jobs > () {
                new Jobs {
                    JobsID = 1, Deadline = 3, Profit = 90
                },
                new Jobs {
                    JobsID = 2, Deadline = 1, Profit = 20
                },
                new Jobs {
                    JobsID = 3, Deadline = 2, Profit = 30
                },
                new Jobs {
                    JobsID = 4, Deadline = 1, Profit = 25
                },
                new Jobs {
                    JobsID = 5, Deadline = 2, Profit = 15
                }
            };
            // Sort Jobs in the decreasing order of profit
            Jobs = Jobs.OrderByDescending(j => j.Profit).ToList();
            // Determine the maximum deadline of job
            int maxDeadline = Jobs.Max(j => j.Deadline);
            // Initialize the slots array
            int[] slots = new int[maxDeadline];
            foreach(Jobs Job in Jobs) {
                int slot = -1;
                for (int i = Job.Deadline - 1; i >= 0; i--) {
                    if (slots[i] == 0) {
                        slot = i;
                        break;
                    }
                }
                // If the slot is available, assign the Jobs to that slot
                if (slot != -1) {
                    slots[slot] = Job.JobsID;
                }
            }
            Console.WriteLine("Selected Jobs:");
            for (int i = 0; i < maxDeadline; i++) {
                if (slots[i] != 0) {
                    Console.WriteLine("Jobs " + slots[i]);
                }
            }
        }
    }
}
Public static void Main(string[] args) {
    JobsSequencingProblem.JobsSequencing();
    Console.ReadLine();
}
C#

Problem Explanation

This implementation is done by the greedy approach; we start by sorting the jobs in the decreasing order of the profit and then we determine the maximum deadline of the job then we initialize an array of slots with a length equal to the maximum deadline.

And then we iterate through each job and find the latest available slot for that job by starting from its deadline and going backward until we find an empty slot. If we find an available slot, we assign the job to that slot. If we don't find an available slot, we skip the job.

Finally, we print the selected jobs by iterating through the slots array and printing the job ID for each non-zero slot.

 



------------------------------------------

ChatGPT is now available in the Azure OpenAI Service

In this article we will see about ChatGPT is now available in Azure OpenAI Service

If you are new to ChatGPT then previously we discussed OpenAI and we have seen how to create a ChatGTP application using OpenAI, 

For more please follow the below link 👇

Now ChatGPT is available in Azure Open AI service as well, it is introduced and announced by Microsoft on 9th march 2023.

Now developers and the business team will be able to integrate the Open AI’s ChatGPT modes into their cloud applications, which enables communication in other apps and services.

Now the business team can use ChatGPT to customize the queries as per the customer feedback and can help in applications to send automated emails and so many others.

As per Microsoft's announcement Azure OpenAI, users now can start to access the preview of ChatGPT today, with the pricing set of $0.002 for the 1,000 tokens, now the billing for all the ChatGPT service starts on March 13th as part of the Azure OpenAI.

Developers need to apply for special access, as the Azure OpenAI Services requires registration and it is currently only available for Microsoft-managed customers and partners.

With the help of Azure OpenAI Service, more than 1,000 customers are applying the advanced AI model, including the Dall-E 2, GPT-3.5, Codex, and many other large language models to innovate in new ways, now the developers use OpenAI’s ChatGPT in the enhancement of PowerBI, MicrosoftTeams, GitHub, etc.

Now all the big and small levels of organizations are using the Azure OpenAI Services to achieve their business value, Microsoft also working internally to combine the large language models with the OpenAI and the AI-optimized infrastructure of Azure to introduce new experiences across the customer and products ex.

  • GitHub Copilot leverages AI models in the Azure OpenAI Service to give benefits to developers to enhance the code with its AI pair programmer.
  • Microsoft Teams Premium includes the intelligent recap and the AI-generated chapters to help the teams, individuals, and organizations become more productive.
  • Microsoft Viva Sales is the new AI-powered seller experience that offers suggested email content and data-driven insights to help the sales teams to focus on selling products to customers.
  • Microsoft Bing introduced the AI-powered chat option to enhance the consumer's search experience.

above are just a few examples of how Microsoft openAI is helping organizations to transform in AI models.

So in the future, AI will definitely change the way of work, and also improve the organization's growth with Open A.

Below are a couple of the Links given by Microsoft through that we can start working with Azure OpenAI services.

Getting started with the Azure OpenAI Service


    ---------------------------------------------------------------------------------------

    FluentValidation in minimal APIs in ASP.NET Core 7.0

     In this article, we will learn how to implement FluentValidation to our model classes in ASP.NET core 7 with minimal APIs.

    We have already discussed what is minimal API in .Net Core. While working with an application, validation plays an important role. It helps us validate our data and ensure we insert the correct data into the database.

    There is no in-built support to validate the model in the minimal APIs (unlike what we used to have in ASP.NET Core MVC and Razor Pages). Hence, we need to write custom code to validate the models in our minimal API applications.

    FluentValidation validation helps us validate our model when working with DotNet Core 7.0 application.

    To utilize the functionality of FluentValidation, we need to install the FluentValidation library from the NuGet package manager. FluentValidation uses the fluent API and the lambda expression to create the data validation rules for a model. Below is the command to install the FluentValidation package

    PM> Install-Package FluentValidation.AspNetCore

    Let's understand fluentValidation practically

    First, let's create an ASP.NET core application. So to create it, open VS 2022, select the ASP.NET Core Web API template and click Next.



    Now give the project name 'FluentValidationInAspCore' and click Next.



    Now select the Framework as .NET 7.0 and click Next.

    Once you click Next, it will create an ASP.NET core application. Now go to Nuget Package Manager by right-clicking on the project solution.




    Once you click on Manage Nuget packages, a new window will appear. Search for "FluentValidation.AspNetCore" and install it.




    Now right-click on the project and create a Student Model class like below

    namespace FluentValidationInAspCore {
        public class Student {
            public int Id {
                get;
                set;
            }
            public string ? Name {
                get;
                set;
            }
            public int Age {
                get;
                set;
            }
            public double Address {
                get;
                set;
            }
        }
    }
    C#



    Create the StudentValidator class

    To implement the required field or other validation to the student Model class, we need to create a StudentValidation class. Here we can create our custom validation by inheriting StudentValidation class from AbstractValidator, like below.

    public class StudentValidator: AbstractValidator < Student > {
        public StudentValidator() {
            RuleFor(x => x.Id).NotNull();
            RuleFor(x => x.Name).Length(15);
            RuleFor(x => x.Age).NotNull().InclusiveBetween(5, 20);
            RuleFor(x => x.Address).NotNull().LessThanOrEqualTo(200);
        }
    }
    C#



    Now create an interface IstudentBuilder class and write the below code to add the student information.

    public interface IstudentBuilder {
        public void AddStudent(Student student);
    }
    C#



    Now create a studentBuilder class and inherit it from the Istudentbuilder interface and implement the method.

    public class StudentBuilder: IstudentBuilder {
        public void AddStudent(Student student) {
            // Write your logic to add data into the database
        }
    }
    C#



    Now we need to register our validator class and builder classes to Program.cs like below

    using FluentValidation;
    using FluentValidationInAspCore;
    var builder = WebApplication.CreateBuilder(args);
    // Add services to the container.
    builder.Services.AddControllers();
    builder.Services.AddEndpointsApiExplorer();
    builder.Services.AddSwaggerGen();
    builder.Services.AddScoped < IValidator < Student > , StudentValidator > ();
    builder.Services.AddScoped < IstudentBuilder, StudentBuilder > ();
    var app = builder.Build();
    // Configure the HTTP request pipeline.
    if (app.Environment.IsDevelopment()) {
        app.UseSwagger();
        app.UseSwaggerUI();
    }
    app.UseHttpsRedirection();
    app.UseAuthorization();
    app.MapControllers();
    app.Run();
    C#



    Create the HttpPost endpoint in the Program.cs

    Write the below code to create the HTTP POST endpoint in minimal API.

    app.MapPost("/student", async (IValidator < Student > validator, IstudentBuilder builder, Student student) => {
        var validationResult = await validator.ValidateAsync(student);
        if (!validationResult.IsValid) {
            return Results.ValidationProblem(validationResult.ToDictionary());
        }
        builder.AddStudent(student);
        return Results.Created($ "/{student.Id}", student);
    });
    C#



    Now run your application on swagger. Click on the Try it out button.



    Without giving any value, if you run it, you will see a validation error like below.



    So, these are the step to implement model validation to Minimal API in asp.net core 7.0.


    Source Code: https://github.com/TheDotNetOffice/FluentValidation-in-minimal-APIs-in-ASP.NET-Core-7.0

    ----------------------------------------------------------- 


     Want to be an art expert, follow the below link Guide to Teaching Art eBook https://www.digistore24.com/redir/422234/TheDotNetOffice/CAMPAIGNKEY
    --------------------------

    Exclusive Clothing Sale On Flipkart

    Get up to 50% off on our latest collection.

    Shop Now