Skip to content

Commit

Permalink
Merge branch 'main' into select
Browse files Browse the repository at this point in the history
  • Loading branch information
mamtawardhani authored Dec 16, 2024
2 parents 29fa635 + 8a84a91 commit ee0a0c0
Show file tree
Hide file tree
Showing 96 changed files with 5,579 additions and 20 deletions.
2 changes: 1 addition & 1 deletion bin/concept-of-the-week.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
content/ruby/concepts/variables/variables.md
content/ruby/concepts/gems/gems.md
2 changes: 1 addition & 1 deletion content/ai/concepts/generative-ai/generative-ai.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Title: 'Generative AI'
Description: 'Generative AI (GenAI) involves the creation of new and unique digital content. Before GenAI digital content of this level could only be created by human beings. These unique creations are driven by natural language prompts.'
Description: 'Generative AI (GenAI) uses AI to create new content from simple text prompts. It can generate various types of content, including text, images, and code.'
Subjects:
- 'Machine Learning'
- 'Data Science'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,18 @@ In this example, the cost is measured strictly using the heuristic value. In oth
The total cost for the path (**P** -> **C** -> **U** -> **S**) evaluates to 11. The potential problem with a greedy best-first search is revealed by the path (**P** -> **R** -> **E** -> **S**) having a cost of 10, which is lower than (**P** -> **C** -> **U** -> **S**). Greedy best-first search ignored this path because it does not consider the edge weights.

