Strings Merging in C# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
please I want to merge strings of these nature by column in C#:
11111111111
01001111101
10111101010
The procedure for output: Once a column have zero(0) that column turns to zero(0). So expected result for above input should be:
00001101000
Note: My own way was to use mathematical solution via array and looping but I think I should ask if there is a simple way of achieving this in c#. My method seems to take more time if the rows are much.

a way to solve this is to turn the stringBin to a number, then apply an AND between them
public static void Main()
{
string x1 = "11111111111";
string x2 = "01001111101";
string x3 = "10111101010";
long result1 = Convert.ToInt64(x1, 2);
long result2 = Convert.ToInt64(x2, 2);
long result3 = Convert.ToInt64(x3, 2);
long res = result1 & result2 & result3;
string binary = Convert.ToString(res, 2);
Console.WriteLine("This is the result: " + binary);
}

Related

Console.ReadLine().Contains Console.ReadLine()? why do i get false when the number is present in both? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Could someone help me why do i get true only if the numbers are the same? I need to find out if the second input is in the first one. True/false
int[] firstNumber;
firstNumber = new int[10];
firstNumber[0] =
Convert.ToInt32(Console.ReadLine());
int secondNumber =
Convert.ToInt32(Console.ReadLine()) ;
Console.WriteLine(firstNumber.Contains(secondNumber));
Edit. I need to get true for ex.:firstNumber 5214 and secondNumber 4
how can i get true for 5432(firstNum) and 3(secondNum)
Use strings instead of integers, which will use string.Contains instead of Array.Contains:
string firstNumber = Console.ReadLine();
string secondNumber = Console.ReadLine();
Console.WriteLine(firstNumber.Contains(secondNumber));

Sum of the elements per square range of int's in List<int> [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
My list is:
var list = new List<int>()
{
1, 2, 3, 4
};
Without doing
int sum = list[0]*list[0] + list[1]*list[1] + list[2]*list[2] + list[3]*list[3]
var result = list.Sum(o => o * o);
While the other answers work as well, you should learn how to use a loop. It is a fundamental building block of programming. For example, we can use a foreach loop to iterate over each element in the list. see:
int sum = 0;
foreach (int val in list)
sum += val * val;

Convert Instagram Id to Url Segment [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Currently, I am trying to convert coffee script from there https://github.com/slang800/instagram-id-to-url-segment to C# but I am unable to convert because I have less exp in coffee script. What I need to do is simple but I am unable to guess the algo. If id is 1038059720608660215 then value should be 5n7dDmhTr3. The complete code of algo is available at github.
Try this
static void Main(string[] args)
{
string[] convert = {
"A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z","a","b","c","d",
"e","f","g","h","i","j","k","l","m","n",
"o","p","q","r","s","t","u","v","w","x",
"y","z","0","1","2","3","4","5","6","7",
"8","9","-","_"
};
Int64 input = 1038059720608660215;
string output = "";
for (int i = 9; i >= 0; i--)
{
long digit = (input >> (6 * i)) & 0x3F;
output += convert[digit];
}
Console.WriteLine(output);
Console.ReadLine();
}

WindowsFormsApplication Number Generator [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I wish to know if i can make a simple application in WFA that if you enter your name to generate a lucky number, I have an idea but I really don't know how to generate a number if you insert your name...Is it even posible ?
In C# a string is composed of single characters and those can be converted to int
string s = "Abc";
int i = (int)s[0]; // Get code of first character.
Yet another possibility is to get the hash code of the string:
int hash = s.GetHashCode();
This hash code should look random enough. If not, or if you need to create several random numbers from a name, you could use this hash code as a seed value for the random numbers.
var random = new Random(hash);
int n1 = random.Next();
int n2 = random.Next();
Try this:
var random = new Random();
var number = random.Next(0, 1000);
In this example, number is an int digit between 0 and 1000.

Calculate molecular weight based on chemical formula [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to write a program which will calculate the molecular weight of a given molecule based on its chemical formula.
This code can split a molecular formula like "CH3OH" to an array {C H 3 O H} but from here, what would be a good way to use the split text to calculate the molecular weight?
string input = MoleculeTextbox.text;
string pattern = #"([0-9]?\d*|[A-Z][a-z]{0,2}?\d*)";
string[] sunstrings = Regex.Split(input,pattern);
First of all, you'd need to parse the string and turn "H3" into "HHH", etc. That might look something like this:
var x = "CH3OH".replace(/([a-z])([2-9])/gi, function(_,c,n) { return new Array(1+parseInt(n)).join(c); });
First group being the matched character, and second group being the number of repetitions.
You now have a string that looks like "CHHHOH". You can split that line into an array with one character at each index, .split(''), and map each value to its molecular mass. For that you need to define some sort of lookup table. I'm using reduce to take care of the mapping and the addition in one go:
var mass = { C: 12.011, H: 1.008, O: 15.999 };
var weight = x.split('').reduce(function(sum,element) { return sum + mass[element]; }, 0);

Categories