This commit is contained in:
ipvg 2024-08-11 18:19:52 -04:00
parent 90a70ece72
commit 4363c2d1bf
14 changed files with 1490 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,45 @@
Random random = new Random();
Console.WriteLine("Would you like to play? (Y/N)");
if (should_play()) {
play_game();
}
string win_or_lose(int target, int roll) {
if(roll > target){
return "You win!";
} else {
return "You lose!";
}
}
bool should_play() {
bool valid = false;
string? resp;
while (!valid) {
resp = Console.ReadLine();
if (resp != null) {
resp = resp.Trim().ToLower();
if(resp.Equals("y")){
return true;
} else if (resp.Equals("n")) {
return false;
}
}
}
return false;
}
void play_game() {
var play = true;
while (play) {
var target = random.Next(1,6);
var roll = random.Next(1,7);;
Console.WriteLine($"Roll a number greater than {target} to win!");
Console.WriteLine($"You rolled a {roll}");
Console.WriteLine(win_or_lose(target, roll));
Console.WriteLine("\nPlay again? (Y/N)");
play = should_play();
}
}

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,37 @@
//int[] two_coins(int[] coins, int target) {
int[,] two_coins(int[] coins, int target) {
int[,] result = {
{-1,-1},{-1,-1},{-1,-1},
{-1,-1},{-1,-1}
};
int count = 0;
for (int curr = 0; curr < coins.Length; curr++) {
for (int next = curr + 1; next < coins.Length; next++) {
if (coins[curr] + coins[next] == target) {
result[count, 0] = curr;
result[count, 1] = next;
count++;
}
if (count == result.GetLength(0)) {
return result;
}
}
}
return (count == 0) ? new int[0, 0] : result;
}
int target = 55;
int[] coins = new int[] { 5, 5, 50, 25, 25, 10, 5 };
int[,] result = two_coins(coins, target);
if (result.Length == 0) {
Console.WriteLine("No two coins make change");
} else {
Console.WriteLine("Change found at positions:");
for (int i = 0; i < result.GetLength(0); i++) {
if (result[i, 0] == -1) {
break;
}
Console.WriteLine($"{result[i, 0]},{result[i, 1]}");
}
}

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,32 @@
string[] words = { "racecar", "talented", "deified", "tent", "tenet" };
Console.WriteLine("Is it a palindrome?");
foreach (string word in words) {
Console.WriteLine($"{word}: {is_palindrome(word)}");
Console.WriteLine($"{word}: {check_palindrome(word)}");
}
bool is_palindrome(string word) {
int start = 0;
int end = word.Length - 1;
while (start < end) {
if (word[start] != word[end]) {
return false;
}
start++;
end--;
}
return true;
}
bool check_palindrome(string word) {
char[] temp = word.ToCharArray();
Array.Reverse(temp);
string rev_word = string.Join("", temp);
if (String.Equals(word, rev_word)) {
return true;
} else {
return false;
}
}

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,25 @@
double total = 0;
double min_spend = 30.00;
double[] items = {15.97, 3.50, 12.25, 22.99, 10.98};
double[] discounts = {0.30, 0.00, 0.10, 0.20, 0.50};
for (int i = 0; i < items.Length; i++) {
total += get_discounted_price(i);
}
total -= total_meets_minimum() ? 5.00 : 0.00;
Console.WriteLine($"Total: ${format_decimal(total)}");
double get_discounted_price(int item_index) {
return items[item_index] * (1 - discounts[item_index]);
}
bool total_meets_minimum() {
return total >= min_spend;
}
string format_decimal(double input) {
return input.ToString().Substring(0, 5);
}

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,18 @@
double usd = 23.73;
int vnd = usd_to_vnd(usd);
Console.WriteLine($"${usd} USD = ${vnd} VND");
Console.WriteLine($"${vnd} VND = ${vnd_to_usd(vnd)} USD");
int usd_to_vnd(double usd) {
int rate = 23500;
return (int) (rate * usd);
}
double vnd_to_usd(int vnd) {
double rate = 23500;
return vnd / rate;
}
var temp = 100*0.5;
Console.WriteLine(temp.GetType());

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,22 @@
string reverse_word(string word) {
string result = "";
for (int i=word.Length - 1; i>=0 ; i--) {
result += word[i];
}
return result;
}
string reverse_sentence(string input) {
string result = "";
string[] words = input.Split(" ");
foreach (string word in words) {
result += reverse_word(word) + " ";
}
return result.Trim();
}
string input = "there are snakes at the zoo";
Console.WriteLine("Sentece : " + input);
Console.WriteLine("Reversed: " + reverse_word(input));
Console.WriteLine("Special : " + reverse_sentence(input));

View File

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -33,3 +33,4 @@ Following
26. [Challenge - variable data](./026_Challenge_variable_data/026_csharp.md)
27. [Create methods](./027_create_methods/027_csharp.md)
28. [Methods with parameters](./028_method_parameters/028_csharp.md)
29. [Methods that return values](./029_return_value_method/029_csharp.md)