![Greedy Best-first Search Final Example Graph](https://raw.githubusercontent.com/Codecademy/docs/main/media/greedy-best-first-search-final-example-graph.png)

## Advantages

- _Faster Exploration_: Expands nodes closer to the goal, often leading to faster solutions in large search spaces.
- _Simple and Easy Implementation_: Simple to implement with only a heuristic function, making it quick to set up.
- _Low Memory Usage_: Requires less memory since it stores only nodes close to the goal in the open list.
- _Efficient for Certain Problems_: Works well when the heuristic is accurate and the goal is easily identified.

## Disadvantages

- _Non-optimal Solution_: Since the algorithm only considers the heuristic value and ignores edge weights, it may find a solution that is not the shortest or least costly. This can lead to suboptimal paths.
- _Incomplete_: The search may fail to find a solution, especially if there are dead ends or if the goal node is unreachable. Greedy Best-First Search does not always explore all possible paths.
- _Doesn't Consider Edge Weights_: By ignoring edge weights, the algorithm may miss paths that are less heuristic-optimal but ultimately cheaper in terms of cost. This can lead to inefficient pathfinding.
- _Sensitive to Heuristic Quality_: The algorithm's effectiveness is heavily dependent on the accuracy of the heuristic function. A poorly designed heuristic can result in inefficient search or failure to find the goal.
- _Can Get Stuck in Local Minima_: Greedy Best-First Search may get stuck in local minima, focusing too much on immediate low-cost paths and overlooking potentially better, longer paths that lead to the goal.
46 changes: 45 additions & 1 deletion content/c-sharp/concepts/classes/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,51 @@ public class BankAccount {

## Static Classes

Static classes are defined using the `static` keyword and exclusively contain static members, such as methods, properties, and fields. Unlike regular classes, static classes cannot be instantiated with the `new` keyword. Instead, their members are accessed using the class name itself. These classes are commonly used for utility functions or to group related functionality.
Static classes are defined using the `static` keyword and exclusively contain static members, such as methods, properties, and fields. Unlike regular classes, static classes cannot be instantiated with the `new` keyword. Instead, their members are accessed using the class name itself. These classes are commonly used for utility functions or to group related functionalities.

The `static` keyword forces all fields, methods, and properties to require this keyword. This means that if a user tries to create any of these without the `static` keyword, the code will not run and will produce a compile-time error. The reason behind this is that the user can not create an instance of a static class without using this keyword. Any methods, properties and fields that are not marked as `static` would be unreachable.

### Example

In the example below, a static class called `MyStaticClass` is created. The commented out code shows what a user can not do with static classes. If these commented out pieces of code were to be uncommented, the code would not compile. The example also shows how properties and methods in the `MyStaticClass` class are created and used in the `CallingClass` class:

```cs
using System;

public class CallingClass
{
public static void Main()
{
Console.WriteLine(MyStaticClass.StaticField);

// The following will produce a compile-time error
// MyStaticClass NonStaticError = new MyStaticClass();
int DoubleFive = MyStaticClass.GetDouble(5);
Console.WriteLine(DoubleFive);
}
}

public static class MyStaticClass
{
// The following will produce a compile-time error
// public int NonStaticField = 5;
public static int StaticField = 20;

// Returns double of the value given
public static int GetDouble(int value) {
return value * 2;
}
}
```

The above code produces the following output:

```shell
20
10
```

## Partial classes

Expand Down
73 changes: 73 additions & 0 deletions content/c-sharp/concepts/strings/terms/join/join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
Title: '.Join()'
Description: 'Joins elements of a collection into a single string with a specified separator.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Strings'
- 'Methods'
- 'Characters'
CatalogContent:
- 'learn-c-sharp'
- 'paths/computer-science'
---

The **`.Join()`** method concatenates an array or a collection of strings into a single string, with a specified separator inserted between each element.

This method is particularly useful for formatting lists, such as comma-separated values or readable sentences.

## Syntax

```pseudo
string.Join(string separator, IEnumerable<string> values)
```

- `separator`: A string to insert between each element. If empty, elements are joined with no separator.
- `values`: An array or collection of strings to concatenate. `null` values in the collection are replaced with empty strings.

## Example

This example demonstrates how `.Join()` uses a comma and space (`,`) as the separator to create a formatted to-do list.

```cs
using System;

class Program
{
static void Main()
{
string[] studyTasks = { "Complete C# lesson", "Review notes", "Practice coding exercises", "Read documentation" };

// Using .Join() to create a sequential to-do list with ", " as the separator
string toDoList = string.Join(", ", studyTasks);

Console.WriteLine("Today's Codecademy study plan: " + toDoList);
}
}
```

This example results in the following output:

```shell
Today's Codecademy study plan: Complete C# lesson, Review notes, Practice coding exercises, Read documentation
```
## Codebyte Example
The following example demonstrates how `.Join()` combines a collection of course names using an ampersand (`&`) to produce a readable list.
```codebyte/csharp
using System;
public class Program
{
public static void Main()
{
string[] languages = { "C#", "Python", "Java" };
string codecademyCourses = string.Join(" & ", languages);
Console.WriteLine("Popular Codecademy courses: " + codecademyCourses);
}
}
```
118 changes: 118 additions & 0 deletions content/c-sharp/concepts/strings/terms/padright/padright.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
Title: '.PadRight()'
Description: 'Adds padding to the right side of a string to ensure it reaches a specified total length.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Strings'
- 'Characters'
- 'Methods'
- 'Functions'
CatalogContent:
- 'learn-c-sharp'
- 'paths/computer-science'
---

The **`.PadRight()`** method in C# is used to add padding characters to the right of a string to achieve a specified total length. By default, it uses spaces as the padding character, but a custom character can also be specified.

## Syntax

```pseudo
string.PadRight(int totalWidth, char paddingChar)
```

- `totalWidth`: The desired total length of the string, including padding. If the specified width is less than the string's length, no padding is added.
- `paddingChar`(Optional): The character to use for padding. Defaults to a space character (`' '`).

> **Note**: The `.PadRight()` method does not modify the original string. It generates and returns a new string with padding applied to achieve the specified total width.
## Example

### Default Padding with Spaces

```cs
using System;
class Program
{
static void Main()
{
string name = "John";
string paddedName = name.PadRight(10);
Console.WriteLine($"'{paddedName}'");
}
}
```

The above code generates the output as follows:

```shell
'John '
```

### Custom Padding Character

```cs
using System;

class NameFormatter
{
public static void Main(string[] args)
{
string name = "John";
string paddedName = name.PadRight(10, '-');
Console.WriteLine($"'{paddedName}'");
}
}
```

The output of the above code will be:

```shell
'John------'
```

### Handling Shorter Total Width

If the specified `totalWidth` is less than the length of the string, the original string is returned:

```cs
using System;

class NamePaddingExample
{
public static void Main(string[] args)
{
string name = "John";
string result = name.PadRight(3);
Console.WriteLine($"'{result}'");
}
}
```

Here's what the output of this code will be:

```shell
'John'
```

## Codebyte Example

Run the following example to understand how the `.PadRight()` method works:

```codebyte/csharp
using System;
class Program
{
static void Main()
{
string word = "Align";
// Pad with spaces to a total width of 15
Console.WriteLine("'" + word.PadRight(15) + "'");
// Pad with asterisks to a total width of 15
Console.WriteLine("'" + word.PadRight(15, '*') + "'");
}
}
```
95 changes: 95 additions & 0 deletions content/c-sharp/concepts/strings/terms/substring/substring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
Title: '.Substring()'
Description: 'Returns the substring of a string instance starting at a given index.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Methods'
- 'Strings'
CatalogContent:
- 'learn-c-sharp'
- 'paths/computer-science'
---

The **`.Substring()`** method is a string method that returns a substring of a string starting at the specified index. It will return all characters from that index to the end unless a maximum length is specified. If the starting index equals the string length, it returns an empty string (`""`). If the index is greater than the string length, it throws an `ArgumentOutOfRangeException`.

## Syntax

```pseudo
.Substring(int startIndex)
```

Or, alternatively:

```pseudo
.Substring(int startIndex, int length)
```

- `startIndex`: The index from where the substring starts.
- `Length` (Optional): The number of characters to include in the substring..

## Example

In this example, `.Substring()` is used to return the substring of "Codecademy" starting at index `4` and includes all characters from that position to the end of the string:

```cs
using System;

public class Program
{
public static void Main()
{
string str = "Codecademy";
Console.WriteLine(str.Substring(4));
}
}
```

The above example results in the following output:

```shell
cademy
```

## Example 2

In this example, `.Substring()` is used with the optional `length` parameter to return a substring of 6 characters starting from index `2` of the string `"Codecademy"`.

```cs
using System;

public class Program
{
public static void Main()
{
string str = "Codecademy";
Console.WriteLine(str.Substring(2, 6));
}
}
```

The above code generates the following output:

````shell
decade

## Codebyte Example

The below code demonstrates how to use the Substring method:

```codebyte/csharp
using System;
public class Example
{
public static void Main(string[] args)
{
string Name1 = "Brad";
string Name2 = "Angelina";
Console.WriteLine(Name1.Substring(1));
Console.WriteLine(Name2.Substring(1));
}
}
````
Loading

0 comments on commit ee0a0c0

Please sign in to comment.