From 4b6e4b45b7f8d94dc91d9a7510570f4aa271c4ff Mon Sep 17 00:00:00 2001 From: thakkarjanvi Date: Wed, 9 Aug 2023 17:15:15 +0530 Subject: [PATCH 01/14] Add exercise-2 --- week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs b/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs index a4ec62d9..8c02d7d6 100644 --- a/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs +++ b/week-1/day-3/exercise-2/ShortesPathAlgorithm/Program.cs @@ -223,7 +223,7 @@ public int[] DijkstraShortestPath(int source) visited[i] = false; } - distances[source] = int.MinValue; + distances[source] = 0; for (int count = 0; count < vertices - 1; count++) { From c2e15fa19312ec6d15f087f9c0d69fe4267f4352 Mon Sep 17 00:00:00 2001 From: thakkarjanvi Date: Wed, 9 Aug 2023 17:23:22 +0530 Subject: [PATCH 02/14] Add exercise 1 --- .../day-3/exercise-1/FactorialApp/Program.cs | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/week-1/day-3/exercise-1/FactorialApp/Program.cs b/week-1/day-3/exercise-1/FactorialApp/Program.cs index f116eeca..50437724 100644 --- a/week-1/day-3/exercise-1/FactorialApp/Program.cs +++ b/week-1/day-3/exercise-1/FactorialApp/Program.cs @@ -1,20 +1,35 @@ -namespace FactorialApp +using System; + +namespace FactorialApp { public class Program { public static void Main() { + Console.WriteLine("Hello, World!"); Console.Write("Enter a number: "); - int number = int.Parse(Console.ReadLine()); - - long factorial = CalculateFactorial(number); + int userInput = Convert.ToInt32(Console.ReadLine()); - Console.WriteLine($"The factorial of {number} is: {factorial}"); + if (userInput < 0) + { + Console.WriteLine("Factorial is not valid for negative numbers."); + } + else + { + int result = CalculateFactorial(userInput); + Console.WriteLine($"Factorial of {userInput} is {result}"); + } } public static long CalculateFactorial(int number) { - throw new NotImplementedException(); + // throw new NotImplementedException(); + int factorial = 1; + for (int i = 1; i <= number; i++) + { + factorial *= i; + } + return factorial; } } } \ No newline at end of file From 027a94e872cb5e84e4d96c15d376c83708673e63 Mon Sep 17 00:00:00 2001 From: thakkarjanvi Date: Wed, 9 Aug 2023 17:49:57 +0530 Subject: [PATCH 03/14] Add exercise 1 --- week-1/day-2/exercise-1/Circle/Circle.cs | 16 ++++++++++++++++ week-1/day-2/exercise-1/Circle/Program.cs | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/week-1/day-2/exercise-1/Circle/Circle.cs b/week-1/day-2/exercise-1/Circle/Circle.cs index 065ffa5d..43ae1e1c 100644 --- a/week-1/day-2/exercise-1/Circle/Circle.cs +++ b/week-1/day-2/exercise-1/Circle/Circle.cs @@ -9,5 +9,21 @@ namespace Circle internal class Circle { // Implement the Circle class here + double Radius; + + public Circle(double radius) + { + Radius = radius; + } + + public double GetArea() + { + return Math.PI * Radius * Radius; + } + + public double GetCircumference() + { + return 2 * Math.PI * Radius; + } } } diff --git a/week-1/day-2/exercise-1/Circle/Program.cs b/week-1/day-2/exercise-1/Circle/Program.cs index 0df91b0d..b1fd0ad9 100644 --- a/week-1/day-2/exercise-1/Circle/Program.cs +++ b/week-1/day-2/exercise-1/Circle/Program.cs @@ -5,6 +5,13 @@ internal class Program static void Main(string[] args) { // Create a Circle object and display its area and circumference + Console.WriteLine("Enter the radius of the circle: "); + double radius = Convert.ToDouble(Console.ReadLine()); + Circle myCircle = new Circle(radius); + + Console.WriteLine($"Area: {myCircle.GetArea()}"); + + Console.WriteLine($"Circumference: {myCircle.GetCircumference()}"); } } } \ No newline at end of file From b71ff1c493f9c05f30718549a5bab95bb9d7b5cb Mon Sep 17 00:00:00 2001 From: thakkarjanvi Date: Wed, 9 Aug 2023 18:05:02 +0530 Subject: [PATCH 04/14] Add exercise 2 --- .../exercise-2/BankAccount/BankAccount.cs | 117 ++++++++++++++++++ .../day-2/exercise-2/BankAccount/Program.cs | 14 +++ 2 files changed, 131 insertions(+) create mode 100644 week-1/day-2/exercise-2/BankAccount/BankAccount.cs diff --git a/week-1/day-2/exercise-2/BankAccount/BankAccount.cs b/week-1/day-2/exercise-2/BankAccount/BankAccount.cs new file mode 100644 index 00000000..fe216a0b --- /dev/null +++ b/week-1/day-2/exercise-2/BankAccount/BankAccount.cs @@ -0,0 +1,117 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BankAccount +{ + public abstract class BankAccount + { + public Int32 AccountNumber; + public double Balance; + + public BankAccount(Int32 accountNumber, double initBalance) + { + AccountNumber = accountNumber; + Balance = initBalance; + } + + public abstract void Deposit(double amt); + public abstract void Withdraw(double amt); + + public virtual void DisplayInfo() + { + Console.WriteLine($"Account Number: {AccountNumber}"); + Console.WriteLine($"Balance: {Balance}"); + } + } + + public class SavingsAccount : BankAccount + { + public double InterestRate; + public SavingsAccount(Int32 accountNumber, double initBalance, double interestRate) : base(accountNumber, initBalance) + { + InterestRate = interestRate; + AccountNumber = accountNumber; + Balance = initBalance; + } + + public override void Deposit(double amt) + { + if (amt > 0) + { + Balance += amt; + Console.WriteLine($"Deposited: {amt}"); + Console.WriteLine($"Current Balance: {Balance}"); + } + else + { + Console.WriteLine($"Invalid Deposit amount"); + } + } + + public override void Withdraw(double amt) + { + if (amt > Balance || amt < 0) + { + Console.WriteLine($"Invalid Withdraw amount"); + + } + else + { + Balance -= amt; + Console.WriteLine($"Withdraw: {amt}"); + Console.WriteLine($"Current Balance: {Balance}"); + } + } + + public override void DisplayInfo() + { + base.DisplayInfo(); + Console.WriteLine($"InterestRate: {InterestRate}"); + } + } + + class CheckingAccount : BankAccount + { + public double OverdraftLimit; + public CheckingAccount(Int32 accountNumber, double initBalance, double overdraftLimit) : base(accountNumber, initBalance) + { + OverdraftLimit = overdraftLimit; + } + public override void Deposit(double amt) + { + if (amt > 0) + { + Balance += amt; + Console.WriteLine($"Deposited: {amt}"); + Console.WriteLine($"Current Balance: {Balance}"); + } + else + { + Console.WriteLine($"Invalid Deposit amount"); + } + } + + public override void Withdraw(double amt) + { + if (amt > Balance || amt < 0) + { + Balance -= amt; + Console.WriteLine($"Withdraw: {amt}"); + Console.WriteLine($"Current Balance: {Balance}"); + + } + else + { + Console.WriteLine("Invalid withdraw amount"); + } + } + public override void DisplayInfo() + { + base.DisplayInfo(); + Console.WriteLine($"Overdraft Limit: {OverdraftLimit}"); + } + } +} diff --git a/week-1/day-2/exercise-2/BankAccount/Program.cs b/week-1/day-2/exercise-2/BankAccount/Program.cs index c58c30e2..281cd6d2 100644 --- a/week-1/day-2/exercise-2/BankAccount/Program.cs +++ b/week-1/day-2/exercise-2/BankAccount/Program.cs @@ -5,6 +5,20 @@ internal class Program static void Main(string[] args) { // Create SavingsAccount and CheckingAccount objects and perform operations + SavingsAccount savingsAccount = new SavingsAccount(1234, 14.00, 10.00); + CheckingAccount checkingAccount = new CheckingAccount(789012, 1500.00, 500.00); + + savingsAccount.DisplayInfo(); + savingsAccount.Deposit(200.00); + savingsAccount.Withdraw(300.00); + Console.WriteLine(); + + checkingAccount.DisplayInfo(); + checkingAccount.Deposit(300.00); + checkingAccount.Withdraw(200.00); + checkingAccount.Withdraw(1800.00); + + Console.ReadLine(); } } } \ No newline at end of file From e1e6edd01a90d5126fded995aaa0e578010ec2e6 Mon Sep 17 00:00:00 2001 From: thakkarjanvi Date: Wed, 9 Aug 2023 18:10:25 +0530 Subject: [PATCH 05/14] Add exercise 3 --- .../day-2/exercise-3/AnimalExercise/Animal.cs | 44 +++++++++++++++++++ .../exercise-3/AnimalExercise/Program.cs | 30 +++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 week-1/day-2/exercise-3/AnimalExercise/Animal.cs diff --git a/week-1/day-2/exercise-3/AnimalExercise/Animal.cs b/week-1/day-2/exercise-3/AnimalExercise/Animal.cs new file mode 100644 index 00000000..055ad03a --- /dev/null +++ b/week-1/day-2/exercise-3/AnimalExercise/Animal.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AnimalExercise +{ + internal abstract class Animal + { + public string Name; + public int Age; + + public abstract void MakeSound(); + } + class Dog : Animal, IMovable + { + public override void MakeSound() + { + Console.WriteLine($"{Name} the dog make sound is: Woof!"); + } + public void Move() + { + Console.WriteLine($"{Name} the dog is running."); + } + } + + class Cat : Animal, IMovable + { + public override void MakeSound() + { + Console.WriteLine($"{Name} the cat make sound is: Meow!"); + } + public void Move() + { + Console.WriteLine($"{Name} the cat is climbing."); + } + } + + interface IMovable + { + void Move(); + } +} diff --git a/week-1/day-2/exercise-3/AnimalExercise/Program.cs b/week-1/day-2/exercise-3/AnimalExercise/Program.cs index 5928b7cb..a2b99e58 100644 --- a/week-1/day-2/exercise-3/AnimalExercise/Program.cs +++ b/week-1/day-2/exercise-3/AnimalExercise/Program.cs @@ -5,6 +5,36 @@ internal class Program static void Main(string[] args) { // Create a list of Animal objects, add Dog and Cat instances, and call their methods + + List animals = new List(); + + Console.WriteLine("animals", animals); + Dog dog = new Dog + { + Name = "Daisy", + Age = 1 + }; + + Cat cat = new Cat + { + Name = "Bela", + Age = 1 + }; + + animals.Add(dog); + animals.Add(cat); + + + foreach (var animal in animals) + { + animal.MakeSound(); + if (animal is IMovable movableAnimal) + { + movableAnimal.Move(); + } + Console.WriteLine("Name:" + animal.Name + ", Age:" + animal.Age); + } + Console.ReadLine(); } } } \ No newline at end of file From 4df5c1a2e6eb68b937813f0c1381a5d2ead66949 Mon Sep 17 00:00:00 2001 From: thakkarjanvi Date: Wed, 9 Aug 2023 18:40:46 +0530 Subject: [PATCH 06/14] Add exercise 4 --- .../VehicleManagementSystem/IRepository.cs | 24 +++++++ .../VehicleManagementSystem/IVehicle.cs | 28 ++++++++ .../VehicleManagementSystem/Program.cs | 65 +++++++++++++++++++ .../VehicleManagementSystem/VehicleFactory.cs | 34 ++++++++++ .../VehicleManagementSystem/VehicleLogger.cs | 30 +++++++++ .../VehicleRepository.cs | 62 ++++++++++++++++++ .../VehicleManagementSystem/VehicleService.cs | 62 ++++++++++++++++++ 7 files changed, 305 insertions(+) create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/IRepository.cs create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/IVehicle.cs create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/VehicleFactory.cs create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/VehicleLogger.cs create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/VehicleRepository.cs create mode 100644 week-1/day-2/exercise-4/VehicleManagementSystem/VehicleService.cs diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/IRepository.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/IRepository.cs new file mode 100644 index 00000000..0077d3dd --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/IRepository.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem +{ + public interface IRepository + { + T GetById(int id); + IEnumerable GetAll(); + void Add(T entity); + void Update(T entity); + void Delete(T entity); + } + + public class Entity + { + public int Id; + public string Name; + } +} + diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/IVehicle.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/IVehicle.cs new file mode 100644 index 00000000..62e9fb04 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/IVehicle.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem +{ + internal interface IVehicle + { + public void Drive(); + } + class Car : IVehicle + { + public void Drive() + { + Console.WriteLine("Car contains people."); + } + } + + class Truck : IVehicle + { + public void Drive() + { + Console.WriteLine("Truck contains heavy goods."); + } + } +} diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs index 77395b3a..eff8d70e 100644 --- a/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/Program.cs @@ -5,6 +5,71 @@ internal class Program static void Main(string[] args) { Console.WriteLine("Hello, World!"); + + Car myCar = new Car(); + Truck myTruck = new Truck(); + + myCar.Drive(); + myTruck.Drive(); + + VehicleFactory carFactory = new CarFactory(); + carFactory.DoSomethingWithVehicle(); + + VehicleFactory truckFactory = new TruckFactory(); + truckFactory.DoSomethingWithVehicle(); + + IRepository repository = new VehicleRepository(); + VehicleService vehicleService = new VehicleService(repository, VehicleLogger.GetInstance()); + + repository.Add(new Entity { Name = "E1" }); + repository.Add(new Entity { Name = "E2" }); + + var allEntities = repository.GetAll(); + foreach (var entity in allEntities) + { + Console.WriteLine($"ID: {entity.Id}, Name: {entity.Name}"); + } + + var entityToUpdate = repository.GetById(1); + if (entityToUpdate != null) + { + entityToUpdate.Name = "Updated E1"; + repository.Update(entityToUpdate); + } + + var entityToDelete = repository.GetById(2); + if (entityToDelete != null) + { + repository.Delete(entityToDelete); + } + + Console.WriteLine("\nAfter Update and Delete:"); + foreach (var entity in repository.GetAll()) + { + Console.WriteLine($"ID: {entity.Id}, Name: {entity.Name}"); + } + Entity car = (Entity)carFactory.CreateVehicle(); + car.Id = 1; + car.Name = "Audi"; + Entity truck = (Entity)truckFactory.CreateVehicle(); + truck.Id = 2; + truck.Name = "Mahindra"; + + vehicleService.AddVehicle(car); + vehicleService.AddVehicle(truck); + + // Listing vehicles + Console.WriteLine("List of vehicles:"); + vehicleService.ListVehicles(); + + // Doing something with a vehicle + Console.WriteLine("\nDoing something with a vehicle:"); + vehicleService.DoSomethingWithVehicle(1); + + + vehicleService.RemoveVehicle(2); + + Console.ReadLine(); } } } \ No newline at end of file diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleFactory.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleFactory.cs new file mode 100644 index 00000000..bd8a1755 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleFactory.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem +{ + internal abstract class VehicleFactory + { + public abstract IVehicle CreateVehicle(); + public void DoSomethingWithVehicle() + { + IVehicle vehicle = CreateVehicle(); + Console.WriteLine("The vehicle"); + vehicle.Drive(); + } + } + class CarFactory : VehicleFactory + { + public override IVehicle CreateVehicle() + { + return new Car(); + } + } + + class TruckFactory : VehicleFactory + { + public override IVehicle CreateVehicle() + { + return new Truck(); + } + } +} diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleLogger.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleLogger.cs new file mode 100644 index 00000000..b55205bc --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleLogger.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem +{ + internal sealed class VehicleLogger + { + private static VehicleLogger instance = null; + + private VehicleLogger() { } + + public static VehicleLogger GetInstance() + { + if (instance == null) + { + instance = new VehicleLogger(); + } + return instance; + } + + public void Log(string message) + { + Console.WriteLine($"[LOG] {DateTime.Now}: {message}"); + } + } + +} diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleRepository.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleRepository.cs new file mode 100644 index 00000000..91403c06 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleRepository.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem +{ + public class VehicleRepository : IRepository + { + List myentities = new List(); + int id = 1; + + public Entity GetById(int id) + { + return myentities.Find(entity => entity.Id == id)!; + } + + public IEnumerable GetAll() + { + return myentities; + } + + public void Add(Entity entity) + { + if (entity == null) + { + throw new ArgumentNullException(nameof(entity)); + } + + entity.Id = id; + myentities.Add(entity); + id++; + } + + public void Update(Entity entity) + { + if (entity == null) + { + throw new ArgumentNullException(nameof(entity)); + } + + int index = myentities.FindIndex(e => e.Id == entity.Id); + if (index >= 0) + { + myentities[index] = entity; + } + } + + public void Delete(Entity entity) + { + if (entity == null) + { + throw new ArgumentNullException(nameof(entity)); + } + + myentities.RemoveAll(e => e.Id == entity.Id); + } + + } +} + diff --git a/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleService.cs b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleService.cs new file mode 100644 index 00000000..6f6ac679 --- /dev/null +++ b/week-1/day-2/exercise-4/VehicleManagementSystem/VehicleService.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VehicleManagementSystem +{ + internal class VehicleService + { + private IRepository myrepository; + private VehicleLogger mylogger; + + public VehicleService(IRepository repository, VehicleLogger logger) + { + myrepository = repository; + mylogger = logger; + } + + public void AddVehicle(Entity vehicle) + { + myrepository.Add(vehicle); + mylogger.Log($"Added vehicle: {vehicle.Name}"); + } + + public void RemoveVehicle(int id) + { + Entity vehicle = myrepository.GetById(id); + if (vehicle != null) + { + myrepository.Delete(vehicle); + mylogger.Log($"Removed vehicle: {vehicle.Name}"); + } + else + { + mylogger.Log($"Vehicle not found for removal. ID: {id}"); + } + } + + public void ListVehicles() + { + var vehicles = myrepository.GetAll(); + foreach (var vehicle in vehicles) + { + Console.WriteLine($"ID: {vehicle.Id}, Model: {vehicle.Name}"); + } + } + + public void DoSomethingWithVehicle(int id) + { + var vehicle = myrepository.GetById(id); + if (vehicle != null) + { + mylogger.Log("Done"); + } + else + { + Console.WriteLine("Vehicle not found."); + } + } + } +} From a1e4daf082ac1f3536b0c2767b1d16f56b6c0eb3 Mon Sep 17 00:00:00 2001 From: thakkarjanvi Date: Thu, 10 Aug 2023 17:10:20 +0530 Subject: [PATCH 07/14] Add exercise1 day4 --- .../day-4/exercise-1/StackApp/ICustomStack.cs | 32 +++++++++++++++++++ week-1/day-4/exercise-1/StackApp/Program.cs | 14 ++++---- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/week-1/day-4/exercise-1/StackApp/ICustomStack.cs b/week-1/day-4/exercise-1/StackApp/ICustomStack.cs index 55f3936d..9e1b2d30 100644 --- a/week-1/day-4/exercise-1/StackApp/ICustomStack.cs +++ b/week-1/day-4/exercise-1/StackApp/ICustomStack.cs @@ -6,4 +6,36 @@ internal interface ICustomStack T Pop(); bool IsEmpty(); } + + public class CustomStack : ICustomStack + { + private List data; + + public CustomStack() + { + data = new List(); + } + + public void Push(T item) + { + data.Add(item); + } + + public T Pop() + { + if (IsEmpty()) + throw new InvalidOperationException("The Stack is empty."); + + int lastIndex = data.Count - 1; + T item = data[lastIndex]; + data.RemoveAt(lastIndex); + return item; + } + + public bool IsEmpty() + { + return data.Count == 0; + } + + } } diff --git a/week-1/day-4/exercise-1/StackApp/Program.cs b/week-1/day-4/exercise-1/StackApp/Program.cs index e089f665..44c6f115 100644 --- a/week-1/day-4/exercise-1/StackApp/Program.cs +++ b/week-1/day-4/exercise-1/StackApp/Program.cs @@ -4,13 +4,13 @@ internal class Program { static void Main(string[] args) { - //ICustomStack intStack = new CustomStack(); - //intStack.Push(1); - //intStack.Push(2); - //intStack.Push(3); - //Console.WriteLine(intStack.Pop()); // Output: 3 - //Console.WriteLine(intStack.Pop()); // Output: 2 - //Console.WriteLine(intStack.IsEmpty()); // Output: False + ICustomStack intStack = new CustomStack(); + intStack.Push(1); + intStack.Push(2); + intStack.Push(3); + Console.WriteLine(intStack.Pop()); // Output: 3 + Console.WriteLine(intStack.Pop()); // Output: 2 + Console.WriteLine(intStack.IsEmpty()); // Output: False } } } \ No newline at end of file From bdf42a70935b463468468f86de46d57a97fae7e5 Mon Sep 17 00:00:00 2001 From: thakkarjanvi Date: Thu, 10 Aug 2023 17:24:18 +0530 Subject: [PATCH 08/14] Add exercise2 day4 --- week-1/day-4/exercise-2/IteratorsApp/Program.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/week-1/day-4/exercise-2/IteratorsApp/Program.cs b/week-1/day-4/exercise-2/IteratorsApp/Program.cs index f3cb1ae8..df76c607 100644 --- a/week-1/day-4/exercise-2/IteratorsApp/Program.cs +++ b/week-1/day-4/exercise-2/IteratorsApp/Program.cs @@ -7,6 +7,8 @@ static void Main(string[] args) var fibonacci = FibonacciSequence().Take(10); foreach (int number in fibonacci) { + + Console.WriteLine(number); } } @@ -14,7 +16,18 @@ static void Main(string[] args) // https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/yield public static IEnumerable FibonacciSequence() { - throw new NotImplementedException(); + int number1 = 0; + int number2 = 1; + + for (int i = 0; i < int.MaxValue; i++) + { + yield return number1; + + int next = number1 + number2; + number1 = number2; + number2 = next; + //throw new NotImplementedException(); + } } } } \ No newline at end of file From 2133ffd5b74649529e50f3c0580364ab7e65f18b Mon Sep 17 00:00:00 2001 From: thakkarjanvi Date: Thu, 10 Aug 2023 17:31:00 +0530 Subject: [PATCH 09/14] Add exercise 3 day4 --- .../exercise-3/LinqToObjectsApp/Program.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/week-1/day-4/exercise-3/LinqToObjectsApp/Program.cs b/week-1/day-4/exercise-3/LinqToObjectsApp/Program.cs index 95e8ac84..9c613fdd 100644 --- a/week-1/day-4/exercise-3/LinqToObjectsApp/Program.cs +++ b/week-1/day-4/exercise-3/LinqToObjectsApp/Program.cs @@ -19,6 +19,41 @@ static void Main(string[] args) //3. Sort people by name //4. Project/Select only Name and Country of all people + //1. Get all people from USA + var peopleFromUSA = people.Where(person => person.Country == "USA"); + + Console.WriteLine("All People from USA:"); + foreach (var person in peopleFromUSA) + { + Console.WriteLine($"Name: {person.Name}, Age: {person.Age}, Country: {person.Country}"); + } + + // 2. Get all people above 30 + var peopleAbove30 = people.Where(person => person.Age > 30); + + Console.WriteLine("All People above 30:"); + foreach (var person in peopleAbove30) + { + Console.WriteLine($"Name: {person.Name}, Age: {person.Age}, Country: {person.Country}"); + } + + // 3. Sort people by name + var sortedPeopleByName = people.OrderBy(person => person.Name); + + Console.WriteLine("People Sorted by Name:"); + foreach (var person in sortedPeopleByName) + { + Console.WriteLine($"Name: {person.Name}, Age: {person.Age}, Country: {person.Country}"); + } + + // 4. Project/Select only Name and Country of all people + var projectedPeople = people.Select(person => new { person.Name, person.Country }); + + Console.WriteLine("Projected List (Name and Country):"); + foreach (var item in projectedPeople) + { + Console.WriteLine($"Name: {item.Name}, Country: {item.Country}"); + } } } From 88357cb381ee85a858b18227e99e77cc328be91d Mon Sep 17 00:00:00 2001 From: thakkarjanvi Date: Thu, 10 Aug 2023 17:43:04 +0530 Subject: [PATCH 10/14] Add exercise4 day4 --- .../day-4/exercise-4/LinqToXmlApp/Program.cs | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/week-1/day-4/exercise-4/LinqToXmlApp/Program.cs b/week-1/day-4/exercise-4/LinqToXmlApp/Program.cs index 315df143..9f81ebba 100644 --- a/week-1/day-4/exercise-4/LinqToXmlApp/Program.cs +++ b/week-1/day-4/exercise-4/LinqToXmlApp/Program.cs @@ -1,4 +1,6 @@ -namespace LinqToXmlApp +using System.Xml.Linq; + +namespace LinqToXmlApp { internal class Program { @@ -33,11 +35,30 @@ static void Main(string[] args) "; // Create an XDocument object from the XML string + XDocument doc = new XDocument(); + doc = XDocument.Parse(xmlString); // Write the title of all books to the console - + var title = doc.Element("Books").Descendants(); + Console.WriteLine("Titles of all books:"); + + foreach ( var item in title ) + { + Console.WriteLine(item.Value); + } // Write the title of all books with genre "Genre 1" to the console + var xdoc = doc.Element("Books").Descendants(); + + Console.WriteLine("The title of all books with genre - Genre 1 is: "); + foreach ( var item in xdoc ) + { + XElement titlegenre = item.Element("Genre"); + if (titlegenre != null && titlegenre.Value == "Genre 1") + { + Console.WriteLine(item.Value); + } + } } } } \ No newline at end of file From aa07e159494c436031360db013b796d83066446d Mon Sep 17 00:00:00 2001 From: thakkarjanvi Date: Thu, 10 Aug 2023 17:46:22 +0530 Subject: [PATCH 11/14] Add exercise 5 day4 --- week-1/day-4/exercise-5/QueryOptimizationApp/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/week-1/day-4/exercise-5/QueryOptimizationApp/Program.cs b/week-1/day-4/exercise-5/QueryOptimizationApp/Program.cs index b18cea78..64779782 100644 --- a/week-1/day-4/exercise-5/QueryOptimizationApp/Program.cs +++ b/week-1/day-4/exercise-5/QueryOptimizationApp/Program.cs @@ -15,7 +15,8 @@ static void Main(string[] args) // Optimized implementation sw.Restart(); - + IEnumerable optimizedQuery = data.AsParallel().Where(x => x > 100).OrderByDescending(x => x).Take(10); + sw.Stop(); Console.WriteLine("Optimized Query: {0} ms", sw.ElapsedMilliseconds); } From 3f34b43eb07fcb90223d020dd0f2693c93e5bc12 Mon Sep 17 00:00:00 2001 From: thakkarjanvi Date: Thu, 10 Aug 2023 17:55:10 +0530 Subject: [PATCH 12/14] Add exercise 6 day4 --- .../exercise-6/LinqFilterAndSort/Program.cs | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/week-1/day-4/exercise-6/LinqFilterAndSort/Program.cs b/week-1/day-4/exercise-6/LinqFilterAndSort/Program.cs index 5c6d6240..ad0e9862 100644 --- a/week-1/day-4/exercise-6/LinqFilterAndSort/Program.cs +++ b/week-1/day-4/exercise-6/LinqFilterAndSort/Program.cs @@ -1,4 +1,6 @@ -namespace LinqFilterAndSort +using System; + +namespace LinqFilterAndSort { internal class Program { @@ -7,6 +9,38 @@ static void Main(string[] args) // Create a list of Person objects // Use LINQ to filter and sort the list // Print the filtered and sorted list of people to the console + List people = new List + { + new Person { FirstName = "Sakshi", LastName = "Kachhi", Age = 30 }, + new Person { FirstName = "Riya", LastName = "Sharma", Age = 33 }, + new Person { FirstName = "Aisha", LastName = "Singh", Age = 18 }, + new Person { FirstName = "Janvi", LastName = "Thakkar", Age = 21 }, + new Person { FirstName = "Parth", LastName = "Parmar", Age = 25 }, + new Person { FirstName = "Henny", LastName = "Patel", Age = 14 } + }; + + // Use LINQ to filter the list to people who are 18 years old or older + var filteredList = people.Where(person => person.Age >= 18).ToList(); + + // Use LINQ to sort the filtered list by last name, then by first name + var sortedList = filteredList.OrderBy(person => person.LastName).ThenBy(person => person.FirstName).ToList(); + + // Print the filtered and sorted list to the console + Console.WriteLine("Filtered and Sorted List:"); + + + foreach (var person in sortedList) + { + Console.WriteLine($"First Name: {person.FirstName}, Last Name: {person.LastName}, Age: {person.Age}"); + } } + } -} \ No newline at end of file + + class Person + { + public string FirstName { get; set; } + public string LastName { get; set; } + public int Age { get; set; } + } +} From 8aca96fa67a7f5d3719e417e8e5e6cbea458d14e Mon Sep 17 00:00:00 2001 From: thakkarjanvi Date: Thu, 10 Aug 2023 18:04:02 +0530 Subject: [PATCH 13/14] Add exercise 7 day4 --- .../exercise-7/LinqListNumbers/Program.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/week-1/day-4/exercise-7/LinqListNumbers/Program.cs b/week-1/day-4/exercise-7/LinqListNumbers/Program.cs index 6caadd07..210af5e3 100644 --- a/week-1/day-4/exercise-7/LinqListNumbers/Program.cs +++ b/week-1/day-4/exercise-7/LinqListNumbers/Program.cs @@ -13,6 +13,44 @@ static void Main(string[] args) // 3. Calculate the sum of all numbers // 4. Calculate the average of all numbers // 5. Find the minimum and maximum values in the list + + // a. Find all even numbers + var evenNumbers = numbers.Where(num => num % 2 == 0); + + Console.WriteLine("Even numbers:"); + foreach (var num in evenNumbers) + { + Console.WriteLine(num); + } + + // b. Find all numbers greater than a specific value (e.g., 20) + int minimumValue = 20; + var largerThanMin = numbers.Where(num => num > minimumValue); + + Console.WriteLine("\nNumbers greater than " + minimumValue + ":"); + foreach (var num in largerThanMin) + { + Console.WriteLine(num); + } + + // c. Calculate the sum of all numbers + int add = numbers.Sum(); + + Console.WriteLine("\nSum of all numbers: " + add); + + // d. Calculate the average of all numbers + double avg = numbers.Average(); + + Console.WriteLine("Average of all numbers: " + avg); + + // e. Find the minimum and maximum values in the list + int minimum = numbers.Min(); + int maximum = numbers.Max(); + + Console.WriteLine("The Minimum value in the list: " + minimum); + Console.WriteLine("The Maximum value in the list: " + maximum); } + } + } \ No newline at end of file From eb5e190cc13d5a4f96f213adcf0ba179fa4d9c0d Mon Sep 17 00:00:00 2001 From: thakkarjanvi Date: Thu, 10 Aug 2023 18:33:17 +0530 Subject: [PATCH 14/14] Add exercise 8 day4 --- .../exercise-8/LinqGroupAggregate/Program.cs | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/week-1/day-4/exercise-8/LinqGroupAggregate/Program.cs b/week-1/day-4/exercise-8/LinqGroupAggregate/Program.cs index 04dbd615..8d4b2e3c 100644 --- a/week-1/day-4/exercise-8/LinqGroupAggregate/Program.cs +++ b/week-1/day-4/exercise-8/LinqGroupAggregate/Program.cs @@ -20,6 +20,48 @@ static void Main(string[] args) // 2. Count the number of products in each category // 3. Calculate the total price of products in each category // 4. Find the most expensive product in each category + + // a. Group products by category + var groupProducts = products.GroupBy(product => product.Category); + + foreach (var group in groupProducts) + { + Console.WriteLine("Category: " + group.Key); + foreach (var product in group) + { + Console.WriteLine("Product: " + product.Name); + } + } + + // b. Count the number of products in each category + var count = products.GroupBy(product => product.Category) + .Select(group => new { Category = group.Key, Count = group.Count() }); + + Console.WriteLine("\nProducts count in each category:"); + foreach (var result in count) + { + Console.WriteLine($"Category: {result.Category}, Count: {result.Count}"); + } + + // c. Calculate the total price of products in each category + var total = products.GroupBy(product => product.Category) + .Select(group => new { Category = group.Key, TotalPrice = group.Sum(product => product.Price) }); + + Console.WriteLine("\nTotal price of products in each category:"); + foreach (var result in total) + { + Console.WriteLine($"Category: {result.Category}, Total Price: {result.TotalPrice}"); + } + + // d. Find the most expensive product in each category + var costly = products.GroupBy(product => product.Category) + .Select(group => new { Category = group.Key, MostExpensive = group.Max(product => product.Price) }); + + Console.WriteLine("\nMost expensive product in each category:"); + foreach (var output in costly) + { + Console.WriteLine($"Category: {output.Category}, Most Expensive: {output.MostExpensive}"); + } } } } \ No newline at end of file