I'd like to get numbers from user text box. There is no limit on the value. For each numbers' characters, I will first multiply by 2, 4 and 6 repeatedly and then add it together.
For example, if user input '12', then 1*2 + 2*4 = 10. if the user input '1111' the answer will be calculated like this: 1*2 + 1*4 + 1*6 + 1*2 = 12. First I've set the number in an array, but then I'm stuck on how to make it multiply different value (2, 4, and 6) every time each no is called in for loop.
Is it possible to achieve this in C#? Are there any examples that I can learn on this?
Or if you have loop OCD, here is a Linq solution
var result = input.Select((c, i) => array[i % array.Length] * (c - '0')).Sum();
Demo Here
The premise is
input is your string
array is your numbers
Select each char and index in the string (c,i)
Get the remainder after division i % array.Length for each element i % length will cycle through your array
Get the int value from the char, (c - '0') (think ascii table)
* them together
Sum the results
Additional Resources
Select(IEnumerable, Func)
Projects each element of a sequence into a new form by incorporating
the element's index.
Remainder operator %
The remainder operator % computes the remainder after dividing its
left-hand operand by its right-hand operand.
Update
However, i'm wondering what if the input is also an array (eg. {11,12}
in this case they will be calculated like this: 11*2 + 12*4 = 70)
var input = "34 5 234 54";
var result = input.Split(' ')
.Select((c, i) => array[i % array.Length] * int.Parse(x))
.Sum();
//int[] numDigitsArray // 1, 2 ,3 ,4
int[] twoFourSixArray = {2, 4, 6};
int current = 0;
int res = 0;
while (current < numDigitsArray.Length)
{
res += twoFourSixArray[current % twoFourSixArray.Length] * numDigistsArray[current];
current++;
}
Related
I'm looking for a way to replace every int that is greater than 9 in an array with the sum of the digits. I'm gonna demonstrate an example below.
I have this array:
int[] ints2 = new int[] { ints[0] * 2, ints[1], ints[2] * 2, ints[3], ints[4] * 2, ints[5], ints[6] * 2, ints[7], ints[8] * 2, ints[9]};
And I want to replace the first int to whatever the sum is of the two digits.
Lets say ints[0] = 9. And ints[0]*2 = 18. The sum should be 1+8 = 9.
Can I use some kind of replace method for ints? Or do you guys have any better ideas of how to deal with this issue?
I tried it like this, but obviously I'm wrong:
foreach (int number in ints)
{
int newNumberInt;
if (number > 9)
{
string newNum = number.ToString();
char[] charNum = newNum.ToCharArray();
int[] intNum = Array.ConvertAll(charNum, n => (int)Char.GetNumericValue(n));
newNumberInt = intNum[0] + intNum[1];
}
newNumberInt = number;
}
You are close. Your mistakes are an incorrect addition of digits, and failure to replace the values in the array. Here is a one line method that adds all the digits in an integer (using System.Linq):
//Adds up all the digits in a number
private static int AddDigits(int number) => number.ToString().Sum(digitCharacter => int.Parse(digitCharacter.ToString()));
For replacing the values, a foreach loop directly on the array won't work, due to changing the contents of the array during enumeration. A simple solution is to use a for loop.
for (int i = 0; i < ints.Length; i++)
{
var value = ints[i];
ints[i] = value > 9 ? AddDigits(value) : value;
}
Note: the AddDigits function I wrote only works for positive integers
I am ruby developer and for certain case I need to swap digits at odd places in 2 numbers in C#.
I have coded in ruby in basic way like below,
a = 35321
b = 123456
a1, b1 = a.to_s.chars, b.to_s.chars # ['3', '5', '3'. '2', '1'], ['1', '2', '3', '4', '5', '6']
n = [a1.length, b1.length].min - 1 # => 4
swapped in below manner,
n.times do |i|
if i.odd?
temp = a1[i]
a1[i] = b1[i]
b1[i] = a1[i]
end
end
Or optimised code is below using step method in ruby,
1.step(n,2) { |i| a1[i], b1[i] = b1[i], a1[i] }
And at the end I get numbers as per expectation as,
> a = a1.join.to_i
# => 32341
> b = b1.join.to_i
# => 153256
My problem is I could not figure out how can I do same in C# Appreciated if anyone provide either suggestion.
There are plenty of ways in c# to get your desired output but I give you a solution that you can understand easily,
First of all we declare your integer array in c# like
int input1 = 35321;
int input2 = 123456;
int[] a = input1.ToString().Select(t => int.Parse(t.ToString())).ToArray();
int[] b = input2.ToString().Select(t => int.Parse(t.ToString())).ToArray();
Then we use traditional for loop to swap number between two array with odd index
for (int i = 0; i < a.Length; i++) //<= Loop through on first array
{
if (i % 2 != 0) //<= Check for odd index
{
int q = a[i]; //<= Take element from first array with current index
a[i] = b[i]; //<= Swap element from second array to first array on current index
b[i] = q; //<= Swap element from first array to second array on current index
}
}
Then we simply print your desired output by,
int number1 = Convert.ToInt32(string.Join("", a));
int number2 = Convert.ToInt32(string.Join("", b));
Console.WriteLine("Output for first array: \t" + number1);
Console.WriteLine("Output for second array: \t" + number2);
Output:
Demo
Considering the inputs a and b will be strings.
The below code should get you the desired result.
var result = string.Join("", b.ToString().Select((x, i) => (i % 2) == 0 || a.ToString().Length <= i ? x.ToString() : a.ToString().Substring(i, 1)));
this code will take into consideration the difference in length.
Edit:You can also implement using LINQ avoiding loops and considering a and b as numbers as shown below.
int[] ai = a.ToString().Select(x => int.Parse(x.ToString())).ToArray();
int[] bi = b.ToString().Select(x => int.Parse(x.ToString())).ToArray();
var number1 = string.Join("", bi.Select((x, i) => (i % 2) == 0 || ai.Count() <= i ? x.ToString() : ai[i].ToString()));
var number2 = string.Join("", ai.Select((x, i) => (i % 2) == 0 || bi.Count() <= i ? x.ToString() : bi[i].ToString()));
Console.WriteLine("Array 1:" + Convert.ToInt32(number1));
Console.WriteLine("Array 2:" + Convert.ToInt32(number2));
I tried a demo test on Codility for finding the equilibrium index(es) of an array. I'm not sure if the test was to find the equilibrium indexes of an array or what. I googled around and found the following example:
The equilibrium index of a sequence is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in a sequence A:
A[0]=-7 A[1]=1 A[2]=5 A[3]=2 A[4]=-4 A[5]=3 A[6]=0
3 is an equilibrium index, because:
A[0] + A[1] + A[2] = A[4] + A[5] +A[6]
6 is also an equilibrium index, because:
A[0] + A[1] + A[2] + A[3] + A[4] + A[5] = 0
Based on this information I see the test array contains 7 elements. It looks like the middle element A[3]=2 is being ignored. Is it because it is between the first 3 elements and the last 3 elements? How is the equilibrium index of 6 arrived at in this example?
Here is the method that was used to compute this:
int equi(int arr[], int n) {
if (n==0) return -1;
long long sum = 0;
int i;
for(i=0;i<n;i++) sum+=(long long) arr[i];
long long sum_left = 0;
for(i=0;i<n;i++) {
long long sum_right = sum - sum_left - (long long) arr[i];
if (sum_left == sum_right) return i;
sum_left += (long long) arr[i];
}
return -1;
}
When I took the Codility demo test I used the method (below) with the for loops initially beginning at 0. I received a "wrong answer" message for the test case of 1, 5, 2, 1, 4, 0 and that Codility was looking for a result of 11.
I modified the two for loops in my method and started the first loop at i = 1 and the second loop at i = 2 until it yielded a result of 11, which Codility was satisfied with. I basically just tweaked the method until Codility was happy (I was shooting for 11 because Codility specified that was the answer they were looking for), but I don't really know why Codility was happy or what was the significance of my tweaks -- just hit and miss:
...
int[] B = new int[] { 1, 5, 2, 1, 4, 0 };
Console.WriteLine(solution3(B));
}
public int solution3(int[] A)
{
long rsum = 0;
for (int i = 1; i < A.Count(); i++)
rsum += A[i];
long lsum = A[0];
int min = (int)Math.Abs(lsum - rsum);
for (int i = 2; i < A.Count() - 1; i++)
{
lsum += A[i];
rsum -= A[i];
int diff = (int)Math.Abs(lsum - rsum);
if (diff >= min)
min = diff;
}
return min;
}
Why did this tweak (of hit and miss) satisfy the Codility test? What is an equilibrium index actually? How is it arrived at? Note: if there are steps between step A and step E (which I would have to intuit the in between steps) what are steps B, C, and D?
What is an equilibrium index and how is it determined?
Think of your array like a board with weights of different sizes on it (negative weights can be thought of as helium balloons attached to the board). Each weight is numbered from left to right. Your task is to place a fulcrum under the board at any of the numbered positions to make the board balance. (We will treat the board itself as weightless, and assume distance of the weights from the center doesn't matter.) Whichever positions make the board balance are the equilibrium indexes. The weight at the fulcrum doesn't "count" because it's not on either side; it is in the middle.
Here is a picture of the first example:
Here, 3 is an equilibrium index, because the sum of the weights to the left of the fulcrum equal the sum of the weights to the right of the fulcrum (-7 + 5 + 1 = -1 = -4 + 3 + 0).
Here is the second example:
We see that 6 is also an equilibrium index for the same reason. All the elements to the left of the fulcrum add up to zero. As there are no elements to the right of the fulcrum, that sum is also zero.
The basic algorithm to find the equilibrium indexes is this:
Loop over the array and add up all the elements. Call this sum total.
Initialize a variable left_sum to zero.
Initialize a variable right_sum to total.
Now loop over the array a second time. For each array item:
Subtract the value of the current item from right_sum.
Compare the left_sum to the right_sum. If they are equal, then the index of the current item is an equilibrium index.
Add the value of the current item to left_sum.
And that's all there is to it. Does it make sense now?
What's going on with my algorithm?
As for why the "Codility demo test" was expecting a result of 11 for an array containing the elements { 1, 5, 2, 1, 4, 0 }, I cannot say. You never said in your question what the demo problem actually was. If the problem was to find the first equilibrium index in that array, then the answer is that there is none for that array. Stepping through the above algorithm, here are the left and right sums for each array index:
Index Left Right
----- ---- -----
0 0 12
1 1 7
2 6 5
3 8 4
4 9 0
5 13 0
As you can see, there is no index at which the left sum equals the right sum. An expected result of 11 does not even make sense anyway because there are only six elements. So if there were an equilibrium index for this array, it would have to between 0 and 5 inclusive. So I'm guessing the problem posed must have been something else. You would have to include the problem statement in your question before I could even begin to guess why your algorithm is or isn't correct.
Well, based on what I understand about an equilibrium index of an array I came up with the following algorithm which I tested on another array which has equilibrium indexes as index 3 and index 6, but I also tested this algorithm on the original array which returned a -1 (nothing to do with an 11). Here is the sample:
private void button6_Click_1(object sender, EventArgs e)
{
//int[] arr = new int[7] { -7, 1, 5, 2, -4, 3, 0 }; //--new array
int[] arr = new int[6] { 1, 5, 2, 1, 4, 0 }; //--original array
List<int> lst = new List<int>();
int p = arr.Length;
int i = 0;
int q = 0;
int t = 0;
do
{
t = equilibrium(arr, arr.Length, q);
if (lst.IndexOf(t) == -1)
lst.Add(t);
q = t;
i++;
} while (i < p);
foreach (var m in lst)
Console.WriteLine(m);
}
private int equilibrium( int[] arr, int n, int q1)
{
int i, j;
int leftsum, rightsum;
for (i = 0; i < n; ++i)
{
leftsum = 0;
rightsum = 0;
for (j = 0; j < i; j++)
leftsum += arr[j];
for (j = i + 1; j < n; j++)
rightsum += arr[j];
if (leftsum == rightsum && q1 != i)
return i;
}
return -1;
}
This algorithm makes sense to me, but when I tried it on Codility with an array of int[] arr = new int[] { 2, 2, 1, 0, 1 }; I got a "wrong Answer -- expected 3" In my test app my algorithm returned a 1 which is what it returned on Codility. Where did they come up with 3 for this array? Thus, I do not understand the equilibrium index of an array. Any suggestions appreciated.
Question: Print all the number who has unique digits only.
Input : n =15
output: 1 2 3 4 5 6 7 8 9 10 12 13 14 15
Here 11 is not included because it has 1 two times, same way 123, 456 .. are also valid but 121 1344 are not valid because there is same digit more than once.
I am running loop from 1- n and checking each number.
I am using Hash-map to determine the uniqueness of number.
Is there any better solution of above problem.
i'm not sure , but something like that..
List<int> numbers = new List<int>(){};
numbers =numbers.Where(p=>validCheck(p)==true).ToList();
static bool validCheck(int n)
{
return (n.ToString().Length==n.ToString().Disctinct().Count());
}
You could use LINQ, convert the number into a string and check if the length of the string is equal to the number of distinct charchters.
for (int i = 1; i < n; i++){
if (i.ToString().Length == i.ToString().Distinct().Count())
Console.Out.Write(i + " ");
}
as a semi useful library function where you seed it with a start and how many you want.
public static IEnumerable<int> UniqueDigits(int start, int count)
{
for (var i = start; i < (start + count); i++)
{
var s = i.ToString();
if (s.Distinct().Count() == s.Length)
{
yield return i;
}
}
}
then
UniqueDigits(0,15).ToList().ForEach(Console.WriteLine);
or
foreach (var digit in UniqueDigits(100,50))
{
Console.WriteLine(digit);
}
This is how I eliminate the numbers that have a duplicate characters.
Console.Write("Input:");
int number = int.Parse(Console.ReadLine());
List<int> numbers = new List<int>();
List<int> acceptedNumbers = new List<int>();
for (int i = 1; i <= number; i++)
{
numbers.Add(i);
}
foreach (var num in numbers)
{
bool rejected = false;
char[] numChars = num.ToString().ToCharArray();
foreach (var numChar in numChars)
{
if (numChars.Where(n => n == numChar).Count() > 1)
{
rejected = true;
}
}
if (!rejected)
{
acceptedNumbers.Add(num);
}
}
acceptedNumbers.ForEach(n => Console.Write($"{n} "));
Console.Read();
A string is an IEnumerable - so you can use a LINQ statement to solve your problem:
Numbers.Where(N => N.ToString().Distinct().Count() == N.ToString().Length);
The query is checking how many characters of the string of your number distinct and comares this number with the number of total characters.
Here is the whole code printing out all distinct numbers until 20:
List<int> Numbers = new List<int>();
for (int i = 1; i <= 20; i++)
{
Numbers.Add(i);
}
IEnumerable<int> AcceptedNumbers = Numbers.Where(N => N.ToString().Distinct().Count() == N.ToString().Length);
foreach (int AcceptedNumber in AcceptedNumbers)
{
Console.WriteLine(AcceptedNumber);
}
My thoughts:
Run the Loop from 0 to n
For each batch of 10 ( like from 0 to 9 , 10 to 19, 230 to 239..), pick the digits apart from the last one. These digits map to the counter which tends to be skipped. Rest all are to be emitted. For eg : for batch 12x , pick 1 & 2 , now we know that we have to skip numbers at position 1 and 2 , and rest all are acceptable so no need to do any processing for them.
Keep the above digits in sorted manner in an arrayList and keep a pointer at index 0. Lets call it 'ptr'. While running through that batch, check if count ( which moves from 0 to 9 ) for each batch is equal to the array[ptr]. If no, emit the number out. Else, skip it and do ptr++.
When you are doing step 2, check if any digits are duplicate. If yes, skip the entire batch of 10.
There are no string operations happening, so it should bring in the efficiency
Another solution is using integer division and modulo (no number to string conversion). You can verify the uniqueness of a number with the following method (assume digits is int array having 10 elements).
public static bool IsUnique(int num) {
int[] digits = new int[10];
num = Math.Abs(num);
while (num > 0) {
int r = num % 10;
num /= 10;
digits[r] ++;
if (digits[r] > 1) {
return false;
}
}
return true;
}
Working example http://ideone.com/9emEoz
There are only 9 * 9! / (10 - n)! unique-digit numbers with n digits. For larger n, you might want a next lexicographic algorithm to avoid unnecessary iterations. (For example, there are only 544,320 7-unique-digit numbers, yet your program would need to iterate through almost 10 million numbers to produce them!)
Here's my attempt at a next lexicographic procedure for a set of n-unique-digit numbers (where n > 1):
(1) From left to right, start with the digits 10, then ascend from 2.
For example, the first 4-digit number would be 1023.
(2) Increment the right-most digit that can be incremented to the next available
higher digit unused by digits to its left. Ascend to the right of the
incremented digit with the rest of the available digits, starting with lowest.
Examples: 1023 -> 1024 (4 is unused by the digits left of 3)
^
9786 -> 9801 (8 is unused be the digits left of 7)
^
9658 -> 9670 (7 is unused by the digits left of 5)
^
I am a beginner to programming and I am attempting to write a program that should solve the following mathematical problem:
"On a circle we have 108 numbers. The sum of any 20 consecutive numbers is always 1000. At position "1" we have the number 1, at position "19" we have the number 19 and at position "50" we have number 50. What number do we have on position "100"?" The numbers are always integers.
Up to this point i know how to use array and looping structures like "for".
I attempted to set a variable "check" which I initialized it as "false" in order to change it later to true, when I will find the number on position "100".
My initial thought is to go through the array and make the sum of each 20 consecutive numbers. I used two variables in order to assign values for each position that is not 1, 19 or 50 and then make the sum. I did this in two "for" structures, one inside another. Of course, this creates an issue that i am not able to properly calculate the values of the first 20 number.
I checked on the MSDN some information about the "Queue" structure, yet I can't understand how to use it in my particular case.
Let me know please if anyone has some suggestions on how i should handle this.
Thank you.
As you asked, here is the code that I have managed to write so far. Please take into consideration that I am really in the begging of programming and I still have a lot to learn.
check = true;
int[] array = new int[108];
for (counter = 1; counter <= array.Length; counter++)
{
if (counter !=1 || counter !=19 || counter !=50)
{
for (i = 1; i <= 999; i++)
{
array[counter] = i;
if (counter >= 20)
{
for (consecutive = counter; consecutive => 2; consecutive--)
{
checkValue = array[consecutive] + array[consecutive]-1;
}
if (checkvalue != 1000) check = false;
}
}
}
}
As say #fryday the code of this problem doesn't mean anything without logic, so you should understand well the solution before read this.
Let me explain to you the logic of this problem and then read the code (you will see that is quite simple). First you may note that each time you get 20 consecutive numbers, the next number must be equal to the first in the 20 consecutive, because you have
x_1 + x_2 + ... + x_20 = 1000
and
x_2 + ... + x_20 + x_21 = 1000
so
x_1 + x_2 + ... + x_20 = x_2 + ... + x_20 + x_21
and finally
x_1 = x_21
Following this insight all we have to do is walk through the array starting at each one of this positions 1, 19 and 50, fill the positions in the array with this values and then calculate the next position adding 20 to the current and mod by 108, repeat the process until the value in the array is equal to 1, 19 or 50 in each case. Then the values in the array equals to 0 means that all must have the same value (and has not found yet). To found this value sum 20 values consecutive and divide the difference with 1000 by the count of elements with value equal to 0 in such 20 consecutive. You can notice that the position 100 is one of this that have value equals to 0. Finally we get the value of 130.
This is the code:
using System;
namespace Stackoverflow
{
class Program
{
static void Main(string[] args)
{
int[] values = { 1, 19, 50 };
int[] arr = new int[108];
foreach (var val in values)
{
int pos = val - 1;
while (arr[pos] != val)
{
arr[pos] = val;
pos += 20;
pos %= 108;
}
}
int sum = 0;
int zeros = 0;
for (int i = 0; i < 20; i++)
{
if (arr[i] == 0) zeros++;
sum += arr[i];
}
Console.WriteLine((1000 - sum) / zeros);
}
}
}
I solved this problem with array, but better use some type of cycle list. It's about structure choice.
In general you should solve this problem with logic, computer only can help you to check result or visualize data. I'll append code which show how I use array for solving this problem:
N = 108
step = 20
a = [None] * (N + 1)
def toArrayIndex(i):
if i < 0:
return toArrayIndex(N + i)
if i % N == 0:
return N
else:
return i % N
def setKnownNumbers(value, index):
while a[toArrayIndex(index)] != value:
if a[toArrayIndex(index)] != None:
print "ERROR: problem unsolvable i = %d" % toArrayIndex(index)
a[toArrayIndex(index)] = value
index += step
setKnownNumbers(1, 1)
setKnownNumbers(19, 19)
setKnownNumbers(50, 50)
Now you only need to understand how finish it. It's not hard ;)
You can use thi code to output array and check correctness
# output
for i in range(1,N + 1):
print i, a[i]
counter = 0
for i in range(1,N + 1):
s = 0
for j in range(0,step):
s += a[toArrayIndex(i + j)]
if s == 1000:
counter += 1
if counter == N:
print "Correct"
print "a[100] = %d" % a[100]