Can any one help me for these basic programs [Resolved]

Posted by Shreedar under C# on 10/21/2015 | Points: 10 | Views : 768 | Status : [Member] | Replies : 11
1.program to download a file from webfolder.
2.windows schedular program to send an automatic e-mail for a perticular time every day.
3.find a Nth highest or lowest number in a given series of numbers.
4.find how many times a char or word repeated in a given string.

Regards

Sridhar Thota.
www.dotnet-sridhar.blogspot.com



Responses

Posted by: Rajnilari2015 on: 10/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Question #3: Find a Nth highest or lowest number in a given series of numbers.
Solution 1:
Use Min() and Max() Extension Methods of Enumerable Class that resides under the namespace System.Linq

using System;

using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<int> lstInts = new List<int>() { 1, 206, 34, -4, 590, 61, 78, 8, 9, 180,237 };

int minNum = lstInts.Min(); //lowest number
int maxNum = lstInts.Max(); //highest number

Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum);

Console.ReadKey(true);
}
}
}


Result
Min = -4 , Max = 590


OR Since you asked for numbers which can be +ve,-ve, fractional (not considering Complex numbers here), you can modify the above program as under

List<double> lstNumbers= new List<double>() { 1, 2.06, 34, -4, 5.90, 61.9, 7.8, 8, 9, 18.0,23.7 };


double minNum = lstNumbers.Min(); //lowest number
double maxNum = lstNumbers.Max(); //highest number

Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum);


Hope this helps

--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 10/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Question #3: Find a Nth highest or lowest number in a given series of numbers.
Solution 2:
Use OrderBy for sorting and then apply the First() and Last() Extension Methods of Enumerable Class that resides under the namespace System.Linq

using System;

using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//Question #3: Find a Nth highest or lowest number in a given series of numbers.
List<double> lstNumbers = new List<double>() { 1, 2.06, 34, -4, 5.90, 61.9, 7.8, 8, 9, 18.0,23.7 };

var sortedNumbers = lstNumbers.OrderBy(o => o).ToList();
double minNum = sortedNumbers.First();
double maxNum = sortedNumbers.Last();
Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum);
Console.ReadKey(true);
}
}
}


--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 10/22/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Question #3: Find a Nth highest or lowest number in a given series of numbers.
Solution 3:
The Old school way

using System;
using System.Collections.Generic;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//Question #3: Find a Nth highest or lowest number in a given series of numbers.
List<double> lstNumbers = new List<double>() { 1, 2.06, -36.4, -4, 5.90, 61.9, 79.8, 8, 9, 18.0,23.7 };
double minNum = 0;
double maxNum = 0;

minNum = lstNumbers[0];
maxNum = lstNumbers[0];
for(int i = 1;i< lstNumbers.Count;i++)
{
//logic for obtaining the Minimum Number
if (minNum > lstNumbers[i]) minNum = lstNumbers[i];

//logic for obtaining the Maximum Number
if (maxNum < lstNumbers[i]) maxNum = lstNumbers[i];
}
Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum);
Console.ReadKey(true);
}
}
}


--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 10/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Question #3: Find a Nth highest or lowest number in a given series of numbers.
Solution 4:
Using Take and Single Extension methods

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//Question #3: Find a Nth highest or lowest number in a given series of numbers.
List<double> lstNumbers = new List<double>() { 1, 2.06, -36.4, -4, 5.90, 61.9, 79.8, 8, 9, 18.0,23.7 };

double minNum = lstNumbers.OrderBy(o => o).Take(1).Single();
double maxNum = lstNumbers.OrderByDescending(o => o).Take(1).Single();
Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum);

Console.ReadKey(true);
}
}
}



--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 10/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Question #3: Find a Nth highest or lowest number in a given series of numbers.
Solution 5:
Using indexing technique after sorting

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//Question #3: Find a Nth highest or lowest number in a given series of numbers.
List<double> lstNumbers = new List<double>() { 1, 2.06, -36.4, -4, 5.90, 618.9, 79.8, 8, 9, 18.0,23.7 };

