end 029
This commit is contained in:
parent
90a70ece72
commit
4363c2d1bf
1250
029_return_value_method/029_csharp.md
Normal file
1250
029_return_value_method/029_csharp.md
Normal file
File diff suppressed because it is too large
Load Diff
45
029_return_value_method/challenge_game/Program.cs
Normal file
45
029_return_value_method/challenge_game/Program.cs
Normal 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();
|
||||
}
|
||||
}
|
10
029_return_value_method/challenge_game/challenge_game.csproj
Normal file
10
029_return_value_method/challenge_game/challenge_game.csproj
Normal 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>
|
37
029_return_value_method/return_array/Program.cs
Normal file
37
029_return_value_method/return_array/Program.cs
Normal 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]}");
|
||||
}
|
||||
}
|
10
029_return_value_method/return_array/return_array.csproj
Normal file
10
029_return_value_method/return_array/return_array.csproj
Normal 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>
|
32
029_return_value_method/return_bool/Program.cs
Normal file
32
029_return_value_method/return_bool/Program.cs
Normal 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;
|
||||
}
|
||||
}
|
10
029_return_value_method/return_bool/return_bool.csproj
Normal file
10
029_return_value_method/return_bool/return_bool.csproj
Normal 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>
|
25
029_return_value_method/return_meth/Program.cs
Normal file
25
029_return_value_method/return_meth/Program.cs
Normal 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);
|
||||
}
|
10
029_return_value_method/return_meth/return_meth.csproj
Normal file
10
029_return_value_method/return_meth/return_meth.csproj
Normal 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>
|
18
029_return_value_method/return_nums/Program.cs
Normal file
18
029_return_value_method/return_nums/Program.cs
Normal 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());
|
10
029_return_value_method/return_nums/return_nums.csproj
Normal file
10
029_return_value_method/return_nums/return_nums.csproj
Normal 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>
|
22
029_return_value_method/return_str/Program.cs
Normal file
22
029_return_value_method/return_str/Program.cs
Normal 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));
|
10
029_return_value_method/return_str/return_str.csproj
Normal file
10
029_return_value_method/return_str/return_str.csproj
Normal 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>
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user