C# Programming Language - Set of Tasks (20) and Solutions
C# Programming Language - Set of Tasks (20) and Solutions
CodeWars
Zad2: The
Zad1: Array Maximum Zad3: Average Zad4: A Hero
Number
Zad7:
Zad5: Zoo Zad6: Big City Zad8: Cube
Uppercase
Zad15:
Zad13: Circular Zad14: Zad16: Shark
Generate
arcA Championship Pontoon
Range
Zad19:
Zad17: Set of Zad18: Pole Zad20: Take an
Filtering Even
First Elements Vault Arrow
Numbers
Kazalo
pojmova
ZADATAK 1/20 - ARRAY
You are given an array with positive numbers and a non-negative number N.
You should find the N-th power of the element in the array with the index
N. If N is outside of the array, then return -1. Don't forget
that the first element has the index 0. Izbornik
SOLUTION 1 OF 1 – ZAD 1
using System;
public class Kata
{
public static double Index(int[] array, int n)
=> n < array.Length ? Math.Pow(array[n],n) : -1;
}
Page 1 of 89
ZADATAK 2/20 - THE MAXIMUM NUMBER
Given a number , Return _The Maximum number _ could be formed from the digits of
the number given .
Notes
Izbornik
Only Natural numbers passed to the function , numbers Contain digits
[0:9] inclusive
Digit Duplications could occur , So also consider it when forming the Largest
Explanation:
As 321 is _The Maximum number _ could be formed from the digits of the number 213
.
As 9873 is _The Maximum number _ could be formed from the digits of the number
7389 .
As 97632 is _The Maximum number _ could be formed from the digits of the number
63729 .
SOLUTION 1 OF 4 – ZAD 2
using System.Linq;
class Kata
{
public static int MaxNumber(int n) =>
int.Parse($"{n}".OrderByDescending(c => c).ToArray());
}
SOLUTION 2 OF 4 – ZAD 2
using System.Linq;
class Kata
{
public static int MaxNumber(int n)
{
return int.Parse(string.Concat(n.ToString().OrderByDescending(x =>
char.GetNumericValue(x))));
}
}
Page 2 of 89
SOLUTION 3 OF 4 – ZAD 2
using System;
using System.Linq;
using System.Collections.Generic;
class Kata
{
public static int MaxNumber(int n) =>
int.Parse(
n.ToString()
.Select(x => x)
.OrderByDescending(x => x)
.Select(x => x.ToString())
.StringJoin("")
);
}
SOLUTION 4 OF 4 – ZAD 2
using System;
class Kata
{
public static int MaxNumber(int n)
{
char[] arr=n.ToString().ToCharArray();
Array.Sort(arr);
Array.Reverse(arr);
Page 3 of 89
ZADATAK 3/20 - AVERAGE
It's the academic year's end, fateful moment of your school report.
The averages must be calculated. All the students come to you and
Izbornik
entreat you to calculate their average for them. Easy ! You just
need to write a script.
Return the average of the given array rounded down to its nearest integer.
SOLUTION 1 OF 7 – ZAD 3
using System.Linq;
using System.Collections.Generic;
using System;
Page 4 of 89
public class Kata
{
public static int GetAverage(int[] marks)
{
long marksSum = 0;
foreach(int mark in marks)
{
marksSum += mark;
}
long result = marksSum / (long)marks.Length;
return (int)result;
}
}
SOLUTION 5 OF 7 – ZAD 3
using System.Linq;
using System.Collections.Generic;
using System;
}
}
Page 5 of 89
SOLUTION 7 OF 7 – ZAD 3
using System.Linq;
using System.Collections.Generic;
using System;
Page 6 of 89
ZADATAK 4/20 - A HERO
A hero is on his way to the castle to complete his mission. However, he's
been told that the castle is surrounded with a couple of powerful dragons!
each dragon takes 2 bullets to be defeated, our hero has no idea how many
bullets he should carry.. Assuming he's gonna grab a specific given number
of bullets and move forward to fight another specific given
number of dragons, will he survive? Izbornik
SOLUTION 1 OF 12 – ZAD 4
class Kata
{
public static bool Hero(int bullets, int dragons)
{
return bullets / 2 >= dragons;
}
}
SOLUTION 2 OF 12 – ZAD 4
class Kata
{
public static bool Hero(int bullets, int dragons)
{
return bullets>=dragons*2;
}
}
Page 7 of 89
SOLUTION 3 OF 12 – ZAD 4
class Kata
public static bool Hero(int bullets, int dragons) => dragons * 2 <=
bullets;
SOLUTION 4 OF 12 – ZAD 4
class Kata
{
public static bool Hero(int bullets, int dragons)
{
return dragons*2 <= bullets;
}
}
SOLUTION 5 OF 12 – ZAD 4
using System;
class Kata
{
public static bool Hero(int bullets, int dragons) => new Func<int, int,
bool>((b, d) => {
bool result = default;
for (var k = 0; k <= 2; ++k)
{
result = d * k <= b ? true : false;
}
return result;
})(bullets, dragons);
}
SOLUTION 6 OF 12 – ZAD 4
class Kata
{
public static bool Hero(int bullets, int dragons)
{
if ((dragons - (bullets/2)) <= 0) return true;
return false;
}
Page 8 of 89
}
SOLUTION 7 OF 12 – ZAD 4
class Kata
{
public static bool Hero(int bullets, int dragons) => bullets / dragons
>= 2;
}
SOLUTION 8 OF 12 – ZAD 4
class Kata
{
public static bool Hero(int bullets, int dragons)
{
return bullets >= 2 * dragons ? true : false;
}
}
Page 9 of 89
SOLUTION 9 OF 12 – ZAD 4
class Kata
{
public static bool Hero(int bullets, int dragons)
{
//Do Some Magic...
if (bullets >= (dragons*2)){
return true;
}
else{
return false;
}
}
}
SOLUTION 10 OF 12 – ZAD 4
class Kata
{
public static bool Hero(int bullets, int dragons)
{
if(bullets/2 >= dragons)
return true;
else
return false;
}
}
Page 10 of 89
SOLUTION 11 OF 12 – ZAD 4
class Kata
{
public static bool Hero(int bullets, int dragons)
{
return bullets >= dragons << 1;
}
}
SOLUTION 12 OF 12 – ZAD 4
class Kata
{
public static bool Hero(int bullets, int dragons) => bullets >= dragons*2
? true: false;
}
Page 11 of 89
ZADATAK 5/20 - ZOO
You're at the zoo... all the meerkats look weird. Something has gone terribly
wrong - someone has gone and switched their heads and tails around!
Same goes for all the other arrays/lists that you will get in the tests: you
have to change the element positions with the same exact logics
Simples!
SOLUTION 1 OF 12 – ZAD 5
using System.Linq;
=> arr.Reverse().ToArray();
SOLUTION 2 OF 12 – ZAD 5
using System;
public class Kata
{
public static string[] FixTheMeerkat(string[] a)
{
Array.Reverse(a);
return a;
}
}
Page 12 of 89
SOLUTION 3 OF 12 – ZAD 5
return arr;
}
}
SOLUTION 4 OF 12 – ZAD 5
using System;
using System.Linq;
public class Kata
{
public static string[] FixTheMeerkat(string[] arr) =>
arr.Reverse().ToArray();
}
Page 13 of 89
SOLUTION 5 OF 12 – ZAD 5
using System.Linq;
using System.Collections.Generic;
using System.Linq;
return arr;
}
}
Page 14 of 89
SOLUTION 7 OF 12 – ZAD 5
using System;
public class Kata
{
public static String[] FixTheMeerkat(String[] arr)
{
String temp = String.Empty;
for (Int32 i = arr.Length - 1, j = 0; i > j; i--, j++)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
}
}
SOLUTION 8 OF 12 – ZAD 5
Page 15 of 89
public static string[] FixTheMeerkat(string[] arr)
{
(arr[0], arr[2]) = (arr[2], arr[0]);
return arr;
}
}
SOLUTION 10 OF 12 – ZAD 5
int j = 0;
Page 16 of 89
return arr;
}
}
SOLUTION 12 OF 12 – ZAD 5
Page 17 of 89
ZADATAK 6/20 - BIG CITY
It's bonus time in the big city! The fatcats are rubbing their paws in
anticipation... but who is going to make the most money?
Return the total figure the individual will receive as a string prefixed
with "£" (= "\u00A3", JS, Go, Java and Julia), "$" (C#, C++, Ruby, Clojure,
Elixir, PHP, Python, Haskell and Lua) or "¥" (Rust).
SOLUTION 1 OF 10 – ZAD 6
Page 18 of 89
SOLUTION 3 OF 10 – ZAD 6
Page 19 of 89
SOLUTION 5 OF 10 – ZAD 6
Page 20 of 89
SOLUTION 8 OF 10 – ZAD 6
Page 21 of 89
SOLUTION 10 OF 10 – ZAD 6
Page 22 of 89
ZADATAK 7/20 - UPPERCASE
"c".IsUpperCase() == false
"C".IsUpperCase() == true
Izbornik
"hello I AM DONALD".IsUpperCase() == false
"ACSKLDFJSgSKLDFJSKLDFJ".IsUpperCase() == false
"ACSKLDFJSGSKLDFJSKLDFJ".IsUpperCase() == true
SOLUTION 1 OF 10 – ZAD 7
SOLUTION 2 OF 10 – ZAD 7
using System;
using System.Linq;
Page 23 of 89
SOLUTION 3 OF 10 – ZAD 7
using System;
using System.Collections.Generic;
using System.Linq;
public static class StringExtensions {
public static bool IsUpperCase(this string s) => new
List<char>(s).TrueForAll( x => !char.IsLower(x) );
}
Page 24 of 89
SOLUTION 6 OF 10 – ZAD 7
using System;
using System.Linq;
Page 25 of 89
SOLUTION 9 OF 10 – ZAD 7
using System.Text.RegularExpressions;
}
SOLUTION 10 OF 10 – ZAD 7
using System;
Page 26 of 89
ZADATAK 8/20 - CUBE
Create a public class called Cube without a constructor which gets one
single private integer variable Side, a getter GetSide() and a setter
SetSide(int num) method for this property. Actually, getter and setter
methods are not the common way to write this code in C#. In the next kata
of this series, we gonna refactor the code and make it a bit more
professional...
Izbornik
Notes:
SOLUTION 1 OF 10 – ZAD 8
using System;
}
SOLUTION 2 OF 10 – ZAD 8
public class Cube
{
private int m_side = 0;
Page 27 of 89
public void SetSide(int num) {
Side = num;
}
}
SOLUTION 4 OF 10 – ZAD 8
public class Cube
{
private int _side = 0;
Page 28 of 89
SOLUTION 7 OF 10 – ZAD 8
public class Cube
{
private int side;
SOLUTION 8 OF 10 – ZAD 8
public class Cube
{
private int Side { get; set; } = 0;
Page 29 of 89
ZADATAK 9/20 - BETWEEN
Complete the function that takes two integers (a, b, where a < b) and
return an array of all integers between the input parameters, including
them.
a = 1
b = 4
--> [1, 2, 3, 4]
SOLUTION 1 OF 10 – ZAD 9
using System;
using System.Linq;
Page 30 of 89
SOLUTION 3 OF 10 – ZAD 9
using System.Collections.Generic;
Page 31 of 89
SOLUTION 5 OF 10 – ZAD 9
using System;
using System.Linq;
using System.Collections.Generic;
}
}
SOLUTION 6 OF 10 – ZAD 9
using System.Linq;
public static class Kata {
public static int[] Between(int a, int b) {
return Enumerable.Range(a, b - a + 1).ToArray();
}
}
Page 32 of 89
SOLUTION 7 OF 10 – ZAD 9
using System;
using System.Linq;
SOLUTION 8 OF 10 – ZAD 9
using System.Linq;
using System;
using System.Linq;
using System.Linq;
Page 33 of 89
ZADATAK 10/20 - EVEN OR ODD
SOLUTION 1 OF 13 – ZAD 10
using System;
namespace Solution
{
public class SolutionClass
{
public static string EvenOrOdd(int number)
{
// Code goes here...
if(number % 2 == 0)
{
return("Even");
}
else
{
return("Odd");
}
}
}
}
Page 34 of 89
SOLUTION 3 OF 13 – ZAD 10
public class SolutionClass
{
public static string EvenOrOdd(int number)
=> number%2 == 0 ? "Even": "Odd";
}
SOLUTION 4 OF 13 – ZAD 10
class SolutionClass
{
public static string EvenOrOdd(int number)
{
return number % 2 == 0 ? "Even" : "Odd";
}
}
SOLUTION 5 OF 13 – ZAD 10
using System;
namespace Solution
{
public class SolutionClass
{
public static string EvenOrOdd(int number)
{
if(number % 2 == 0)
{
return "Even";
}
return "Odd";
}
}
}
Page 35 of 89
SOLUTION 6 OF 13 – ZAD 10
using System;
namespace Solution
{
public class SolutionClass
{
public static string EvenOrOdd(int number)
{
return (number & 1) == 0 ? "Even" : "Odd";
}
}
}
SOLUTION 7 OF 13 – ZAD 10
namespace Solution
{
public class SolutionClass
{
public static string EvenOrOdd(int x) => x % 2 == 0 ? "Even" : "Odd";
}
}
SOLUTION 8 OF 13 – ZAD 10
using System;
namespace Solution
{
public class SolutionClass
{
public static Func<int,string> EvenOrOdd = (x) => x % 2 == 0 ? "Even"
: "Odd";
}
}
Page 36 of 89
SOLUTION 9 OF 13 – ZAD 10
using System;
namespace Solution
{
public class SolutionClass
{
public static string EvenOrOdd(int number)
{
return (number & 1) == 0 ? "Even" : "Odd";
}
}
}
SOLUTION 10 OF 13 – ZAD 10
using System;
namespace Solution
{
public class SolutionClass
{
public static string EvenOrOdd(int number)
{
return new string[] {"Even", "Odd"}[number % 2];
}
}
}
SOLUTION 11 OF 13 – ZAD 10
using System.Reflection;
namespace Solution
{
public class SolutionClass
{
public static string EvenOrOdd(int number)
{
var magic = MethodBase.GetCurrentMethod().Name;
return number % 2 == 0 ? magic.Substring(0, 4) :
magic.Substring(6);
}
}
}
Page 37 of 89
SOLUTION 12 OF 13 – ZAD 10
using System;
namespace Solution
{
public class SolutionClass
{
public static string EvenOrOdd(int number)
{
return (number & 0x01) > 0 ? "Odd" : "Even";
}
}
}
SOLUTION 13 OF 13 – ZAD 10
namespace Solution
{
public class SolutionClass
{
public static string EvenOrOdd(int x) => x % 2 == 0 ? "Even" : "Odd";
}
}
Page 38 of 89
ZADATAK 11/20 - SUM OF POSITIVE
SOLUTION 1 OF 10 – ZAD 11
using System;
using System.Linq;
using System;
using System.Linq;
Page 39 of 89
SOLUTION 3 OF 10 – ZAD 11
using System;
using System.Linq;
if (arr != null)
{
foreach (int i in arr)
{
if (i > 0)
{
sum += i;
}
else
{
sum += 0;
}
}
return sum;
}
return 0;
}
}
SOLUTION 4 OF 10 – ZAD 11
using System;
using System.Linq;
Page 40 of 89
SOLUTION 5 OF 10 – ZAD 11
using System;
using System.Linq;
using System.Linq;
class Kata
{
public static int PositiveSum(int[] arr) => arr.Sum(i => i < 1 ? 0 : i);
}
SOLUTION 7 OF 10 – ZAD 11
using System;
using System.Linq;
Page 41 of 89
SOLUTION 8 OF 10 – ZAD 11
using System;
using System.Linq;
int suma = 0;
int ile = arr.Count();
for(int i=0; i<ile;i++)
{
if(arr[i] > 0) suma += arr[i];
}
return suma;
}
}
SOLUTION 9 OF 10 – ZAD 11
using System;
using System.Linq;
using System;
using System.Linq;
Page 42 of 89
ZADATAK 12/20 - BASIC REGEX
SOLUTION 1 OF 8 – ZAD 12
using System;
using System.Text.RegularExpressions;
using System;
using System.Text.RegularExpressions;
using System.Text.RegularExpressions;
Page 43 of 89
SOLUTION 4 OF 8 – ZAD 12
using System.Text.RegularExpressions;
using System;
using System.Text.RegularExpressions;
public class Kata {
public static bool ValidateCode(string code) => code != string.Empty ?
code[0] == '1' || code[0] == '2' || code[0] == '3' : false;
}
SOLUTION 6 OF 8 – ZAD 12
using System.Text.RegularExpressions;
using System;
using System.Text.RegularExpressions;
Page 44 of 89
SOLUTION 8 OF 8 – ZAD 12
using System;
using System.Text.RegularExpressions;
Page 45 of 89
ZADATAK 13/20 – CIRCULAR ARC A
SOLUTION 1 OF 10 – ZAD 13
using System;
Izbornik
double arcLength = A;
double angle = 90;
double radian = Math.PI * angle / 180;
double radius = arcLength / radian;
double sqRadius = radius * radius;
double result = Math.Round(sqRadius, 2);
return result;
}
}
SOLUTION 2 OF 10 – ZAD 13
using System;
public class Kata
{
public static double SquareArea(double A) => Math.Round((4 * A / (2 *
Math.PI)) * (4 * A / (2 * Math.PI)), 2);
}
SOLUTION 3 OF 10 – ZAD 13
Page 46 of 89
SOLUTION 4 OF 10 – ZAD 13
using System;
return result;
}
}
SOLUTION 5 OF 10 – ZAD 13
using System;
public class Kata
{
public static double SquareArea(double A)
{
return Math.Round(Math.Pow(4*A/(2*Math.PI),2),2);
}
}
SOLUTION 6 OF 10 – ZAD 13
using System;
Page 47 of 89
SOLUTION 7 OF 10 – ZAD 13
using System;
using System.Collections.Generic;
using System.Linq;
using System;
Page 48 of 89
SOLUTION 10 OF 10 – ZAD 13
using System;
public class Kata
{
public static double SquareArea(double A)
{
Page 49 of 89
ZADATAK 14/20 – CHAMPIONSHIP
Our football team finished the championship. The result of each match look
like "x:y". Results of all matches are recorded in the collection.
Write a function that takes such collection and counts the points of our
team in the championship. Rules for counting points for each match:
• if x>y - 3 points
• if x<y - 0 point Izbornik
• if x=y - 1 point
Notes:
using System.Linq;
using System;
public static class Kata {
public static int TotalPoints(string[] games) {
int total = 0;
foreach (string game in games) {
if (game[0] > game[2])
total += 3;
else if (game[0] == game[2])
total += 1;
}
return total;
}
}
SOLUTION 2 OF 14 – ZAD 14
using System.Linq;
Page 50 of 89
SOLUTION 3 OF 14 – ZAD 14
using System.Linq;
Page 51 of 89
SOLUTION 5 OF 14 – ZAD 14
using System;
using System.Linq;
using System.Linq;
Page 52 of 89
SOLUTION 8 OF 14 – ZAD 14
using System.Linq;
using System.Linq;
using System;
public static class Kata {
public static int TotalPoints(string[] games) {
int total = 0;
foreach (string game in games) {
if (game[0] > game[2])
total += 3;
else if (game[0] == game[2])
total += 1;
}
return total;
}
}
Page 53 of 89
SOLUTION 10 OF 14 – ZAD 14
using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Page 54 of 89
SOLUTION 12 OF 14 – ZAD 14
using System.Linq;
Page 55 of 89
SOLUTION 13 OF 14 – ZAD 14
using System.Linq;
return points;
}
}
Page 56 of 89
ZADATAK 15/20 - GENERATE RANGE
Task
Note
SOLUTION 1 OF 15 – ZAD 15
using System.Collections.Generic;
Page 57 of 89
for (int i = 1; i < res.Length; i++)
{
res[i] = res[i - 1]+step;
}
return res;
}
}
SOLUTION 3 OF 15 – ZAD 15
using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq;
Page 58 of 89
SOLUTION 5 OF 15 – ZAD 15
using System.Collections.Generic;
return list.ToArray();
}
}
SOLUTION 6 OF 15 – ZAD 15
using System.Collections.Generic;
using System.Linq;
public class Kata
{
public static int[] GenerateRange(int min, int max, int step)
{
return Enumerable
.Repeat(min, (int)((max - min) / step) + 1)
.Select((tr, ti) => tr + (step * ti))
.ToArray();
}
}
SOLUTION 7 OF 15 – ZAD 15
using System.Linq;
Page 59 of 89
SOLUTION 8 OF 15 – ZAD 15
using System;
using System.Linq;
Page 60 of 89
SOLUTION 10 OF 15 – ZAD 15
using System.Collections.Generic;
return list.ToArray();
}
}
SOLUTION 11 OF 15 – ZAD 16
using System.Collections.Generic;
public class Kata
{
public static int[] GenerateRange(int min, int max, int step)
{
List<int> myList = new List<int>();
while (min <= max)
{
myList.Add(min);
min += step;
}
int[] arr = myList.ToArray();
return arr;
}
SOLUTION 12 OF 15 – ZAD 16
public class Kata
{
public static int[] GenerateRange(int min, int max, int step)
{
int number = ((max - min) / step) + 1;
int[] arr = new int[number];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = min + (i * step);
}
return arr;
}
}
Page 61 of 89
SOLUTION 13 OF 15 – ZAD 16
using System;
Console.WriteLine(res[b]);
b++;
}
return res;
}
}
SOLUTION 14 OF 15 – ZAD 16
using System;
using System.Collections.Generic;
Page 62 of 89
ZADATAK 16/20 - HOLIDAY VI - SHARK PONTOON
Your friend invites you out to a cool floating pontoon around 1km off the
beach. Among other things, the pontoon has a huge slide that drops you out
right into the ocean, a small way from a set of stairs used to climb out.
As you plunge out of the slide into the water, you see a shark hovering in
the darkness under the pontoon... Crap!
You need to work out if the shark will get to you before you can
get to the pontoon. To make it easier... as you do the mental Izbornik
calculations in the water you either freeze when you realise you
are dead, or swim when you realise you can make it!
• sharkDistance = distance from the shark to the pontoon. The shark will
eat you if it reaches you before you escape to the pontoon.
• dolphin = a boolean, if true, you can half the swimming speed of the
shark as the dolphin will attack it.
• The pontoon, you, and the shark are all aligned in one dimension.
Page 63 of 89
SOLUTION 2 OF 10 – ZAD 16
public class Kata
{
public static string Shark(
int pontoonDistance,
int sharkDistance,
int yourSpeed,
int sharkSpeed,
bool dolphin)
{
//your code here
return ((float)sharkDistance / sharkSpeed) >
((float)pontoonDistance / (dolphin ? yourSpeed * 2 : yourSpeed)) ? "Alive!"
: "Shark Bait!";
}
}
SOLUTION 3 OF 10 – ZAD 16
public class Kata
{
public static string Shark(
int pontoonDistance,
int sharkDistance,
int yourSpeed,
int sharkSpeed,
bool dolphin)
{
if(dolphin==true)
{
sharkSpeed/=2;
}
float Tt = (float)sharkDistance/sharkSpeed;
float Tp = (float)pontoonDistance/yourSpeed;
if((Tt) > (Tp))
{
return "Alive!";
}
else
{
return "Shark Bait!";
}
}
}
Page 64 of 89
SOLUTION 4 OF 10 – ZAD 16
public class Kata
{
public static string Shark(
int pontoonDistance,
int sharkDistance,
int yourSpeed,
int sharkSpeed,
bool dolphin)
{
if(sharkDistance <= 0)
{
return "Shark Bait!";
}
else
{
return "Alive!";
}
}
}
Page 65 of 89
SOLUTION 5 OF 10 – ZAD 16
public class Kata
{
public static string Shark(
int pontoonDistance,
int sharkDistance,
int yourSpeed,
int sharkSpeed,
bool dolphin)
{
if (dolphin)
{ sharkSpeed /= 2; }
Page 66 of 89
SOLUTION 7 OF 10 – ZAD 16
public class Kata
{
public static string Shark(
int pontoonDistance,
int sharkDistance,
int yourSpeed,
int sharkSpeed,
bool dolphin)
{
if (dolphin)
{ sharkSpeed /= 2; }
}
}
Page 67 of 89
SOLUTION 9 OF 10 – ZAD 16
using System;
Page 68 of 89
SOLUTION 10 OF 10 – ZAD 16
public class Kata
{
public static string Shark(
int pontoonDistance,
int sharkDistance,
int yourSpeed,
int sharkSpeed,
bool dolphin)
{
if(sharkSpeed == 0)
return "Alive";
if(yourSpeed == 0)
return "Shark Bait!";
if(dolphin)
{
}
}
Page 69 of 89
ZADATAK 17/20 - PICK A SET OF FIRST ELEMENTS
Examples Izbornik
var arr = new object[] { 'a', 'b', 'c', 'd', 'e' };
Kata.TakeFirstElements(arr); //=> new object[] { 'a' }
Kata.TakeFirstElements(arr, 2);// => new object[] { 'a', 'b' }
Kata.TakeFirstElements(arr, 3); //=> new object[] { 'a', 'b', 'c' }
Kata.TakeFirstElements(arr, 0); //=> new object[] { }
SOLUTION 1 OF 10 – ZAD 17
using System.Linq;
public class Kata {
public static object[] TakeFirstElements(object[] array, int n = 1) =>
array.Take(n).ToArray();
}
SOLUTION 2 OF 10 – ZAD 17
using System.Linq;
SOLUTION 3 OF 10 – ZAD 17
public class Kata
{
public static object[] TakeFirstElements(object[] array, int n = 1)
{
n = n > array.Length ? array.Length : n;
object[] ans = new object[n];
for (int i = 0; i < n; i++) ans[i] = array[i];
return n == 0 ? new object[] {} : ans;
}
}
Page 70 of 89
SOLUTION 4 OF 10 – ZAD 17
using System.Linq;
SOLUTION 7 OF 10 – ZAD 17
public class Kata
{
public static object[] TakeFirstElements(object[] array, int n = 1)
{
n = n > array.Length ? array.Length : n;
object[] ans = new object[n];
for (int i = 0; i < n; i++) ans[i] = array[i];
return n == 0 ? new object[] {} : ans;
}
}
Page 71 of 89
SOLUTION 8 OF 10 – ZAD 17
using System.Linq;
Page 72 of 89
ZADATAK 18/20 – POLE VAULT STARTING MARKS
For a pole vaulter, it is very important to begin the approach run at the
best possible starting mark. This is affected by numerous factors and
requires fine-tuning in practice. But there is a guideline that
will help a beginning vaulter start at approximately the right Izbornik
location for the so-called "three-step approach," based on the
vaulter's body height.
This guideline was taught to me in feet and inches, but due to the
international nature of Codewars, I am creating this kata to use metric
units instead.
You are given the following two guidelines to begin with: (1) A vaulter with
a height of 1.52 meters should start at 9.45 meters on the runway. (2) A
vaulter with a height of 1.83 meters should start at 10.67 meters on the
runway.
You will receive a vaulter's height in meters (which will always lie in a
range between a minimum of 1.22 meters and a maximum of 2.13 meters). Your
job is to return the best starting mark in meters, rounded to two decimal
places.
Hint: Based on the two guidelines given above, you will want to account for
the change in starting mark per change in body height. This involves a linear
relationship. But there is also a constant offset involved. If you can
determine the rate of change described above, you should be able to determine
that constant offset.
SOLUTION 1 OF 10 – ZAD 18
using System;
class PoleVault
{
public static double StartingMark(double bodyHeight)
{
return Math.Round((3.93548 * bodyHeight + 10.67 - 3.93548 * 1.83) * 100)
/ 100;
}
}
SOLUTION 2 OF 10 – ZAD 18
using System;
class PoleVault
{
public static double StartingMark(double bodyHeight) => Math.Round(1.22/
0.31 * bodyHeight + 9.45 - (1.52 * 1.22 /0.31 ),2);
}
Page 73 of 89
SOLUTION 3 OF 10 – ZAD 18
using System;
class PoleVault
{
public static double StartingMark(double bodyHeight)
{
return Math.Round(m * bodyHeight + b.Item2 - m * b.Item1, 2);
}
static readonly Tuple<double, double> a = Tuple.Create(1.52, 9.45);
static readonly Tuple<double, double> b = Tuple.Create(1.83, 10.67);
static readonly double m = (b.Item2 - a.Item2) / (b.Item1 - a.Item1);
}
SOLUTION 4 OF 10 – ZAD 18
using System;
class PoleVault
{
public static double StartingMark(double bodyHeight)
{
var heightVariation = 1.83-1.52;
var markVariation = 10.67-9.45;
double initialHeight = 1.52;
double initialMark = 9.45;
var markPerHeight = markVariation/heightVariation;
return Math.Round(initialMark+(bodyHeight-
initialHeight)*markPerHeight,2);
}
}
SOLUTION 5 OF 10 – ZAD 18
using System;
class PoleVault
{
public static double StartingMark(double bodyHeight)
{
double delta = (11.85-8.27)/(2.13-1.22);
return Math.Round(11.85 - (2.13-bodyHeight)*delta,2);
}
}
SOLUTION 6 OF 10 – ZAD 18
class PoleVault
{
public static double StartingMark(double bodyHeight)
=> System.Math.Round(1.22 / 0.31 * bodyHeight + 9.45 - 1.22 * 1.52 /
0.31,2);
}
Page 74 of 89
SOLUTION 7 OF 10 – ZAD 18
using System;
class PoleVault
{
public static double StartingMark(double bodyHeight)
{
return
Convert.ToDouble(String.Format("{0:0.00}",bodyHeight*3.9354838709677+3.468
064516129));
}
}
SOLUTION 8 OF 10 – ZAD 18
using System;
class PoleVault
{
public static double StartingMark(double bodyHeight)
{
double delta = (11.85-8.27)/(2.13-1.22);
return Math.Round(11.85 - (2.13-bodyHeight)*delta,2);
}
}
Solution 9 – zad 18
using System;
class PoleVault
{
public static double StartingMark(double bodyHeight) => Math.Round(1.22/
0.31 * bodyHeight + 9.45 - (1.52 * 1.22 /0.31 ),2);
}
SOLUTION 9 OF 10 – ZAD 18
using System;
class PoleVault
{
public static double StartingMark(double bodyHeight)
{
// Remember: Body height of 1.52 m --> starting mark: 9.45 m
// Body height of 1.83 m --> starting mark: 10.67 m
// All other starting marks are based on these guidelines!
/*
Using test cases
Sum of X = 6.7
Sum of Y = 40.24
Mean X = 1.675
Mean Y = 10.06
Sum of squares (SSX) = 0.4621
Sum of products (SP) = 1.818
Page 75 of 89
Regression Equation = ŷ = bX + a
ŷ = 3.93421X + 3.47019
*/
Console.WriteLine($"{bodyHeight} {Math.Round(bodyHeight * 3.93421 +
3.47019,2)}");
return Math.Round(bodyHeight * 3.93421 + 3.47019,2);
}
}
SOLUTION 10 OF 10 – ZAD 18
using System;
class PoleVault
Page 76 of 89
ZADATAK 19/20 – FILTERING EVEN NUMBERS (BUG FIXES)
The method is supposed to remove even numbers from the list and return a
list that contains odd number.
SOLUTION 1 OF 16 – ZAD 19
using System;
using System.Collections.Generic; Izbornik
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
Page 77 of 89
SOLUTION 3 OF 16 – ZAD 19
using System;
using System.Collections.Generic;
using System.Linq;
Page 78 of 89
SOLUTION 5 OF 16 – ZAD 19
using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
Page 79 of 89
SOLUTION 7 OF 16 – ZAD 19
using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
Page 80 of 89
SOLUTION 10 OF 16 – ZAD 19
using System.Linq;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
Page 81 of 89
SOLUTION 12 OF 16 – ZAD 19
using System;
using System.Collections.Generic;
using System.Linq;
return odds;
}
}
SOLUTION 13 OF 16 – ZAD 19
using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
Page 82 of 89
{
List<int> NewList = new List<int>();
return NewList;
}
}
SOLUTION 15 OF 16 – ZAD 19
using System;
using System.Collections.Generic;
using System.Linq;
Page 83 of 89
ZADATAK 20/20 - TAKE AN ARROW TO THE KNEE, FUNCTIO NALLY
Arrow style Functions
Come here to practice the Arrow style functions Not much else to say good
luck!
Details
You will be given an array of numbers which can be used using the
String.fromCharCode() (JS), Tools.FromCharCode() (C#) method to convert the
number to a character. It is recommended to map over the array of numbers
and convert each number to the corresponding ascii character.
Examples
Izbornik
These are example of how to convert a number to an ascii
Character:
Javascript => String.fromCharCode(97) // a
C# => Tools.FromCharCode(97) // a
Reference: https://developer.mozilla.org/en-
US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
SOLUTION 1 OF 10 – ZAD 20
using System;
using System.Linq;
Page 84 of 89
SOLUTION 3 OF 10 – ZAD 20
using System;
using System.Linq;
SOLUTION 4 OF 10 – ZAD 20
using System;
using System.Linq;
using System.Linq;
Page 85 of 89
SOLUTION 7 OF 10 – ZAD 20
using System;
using System.Linq;
Page 86 of 89
KAZALO POJMOVA
$
G
$ 2, 18, 19, 20, 21, 22, 26, 76
GetCurrentMethod ................................................... 37
=
I
=>1, 2, 3, 4, 8, 9, 11, 12, 13, 18, 23, 24, 25, 27, 29, 30,
33, 35, 36, 38, 39, 40, 41, 42, 43, 44, 46, 48, 50, 51, IndexOf ....................................................................... 60
52, 53, 54, 56, 58, 59, 60, 70, 71, 73, 74, 75 IsLower ........................................................... 23, 24, 25
IsMatch ..................................................... 26, 43, 44, 45
0
J
0x01 ............................................................................. 38
Join ............................................................................... 3
A
L
Abs ......................................................................... 31, 33
Add ..................................................31, 32, 57, 59, 61, 62 Length .............. 1, 4, 5, 15, 16, 17, 56, 58, 61, 70, 71, 72
List ..........................................24, 31, 32, 57, 59, 61, 62
B
M
bool 7, 8, 9, 10, 11, 18, 19, 20, 21, 22, 23, 24, 25, 26, 43,
44, 45, 63, 64, 65, 66, 67, 68, 69 Math .......... 1, 5, 31, 33, 42, 46, 47, 48, 49, 73, 74, 75, 76
max.................................................. 57, 58, 59, 60, 61, 62
Max.......................................................................... 33, 42
C min.................................................. 57, 58, 59, 60, 61, 62
F P
for 4, 8, 12, 15, 16, 17, 27, 31, 32, 34, 42, 50, 56, 57, 58, Parse ............................................................. 2, 3, 51, 54
59, 60, 61, 62, 65, 70, 71, 72, 73 PI46, 47, 48, 49
foreach .................................. 5, 6, 40, 50, 52, 53, 54, 55 Pow...................................................................... 1, 47, 48
Format ................................................................... 20, 75 private ..................................................... 27, 28, 29, 58
Func ......................................................................... 8, 36
R
Page 87 of 89
RemoveAll .................................................................. 60
U
Repeat ......................................................................... 59
Reverse ....................................................... 3, 12, 13, 14
using System.Linq 2, 3, 4, 5, 6, 12, 13, 14, 23, 24, 25,
Round .................................. 46, 47, 48, 49, 73, 74, 75, 76
30, 31, 32, 33, 39, 40, 41, 42, 48, 50, 51, 52, 53, 54,
55, 56, 58, 59, 60, 70, 71, 72
S using System.Reflection ........................ 37, 51, 54
using System.Text.RegularExpressions . 26, 43,
Select .................................. 3, 51, 52, 53, 54, 56, 58, 59 44, 45, 56
Sort ............................................................................... 3
Split ..................................................................... 51, 54
V
Substring .................................................................. 37
Sum ................ 4, 39, 40, 41, 42, 50, 51, 52, 53, 54, 56, 75
value ............................................. 15, 20, 23, 27, 29, 57
var.. 5, 6, 8, 13, 14, 20, 31, 37, 47, 48, 51, 52, 54, 59, 60,
T 61, 62, 70, 74
Page 88 of 89