Description
Create a function that accepts a string s
and returns the sum
of the ASCII values of each character in the given string.
using System; using System.Linq; namespace Solution { public static class ToAscii { public static int GetAsciiSum(string s) => s.Select(character => (int)character).Sum(); } }
- using System;
namespace Solution{public class ToAsci{public static int getAsciSum(string s){int returner = 0;foreach (var el in s)returner += (int)el;return returner;}}- using System.Linq;
- namespace Solution
- {
- public static class ToAscii
- {
- public static int GetAsciiSum(string s) => s.Select(character => (int)character).Sum();
- }
- }
using System; using NUnit.Framework; namespace Solution { // TODO: Replace examples and use TDD development by writing your own tests [TestFixture] public class SolutionTest { [Test] public void MyTest() { Assert.AreEqual(0, ToAscii.GetAsciiSum("")); Assert.AreEqual(97, ToAscii.GetAsciiSum("a")); Assert.AreEqual(448, ToAscii.GetAsciiSum("test")); Assert.AreEqual(2286, ToAscii.GetAsciiSum("Some Words and numbers 453")); Assert.AreEqual(730, ToAscii.GetAsciiSum("&@Sp15, R@m")); } } }
namespace Solution {using NUnit.Framework;using System;- using System;
- using NUnit.Framework;
// TODO: Replace examples and use TDD development by writing your own tests- namespace Solution
- {
- // TODO: Replace examples and use TDD development by writing your own tests
[TestFixture]public class SolutionTest{[Test]public void MyTest()- [TestFixture]
- public class SolutionTest
- {
Assert.AreEqual(448, ToAsci.getAsciSum("test"));Assert.AreEqual(97, ToAsci.getAsciSum("a"));Assert.AreEqual(2286, ToAsci.getAsciSum("Some Words and numbers 453"));Assert.AreEqual(730, ToAsci.getAsciSum("&@Sp15, R@m"));- [Test]
- public void MyTest()
- {
- Assert.AreEqual(0, ToAscii.GetAsciiSum(""));
- Assert.AreEqual(97, ToAscii.GetAsciiSum("a"));
- Assert.AreEqual(448, ToAscii.GetAsciiSum("test"));
- Assert.AreEqual(2286, ToAscii.GetAsciiSum("Some Words and numbers 453"));
- Assert.AreEqual(730, ToAscii.GetAsciiSum("&@Sp15, R@m"));
- }
- }
}- }
Task
Complete the method which accepts an integer value
and returns a string of numbers delimited by either -
if the value is positive or +
if the value is negative.
Examples
0 -> ""
1 -> "1"
5 -> "1-22-333-4444-55555"
-1 -> "1"
-5 -> "1+22+333+4444+55555"
import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; class Solution { public static String numberOfNumbers(final int value) { if (value == 0) { return ""; } final String delimiter = value > 0 ? "-" : "+"; return IntStream.rangeClosed(1, Math.abs(value)) .mapToObj(n -> new String(new char[n]).replace("\0", String.valueOf(n))) .collect(Collectors.joining(delimiter)); } }
class Solution {public static String numberOfNumbers(int value) {StringBuilder finalWord = new StringBuilder();int j = 0;String sign = value < 0 ? "+" : "-";if (value == 0) return "";if (value < 0) value *=-1;- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- import java.util.stream.Stream;
for (int i = 1; i <= value; ++i) {j = i;while (j > 0) {finalWord.append(i);--j;}finalWord.append(sign);}return finalWord.deleteCharAt(finalWord.length() - 1).toString();}- class Solution {
- public static String numberOfNumbers(final int value) {
- if (value == 0) {
- return "";
- }
- final String delimiter = value > 0 ? "-" : "+";
- return IntStream.rangeClosed(1, Math.abs(value))
- .mapToObj(n -> new String(new char[n]).replace("\0", String.valueOf(n)))
- .collect(Collectors.joining(delimiter));
- }
- }
import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; public class SolutionTest { @Test public void testWhenValueIsZero() { assertEquals("", Solution.numberOfNumbers(0)); } @Test public void testPositiveValues() { assertEquals("1", Solution.numberOfNumbers(1)); assertEquals("1-22", Solution.numberOfNumbers(2)); assertEquals("1-22-333", Solution.numberOfNumbers(3)); assertEquals("1-22-333-4444", Solution.numberOfNumbers(4)); assertEquals("1-22-333-4444-55555", Solution.numberOfNumbers(5)); } @Test public void testNegativeValues() { assertEquals("1", Solution.numberOfNumbers(-1)); assertEquals("1+22", Solution.numberOfNumbers(-2)); assertEquals("1+22+333", Solution.numberOfNumbers(-3)); assertEquals("1+22+333+4444", Solution.numberOfNumbers(-4)); assertEquals("1+22+333+4444+55555", Solution.numberOfNumbers(-5)); } @Test public void testWithDoubleDigitValue() { assertEquals("1-22-333-4444-55555-666666-7777777-88888888-999999999-10101010101010101010", Solution.numberOfNumbers(10)); } }
- import org.junit.Test;
- import static org.junit.Assert.assertEquals;
- import org.junit.runners.JUnit4;
// TODO: Replace examples and use TDD development by writing your own tests- public class SolutionTest {
- @Test
public void testThree() {assertEquals("1-22-333", Solution.numberOfNumbers(3));- public void testWhenValueIsZero() {
- assertEquals("", Solution.numberOfNumbers(0));
- }
- @Test
public void testOne() {- public void testPositiveValues() {
- assertEquals("1", Solution.numberOfNumbers(1));
- assertEquals("1-22", Solution.numberOfNumbers(2));
- assertEquals("1-22-333", Solution.numberOfNumbers(3));
- assertEquals("1-22-333-4444", Solution.numberOfNumbers(4));
- assertEquals("1-22-333-4444-55555", Solution.numberOfNumbers(5));
- }
- @Test
public void testNone() {- public void testNegativeValues() {
- assertEquals("1", Solution.numberOfNumbers(-1));
}@Testpublic void testNFive() {- assertEquals("1+22", Solution.numberOfNumbers(-2));
- assertEquals("1+22+333", Solution.numberOfNumbers(-3));
- assertEquals("1+22+333+4444", Solution.numberOfNumbers(-4));
- assertEquals("1+22+333+4444+55555", Solution.numberOfNumbers(-5));
- }
@Testpublic void testZero() {assertEquals("", Solution.numberOfNumbers(0));}@Testpublic void testFive() {assertEquals("1-22-333-4444-55555", Solution.numberOfNumbers(5));}- @Test
public void testTen() {- public void testWithDoubleDigitValue() {
- assertEquals("1-22-333-4444-55555-666666-7777777-88888888-999999999-10101010101010101010", Solution.numberOfNumbers(10));
- }
- }