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);
Related
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);
}
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'm doing a counting program and i need to multiple all digits of x number by it self.
for example: number 123456789;
1*2*3*4*5*6*7*8*9=362,880
A good solution is provided in the comments, but it isn't very easy to follow if you are trying to figure out what you are actually doing. The following code is a bit more verbose, but shows you what is actually happening each step of the way:
using System;
class MainClass {
public static void Main () {
int myNumber = 123456789; //store original number
int newNumber = (int)Math.Abs(myNumber); //changes the number to positive if it is negative to prevent string formatting errors.
int product = 1; //store product start point
string myString = newNumber.ToString(); //convert number to string
foreach(char number in myString){ //loop through string
string numberString = number.ToString(); //convert chars to strings to ensure proper output
int oneDigit = Convert.ToInt32(numberString); //convert strings to integers
product = product*oneDigit; //multiply each integer by the product and set that as the new product
}
if(myNumber < 0){product = -product;}
//if the number is negative, the result will be negative, since it is a negative followed by a bunch of positives.
//If you want your result to reflect that, add the above line to account for negative numbers.
Console.WriteLine(product); //display product
}
}
Output>>> 362880 //The output that was printed.
So we start by converting our number into a string so we can iterate through it. Then we have a foreach loop that goes through each character in the string, converts it into an integer, and multiplies it by the product of the previous numbers. Each time a new multiplication is performed, the product is updated, until, when you reach the end of the number, you have the product of all digits. This is a good project to become familiar with looping. I would recommend playing around with variations of it such as multiplying each number by the original number, multiplying together only multiples of 3, only multiplying numbers less than 5, or only multiplying the first 5 numbers to get a better handle on what's happening in a loop.
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 7 years ago.
Improve this question
I need to count the number of integers between two sorted sets to determine if a lotto ticket is a winner. One sorted set being winningNumbers and the other ticketNumbers. I was told there is a intersect function but I cannot find a function to achieve the required results. Ideally I would like a function that returns an int representing the number of common integers between the sets.
Even if it's not sorted this works:
IEnumerable<int> winning = winningNumbers.Intersect(ticketNumbers);
int countOfWinningNumbers = winning.Count();
If you want to process it further it would be better to create a collection:
List<int> winningList = winning.ToList();
int countOfWinningNumbers = winningList.Count;
Since you've asked explicitly for a SortedSet approach, using SortedSet.IntersectWith might be more efficient(O(n)) but modifies the source set:
SortedSet<int> winningNumbers = new SortedSet<int> { 2, 3, 7 };
SortedSet<int> ticketNumbers = new SortedSet<int> { 1, 2, 3, 4, 5 };
ticketNumbers.IntersectWith(winningNumbers); // now ticketNumbers contains only 2 and 3
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 7 years ago.
Improve this question
I have a text file containing numbers and each number is separated by a tab. There are also several lines of these numbers. I wanted to read the file and store the file into an array but I am not sure how to do so.
Sample file content
137 12.36922 .28200 4312170 .0550 108.4431 14.9959
127 12.23045 .28200 10400044 140.9635 22.9278 19.8656
514 12.91381 .42300 12550428 .1157 61.7263 123.4808
209 12.26951 .28200 10432158 .0361 8.4094 69.3899
271 12.68842 .35250 91375 .0663 3.6094 25.2950
548 12.99388 .49350 2131433 .1386 .6384 78.6621
314 12.54900 .35250 12469075 .1451 44.1327 115.9872
1466 13.40586 .63450 27236 140.6160 53.3465 65.4476
55 11.97313 .21150 100246 .0911 63.5528 60.7556
27 11.66276 .21150 12353651 140.9790 42.3193 110.4559
44 11.81954 .21150 10420688 .0447 38.5853 3.6592
The easiest way is to use Linq:
var text = File.ReadAllText(#"Your file path goes here");
var result = text.Split(" \r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)?.Select(num => double.Parse(num)).ToArray()
If you want sorted data then you can use OrderBy or OrderByDescending extension methods:
var result = text.Split(" \r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)?.Select(num => double.Parse(num)).OrderBy(d => d).ToArray()
Assuming the numbers are floats (you can parse to any type you like, this is just an example) then you could do soemthing like this:
string myFileString = System.IO.File.ReadAllText(#"C:\Temp\MyFile.txt");
string[] myStringArray = myFileString.Split('\t');
List<float> myNumberList = new List<float>();
foreach (string s in myStringArray)
{
myNumberList.Add(float.Parse(s.Trim()));
}
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.