int itemsCount = lstNumbers.Count;
var sortedList = lstNumbers.OrderBy(o => o).ToList();
double minNum = sortedList[0];
double maxNum = sortedList[itemsCount -1];
Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum);
}
}
}


--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 10/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Question #4: Find how many times a word repeated in a given string.
Solution 1:
Using Group By and LINQ

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//Question #4: Find how many times a word repeated in a given string.
List<string> lstWords = new List<string>() { "hello","this","is","dotnet","dotnet","funda","is","is"};

(from word in lstWords
group word by word into g
select new
{
Words = g.Key,
WordsRepeated = (g.Count() - 1) == 0
? String.Concat("Word ", g.Key, " has never repeated")
: String.Concat("Word ", g.Key, " repeated ", g.Count(), " times")
}).ToList().ForEach(i => Console.WriteLine("{0} : {1}", i.Words, i.WordsRepeated));



Console.ReadKey(true);
}
}
}



Result

hello : Word hello has never repeated

this : Word this has never repeated
is : Word is repeated 3 times
dotnet : Word dotnet repeated 2 times
funda : Word funda has never repeated


If you are looking for a words repeated in a single string, then you can split them by the delimiters and apply the same program as under


using System;
using System.Linq;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//Question #4: Find how many times a word repeated in a given string..
string lstWords = "hello this is dotnet funda . dotnet funda is a dotnet forum";

(from word in lstWords.Split(' ')
group word by word into g
select new
{
Words = g.Key,
WordsRepeated = (g.Count() - 1) == 0
? String.Concat("Word ", g.Key, " has never repeated")
: String.Concat("Word ", g.Key, " repeated ", g.Count(), " times")
}).ToList().ForEach(i => Console.WriteLine("{0} : {1}", i.Words, i.WordsRepeated));

Console.ReadKey(true);
}
}
}


--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 10/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Question #4: Find how many times a word repeated in a given string.
Solution 2:
Using Group By and LAMBDA

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//Question #4: Find how many times a word repeated in a given string.
List<string> lstWords = new List<string>() { "hello","this","is","dotnet","dotnet","funda","is","is"};

lstWords
.GroupBy(x => x)
.Select(g => new {
Words = g.Key,
WordsRepeated = (g.Count() - 1) == 0
? String.Concat("Word ", g.Key, " has never repeated")
: String.Concat("Word ", g.Key, " repeated ", g.Count(), " times")
}).ToList().ForEach(i => Console.WriteLine("{0} : {1}", i.Words, i.WordsRepeated));



Console.ReadKey(true);
}
}
}



If you are looking for a words repeated in a single string, then you can split them by the delimiters and apply the same program as under


using System;
using System.Linq;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//Question #4: Find how many times a word repeated in a given string..
string lstWords = "hello this is dotnet funda . dotnet funda is a dotnet forum";

(from word in lstWords.Split(' ')
group word by word into g
select new
{
Words = g.Key,
WordsRepeated = (g.Count() - 1) == 0
? String.Concat("Word ", g.Key, " has never repeated")
: String.Concat("Word ", g.Key, " repeated ", g.Count(), " times")
}).ToList().ForEach(i => Console.WriteLine("{0} : {1}", i.Words, i.WordsRepeated));



Console.ReadKey(true);
}
}
}


--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 10/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Question #4: Find how many times a char repeated in a given string.
Solution :


using System;
using System.Linq;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//Question #4: Find how many times a char repeated in a given string..
string lstWords = "hello this is dotnet funda";

(from character in lstWords.ToCharArray().Where(x => !Char.IsWhiteSpace(x))
group character by character into g
select new
{
Characters = g.Key,
RepeatedCharacters = (g.Count() - 1) == 0
? String.Concat("Character ", g.Key, " has never repeated")
: String.Concat("Character ", g.Key, " repeated ", g.Count(), " times")
}).ToList().ForEach(i => Console.WriteLine("{0} : {1}", i.Characters, i.RepeatedCharacters));

Console.ReadKey(true);
}
}
}


Observe that I am removing the WhiteSpaces . Final output

