settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Softuni Numerals - Java Task

+9 votes

The standard decimal numbers are boring as hell, so we at SoftUni never use them. Instead, we use several predefined digits with which we create our own numbers. Those digits are:

0

1

2

3

4

aa

aba

bcc

cc

cdc

For example, the string ababcccccdc is the number 1234. Your task is to convert a Softuni numeral string to a standard decimal number. (1234519410)

A numeral string can be transformed as exactly one decimal number.

Input:

  • There is one line of input – the numeral string.

Output:

  • On the only output line, print the resulting decimal number.

Constraints:

  • The length of the input string is between 2 and 150.
  • Allowed time/space 0.1s (C#), 0.25s (Java)/16MB

Examples:

example softuni numerals java task

asked in Java category by user john7
edited by user golearnweb

2 Answers

+3 votes

Here's my solution - with HashMap + StringBuilder + BigInteger (because of the constraints):

package com.tutorials7.java.exam;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Numeral {
    public static void main(String[] args) {

        Scanner console = new Scanner(System.in);

        String input = console.nextLine();

        HashMap<String, String> dict = new HashMap<>();
        dict.put("aa", "0");
        dict.put("aba", "1");
        dict.put("bcc", "2");
        dict.put("cc", "3");
        dict.put("cdc", "4");

        String regex = "(aa|aba|bcc|cc|cdc)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        StringBuilder sb = new StringBuilder();
        while (matcher.find()) {
            sb.append(dict.get(matcher.group()));
        }

        //System.out.println(sb);
        BigInteger number = new BigInteger(sb.toString(), 5);
        System.out.println(number);
    }
}

 

answered by user hues
edited by user golearnweb
+3 votes

Here's my solution - on almost one row (although a long one) with replace and BigInteger as well:-) 

import java.math.BigInteger;
import java.util.Scanner;

public class SoftUniNumerals {

	public static void main(String[] args) {
		
		Scanner scanner = new Scanner(System.in);		
		System.out.println(new BigInteger(new BigInteger(scanner.nextLine().replace("aba", "1").replace("bcc", "2").replace("aa", "0").replace("cdc", "4").replace("cc", "3"), 5).toString(10)));
	}
}

 

answered by user richard8502
...