Ad

Description

Create a function that accepts a string s and returns the sum of the ASCII values of each character in the given string.

Code
Diff
  • 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();
    • }
    • }

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"

Code
Diff
  • 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));
    • }
    • }