h : Character h repeated 2 times
e : Character e repeated 2 times
l : Character l repeated 2 times
o : Character o repeated 2 times
t : Character t repeated 3 times
i : Character i repeated 2 times
s : Character s repeated 2 times
d : Character d repeated 2 times
n : Character n repeated 2 times
f : Character f has never repeated
u : Character u has never repeated
a : Character a has never repeated


--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 10/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Question #1: Program to download a file from webfolder.

If you mean from a FTP server, then you can use


using System;
using System.IO;
using System.Net;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{

try
{
string ftpAddr = "ftp://FTPSERVERADDRESS/FILENAME";
string userName = "FTPUSERNAME";
string password = "FTPPASSWORD";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpAddr);
request.Credentials = new NetworkCredential(userName, password);
request.UseBinary = true;
request.Method = WebRequestMethods.Ftp.DownloadFile;

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream writer = new FileStream("LOCALDOWNLOADPATH", FileMode.Create);

long length = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[2048];

readCount = responseStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
writer.Write(buffer, 0, readCount);
readCount = responseStream.Read(buffer, 0, bufferSize);
}

responseStream.Close();
response.Close();
writer.Close();

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}

}
}
}



You can also refer http://www.codeproject.com/Articles/17202/Simple-FTP-demo-application-using-C-Net

If you mean from a website , then you can take the help of WebClient class DownloadFile Method (https://msdn.microsoft.com/en-us/library/ez801hhe.aspx ) as under



using System;
using System.Linq;
using System.Net;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{

string address = "http://www.dotnetfunda.com/images/showimage.aspx?image=/userFiles/profiles/Shreedar_Profile_3741_sridhar%20thota.jpg&width=100&height=100";
string fileName = @"D:\DownloadImage" + System.IO.Path.GetExtension(address).ToCharArray().Take(4).Aggregate(" ", (a, b) => a + b);
new WebClient().DownloadFile(address, fileName);
Console.WriteLine("Download Completed...");

}
}
}


Hope this helps

--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 10/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Question 2. Windows scheduler program to send an automatic e-mail for a particular time every day.

Solution 1: Using Task Scheduler Managed Wrapper

First download Task Scheduler Managed Wrapper from http://taskscheduler.codeplex.com/

using Microsoft.Win32.TaskScheduler;

using System;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{

using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();

//Set the task description.
td.RegistrationInfo.Description = "Send an automatic e-mail for at 10 AM daily";
td.RegistrationInfo.Author = "RNA Team";

// Create a trigger that will fire the task at 10 AM every day
td.Triggers.Add(new DailyTrigger {
StartBoundary = new DateTime(2015, 10, 23, 10, 0, 0),
EndBoundary = new DateTime(2015, 10, 23, 10, 0, 0),
Id = "DailyTriggerId",
Enabled = true,
DaysInterval = 1 });

// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction("DailyEmailService.exe"));

// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition(@"Test", td);

// Remove the task we just created
ts.RootFolder.DeleteTask("Test");
}

}
}
}


--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 10/23/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Question 2. Windows schedular program to send an automatic e-mail for a particular time every day.

Solution 2: Using ASquare.WindowsTaskScheduler 1.5.0 . It is nicely designed Fluent API.

Using Nuget , install the ASquare.WindowsTaskScheduler package

PM> Install-Package ASquare.WindowsTaskScheduler


(https://www.nuget.org/packages/ASquare.WindowsTaskScheduler/ )

Program:

using ASquare.WindowsTaskScheduler;
using ASquare.WindowsTaskScheduler.Models;
using System;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{

SchedulerResponse response = WindowTaskScheduler
.Configure()
.CreateTask("Send an automatic e-mail for at 10 AM daily", "DailyEmailService.exe")
.RunDaily()
.RunEveryXMinutes(null)
.SetStartDate(new DateTime(2015, 10, 24))
.SetStartTime(new TimeSpan(10, 0, 0))
.Execute();
}
}
}


Hope this helps

--
Thanks & Regards,
RNA Team

Shreedar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response