Skip to content

Instantly share code, notes, and snippets.

View jamescurran's full-sized avatar

James Curran jamescurran

View GitHub Profile
@jamescurran
jamescurran / JS-105.cs
Created February 22, 2025 01:43
Daily Challenge #JS-105: Check if a Number is a Happy Number
/*
A happy number is a number which eventually reaches 1
when replaced repeatedly by the sum of the square of
its digits. If it loops endlessly in a cycle,
then it is not a happy number.
For example, starting with 19, the sequence is: 1² + 9² = 82,
8² + 2² = 68,
6² + 8² = 100,
1² + 0² + 0² = 1.
Hence, 19 is a happy number.
@jamescurran
jamescurran / JS-98.cs
Created February 15, 2025 16:46
Daily Challenge #JS-98: Find the Second Largest Number in an Array (in C#)
/*
Find the Second Largest Number in an Array
The Challenge
Difficulty: Medium
Topic: Array Manipulation
Description
Write a JavaScript function that takes an array of numbers
@jamescurran
jamescurran / JS-102.cs
Created February 15, 2025 16:05
Daily Challenge #JS-102: Calculate Unique Elements in a Square Matrix (In C#)
/*
Calculate Unique Elements in a Square Matrix
Given a square matrix, write a function that computes the number of unique elements in the matrix. A square matrix is a two-dimensional array where the number of rows is equal to the number of columns.
Example:
Input: [[2, 3, 2], [9, 3, 1], [1, 2, 5]]
Output: 5
*/
int countUniqueElements(int[][] matrix) => matrix.SelectMany(m => m).Distinct().Count();
@jamescurran
jamescurran / JS101.cs
Last active February 13, 2025 21:10
Daily Challenge #JS-101: Find the Maximum Depth of a Nested Array (In C#)
/*
Find the Maximum Depth of a Nested Array
Develop a function to find the maximum depth of nested arrays.
The function will receive an array as its input,
which can contain other arrays to any depth,
and it should return the maximum depth integer.
Example:
Input: [1, [2, [3, [4]], 5], 6]
Output: 4
@jamescurran
jamescurran / JS-100.cs
Last active February 12, 2025 17:59
Daily Challenge: JS-100 Find Smallest Missing Positive Integer (in C#)
/*
Find Smallest Missing Positive Integer
Write a function that finds the smallest positive integer
that is missing from an unsorted array of integers.
Your function should run with an expected time complexity of O(n).
Example:
Input: [-1, 4, 2, 1, 9, 10]
Output: 3
*/
@jamescurran
jamescurran / JS-96.cs
Created February 12, 2025 17:55
Daily Challenge: JS-96 Sum Digit Sequence
/* The Challenge
Difficulty: Easy
Topic: Array manipulation
Description
Given an array of numbers, calculate the sum of digits for each number
and return a new array containing these sums in the same order.
*/
@jamescurran
jamescurran / JS-95.cs
Created February 12, 2025 17:52
Daily Challenge: JS-95 Calculate the Product of Non-Zero Elements (in C#)
/*
The Challenge
Difficulty: Easy
Topic: Array Manipulation
Description
Given an array of integers, write a function to calculate the product
of all non-zero elements in the array.
If no non-zero element exists, return 1.
public static int AskGunSkill(IIO io)
{
io.Display("How good a shot are you with your rifle?");
io.Display(" (1) Ace Marksman, (2) Good Shot, (3) Fair to Middlin'");
io.Display(" (4) Need more practice, (5) Shaky Knees");
io.Display("Enter one of the above . The better you claim you are, the");
return io.InputValue("faster you'll have to be with your gun to be successful... ", 1, "Enter 1-5", 5,
"Enter 1-5");
}
@jamescurran
jamescurran / ObjectUpdater3.cs
Created December 14, 2023 20:05
Update an object to one of two values, taken from a string
public static void UpdateFromText<T>(this T target, string changes, bool trueCase = true)
where T : class
{
var propsTarget = target.GetType().GetProperties();
var parts = changes.Split('=');
if (parts.Length != 2)
throw new ArgumentOutOfRangeException(parts[0], $"{parts[0]} is not a property of the target");
var pT = propsTarget.FirstOrDefault(t => t.Name == parts[0]);
if (pT == null)
@jamescurran
jamescurran / ObjectUpdater2.cs
Created December 14, 2023 19:55
Update an Object using a dictionary with the properties to change
public static void UpdateFromText<T>(this T target, IDictionary<string, object> changes)
where T : class
{
foreach (var kvp in changes)
{
var propsTarget = typeof(T).GetProperty(kvp.Key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
if (propsTarget == null)
//throw new ArgumentOutOfRangeException(nameof(changes), $"{kvp.Key} is not a property of the target");
continue;