# CodeWars(C# language) basic self training record 1

## preface

My C# language Xiaobai, adhering to the essence of learning and practicing while learning programming, came into contact with the website codebars Codewars , I learned more about the usage of C # from 8kyu at the bottom. Here is also a summary of the topics I have been in contact with. It is also for your reference while reviewing myself.

The subsequent self-training records will also be like today. The contents include questions, Solutions given by me for the first time, personal interpretation of the questions, and some of the questions will also be attached with the answer area. I think the better Solutions.

## Basic self training record 1

1. Convert a Number to a String

Details:

We need a function that can transform a number into a string.

What ways of achieving this do you know?

Examples:

123 --> "123"

999 --> "999"

My Solution:

using System; public class Kata { public static string NumberToString(int num) { return num.ToString(); } }

Tips:

The knowledge point investigated is how to convert numbers into strings. The main application method is. ToString()

2. Return Negative

Details:

In this simple assignment you are given a number and have to make it negative. But maybe the number is already negative?

Examples:

Kata.MakeNegative(1); // return -1

Kata.MakeNegative(-5); // return -5

Kata.MakeNegative(0); // return 0

My Solution:

using System; public static class Kata { public static int MakeNegative(int number) { if(number>0) { number = -number; } else number = number; return number; } }

Other Solution:

using System; public static class Kata { public static int MakeNegative(int number) { return -Math.Abs(number); } }

Tips:

My first reaction to this question is to use the if judgment statement to complete it. After looking at the answer area, I found that using the Math library is also a good way.

3. Square(n) Sum

Details:

Complete the square sum function so that it squares each number passed into it and then sums the results together.

Examples:

For [1, 2, 2] it should return 9 because 12 + 22 + 22 = 9.

My Solution:

public static class Kata { public static int SquareSum(int[] n) { int sum = 0; for(int i=0;i<n.Length;i++) { sum += n[i]*n[i]; } return sum; } }

Other Solution:

using System.Linq; public static class Kata { public static int SquareSum(int[] n) => n.Sum(i => i * i); }

Tips:

Here are some of the benefits of the library. I use the for loop to solve it, but if I call the Linq library, I can simplify the code. Here is also an article to learn from the Linq library [C#] System.Linq, universal query statement

4. Descending Order

Details:

Your task is to make a function that can take any non-negative integer as an argument and return it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number.

Examples:

Input: 42145 Output: 54421

Input: 145263 Output: 654321

Input: 123456789 Output: 987654321

My Solution:

using System; public static class Kata { public static int DescendingOrder(int num) { int [] train = new int[20]; int k = num.ToString().Length; //Take out the number and put it into the array for(int i=0;i<k;i++) { train[i]=num%10; num/=10; } Array.Sort(train);//sort Array.Reverse(train);//Reverse order int output = 0; for(int j=0;j<k;j++){ output*=10; output+=train[j]; } return output; } }

Other Solution:

using System; using System.Linq; public static class Kata { public static int DescendingOrder(int num) { return int.Parse(string.Concat(num.ToString().OrderByDescending(x => x))); } }

Tips:

I have learned a series of operations for arrays, including skillfully using% to take out numbers, and how to combine numbers into an int type digital output.

5. Mumbling

Details:

This time no story, no theory. The examples below show you how to write function accum.

Examples:

accum("abcd") -> "A-Bb-Ccc-Dddd"

accum("RqaEzty") -> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"

accum("cwAt") -> "C-Ww-Aaa-Tttt"

My Solution:

using System; public class Accumul { public static String Accum(string s) { string result = ""; char [] stringArray = s.ToCharArray(); for(int i=0;i<stringArray.Length;i++) { char.ToLower(stringArray[i]); for(int j=0;j<=i;j++) { if(j==0) result = result + char.ToUpper(stringArray[i]); else result = result + char.ToLower(stringArray[i]); } if(i!=stringArray.Length-1) result = result +"-"; } return result; } }

Other Solution:

using System; using System.Linq; public class Accumul { public static String Accum(string s) { return string.Join("-",s.Select((x,i)=>char.ToUpper(x)+new string(char.ToLower(x),i))); } }

Tips:

I've been stuck with this problem for a long time, mainly because I didn't use the method of. ToCharArray() at the beginning. Later, I found that this method can easily store strings into arrays for operation, including some methods such as. ToLower(), which can also help us solve the problem.

## Summary

Because I just started to contact codeworks, this is also my first blog. I tried hard. The uploaded topics are very basic. The process of writing is mainly that I can review my ideas. At present, the difficulty of three of the five questions belongs to 8kyu, and the latter two are 7kyu. The difficulty has a small increasing trend. Personally, I think codebars is an excellent self-training platform. The answers of various bosses in the answer area also opened my eyes. I also feel that my foundation is weak. I hope I can go farther and farther on the road of programming in the future!