deconstruct array into variables?

pythonatSheriff

Full Stack Web Developer
Joined
Dec 15, 2021
Messages
25
Location
Egypt
Programming Experience
1-3
I have an array of unlimited number of strings and I need to deconstruct this array into variables, so I searched for built-in method but in vain, so I decided to make my own function :
C#:
 public static void Deconstruct(this string[] strArr, out params string)
                {
                 for (int i = 0; i < strArr.Length; i++)
                     {
                         s[i] = strArr[i];
                     }

                }
There is something wrong in using "params" as out , how can I make such a function, please ?
 
Last edited by a moderator:
Code in post 6 works fine.
 
WWWWWoooooWWWW , I have made it ... thank you guys :
C#:
        foreach(var item in Mapping)
    {
        StringBuilder builder = new StringBuilder();
        string str_key = item.Key;
        string str_value = item.Value.ToString();
        // Combine the 2 values with AppendFormat.
        builder.AppendFormat("{0}:{1}", str_key, str_value);
        Console.WriteLine(builder);
    }

Now, I have to exclude the values with (zero) as it requested and in alphabetical order :
((Your final output string should return the keys in alphabetical order. Exclude keys that have a value of 0 after being summed up.))
I guess I have to do so before the (builder) because I have to make a condition for checking the zero values and delete them with Remove() , and
then make it in ascending order then make my (stringbuilder) then convert the dictionary to array in the end, right ?
 
searching function in c-sharp:
      foreach(var item in Mapping.Where(kvp => kvp.Value == 0).ToList())
    {
        Mapping.Remove(item.Key);
    }

    new SortedDictionary<string, int>(Mapping);

    foreach (KeyValuePair<string, int> item in Mapping.OrderBy(Key => Key.Key))
    {
        Console.WriteLine("Key: {0}, Value: {1}", item.Key, item.Value);
    }

      foreach(var item in Mapping)
    {
        Console.WriteLine(item.Key);
        Console.WriteLine(item.Value);
    }



So, I converted the dictionary to Sorted dictionary in order to sort it but the linq method is temorary. The above code output was :
key: B, Value: 3
key: y, Value:1
Y
1
B
3
>> this means that it only sorted with Console.WriteLine but it did not store it in the sorted dictionary. So, I tried this one :
[/B] Mapping = Mapping.OrderBy(Key => Key.Key.Count); [B]


But it gave me error, when I tried to fixed with ((System.Linq.Dynamic)), it gives me another error (( "Dynamic" does not exist in the namespace )),
and I doubt if i tried to install the dynamic library on my PC it will work, and even if it works, it will not work on the test page, so can you help me??
 
Great that you have started looking into Linq methods, you can chain them too:
C#:
var ordered = dictionary.Where(kvp => kvp.Value != 0).OrderBy(kvp => kvp.Key);
Now ordered contains the pairs sorted by key and without those that has value 0.
 
Have the function SearchingChallenge(strArr) read in the strArr parameter containing key:value pairs where the key is a string and the value is an integer. Your program should return a string with new key:value pairs separated by a comma such that each key appears only once with the total values summed up.
For example: if strArr is ["B:-1", "A:1", "B:3", "A:5"] then your program should return the string A:6,B:2.
Where does it say to skip values that result in 0?
 
read in the strArr parameter containing key:value pairs where the key is a string and the value is an integer. Your program should return a string with new key:value pairs separated by a comma such that each key appears only once with the total values summed up.

For example: if strArr is ["B:-1", "A:1", "B:3", "A:5"] then your program should return the string A:6,B:2.

Your final output string should return the keys in alphabetical order. Exclude keys that have a value of 0 after being summed up.
 
For sorting you can also just change the first line of code from post 6 to this, that will maintain the sort order directly as you add items to the dictionary:
C#:
var dictionary = new SortedDictionary<string, int>();
After it is filled you can then filter out 0 sums:
C#:
 var filtered = dictionary.Where(kvp => kvp.Value != 0);
Then you want each pair to output as "key:value", I suggest a Linq Select for this:
C#:
var pairs = filtered.Select(pair => $"{pair.Key}:{pair.Value}");
Now all that is needed is to join these strings together with "," separator. Do you know there is a string.Join method that does this?
 
The skipping of zero sums would have been useful to know in post #4, not 17 posts later in post #22.
 
First of all, I would like to thank you all for the effort you've done with me. Secondly, I am so sorry (Skydiver) for not mentioning the whole paragraph of the test as I thought ( as a newbie as you can recognize) that it will be done at the end of the program and hence I did not mention it in the beginning and it will be a great pleasure to tell me why ( to keep in mind for next cases ). And with respect to the question of you (JOHNH), yes I knew it. I took a complete course studying all stuff related to Full-Stack developer and I hereby I attach the course pamphlet. I did made two complete CMS using dot net core 5, but I've never tried to solve problems on hackerranks or codechef or projectEuler. I studied algorithm (sort, search,..) but I've never applied. I am sorry again if I bother you with a lot of questions, but make sure that I did asked you after searching for hours. I love being a programmer and enjoy learning, I also would like to know if what happened with me right or wrong, I felt so bad after JohnH wrote the code in the contrary to the breadcrumbs that SkyDiver have given to me. This is the first time to ask the community or somebody to help me directly, always was searching google for issues. I really appreciate what you've done for me and I'm open to hear your advice. Thanks.
 

Attachments

  • Sharp Full Stack Diploma Content.pdf
    159.4 KB · Views: 7
  • photo_2021-12-16_17-20-09.jpg
    photo_2021-12-16_17-20-09.jpg
    67.2 KB · Views: 8
Back
Top Bottom